mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
middleware: Make scenario test functions take a testing.T argument (#29564)
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
parent
55d536c6bc
commit
4e0ad50102
@ -14,7 +14,7 @@ import (
|
||||
func TestMiddlewareAuth(t *testing.T) {
|
||||
reqSignIn := Auth(&AuthOptions{ReqSignedIn: true})
|
||||
|
||||
middlewareScenario(t, "ReqSignIn true and unauthenticated request", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "ReqSignIn true and unauthenticated request", func(t *testing.T, sc *scenarioContext) {
|
||||
sc.m.Get("/secure", reqSignIn, sc.defaultHandler)
|
||||
|
||||
sc.fakeReq("GET", "/secure").exec()
|
||||
@ -22,7 +22,7 @@ func TestMiddlewareAuth(t *testing.T) {
|
||||
assert.Equal(t, 302, sc.resp.Code)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "ReqSignIn true and unauthenticated API request", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "ReqSignIn true and unauthenticated API request", func(t *testing.T, sc *scenarioContext) {
|
||||
sc.m.Get("/api/secure", reqSignIn, sc.defaultHandler)
|
||||
|
||||
sc.fakeReq("GET", "/api/secure").exec()
|
||||
@ -44,7 +44,8 @@ func TestMiddlewareAuth(t *testing.T) {
|
||||
setting.AnonymousEnabled = true
|
||||
setting.AnonymousOrgName = "test"
|
||||
|
||||
middlewareScenario(t, "ReqSignIn true and request with forceLogin in query string", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "ReqSignIn true and request with forceLogin in query string", func(
|
||||
t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("test", func(query *models.GetOrgByNameQuery) error {
|
||||
query.Result = &models.Org{Id: orgID, Name: "test"}
|
||||
return nil
|
||||
@ -54,13 +55,14 @@ func TestMiddlewareAuth(t *testing.T) {
|
||||
|
||||
sc.fakeReq("GET", "/secure?forceLogin=true").exec()
|
||||
|
||||
assert.Equal(sc.t, 302, sc.resp.Code)
|
||||
assert.Equal(t, 302, sc.resp.Code)
|
||||
location, ok := sc.resp.Header()["Location"]
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "/login", location[0])
|
||||
})
|
||||
|
||||
middlewareScenario(t, "ReqSignIn true and request with same org provided in query string", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "ReqSignIn true and request with same org provided in query string", func(
|
||||
t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("test", func(query *models.GetOrgByNameQuery) error {
|
||||
query.Result = &models.Org{Id: orgID, Name: "test"}
|
||||
return nil
|
||||
@ -70,10 +72,11 @@ func TestMiddlewareAuth(t *testing.T) {
|
||||
|
||||
sc.fakeReq("GET", fmt.Sprintf("/secure?orgId=%d", orgID)).exec()
|
||||
|
||||
assert.Equal(sc.t, 200, sc.resp.Code)
|
||||
assert.Equal(t, 200, sc.resp.Code)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "ReqSignIn true and request with different org provided in query string", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "ReqSignIn true and request with different org provided in query string", func(
|
||||
t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("test", func(query *models.GetOrgByNameQuery) error {
|
||||
query.Result = &models.Org{Id: orgID, Name: "test"}
|
||||
return nil
|
||||
@ -83,24 +86,26 @@ func TestMiddlewareAuth(t *testing.T) {
|
||||
|
||||
sc.fakeReq("GET", "/secure?orgId=2").exec()
|
||||
|
||||
assert.Equal(sc.t, 302, sc.resp.Code)
|
||||
assert.Equal(t, 302, sc.resp.Code)
|
||||
location, ok := sc.resp.Header()["Location"]
|
||||
assert.True(sc.t, ok)
|
||||
assert.Equal(sc.t, "/login", location[0])
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "/login", location[0])
|
||||
})
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Snapshot public mode disabled and unauthenticated request should return 401", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Snapshot public mode disabled and unauthenticated request should return 401", func(
|
||||
t *testing.T, sc *scenarioContext) {
|
||||
sc.m.Get("/api/snapshot", SnapshotPublicModeOrSignedIn(), sc.defaultHandler)
|
||||
sc.fakeReq("GET", "/api/snapshot").exec()
|
||||
assert.Equal(sc.t, 401, sc.resp.Code)
|
||||
assert.Equal(t, 401, sc.resp.Code)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Snapshot public mode enabled and unauthenticated request should return 200", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Snapshot public mode enabled and unauthenticated request should return 200", func(
|
||||
t *testing.T, sc *scenarioContext) {
|
||||
setting.SnapshotPublicMode = true
|
||||
sc.m.Get("/api/snapshot", SnapshotPublicModeOrSignedIn(), sc.defaultHandler)
|
||||
sc.fakeReq("GET", "/api/snapshot").exec()
|
||||
assert.Equal(sc.t, 200, sc.resp.Code)
|
||||
assert.Equal(t, 200, sc.resp.Code)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@ func TestMiddlewareDashboardRedirect(t *testing.T) {
|
||||
fakeDash.HasAcl = false
|
||||
fakeDash.Uid = util.GenerateShortUID()
|
||||
|
||||
middlewareScenario(t, "GET dashboard by legacy url", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "GET dashboard by legacy url", func(t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
|
||||
query.Result = fakeDash
|
||||
return nil
|
||||
@ -43,7 +43,7 @@ func TestMiddlewareDashboardRedirect(t *testing.T) {
|
||||
assert.Equal(t, 2, len(redirectURL.Query()))
|
||||
})
|
||||
|
||||
middlewareScenario(t, "GET dashboard solo by legacy url", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "GET dashboard solo by legacy url", func(t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
|
||||
query.Result = fakeDash
|
||||
return nil
|
||||
@ -65,7 +65,7 @@ func TestMiddlewareDashboardRedirect(t *testing.T) {
|
||||
})
|
||||
})
|
||||
|
||||
middlewareScenario(t, "GET dashboard by legacy edit url", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "GET dashboard by legacy edit url", func(t *testing.T, sc *scenarioContext) {
|
||||
sc.m.Get("/d/:uid/:slug", RedirectFromLegacyPanelEditURL(), sc.defaultHandler)
|
||||
|
||||
sc.fakeReqWithParams("GET", "/d/asd/dash?orgId=1&panelId=12&fullscreen&edit", map[string]string{}).exec()
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
authLogin "github.com/grafana/grafana/pkg/login"
|
||||
"github.com/grafana/grafana/pkg/login"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
@ -27,7 +27,7 @@ func TestMiddlewareBasicAuth(t *testing.T) {
|
||||
|
||||
const id int64 = 12
|
||||
|
||||
middlewareScenario(t, "Valid API key", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Valid API key", func(t *testing.T, sc *scenarioContext) {
|
||||
const orgID int64 = 2
|
||||
keyhash, err := util.EncodePassword("v5nAwpMafFP6znaS4urhdWDLS5511M42", "asd")
|
||||
require.NoError(t, err)
|
||||
@ -46,7 +46,7 @@ func TestMiddlewareBasicAuth(t *testing.T) {
|
||||
assert.Equal(t, models.ROLE_EDITOR, sc.context.OrgRole)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Handle auth", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Handle auth", func(t *testing.T, sc *scenarioContext) {
|
||||
const password = "MyPass"
|
||||
const salt = "Salt"
|
||||
const orgID int64 = 2
|
||||
@ -78,11 +78,11 @@ func TestMiddlewareBasicAuth(t *testing.T) {
|
||||
assert.Equal(t, id, sc.context.UserId)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Auth sequence", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Auth sequence", func(t *testing.T, sc *scenarioContext) {
|
||||
const password = "MyPass"
|
||||
const salt = "Salt"
|
||||
|
||||
authLogin.Init()
|
||||
login.Init()
|
||||
|
||||
bus.AddHandler("user-query", func(query *models.GetUserByLoginQuery) error {
|
||||
encoded, err := util.EncodePassword(password, salt)
|
||||
@ -109,7 +109,7 @@ func TestMiddlewareBasicAuth(t *testing.T) {
|
||||
assert.Equal(t, id, sc.context.UserId)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Should return error if user is not found", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Should return error if user is not found", func(t *testing.T, sc *scenarioContext) {
|
||||
sc.fakeReq("GET", "/")
|
||||
sc.req.SetBasicAuth("user", "password")
|
||||
sc.exec()
|
||||
@ -121,7 +121,7 @@ func TestMiddlewareBasicAuth(t *testing.T) {
|
||||
assert.Equal(t, errStringInvalidUsernamePassword, sc.respJson["message"])
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Should return error if user & password do not match", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Should return error if user & password do not match", func(t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("user-query", func(loginUserQuery *models.GetUserByLoginQuery) error {
|
||||
return nil
|
||||
})
|
||||
|
@ -50,7 +50,7 @@ func TestMiddleWareSecurityHeaders(t *testing.T) {
|
||||
})
|
||||
setting.ErrTemplateName = errorTemplate
|
||||
|
||||
middlewareScenario(t, "middleware should get correct x-xss-protection header", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "middleware should get correct x-xss-protection header", func(t *testing.T, sc *scenarioContext) {
|
||||
origXSSProtectionHeader := setting.XSSProtectionHeader
|
||||
t.Cleanup(func() {
|
||||
setting.XSSProtectionHeader = origXSSProtectionHeader
|
||||
@ -60,7 +60,7 @@ func TestMiddleWareSecurityHeaders(t *testing.T) {
|
||||
assert.Equal(t, "1; mode=block", sc.resp.Header().Get("X-XSS-Protection"))
|
||||
})
|
||||
|
||||
middlewareScenario(t, "middleware should not get x-xss-protection when disabled", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "middleware should not get x-xss-protection when disabled", func(t *testing.T, sc *scenarioContext) {
|
||||
origXSSProtectionHeader := setting.XSSProtectionHeader
|
||||
t.Cleanup(func() {
|
||||
setting.XSSProtectionHeader = origXSSProtectionHeader
|
||||
@ -70,7 +70,7 @@ func TestMiddleWareSecurityHeaders(t *testing.T) {
|
||||
assert.Empty(t, sc.resp.Header().Get("X-XSS-Protection"))
|
||||
})
|
||||
|
||||
middlewareScenario(t, "middleware should add correct Strict-Transport-Security header", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "middleware should add correct Strict-Transport-Security header", func(t *testing.T, sc *scenarioContext) {
|
||||
origStrictTransportSecurity := setting.StrictTransportSecurity
|
||||
origProtocol := setting.Protocol
|
||||
origStrictTransportSecurityMaxAge := setting.StrictTransportSecurityMaxAge
|
||||
@ -101,31 +101,33 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
})
|
||||
setting.ErrTemplateName = errorTemplate
|
||||
|
||||
middlewareScenario(t, "middleware should add context to injector", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "middleware should add context to injector", func(t *testing.T, sc *scenarioContext) {
|
||||
sc.fakeReq("GET", "/").exec()
|
||||
assert.NotNil(t, sc.context)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Default middleware should allow get request", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Default middleware should allow get request", func(t *testing.T, sc *scenarioContext) {
|
||||
sc.fakeReq("GET", "/").exec()
|
||||
assert.Equal(t, 200, sc.resp.Code)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "middleware should add Cache-Control header for requests to API", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "middleware should add Cache-Control header for requests to API", func(t *testing.T, sc *scenarioContext) {
|
||||
sc.fakeReq("GET", "/api/search").exec()
|
||||
assert.Equal(t, "no-cache", sc.resp.Header().Get("Cache-Control"))
|
||||
assert.Equal(t, "no-cache", sc.resp.Header().Get("Pragma"))
|
||||
assert.Equal(t, "-1", sc.resp.Header().Get("Expires"))
|
||||
})
|
||||
|
||||
middlewareScenario(t, "middleware should not add Cache-Control header for requests to datasource proxy API", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "middleware should not add Cache-Control header for requests to datasource proxy API", func(
|
||||
t *testing.T, sc *scenarioContext) {
|
||||
sc.fakeReq("GET", "/api/datasources/proxy/1/test").exec()
|
||||
assert.Empty(t, sc.resp.Header().Get("Cache-Control"))
|
||||
assert.Empty(t, sc.resp.Header().Get("Pragma"))
|
||||
assert.Empty(t, sc.resp.Header().Get("Expires"))
|
||||
})
|
||||
|
||||
middlewareScenario(t, "middleware should add Cache-Control header for requests with html response", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "middleware should add Cache-Control header for requests with html response", func(
|
||||
t *testing.T, sc *scenarioContext) {
|
||||
sc.handler(func(c *models.ReqContext) {
|
||||
data := &dtos.IndexViewData{
|
||||
User: &dtos.CurrentUser{},
|
||||
@ -141,12 +143,14 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Equal(t, "-1", sc.resp.Header().Get("Expires"))
|
||||
})
|
||||
|
||||
middlewareScenario(t, "middleware should add X-Frame-Options header with deny for request when not allowing embedding", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "middleware should add X-Frame-Options header with deny for request when not allowing embedding", func(
|
||||
t *testing.T, sc *scenarioContext) {
|
||||
sc.fakeReq("GET", "/api/search").exec()
|
||||
assert.Equal(t, "deny", sc.resp.Header().Get("X-Frame-Options"))
|
||||
})
|
||||
|
||||
middlewareScenario(t, "middleware should not add X-Frame-Options header for request when allowing embedding", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "middleware should not add X-Frame-Options header for request when allowing embedding", func(
|
||||
t *testing.T, sc *scenarioContext) {
|
||||
origAllowEmbedding := setting.AllowEmbedding
|
||||
t.Cleanup(func() {
|
||||
setting.AllowEmbedding = origAllowEmbedding
|
||||
@ -156,7 +160,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Empty(t, sc.resp.Header().Get("X-Frame-Options"))
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Invalid api key", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Invalid api key", func(t *testing.T, sc *scenarioContext) {
|
||||
sc.apiKey = "invalid_key_test"
|
||||
sc.fakeReq("GET", "/").exec()
|
||||
|
||||
@ -165,7 +169,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Equal(t, errStringInvalidAPIKey, sc.respJson["message"])
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Valid api key", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Valid api key", func(t *testing.T, sc *scenarioContext) {
|
||||
const orgID int64 = 12
|
||||
keyhash, err := util.EncodePassword("v5nAwpMafFP6znaS4urhdWDLS5511M42", "asd")
|
||||
require.NoError(t, err)
|
||||
@ -184,7 +188,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Equal(t, models.ROLE_EDITOR, sc.context.OrgRole)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Valid api key, but does not match db hash", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Valid api key, but does not match db hash", func(t *testing.T, sc *scenarioContext) {
|
||||
keyhash := "Something_not_matching"
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetApiKeyByNameQuery) error {
|
||||
@ -198,7 +202,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Equal(t, errStringInvalidAPIKey, sc.respJson["message"])
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Valid api key, but expired", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Valid api key, but expired", func(t *testing.T, sc *scenarioContext) {
|
||||
mockGetTime()
|
||||
defer resetGetTime()
|
||||
|
||||
@ -219,7 +223,8 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Equal(t, "Expired API key", sc.respJson["message"])
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Non-expired auth token in cookie which not are being rotated", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Non-expired auth token in cookie which not are being rotated", func(
|
||||
t *testing.T, sc *scenarioContext) {
|
||||
const userID int64 = 12
|
||||
|
||||
sc.withTokenSessionCookie("token")
|
||||
@ -245,7 +250,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Equal(t, "", sc.resp.Header().Get("Set-Cookie"))
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Non-expired auth token in cookie which are being rotated", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Non-expired auth token in cookie which are being rotated", func(t *testing.T, sc *scenarioContext) {
|
||||
const userID int64 = 12
|
||||
|
||||
sc.withTokenSessionCookie("token")
|
||||
@ -335,7 +340,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
})
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Invalid/expired auth token in cookie", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Invalid/expired auth token in cookie", func(t *testing.T, sc *scenarioContext) {
|
||||
sc.withTokenSessionCookie("token")
|
||||
|
||||
sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*models.UserToken, error) {
|
||||
@ -349,7 +354,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Nil(t, sc.context.UserToken)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "When anonymous access is enabled", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "When anonymous access is enabled", func(t *testing.T, sc *scenarioContext) {
|
||||
const orgID int64 = 2
|
||||
|
||||
origAnonymousEnabled := setting.AnonymousEnabled
|
||||
@ -410,7 +415,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
const hdrName = "markelog"
|
||||
const group = "grafana-core-team"
|
||||
|
||||
middlewareScenario(t, "Should not sync the user if it's in the cache", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Should not sync the user if it's in the cache", func(t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("test", func(query *models.GetSignedInUserQuery) error {
|
||||
query.Result = &models.SignedInUser{OrgId: orgID, UserId: query.UserId}
|
||||
return nil
|
||||
@ -430,7 +435,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Equal(t, orgID, sc.context.OrgId)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Should respect auto signup option", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Should respect auto signup option", func(t *testing.T, sc *scenarioContext) {
|
||||
origLDAPEnabled = setting.LDAPEnabled
|
||||
origAuthProxyAutoSignUp = setting.AuthProxyAutoSignUp
|
||||
t.Cleanup(func() {
|
||||
@ -456,7 +461,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Nil(t, sc.context)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Should create an user from a header", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Should create an user from a header", func(t *testing.T, sc *scenarioContext) {
|
||||
origLDAPEnabled = setting.LDAPEnabled
|
||||
origAuthProxyAutoSignUp = setting.AuthProxyAutoSignUp
|
||||
t.Cleanup(func() {
|
||||
@ -488,7 +493,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Equal(t, orgID, sc.context.OrgId)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Should get an existing user from header", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Should get an existing user from header", func(t *testing.T, sc *scenarioContext) {
|
||||
const userID int64 = 12
|
||||
const orgID int64 = 2
|
||||
|
||||
@ -517,7 +522,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Equal(t, orgID, sc.context.OrgId)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Should allow the request from whitelist IP", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Should allow the request from whitelist IP", func(t *testing.T, sc *scenarioContext) {
|
||||
origAuthProxyWhitelist = setting.AuthProxyWhitelist
|
||||
origLDAPEnabled = setting.LDAPEnabled
|
||||
t.Cleanup(func() {
|
||||
@ -547,7 +552,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Equal(t, orgID, sc.context.OrgId)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Should not allow the request from whitelist IP", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Should not allow the request from whitelist IP", func(t *testing.T, sc *scenarioContext) {
|
||||
origAuthProxyWhitelist = setting.AuthProxyWhitelist
|
||||
origLDAPEnabled = setting.LDAPEnabled
|
||||
t.Cleanup(func() {
|
||||
@ -576,7 +581,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Nil(t, sc.context)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Should return 407 status code if LDAP says no", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Should return 407 status code if LDAP says no", func(t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("LDAP", func(cmd *models.UpsertUserCommand) error {
|
||||
return errors.New("Do not add user")
|
||||
})
|
||||
@ -589,7 +594,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
assert.Nil(t, sc.context)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Should return 407 status code if there is cache mishap", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "Should return 407 status code if there is cache mishap", func(t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("Do not have the user", func(query *models.GetSignedInUserQuery) error {
|
||||
return errors.New("Do not add user")
|
||||
})
|
||||
@ -653,7 +658,7 @@ func middlewareScenario(t *testing.T, desc string, fn scenarioFunc) {
|
||||
|
||||
sc.m.Get("/", sc.defaultHandler)
|
||||
|
||||
fn(sc)
|
||||
fn(t, sc)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func TestOrgRedirectMiddleware(t *testing.T) {
|
||||
middlewareScenario(t, "when setting a correct org for the user", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "when setting a correct org for the user", func(t *testing.T, sc *scenarioContext) {
|
||||
sc.withTokenSessionCookie("token")
|
||||
bus.AddHandler("test", func(query *models.SetUsingOrgCommand) error {
|
||||
return nil
|
||||
@ -35,7 +35,7 @@ func TestOrgRedirectMiddleware(t *testing.T) {
|
||||
assert.Equal(t, 302, sc.resp.Code)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "when setting an invalid org for user", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "when setting an invalid org for user", func(t *testing.T, sc *scenarioContext) {
|
||||
sc.withTokenSessionCookie("token")
|
||||
bus.AddHandler("test", func(query *models.SetUsingOrgCommand) error {
|
||||
return fmt.Errorf("")
|
||||
|
@ -42,7 +42,7 @@ func TestMiddlewareQuota(t *testing.T) {
|
||||
quotaFn := Quota(qs)
|
||||
|
||||
t.Run("With user not logged in", func(t *testing.T) {
|
||||
middlewareScenario(t, "and global quota not reached", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "and global quota not reached", func(t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("globalQuota", func(query *models.GetGlobalQuotaByTargetQuery) error {
|
||||
query.Result = &models.GlobalQuotaDTO{
|
||||
Target: query.Target,
|
||||
@ -54,10 +54,10 @@ func TestMiddlewareQuota(t *testing.T) {
|
||||
|
||||
sc.m.Get("/user", quotaFn("user"), sc.defaultHandler)
|
||||
sc.fakeReq("GET", "/user").exec()
|
||||
assert.Equal(sc.t, 200, sc.resp.Code)
|
||||
assert.Equal(t, 200, sc.resp.Code)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "and global quota reached", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "and global quota reached", func(t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("globalQuota", func(query *models.GetGlobalQuotaByTargetQuery) error {
|
||||
query.Result = &models.GlobalQuotaDTO{
|
||||
Target: query.Target,
|
||||
@ -78,7 +78,7 @@ func TestMiddlewareQuota(t *testing.T) {
|
||||
assert.Equal(t, 403, sc.resp.Code)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "and global session quota not reached", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "and global session quota not reached", func(t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("globalQuota", func(query *models.GetGlobalQuotaByTargetQuery) error {
|
||||
query.Result = &models.GlobalQuotaDTO{
|
||||
Target: query.Target,
|
||||
@ -99,7 +99,7 @@ func TestMiddlewareQuota(t *testing.T) {
|
||||
assert.Equal(t, 200, sc.resp.Code)
|
||||
})
|
||||
|
||||
middlewareScenario(t, "and global session quota reached", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "and global session quota reached", func(t *testing.T, sc *scenarioContext) {
|
||||
origSession := setting.Quota.Global.Session
|
||||
t.Cleanup(func() {
|
||||
setting.Quota.Global.Session = origSession
|
||||
@ -108,11 +108,11 @@ func TestMiddlewareQuota(t *testing.T) {
|
||||
|
||||
sc.m.Get("/user", quotaFn("session"), sc.defaultHandler)
|
||||
sc.fakeReq("GET", "/user").exec()
|
||||
assert.Equal(sc.t, 403, sc.resp.Code)
|
||||
assert.Equal(t, 403, sc.resp.Code)
|
||||
})
|
||||
})
|
||||
|
||||
middlewareScenario(t, "with user logged in", func(sc *scenarioContext) {
|
||||
middlewareScenario(t, "with user logged in", func(t *testing.T, sc *scenarioContext) {
|
||||
sc.withTokenSessionCookie("token")
|
||||
bus.AddHandler("test", func(query *models.GetSignedInUserQuery) error {
|
||||
query.Result = &models.SignedInUser{OrgId: 2, UserId: 12}
|
||||
|
@ -20,7 +20,7 @@ func TestRecoveryMiddleware(t *testing.T) {
|
||||
|
||||
t.Run("Given an API route that panics", func(t *testing.T) {
|
||||
apiURL := "/api/whatever"
|
||||
recoveryScenario(t, "recovery middleware should return json", apiURL, func(sc *scenarioContext) {
|
||||
recoveryScenario(t, "recovery middleware should return json", apiURL, func(t *testing.T, sc *scenarioContext) {
|
||||
sc.handlerFunc = panicHandler
|
||||
sc.fakeReq("GET", apiURL).exec()
|
||||
sc.req.Header.Add("content-type", "application/json")
|
||||
@ -33,7 +33,7 @@ func TestRecoveryMiddleware(t *testing.T) {
|
||||
|
||||
t.Run("Given a non-API route that panics", func(t *testing.T) {
|
||||
apiURL := "/whatever"
|
||||
recoveryScenario(t, "recovery middleware should return html", apiURL, func(sc *scenarioContext) {
|
||||
recoveryScenario(t, "recovery middleware should return html", apiURL, func(t *testing.T, sc *scenarioContext) {
|
||||
sc.handlerFunc = panicHandler
|
||||
sc.fakeReq("GET", apiURL).exec()
|
||||
|
||||
@ -85,6 +85,6 @@ func recoveryScenario(t *testing.T, desc string, url string, fn scenarioFunc) {
|
||||
|
||||
sc.m.Get(url, sc.defaultHandler)
|
||||
|
||||
fn(sc)
|
||||
fn(t, sc)
|
||||
})
|
||||
}
|
||||
|
@ -109,5 +109,5 @@ func (sc *scenarioContext) exec() {
|
||||
}
|
||||
}
|
||||
|
||||
type scenarioFunc func(c *scenarioContext)
|
||||
type scenarioFunc func(t *testing.T, c *scenarioContext)
|
||||
type handlerFunc func(c *models.ReqContext)
|
||||
|
Loading…
Reference in New Issue
Block a user