1package middleware
2
3import (
4	"encoding/json"
5	"net/http"
6	"net/http/httptest"
7	"testing"
8
9	"github.com/grafana/grafana/pkg/infra/remotecache"
10	"github.com/grafana/grafana/pkg/models"
11	"github.com/grafana/grafana/pkg/services/auth"
12	"github.com/grafana/grafana/pkg/services/contexthandler"
13	"github.com/grafana/grafana/pkg/services/sqlstore"
14	"github.com/grafana/grafana/pkg/setting"
15	"github.com/grafana/grafana/pkg/web"
16	"github.com/stretchr/testify/require"
17)
18
19type scenarioContext struct {
20	t                    *testing.T
21	m                    *web.Mux
22	context              *models.ReqContext
23	resp                 *httptest.ResponseRecorder
24	apiKey               string
25	authHeader           string
26	jwtAuthHeader        string
27	tokenSessionCookie   string
28	respJson             map[string]interface{}
29	handlerFunc          handlerFunc
30	defaultHandler       web.Handler
31	url                  string
32	userAuthTokenService *auth.FakeUserAuthTokenService
33	jwtAuthService       *models.FakeJWTService
34	remoteCacheService   *remotecache.RemoteCache
35	cfg                  *setting.Cfg
36	sqlStore             *sqlstore.SQLStore
37	contextHandler       *contexthandler.ContextHandler
38
39	req *http.Request
40}
41
42func (sc *scenarioContext) withValidApiKey() *scenarioContext {
43	sc.apiKey = "eyJrIjoidjVuQXdwTWFmRlA2em5hUzR1cmhkV0RMUzU1MTFNNDIiLCJuIjoiYXNkIiwiaWQiOjF9"
44	return sc
45}
46
47func (sc *scenarioContext) withTokenSessionCookie(unhashedToken string) *scenarioContext {
48	sc.tokenSessionCookie = unhashedToken
49	return sc
50}
51
52func (sc *scenarioContext) withAuthorizationHeader(authHeader string) *scenarioContext {
53	sc.authHeader = authHeader
54	return sc
55}
56
57func (sc *scenarioContext) withJWTAuthHeader(jwtAuthHeader string) *scenarioContext {
58	sc.jwtAuthHeader = jwtAuthHeader
59	return sc
60}
61
62func (sc *scenarioContext) fakeReq(method, url string) *scenarioContext {
63	sc.t.Helper()
64
65	sc.resp = httptest.NewRecorder()
66	req, err := http.NewRequest(method, url, nil)
67	require.NoError(sc.t, err)
68	sc.req = req
69
70	return sc
71}
72
73func (sc *scenarioContext) fakeReqWithParams(method, url string, queryParams map[string]string) *scenarioContext {
74	sc.t.Helper()
75
76	sc.resp = httptest.NewRecorder()
77	req, err := http.NewRequest(method, url, nil)
78	require.NoError(sc.t, err)
79
80	q := req.URL.Query()
81	for k, v := range queryParams {
82		q.Add(k, v)
83	}
84	req.URL.RawQuery = q.Encode()
85	require.NoError(sc.t, err)
86	sc.req = req
87
88	return sc
89}
90
91func (sc *scenarioContext) exec() {
92	sc.t.Helper()
93
94	if sc.apiKey != "" {
95		sc.t.Logf(`Adding header "Authorization: Bearer %s"`, sc.apiKey)
96		sc.req.Header.Set("Authorization", "Bearer "+sc.apiKey)
97	}
98
99	if sc.authHeader != "" {
100		sc.t.Logf(`Adding header "Authorization: %s"`, sc.authHeader)
101		sc.req.Header.Set("Authorization", sc.authHeader)
102	}
103
104	if sc.jwtAuthHeader != "" {
105		sc.t.Logf(`Adding header "%s: %s"`, sc.cfg.JWTAuthHeaderName, sc.jwtAuthHeader)
106		sc.req.Header.Set(sc.cfg.JWTAuthHeaderName, sc.jwtAuthHeader)
107	}
108
109	if sc.tokenSessionCookie != "" {
110		sc.t.Log(`Adding cookie`, "name", sc.cfg.LoginCookieName, "value", sc.tokenSessionCookie)
111		sc.req.AddCookie(&http.Cookie{
112			Name:  sc.cfg.LoginCookieName,
113			Value: sc.tokenSessionCookie,
114		})
115	}
116
117	sc.m.ServeHTTP(sc.resp, sc.req)
118
119	if sc.resp.Header().Get("Content-Type") == "application/json; charset=UTF-8" {
120		err := json.NewDecoder(sc.resp.Body).Decode(&sc.respJson)
121		require.NoError(sc.t, err)
122		sc.t.Log("Decoded JSON", "json", sc.respJson)
123	} else {
124		sc.t.Log("Not decoding JSON")
125	}
126}
127
128type scenarioFunc func(t *testing.T, c *scenarioContext)
129type handlerFunc func(c *models.ReqContext)
130