2018-01-30 06:17:48 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-09-27 09:43:16 -05:00
|
|
|
"context"
|
2020-05-12 06:04:18 -05:00
|
|
|
"fmt"
|
2018-01-30 06:17:48 -06:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"path/filepath"
|
2020-05-12 06:04:18 -05:00
|
|
|
"testing"
|
2018-01-30 06:17:48 -06:00
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"gopkg.in/macaron.v1"
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
2018-01-30 06:17:48 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2020-12-15 12:09:04 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/fs"
|
2020-12-11 04:44:44 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/remotecache"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-08-24 04:36:28 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2021-08-24 13:12:48 -05:00
|
|
|
accesscontrolmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
|
2019-03-08 08:15:38 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/auth"
|
2020-12-11 04:44:44 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/contexthandler"
|
2021-08-25 08:11:22 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/quota"
|
2020-12-11 04:44:44 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/rendering"
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2018-01-30 06:17:48 -06:00
|
|
|
)
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func loggedInUserScenario(t *testing.T, desc string, url string, fn scenarioFunc) {
|
|
|
|
loggedInUserScenarioWithRole(t, desc, "GET", url, url, models.ROLE_EDITOR, fn)
|
2018-01-30 06:17:48 -06:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func loggedInUserScenarioWithRole(t *testing.T, desc string, method string, url string, routePattern string, role models.RoleType, fn scenarioFunc) {
|
|
|
|
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
2020-12-15 12:09:04 -06:00
|
|
|
t.Cleanup(bus.ClearBusHandlers)
|
2018-01-30 06:17:48 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc := setupScenarioContext(t, url)
|
2021-01-15 07:43:20 -06:00
|
|
|
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
|
2018-01-30 06:17:48 -06:00
|
|
|
sc.context = c
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.context.UserId = testUserID
|
|
|
|
sc.context.OrgId = testOrgID
|
2020-11-24 05:10:32 -06:00
|
|
|
sc.context.Login = testUserLogin
|
2018-01-30 06:17:48 -06:00
|
|
|
sc.context.OrgRole = role
|
|
|
|
if sc.handlerFunc != nil {
|
|
|
|
return sc.handlerFunc(sc.context)
|
|
|
|
}
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
if sc.handlerFuncCtx != nil {
|
|
|
|
return sc.handlerFuncCtx(context.Background(), sc.context)
|
|
|
|
}
|
|
|
|
|
2018-01-30 06:17:48 -06:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
switch method {
|
|
|
|
case "GET":
|
|
|
|
sc.m.Get(routePattern, sc.defaultHandler)
|
|
|
|
case "DELETE":
|
|
|
|
sc.m.Delete(routePattern, sc.defaultHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn(sc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func anonymousUserScenario(t *testing.T, desc string, method string, url string, routePattern string, fn scenarioFunc) {
|
|
|
|
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
2018-05-24 01:55:16 -05:00
|
|
|
defer bus.ClearBusHandlers()
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc := setupScenarioContext(t, url)
|
2021-01-15 07:43:20 -06:00
|
|
|
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
|
2018-05-24 01:55:16 -05:00
|
|
|
sc.context = c
|
|
|
|
if sc.handlerFunc != nil {
|
|
|
|
return sc.handlerFunc(sc.context)
|
|
|
|
}
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
if sc.handlerFuncCtx != nil {
|
|
|
|
return sc.handlerFuncCtx(context.Background(), sc.context)
|
|
|
|
}
|
|
|
|
|
2018-05-24 01:55:16 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
switch method {
|
|
|
|
case "GET":
|
|
|
|
sc.m.Get(routePattern, sc.defaultHandler)
|
|
|
|
case "DELETE":
|
|
|
|
sc.m.Delete(routePattern, sc.defaultHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn(sc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-30 06:17:48 -06:00
|
|
|
func (sc *scenarioContext) fakeReq(method, url string) *scenarioContext {
|
|
|
|
sc.resp = httptest.NewRecorder()
|
|
|
|
req, err := http.NewRequest(method, url, nil)
|
2020-11-13 02:52:38 -06:00
|
|
|
require.NoError(sc.t, err)
|
2018-01-30 06:17:48 -06:00
|
|
|
sc.req = req
|
|
|
|
|
|
|
|
return sc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *scenarioContext) fakeReqWithParams(method, url string, queryParams map[string]string) *scenarioContext {
|
|
|
|
sc.resp = httptest.NewRecorder()
|
|
|
|
req, err := http.NewRequest(method, url, nil)
|
2020-05-12 06:04:18 -05:00
|
|
|
// TODO: Depend on sc.t
|
|
|
|
if sc.t != nil {
|
|
|
|
require.NoError(sc.t, err)
|
|
|
|
} else if err != nil {
|
|
|
|
panic(fmt.Sprintf("Making request failed: %s", err))
|
|
|
|
}
|
|
|
|
|
2018-01-30 06:17:48 -06:00
|
|
|
q := req.URL.Query()
|
|
|
|
for k, v := range queryParams {
|
|
|
|
q.Add(k, v)
|
|
|
|
}
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
sc.req = req
|
|
|
|
return sc
|
|
|
|
}
|
|
|
|
|
2019-07-09 01:37:24 -05:00
|
|
|
func (sc *scenarioContext) fakeReqNoAssertions(method, url string) *scenarioContext {
|
|
|
|
sc.resp = httptest.NewRecorder()
|
|
|
|
req, _ := http.NewRequest(method, url, nil)
|
|
|
|
sc.req = req
|
|
|
|
|
|
|
|
return sc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *scenarioContext) fakeReqNoAssertionsWithCookie(method, url string, cookie http.Cookie) *scenarioContext {
|
|
|
|
sc.resp = httptest.NewRecorder()
|
|
|
|
http.SetCookie(sc.resp, &cookie)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest(method, url, nil)
|
|
|
|
req.Header = http.Header{"Cookie": sc.resp.Header()["Set-Cookie"]}
|
|
|
|
|
|
|
|
sc.req = req
|
|
|
|
|
|
|
|
return sc
|
|
|
|
}
|
|
|
|
|
2018-01-30 06:17:48 -06:00
|
|
|
type scenarioContext struct {
|
2020-05-12 06:04:18 -05:00
|
|
|
t *testing.T
|
2020-12-15 12:09:04 -06:00
|
|
|
cfg *setting.Cfg
|
2019-03-08 08:15:38 -06:00
|
|
|
m *macaron.Macaron
|
2020-03-04 05:57:20 -06:00
|
|
|
context *models.ReqContext
|
2019-03-08 08:15:38 -06:00
|
|
|
resp *httptest.ResponseRecorder
|
|
|
|
handlerFunc handlerFunc
|
2021-09-27 09:43:16 -05:00
|
|
|
handlerFuncCtx handlerFuncCtx
|
2019-03-08 08:15:38 -06:00
|
|
|
defaultHandler macaron.Handler
|
|
|
|
req *http.Request
|
|
|
|
url string
|
|
|
|
userAuthTokenService *auth.FakeUserAuthTokenService
|
2018-01-30 06:17:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *scenarioContext) exec() {
|
|
|
|
sc.m.ServeHTTP(sc.resp, sc.req)
|
|
|
|
}
|
|
|
|
|
|
|
|
type scenarioFunc func(c *scenarioContext)
|
2021-01-15 07:43:20 -06:00
|
|
|
type handlerFunc func(c *models.ReqContext) response.Response
|
2021-09-27 09:43:16 -05:00
|
|
|
type handlerFuncCtx func(ctx context.Context, c *models.ReqContext) response.Response
|
2018-01-30 06:17:48 -06:00
|
|
|
|
2020-12-15 12:09:04 -06:00
|
|
|
func getContextHandler(t *testing.T, cfg *setting.Cfg) *contexthandler.ContextHandler {
|
2020-12-11 04:44:44 -06:00
|
|
|
t.Helper()
|
|
|
|
|
2020-12-15 12:09:04 -06:00
|
|
|
if cfg == nil {
|
|
|
|
cfg = setting.NewCfg()
|
|
|
|
}
|
|
|
|
|
2020-12-11 04:44:44 -06:00
|
|
|
sqlStore := sqlstore.InitTestDB(t)
|
|
|
|
remoteCacheSvc := &remotecache.RemoteCache{}
|
|
|
|
cfg.RemoteCacheOptions = &setting.RemoteCacheOptions{
|
|
|
|
Name: "database",
|
|
|
|
}
|
|
|
|
userAuthTokenSvc := auth.NewFakeUserAuthTokenService()
|
|
|
|
renderSvc := &fakeRenderService{}
|
2021-03-31 10:40:44 -05:00
|
|
|
authJWTSvc := models.NewFakeJWTService()
|
2021-08-25 08:11:22 -05:00
|
|
|
ctxHdlr := contexthandler.ProvideService(cfg, userAuthTokenSvc, authJWTSvc, remoteCacheSvc, renderSvc, sqlStore)
|
2020-12-11 04:44:44 -06:00
|
|
|
|
|
|
|
return ctxHdlr
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func setupScenarioContext(t *testing.T, url string) *scenarioContext {
|
2020-12-15 12:09:04 -06:00
|
|
|
cfg := setting.NewCfg()
|
2018-01-30 06:17:48 -06:00
|
|
|
sc := &scenarioContext{
|
|
|
|
url: url,
|
2020-11-13 02:52:38 -06:00
|
|
|
t: t,
|
2020-12-15 12:09:04 -06:00
|
|
|
cfg: cfg,
|
2018-01-30 06:17:48 -06:00
|
|
|
}
|
2020-12-11 04:44:44 -06:00
|
|
|
viewsPath, err := filepath.Abs("../../public/views")
|
|
|
|
require.NoError(t, err)
|
2020-12-15 12:09:04 -06:00
|
|
|
exists, err := fs.Exists(viewsPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Truef(t, exists, "Views should be in %q", viewsPath)
|
2018-01-30 06:17:48 -06:00
|
|
|
|
|
|
|
sc.m = macaron.New()
|
2021-08-10 06:29:46 -05:00
|
|
|
sc.m.UseMiddleware(macaron.Renderer(viewsPath, "[[", "]]"))
|
2020-12-15 12:09:04 -06:00
|
|
|
sc.m.Use(getContextHandler(t, cfg).Middleware)
|
2018-01-30 06:17:48 -06:00
|
|
|
|
|
|
|
return sc
|
|
|
|
}
|
2020-12-11 04:44:44 -06:00
|
|
|
|
|
|
|
type fakeRenderService struct {
|
|
|
|
rendering.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *fakeRenderService) Init() error {
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-11 08:58:18 -05:00
|
|
|
|
2021-06-14 10:36:48 -05:00
|
|
|
func setupAccessControlScenarioContext(t *testing.T, cfg *setting.Cfg, url string, permissions []*accesscontrol.Permission) (*scenarioContext, *HTTPServer) {
|
2021-06-11 08:58:18 -05:00
|
|
|
cfg.FeatureToggles = make(map[string]bool)
|
|
|
|
cfg.FeatureToggles["accesscontrol"] = true
|
2021-09-01 08:18:17 -05:00
|
|
|
cfg.Quota.Enabled = false
|
2021-06-11 08:58:18 -05:00
|
|
|
|
|
|
|
hs := &HTTPServer{
|
|
|
|
Cfg: cfg,
|
2021-09-01 08:18:17 -05:00
|
|
|
Bus: bus.GetBus(),
|
|
|
|
Live: newTestLive(t),
|
|
|
|
QuotaService: "a.QuotaService{Cfg: cfg},
|
2021-06-11 08:58:18 -05:00
|
|
|
RouteRegister: routing.NewRouteRegister(),
|
2021-08-24 13:12:48 -05:00
|
|
|
AccessControl: accesscontrolmock.New().WithPermissions(permissions),
|
2021-06-11 08:58:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sc := setupScenarioContext(t, url)
|
|
|
|
|
|
|
|
hs.registerRoutes()
|
|
|
|
hs.RouteRegister.Register(sc.m.Router)
|
|
|
|
|
2021-06-14 10:36:48 -05:00
|
|
|
return sc, hs
|
|
|
|
}
|
|
|
|
|
|
|
|
type accessControlTestCase struct {
|
|
|
|
expectedCode int
|
|
|
|
desc string
|
|
|
|
url string
|
|
|
|
method string
|
|
|
|
permissions []*accesscontrol.Permission
|
2021-06-11 08:58:18 -05:00
|
|
|
}
|