2018-01-30 06:17:48 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-11-29 03:18:01 -06:00
|
|
|
"bytes"
|
2021-09-27 09:43:16 -05:00
|
|
|
"context"
|
2021-11-29 03:18:01 -06:00
|
|
|
"encoding/json"
|
2020-05-12 06:04:18 -05:00
|
|
|
"fmt"
|
2021-10-27 06:13:59 -05:00
|
|
|
"io"
|
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-11-17 03:12:28 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-09-29 05:51:49 -05:00
|
|
|
|
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"
|
2021-10-27 06:13:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2020-12-11 04:44:44 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/remotecache"
|
2022-01-20 04:10:12 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
2021-11-17 03:12:28 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/usagestats"
|
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"
|
2022-01-13 07:40:32 -06:00
|
|
|
acmiddleware "github.com/grafana/grafana/pkg/services/accesscontrol/middleware"
|
2021-08-24 13:12:48 -05:00
|
|
|
accesscontrolmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
|
2021-11-17 03:12:28 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol/ossaccesscontrol"
|
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"
|
2022-01-20 15:42:05 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
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"
|
2021-11-17 03:12:28 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/searchusers"
|
|
|
|
"github.com/grafana/grafana/pkg/services/searchusers/filters"
|
2020-12-11 04:44:44 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2018-01-30 06:17:48 -06:00
|
|
|
)
|
|
|
|
|
2022-01-14 10:55:57 -06:00
|
|
|
func loggedInUserScenario(t *testing.T, desc string, url string, routePattern string, fn scenarioFunc) {
|
|
|
|
loggedInUserScenarioWithRole(t, desc, "GET", url, routePattern, 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-10-11 07:30:59 -05:00
|
|
|
m *web.Mux
|
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-10-11 07:30:59 -05:00
|
|
|
defaultHandler web.Handler
|
2019-03-08 08:15:38 -06:00
|
|
|
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
|
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()
|
2022-01-20 04:10:12 -06:00
|
|
|
tracer, err := tracing.InitializeTracerForTest()
|
|
|
|
require.NoError(t, err)
|
|
|
|
ctxHdlr := contexthandler.ProvideService(cfg, userAuthTokenSvc, authJWTSvc, remoteCacheSvc, renderSvc, sqlStore, tracer)
|
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
|
|
|
|
2021-10-11 07:30:59 -05:00
|
|
|
sc.m = web.New()
|
|
|
|
sc.m.UseMiddleware(web.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) {
|
2022-01-20 15:42:05 -06:00
|
|
|
features := featuremgmt.WithFeatures("accesscontrol")
|
|
|
|
cfg.IsFeatureToggleEnabled = features.IsEnabled
|
2021-09-01 08:18:17 -05:00
|
|
|
cfg.Quota.Enabled = false
|
2021-06-11 08:58:18 -05:00
|
|
|
|
2021-09-29 05:51:49 -05:00
|
|
|
bus := bus.GetBus()
|
2021-06-11 08:58:18 -05:00
|
|
|
hs := &HTTPServer{
|
2021-09-29 05:51:49 -05:00
|
|
|
Cfg: cfg,
|
|
|
|
Bus: bus,
|
|
|
|
Live: newTestLive(t),
|
2022-01-20 15:42:05 -06:00
|
|
|
Features: features,
|
2021-09-29 05:51:49 -05:00
|
|
|
QuotaService: "a.QuotaService{Cfg: cfg},
|
|
|
|
RouteRegister: routing.NewRouteRegister(),
|
|
|
|
AccessControl: accesscontrolmock.New().WithPermissions(permissions),
|
2021-10-07 09:06:16 -05:00
|
|
|
searchUsersService: searchusers.ProvideUsersService(bus, filters.ProvideOSSSearchUserFilter()),
|
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
|
|
|
}
|
2021-10-27 06:13:59 -05:00
|
|
|
|
|
|
|
// accessControlScenarioContext contains the setups for accesscontrol tests
|
|
|
|
type accessControlScenarioContext struct {
|
|
|
|
// server we registered hs routes on.
|
|
|
|
server *web.Mux
|
|
|
|
|
|
|
|
// initCtx is used in a middleware to set the initial context
|
|
|
|
// of the request server side. Can be used to pretend sign in.
|
|
|
|
initCtx *models.ReqContext
|
|
|
|
|
|
|
|
// hs is a minimal HTTPServer for the accesscontrol tests to pass.
|
|
|
|
hs *HTTPServer
|
|
|
|
|
|
|
|
// acmock is an accesscontrol mock used to fake users rights.
|
|
|
|
acmock *accesscontrolmock.Mock
|
|
|
|
|
|
|
|
// db is a test database initialized with InitTestDB
|
|
|
|
db *sqlstore.SQLStore
|
|
|
|
|
|
|
|
// cfg is the setting provider
|
|
|
|
cfg *setting.Cfg
|
|
|
|
}
|
|
|
|
|
2021-11-17 03:12:28 -06:00
|
|
|
func setAccessControlPermissions(acmock *accesscontrolmock.Mock, perms []*accesscontrol.Permission, org int64) {
|
|
|
|
acmock.GetUserPermissionsFunc = func(_ context.Context, u *models.SignedInUser) ([]*accesscontrol.Permission, error) {
|
|
|
|
if u.OrgId == org {
|
|
|
|
return perms, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
2021-10-27 06:13:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 03:12:28 -06:00
|
|
|
// setInitCtxSignedInUser sets a copy of the user in initCtx
|
|
|
|
func setInitCtxSignedInUser(initCtx *models.ReqContext, user models.SignedInUser) {
|
|
|
|
initCtx.IsSignedIn = true
|
|
|
|
initCtx.SignedInUser = &user
|
|
|
|
}
|
|
|
|
|
2021-10-27 06:13:59 -05:00
|
|
|
func setInitCtxSignedInViewer(initCtx *models.ReqContext) {
|
|
|
|
initCtx.IsSignedIn = true
|
|
|
|
initCtx.SignedInUser = &models.SignedInUser{UserId: testUserID, OrgId: 1, OrgRole: models.ROLE_VIEWER, Login: testUserLogin}
|
|
|
|
}
|
|
|
|
|
2022-01-10 11:05:53 -06:00
|
|
|
func setInitCtxSignedInEditor(initCtx *models.ReqContext) {
|
|
|
|
initCtx.IsSignedIn = true
|
|
|
|
initCtx.SignedInUser = &models.SignedInUser{UserId: testUserID, OrgId: 1, OrgRole: models.ROLE_EDITOR, Login: testUserLogin}
|
|
|
|
}
|
|
|
|
|
2021-10-27 06:13:59 -05:00
|
|
|
func setInitCtxSignedInOrgAdmin(initCtx *models.ReqContext) {
|
|
|
|
initCtx.IsSignedIn = true
|
|
|
|
initCtx.SignedInUser = &models.SignedInUser{UserId: testUserID, OrgId: 1, OrgRole: models.ROLE_ADMIN, Login: testUserLogin}
|
|
|
|
}
|
|
|
|
|
2022-01-20 15:42:05 -06:00
|
|
|
func setupSimpleHTTPServer(features *featuremgmt.FeatureManager) *HTTPServer {
|
|
|
|
if features == nil {
|
|
|
|
features = featuremgmt.WithFeatures()
|
|
|
|
}
|
|
|
|
cfg := setting.NewCfg()
|
|
|
|
cfg.IsFeatureToggleEnabled = features.IsEnabled
|
|
|
|
|
|
|
|
return &HTTPServer{
|
|
|
|
Cfg: cfg,
|
|
|
|
Features: features,
|
|
|
|
Bus: bus.GetBus(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 03:12:28 -06:00
|
|
|
func setupHTTPServer(t *testing.T, useFakeAccessControl bool, enableAccessControl bool) accessControlScenarioContext {
|
2021-10-27 06:13:59 -05:00
|
|
|
// Use a new conf
|
2022-01-20 15:42:05 -06:00
|
|
|
features := featuremgmt.WithFeatures("accesscontrol", enableAccessControl)
|
2021-10-27 06:13:59 -05:00
|
|
|
cfg := setting.NewCfg()
|
2022-01-20 15:42:05 -06:00
|
|
|
cfg.IsFeatureToggleEnabled = features.IsEnabled
|
2021-10-27 06:13:59 -05:00
|
|
|
|
2022-01-10 11:05:53 -06:00
|
|
|
return setupHTTPServerWithCfg(t, useFakeAccessControl, enableAccessControl, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupHTTPServerWithCfg(t *testing.T, useFakeAccessControl, enableAccessControl bool, cfg *setting.Cfg) accessControlScenarioContext {
|
|
|
|
t.Helper()
|
|
|
|
|
2022-01-20 15:42:05 -06:00
|
|
|
features := featuremgmt.WithFeatures("accesscontrol", enableAccessControl)
|
|
|
|
cfg.IsFeatureToggleEnabled = features.IsEnabled
|
|
|
|
|
2022-01-10 11:05:53 -06:00
|
|
|
var acmock *accesscontrolmock.Mock
|
|
|
|
var ac *ossaccesscontrol.OSSAccessControlService
|
|
|
|
|
2021-10-27 06:13:59 -05:00
|
|
|
// Use a test DB
|
|
|
|
db := sqlstore.InitTestDB(t)
|
|
|
|
db.Cfg = cfg
|
|
|
|
|
|
|
|
bus := bus.GetBus()
|
|
|
|
|
|
|
|
// Create minimal HTTP Server
|
|
|
|
hs := &HTTPServer{
|
|
|
|
Cfg: cfg,
|
2022-01-20 15:42:05 -06:00
|
|
|
Features: features,
|
2021-10-27 06:13:59 -05:00
|
|
|
Bus: bus,
|
|
|
|
Live: newTestLive(t),
|
|
|
|
QuotaService: "a.QuotaService{Cfg: cfg},
|
|
|
|
RouteRegister: routing.NewRouteRegister(),
|
|
|
|
SQLStore: db,
|
|
|
|
searchUsersService: searchusers.ProvideUsersService(bus, filters.ProvideOSSSearchUserFilter()),
|
|
|
|
}
|
|
|
|
|
2021-11-17 03:12:28 -06:00
|
|
|
// Defining the accesscontrol service has to be done before registering routes
|
|
|
|
if useFakeAccessControl {
|
|
|
|
acmock = accesscontrolmock.New()
|
|
|
|
if !enableAccessControl {
|
|
|
|
acmock = acmock.WithDisabled()
|
|
|
|
}
|
|
|
|
hs.AccessControl = acmock
|
|
|
|
} else {
|
2022-01-20 15:42:05 -06:00
|
|
|
ac = ossaccesscontrol.ProvideService(hs.Features.Toggles(), &usagestats.UsageStatsMock{T: t})
|
2021-11-17 03:12:28 -06:00
|
|
|
hs.AccessControl = ac
|
|
|
|
// Perform role registration
|
|
|
|
err := hs.declareFixedRoles()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = ac.RegisterFixedRoles()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-10-27 06:13:59 -05:00
|
|
|
// Instantiate a new Server
|
|
|
|
m := web.New()
|
|
|
|
|
|
|
|
// middleware to set the test initial context
|
|
|
|
initCtx := &models.ReqContext{}
|
|
|
|
m.Use(func(c *web.Context) {
|
|
|
|
initCtx.Context = c
|
|
|
|
initCtx.Logger = log.New("api-test")
|
|
|
|
c.Map(initCtx)
|
|
|
|
})
|
|
|
|
|
2022-01-13 07:40:32 -06:00
|
|
|
m.Use(acmiddleware.LoadPermissionsMiddleware(hs.AccessControl))
|
|
|
|
|
2021-10-27 06:13:59 -05:00
|
|
|
// Register all routes
|
|
|
|
hs.registerRoutes()
|
|
|
|
hs.RouteRegister.Register(m.Router)
|
|
|
|
|
|
|
|
return accessControlScenarioContext{
|
|
|
|
server: m,
|
|
|
|
initCtx: initCtx,
|
|
|
|
hs: hs,
|
|
|
|
acmock: acmock,
|
|
|
|
db: db,
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func callAPI(server *web.Mux, method, path string, body io.Reader, t *testing.T) *httptest.ResponseRecorder {
|
|
|
|
req, err := http.NewRequest(method, path, body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
server.ServeHTTP(recorder, req)
|
|
|
|
return recorder
|
|
|
|
}
|
2021-11-29 03:18:01 -06:00
|
|
|
|
|
|
|
func mockRequestBody(v interface{}) io.ReadCloser {
|
|
|
|
b, _ := json.Marshal(v)
|
|
|
|
return io.NopCloser(bytes.NewReader(b))
|
|
|
|
}
|