2019-08-03 14:50:05 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-12-03 01:28:54 -06:00
|
|
|
"testing"
|
2019-08-03 14:50:05 -05:00
|
|
|
|
2022-10-19 08:02:15 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-08-09 01:54:52 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/authn"
|
|
|
|
"github.com/grafana/grafana/pkg/services/authn/authntest"
|
2022-11-16 11:11:26 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/contexthandler/ctxkey"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2022-08-11 06:28:55 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user/usertest"
|
2019-08-03 14:50:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2019-08-03 14:50:05 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type scenarioContext struct {
|
2023-08-09 01:54:52 -05:00
|
|
|
t *testing.T
|
|
|
|
m *web.Mux
|
|
|
|
context *contextmodel.ReqContext
|
|
|
|
resp *httptest.ResponseRecorder
|
2023-08-30 10:46:47 -05:00
|
|
|
respJson map[string]any
|
2023-08-09 01:54:52 -05:00
|
|
|
handlerFunc handlerFunc
|
|
|
|
defaultHandler web.Handler
|
|
|
|
url string
|
|
|
|
authnService *authntest.FakeService
|
|
|
|
userService *usertest.FakeUserService
|
|
|
|
cfg *setting.Cfg
|
2019-08-03 14:50:05 -05:00
|
|
|
|
|
|
|
req *http.Request
|
|
|
|
}
|
|
|
|
|
2023-08-09 01:54:52 -05:00
|
|
|
// set identity to use for request
|
|
|
|
func (sc *scenarioContext) withIdentity(identity *authn.Identity) {
|
|
|
|
sc.authnService.ExpectedErr = nil
|
|
|
|
sc.authnService.ExpectedIdentity = identity
|
2021-03-31 10:40:44 -05:00
|
|
|
}
|
|
|
|
|
2019-08-03 14:50:05 -05:00
|
|
|
func (sc *scenarioContext) fakeReq(method, url string) *scenarioContext {
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.t.Helper()
|
|
|
|
|
2019-08-03 14:50:05 -05:00
|
|
|
sc.resp = httptest.NewRecorder()
|
|
|
|
req, err := http.NewRequest(method, url, nil)
|
2020-12-03 01:28:54 -06:00
|
|
|
require.NoError(sc.t, err)
|
2022-11-16 11:11:26 -06:00
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
reqCtx := &contextmodel.ReqContext{
|
2022-11-16 11:11:26 -06:00
|
|
|
Context: web.FromContext(req.Context()),
|
|
|
|
}
|
|
|
|
sc.req = req.WithContext(ctxkey.Set(req.Context(), reqCtx))
|
2019-08-03 14:50:05 -05:00
|
|
|
|
|
|
|
return sc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *scenarioContext) fakeReqWithParams(method, url string, queryParams map[string]string) *scenarioContext {
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.t.Helper()
|
|
|
|
|
2019-08-03 14:50:05 -05:00
|
|
|
sc.resp = httptest.NewRecorder()
|
|
|
|
req, err := http.NewRequest(method, url, nil)
|
2020-12-15 12:09:04 -06:00
|
|
|
require.NoError(sc.t, err)
|
|
|
|
|
2019-08-03 14:50:05 -05:00
|
|
|
q := req.URL.Query()
|
|
|
|
for k, v := range queryParams {
|
|
|
|
q.Add(k, v)
|
|
|
|
}
|
2023-04-24 02:55:55 -05:00
|
|
|
|
2019-08-03 14:50:05 -05:00
|
|
|
req.URL.RawQuery = q.Encode()
|
2023-04-24 02:55:55 -05:00
|
|
|
req.RequestURI = req.URL.RequestURI()
|
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
require.NoError(sc.t, err)
|
2022-11-16 11:11:26 -06:00
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
reqCtx := &contextmodel.ReqContext{
|
2022-11-16 11:11:26 -06:00
|
|
|
Context: web.FromContext(req.Context()),
|
|
|
|
}
|
|
|
|
sc.req = req.WithContext(ctxkey.Set(req.Context(), reqCtx))
|
2019-08-03 14:50:05 -05:00
|
|
|
|
|
|
|
return sc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *scenarioContext) exec() {
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.t.Helper()
|
|
|
|
|
2019-08-03 14:50:05 -05:00
|
|
|
sc.m.ServeHTTP(sc.resp, sc.req)
|
|
|
|
|
|
|
|
if sc.resp.Header().Get("Content-Type") == "application/json; charset=UTF-8" {
|
|
|
|
err := json.NewDecoder(sc.resp.Body).Decode(&sc.respJson)
|
2020-12-03 01:28:54 -06:00
|
|
|
require.NoError(sc.t, err)
|
2020-12-15 12:09:04 -06:00
|
|
|
sc.t.Log("Decoded JSON", "json", sc.respJson)
|
|
|
|
} else {
|
|
|
|
sc.t.Log("Not decoding JSON")
|
2019-08-03 14:50:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
type scenarioFunc func(t *testing.T, c *scenarioContext)
|
2023-01-27 01:50:36 -06:00
|
|
|
type handlerFunc func(c *contextmodel.ReqContext)
|