2018-12-01 11:28:10 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-11-13 02:52:38 -06:00
|
|
|
"fmt"
|
2018-12-01 11:28:10 -06:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
2018-12-01 11:28:10 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2019-05-21 06:52:49 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2019-03-08 08:15:38 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/auth"
|
2020-11-13 02:52:38 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-12-01 11:28:10 -06:00
|
|
|
)
|
|
|
|
|
2020-04-15 04:11:45 -05:00
|
|
|
const (
|
2020-11-13 02:52:38 -06:00
|
|
|
testLogin = "test@example.com"
|
|
|
|
testPassword = "password"
|
2020-04-15 04:11:45 -05:00
|
|
|
nonExistingOrgID = 1000
|
|
|
|
)
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func TestAdminAPIEndpoint(t *testing.T) {
|
|
|
|
const role = models.ROLE_ADMIN
|
|
|
|
|
2020-12-11 04:44:44 -06:00
|
|
|
t.Run("Given a server admin attempts to remove themselves as an admin", func(t *testing.T) {
|
2018-12-01 11:28:10 -06:00
|
|
|
updateCmd := dtos.AdminUpdateUserPermissionsForm{
|
|
|
|
IsGrafanaAdmin: false,
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
putAdminScenario(t, "When calling PUT on", "/api/admin/users/1/permissions",
|
|
|
|
"/api/admin/users/:id/permissions", role, updateCmd, func(sc *scenarioContext) {
|
|
|
|
bus.AddHandler("test", func(cmd *models.UpdateUserPermissionsCommand) error {
|
|
|
|
return models.ErrLastGrafanaAdmin
|
|
|
|
})
|
2018-12-01 11:28:10 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.fakeReqWithParams("PUT", sc.url, map[string]string{}).exec()
|
|
|
|
assert.Equal(t, 400, sc.resp.Code)
|
|
|
|
})
|
2018-12-01 11:28:10 -06:00
|
|
|
})
|
2019-03-08 08:15:38 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("When a server admin attempts to logout himself from all devices", func(t *testing.T) {
|
|
|
|
adminLogoutUserScenario(t, "Should not be allowed when calling POST on",
|
|
|
|
"/api/admin/users/1/logout", "/api/admin/users/:id/logout", func(sc *scenarioContext) {
|
|
|
|
bus.AddHandler("test", func(cmd *models.GetUserByIdQuery) error {
|
|
|
|
cmd.Result = &models.User{Id: testUserID}
|
|
|
|
return nil
|
|
|
|
})
|
2019-03-08 08:15:38 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
|
|
|
assert.Equal(t, 400, sc.resp.Code)
|
|
|
|
})
|
2019-03-08 08:15:38 -06:00
|
|
|
})
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("When a server admin attempts to logout a non-existing user from all devices", func(t *testing.T) {
|
|
|
|
adminLogoutUserScenario(t, "Should return not found when calling POST on", "/api/admin/users/200/logout",
|
|
|
|
"/api/admin/users/:id/logout", func(sc *scenarioContext) {
|
|
|
|
userID := int64(0)
|
2019-03-08 08:15:38 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
bus.AddHandler("test", func(cmd *models.GetUserByIdQuery) error {
|
|
|
|
userID = cmd.Id
|
|
|
|
return models.ErrUserNotFound
|
|
|
|
})
|
|
|
|
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
|
|
|
assert.Equal(t, 404, sc.resp.Code)
|
|
|
|
assert.Equal(t, int64(200), userID)
|
|
|
|
})
|
2019-03-08 08:15:38 -06:00
|
|
|
})
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("When a server admin attempts to revoke an auth token for a non-existing user", func(t *testing.T) {
|
2020-03-04 05:57:20 -06:00
|
|
|
cmd := models.RevokeAuthTokenCmd{AuthTokenId: 2}
|
2019-03-08 08:15:38 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
adminRevokeUserAuthTokenScenario(t, "Should return not found when calling POST on",
|
|
|
|
"/api/admin/users/200/revoke-auth-token", "/api/admin/users/:id/revoke-auth-token", cmd, func(sc *scenarioContext) {
|
|
|
|
var userID int64
|
|
|
|
bus.AddHandler("test", func(cmd *models.GetUserByIdQuery) error {
|
|
|
|
userID = cmd.Id
|
|
|
|
return models.ErrUserNotFound
|
|
|
|
})
|
2019-03-08 08:15:38 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
|
|
|
assert.Equal(t, 404, sc.resp.Code)
|
|
|
|
assert.Equal(t, int64(200), userID)
|
|
|
|
})
|
|
|
|
})
|
2019-03-08 08:15:38 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("When a server admin gets auth tokens for a non-existing user", func(t *testing.T) {
|
|
|
|
adminGetUserAuthTokensScenario(t, "Should return not found when calling GET on",
|
|
|
|
"/api/admin/users/200/auth-tokens", "/api/admin/users/:id/auth-tokens", func(sc *scenarioContext) {
|
|
|
|
var userID int64
|
|
|
|
bus.AddHandler("test", func(cmd *models.GetUserByIdQuery) error {
|
|
|
|
userID = cmd.Id
|
|
|
|
return models.ErrUserNotFound
|
|
|
|
})
|
|
|
|
|
|
|
|
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
|
|
|
assert.Equal(t, 404, sc.resp.Code)
|
|
|
|
assert.Equal(t, int64(200), userID)
|
|
|
|
})
|
2019-03-08 08:15:38 -06:00
|
|
|
})
|
2019-05-21 06:52:49 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("When a server admin attempts to enable/disable a nonexistent user", func(t *testing.T) {
|
|
|
|
adminDisableUserScenario(t, "Should return user not found on a POST request", "enable",
|
|
|
|
"/api/admin/users/42/enable", "/api/admin/users/:id/enable", func(sc *scenarioContext) {
|
|
|
|
var userID int64
|
|
|
|
isDisabled := false
|
|
|
|
bus.AddHandler("test", func(cmd *models.GetAuthInfoQuery) error {
|
|
|
|
return models.ErrUserNotFound
|
|
|
|
})
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(cmd *models.DisableUserCommand) error {
|
|
|
|
userID = cmd.UserId
|
|
|
|
isDisabled = cmd.IsDisabled
|
|
|
|
return models.ErrUserNotFound
|
|
|
|
})
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, 404, sc.resp.Code)
|
|
|
|
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
|
|
|
require.NoError(t, err)
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, "user not found", respJSON.Get("message").MustString())
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, int64(42), userID)
|
|
|
|
assert.Equal(t, false, isDisabled)
|
|
|
|
})
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
adminDisableUserScenario(t, "Should return user not found on a POST request", "disable",
|
|
|
|
"/api/admin/users/42/disable", "/api/admin/users/:id/disable", func(sc *scenarioContext) {
|
|
|
|
var userID int64
|
|
|
|
isDisabled := false
|
|
|
|
bus.AddHandler("test", func(cmd *models.GetAuthInfoQuery) error {
|
|
|
|
return models.ErrUserNotFound
|
|
|
|
})
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
bus.AddHandler("test", func(cmd *models.DisableUserCommand) error {
|
|
|
|
userID = cmd.UserId
|
|
|
|
isDisabled = cmd.IsDisabled
|
|
|
|
return models.ErrUserNotFound
|
|
|
|
})
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, 404, sc.resp.Code)
|
|
|
|
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
|
|
|
require.NoError(t, err)
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, "user not found", respJSON.Get("message").MustString())
|
|
|
|
|
|
|
|
assert.Equal(t, int64(42), userID)
|
|
|
|
assert.Equal(t, true, isDisabled)
|
|
|
|
})
|
2020-01-10 04:43:44 -06:00
|
|
|
})
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("When a server admin attempts to disable/enable external user", func(t *testing.T) {
|
|
|
|
adminDisableUserScenario(t, "Should return Could not disable external user error", "disable",
|
|
|
|
"/api/admin/users/42/disable", "/api/admin/users/:id/disable", func(sc *scenarioContext) {
|
|
|
|
var userID int64
|
|
|
|
bus.AddHandler("test", func(cmd *models.GetAuthInfoQuery) error {
|
|
|
|
userID = cmd.UserId
|
|
|
|
return nil
|
|
|
|
})
|
2019-05-21 06:52:49 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
|
|
|
assert.Equal(t, 500, sc.resp.Code)
|
2019-05-21 06:52:49 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "Could not disable external user", respJSON.Get("message").MustString())
|
2019-05-21 06:52:49 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, int64(42), userID)
|
|
|
|
})
|
2019-05-21 06:52:49 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
adminDisableUserScenario(t, "Should return Could not enable external user error", "enable",
|
|
|
|
"/api/admin/users/42/enable", "/api/admin/users/:id/enable", func(sc *scenarioContext) {
|
|
|
|
var userID int64
|
|
|
|
bus.AddHandler("test", func(cmd *models.GetAuthInfoQuery) error {
|
|
|
|
userID = cmd.UserId
|
|
|
|
return nil
|
|
|
|
})
|
2019-05-21 06:52:49 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
|
|
|
assert.Equal(t, 500, sc.resp.Code)
|
2019-05-21 06:52:49 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "Could not enable external user", respJSON.Get("message").MustString())
|
|
|
|
|
|
|
|
assert.Equal(t, int64(42), userID)
|
|
|
|
})
|
2019-05-21 06:52:49 -05:00
|
|
|
})
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("When a server admin attempts to delete a nonexistent user", func(t *testing.T) {
|
|
|
|
adminDeleteUserScenario(t, "Should return user not found error", "/api/admin/users/42",
|
|
|
|
"/api/admin/users/:id", func(sc *scenarioContext) {
|
|
|
|
var userID int64
|
|
|
|
bus.AddHandler("test", func(cmd *models.DeleteUserCommand) error {
|
|
|
|
userID = cmd.UserId
|
|
|
|
return models.ErrUserNotFound
|
|
|
|
})
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, 404, sc.resp.Code)
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "user not found", respJSON.Get("message").MustString())
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, int64(42), userID)
|
|
|
|
})
|
2020-01-10 04:43:44 -06:00
|
|
|
})
|
2020-04-15 04:11:45 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("When a server admin attempts to create a user", func(t *testing.T) {
|
|
|
|
t.Run("Without an organization", func(t *testing.T) {
|
|
|
|
createCmd := dtos.AdminCreateUserForm{
|
|
|
|
Login: testLogin,
|
|
|
|
Password: testPassword,
|
|
|
|
}
|
2020-04-15 04:11:45 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
adminCreateUserScenario(t, "Should create the user", "/api/admin/users", "/api/admin/users", createCmd, func(sc *scenarioContext) {
|
|
|
|
bus.ClearBusHandlers()
|
2020-04-15 04:11:45 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
var userLogin string
|
|
|
|
var orgID int64
|
2020-04-15 04:11:45 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
bus.AddHandler("test", func(cmd *models.CreateUserCommand) error {
|
|
|
|
userLogin = cmd.Login
|
|
|
|
orgID = cmd.OrgId
|
2020-04-15 04:11:45 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
if orgID == nonExistingOrgID {
|
|
|
|
return models.ErrOrgNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Result = models.User{Id: testUserID}
|
|
|
|
return nil
|
|
|
|
})
|
2020-04-15 04:11:45 -05:00
|
|
|
|
|
|
|
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
2020-04-15 04:11:45 -05:00
|
|
|
|
|
|
|
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
2020-11-13 02:52:38 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, testUserID, respJSON.Get("id").MustInt64())
|
|
|
|
assert.Equal(t, "User created", respJSON.Get("message").MustString())
|
2020-04-15 04:11:45 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
// Verify that userLogin and orgID were transmitted correctly to the handler
|
|
|
|
assert.Equal(t, testLogin, userLogin)
|
|
|
|
assert.Equal(t, int64(0), orgID)
|
2020-04-15 04:11:45 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("With an organization", func(t *testing.T) {
|
2020-04-15 04:11:45 -05:00
|
|
|
createCmd := dtos.AdminCreateUserForm{
|
2020-11-13 02:52:38 -06:00
|
|
|
Login: testLogin,
|
|
|
|
Password: testPassword,
|
|
|
|
OrgId: testOrgID,
|
2020-04-15 04:11:45 -05:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
adminCreateUserScenario(t, "Should create the user", "/api/admin/users", "/api/admin/users", createCmd, func(sc *scenarioContext) {
|
|
|
|
bus.ClearBusHandlers()
|
|
|
|
|
|
|
|
var userLogin string
|
|
|
|
var orgID int64
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(cmd *models.CreateUserCommand) error {
|
|
|
|
userLogin = cmd.Login
|
|
|
|
orgID = cmd.OrgId
|
|
|
|
|
|
|
|
if orgID == nonExistingOrgID {
|
|
|
|
return models.ErrOrgNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Result = models.User{Id: testUserID}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-04-15 04:11:45 -05:00
|
|
|
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
2020-04-15 04:11:45 -05:00
|
|
|
|
|
|
|
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
2020-11-13 02:52:38 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, testUserID, respJSON.Get("id").MustInt64())
|
|
|
|
assert.Equal(t, "User created", respJSON.Get("message").MustString())
|
2020-04-15 04:11:45 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, testLogin, userLogin)
|
|
|
|
assert.Equal(t, testOrgID, orgID)
|
2020-04-15 04:11:45 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("With a nonexistent organization", func(t *testing.T) {
|
2020-04-15 04:11:45 -05:00
|
|
|
createCmd := dtos.AdminCreateUserForm{
|
2020-11-13 02:52:38 -06:00
|
|
|
Login: testLogin,
|
|
|
|
Password: testPassword,
|
2020-04-15 04:11:45 -05:00
|
|
|
OrgId: nonExistingOrgID,
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
adminCreateUserScenario(t, "Should create the user", "/api/admin/users", "/api/admin/users", createCmd, func(sc *scenarioContext) {
|
|
|
|
bus.ClearBusHandlers()
|
|
|
|
|
|
|
|
var userLogin string
|
|
|
|
var orgID int64
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(cmd *models.CreateUserCommand) error {
|
|
|
|
userLogin = cmd.Login
|
|
|
|
orgID = cmd.OrgId
|
|
|
|
|
|
|
|
if orgID == nonExistingOrgID {
|
|
|
|
return models.ErrOrgNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Result = models.User{Id: testUserID}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-04-15 04:11:45 -05:00
|
|
|
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, 400, sc.resp.Code)
|
2020-04-15 04:11:45 -05:00
|
|
|
|
|
|
|
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
2020-11-13 02:52:38 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "organization not found", respJSON.Get("message").MustString())
|
2020-04-15 04:11:45 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, testLogin, userLogin)
|
|
|
|
assert.Equal(t, int64(1000), orgID)
|
2020-04-15 04:11:45 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-08-13 07:38:54 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("When a server admin attempts to create a user with an already existing email/login", func(t *testing.T) {
|
2020-08-13 07:38:54 -05:00
|
|
|
createCmd := dtos.AdminCreateUserForm{
|
2020-11-13 02:52:38 -06:00
|
|
|
Login: testLogin,
|
|
|
|
Password: testPassword,
|
2020-08-13 07:38:54 -05:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
adminCreateUserScenario(t, "Should return an error", "/api/admin/users", "/api/admin/users", createCmd, func(sc *scenarioContext) {
|
|
|
|
bus.ClearBusHandlers()
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(cmd *models.CreateUserCommand) error {
|
|
|
|
return models.ErrUserAlreadyExists
|
|
|
|
})
|
|
|
|
|
2020-08-13 07:38:54 -05:00
|
|
|
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, 412, sc.resp.Code)
|
2020-08-13 07:38:54 -05:00
|
|
|
|
|
|
|
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
2020-11-13 02:52:38 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "user already exists", respJSON.Get("error").MustString())
|
2020-08-13 07:38:54 -05:00
|
|
|
})
|
|
|
|
})
|
2018-12-01 11:28:10 -06:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func putAdminScenario(t *testing.T, desc string, url string, routePattern string, role models.RoleType,
|
|
|
|
cmd dtos.AdminUpdateUserPermissionsForm, fn scenarioFunc) {
|
|
|
|
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
|
|
|
t.Cleanup(bus.ClearBusHandlers)
|
2018-12-01 11:28:10 -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-12-01 11:28:10 -06:00
|
|
|
sc.context = c
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.context.UserId = testUserID
|
|
|
|
sc.context.OrgId = testOrgID
|
2018-12-01 11:28:10 -06:00
|
|
|
sc.context.OrgRole = role
|
|
|
|
|
2020-07-29 11:00:14 -05:00
|
|
|
return AdminUpdateUserPermissions(c, cmd)
|
2018-12-01 11:28:10 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
sc.m.Put(routePattern, sc.defaultHandler)
|
|
|
|
|
|
|
|
fn(sc)
|
|
|
|
})
|
|
|
|
}
|
2019-03-08 08:15:38 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func adminLogoutUserScenario(t *testing.T, desc string, url string, routePattern string, fn scenarioFunc) {
|
|
|
|
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
|
|
|
t.Cleanup(bus.ClearBusHandlers)
|
2019-03-08 08:15:38 -06:00
|
|
|
|
|
|
|
hs := HTTPServer{
|
|
|
|
Bus: bus.GetBus(),
|
|
|
|
AuthTokenService: auth.NewFakeUserAuthTokenService(),
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Log("Route handler invoked", "url", c.Req.URL)
|
|
|
|
|
2019-03-08 08:15:38 -06:00
|
|
|
sc.context = c
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.context.UserId = testUserID
|
|
|
|
sc.context.OrgId = testOrgID
|
2020-03-04 05:57:20 -06:00
|
|
|
sc.context.OrgRole = models.ROLE_ADMIN
|
2019-03-08 08:15:38 -06:00
|
|
|
|
|
|
|
return hs.AdminLogoutUser(c)
|
|
|
|
})
|
|
|
|
|
|
|
|
sc.m.Post(routePattern, sc.defaultHandler)
|
|
|
|
|
|
|
|
fn(sc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func adminRevokeUserAuthTokenScenario(t *testing.T, desc string, url string, routePattern string, cmd models.RevokeAuthTokenCmd, fn scenarioFunc) {
|
|
|
|
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
|
|
|
t.Cleanup(bus.ClearBusHandlers)
|
2019-03-08 08:15:38 -06:00
|
|
|
|
|
|
|
fakeAuthTokenService := auth.NewFakeUserAuthTokenService()
|
|
|
|
|
|
|
|
hs := HTTPServer{
|
|
|
|
Bus: bus.GetBus(),
|
|
|
|
AuthTokenService: fakeAuthTokenService,
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc := setupScenarioContext(t, url)
|
2019-03-08 08:15:38 -06:00
|
|
|
sc.userAuthTokenService = fakeAuthTokenService
|
2021-01-15 07:43:20 -06:00
|
|
|
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
|
2019-03-08 08:15:38 -06:00
|
|
|
sc.context = c
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.context.UserId = testUserID
|
|
|
|
sc.context.OrgId = testOrgID
|
2020-03-04 05:57:20 -06:00
|
|
|
sc.context.OrgRole = models.ROLE_ADMIN
|
2019-03-08 08:15:38 -06:00
|
|
|
|
|
|
|
return hs.AdminRevokeUserAuthToken(c, cmd)
|
|
|
|
})
|
|
|
|
|
|
|
|
sc.m.Post(routePattern, sc.defaultHandler)
|
|
|
|
|
|
|
|
fn(sc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func adminGetUserAuthTokensScenario(t *testing.T, desc string, url string, routePattern string, fn scenarioFunc) {
|
|
|
|
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
|
|
|
t.Cleanup(bus.ClearBusHandlers)
|
2019-03-08 08:15:38 -06:00
|
|
|
|
|
|
|
fakeAuthTokenService := auth.NewFakeUserAuthTokenService()
|
|
|
|
|
|
|
|
hs := HTTPServer{
|
|
|
|
Bus: bus.GetBus(),
|
|
|
|
AuthTokenService: fakeAuthTokenService,
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc := setupScenarioContext(t, url)
|
2019-03-08 08:15:38 -06:00
|
|
|
sc.userAuthTokenService = fakeAuthTokenService
|
2021-01-15 07:43:20 -06:00
|
|
|
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
|
2019-03-08 08:15:38 -06:00
|
|
|
sc.context = c
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.context.UserId = testUserID
|
|
|
|
sc.context.OrgId = testOrgID
|
2020-03-04 05:57:20 -06:00
|
|
|
sc.context.OrgRole = models.ROLE_ADMIN
|
2019-03-08 08:15:38 -06:00
|
|
|
|
|
|
|
return hs.AdminGetUserAuthTokens(c)
|
|
|
|
})
|
|
|
|
|
|
|
|
sc.m.Get(routePattern, sc.defaultHandler)
|
|
|
|
|
|
|
|
fn(sc)
|
|
|
|
})
|
|
|
|
}
|
2019-05-21 06:52:49 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func adminDisableUserScenario(t *testing.T, desc string, action string, url string, routePattern string, fn scenarioFunc) {
|
|
|
|
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
|
|
|
t.Cleanup(bus.ClearBusHandlers)
|
2019-05-21 06:52:49 -05:00
|
|
|
|
2019-05-23 07:54:47 -05:00
|
|
|
fakeAuthTokenService := auth.NewFakeUserAuthTokenService()
|
|
|
|
|
|
|
|
hs := HTTPServer{
|
|
|
|
Bus: bus.GetBus(),
|
|
|
|
AuthTokenService: fakeAuthTokenService,
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-05-21 06:52:49 -05:00
|
|
|
sc.context = c
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.context.UserId = testUserID
|
2019-05-21 06:52:49 -05:00
|
|
|
|
|
|
|
if action == "enable" {
|
2019-05-23 07:54:47 -05:00
|
|
|
return AdminEnableUser(c)
|
2019-05-21 06:52:49 -05:00
|
|
|
}
|
2019-05-23 07:54:47 -05:00
|
|
|
|
|
|
|
return hs.AdminDisableUser(c)
|
2019-05-21 06:52:49 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
sc.m.Post(routePattern, sc.defaultHandler)
|
|
|
|
|
|
|
|
fn(sc)
|
|
|
|
})
|
|
|
|
}
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func adminDeleteUserScenario(t *testing.T, desc string, url string, routePattern string, fn scenarioFunc) {
|
|
|
|
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
|
|
|
t.Cleanup(bus.ClearBusHandlers)
|
2020-01-10 04:43:44 -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 {
|
2020-01-10 04:43:44 -06:00
|
|
|
sc.context = c
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.context.UserId = testUserID
|
2020-01-10 04:43:44 -06:00
|
|
|
|
2020-07-29 11:00:14 -05:00
|
|
|
return AdminDeleteUser(c)
|
2020-01-10 04:43:44 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
sc.m.Delete(routePattern, sc.defaultHandler)
|
|
|
|
|
|
|
|
fn(sc)
|
|
|
|
})
|
|
|
|
}
|
2020-04-15 04:11:45 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func adminCreateUserScenario(t *testing.T, desc string, url string, routePattern string, cmd dtos.AdminCreateUserForm, fn scenarioFunc) {
|
|
|
|
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
|
|
|
t.Cleanup(bus.ClearBusHandlers)
|
2020-04-15 04:11:45 -05: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 {
|
2020-04-15 04:11:45 -05:00
|
|
|
sc.context = c
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.context.UserId = testUserID
|
2020-04-15 04:11:45 -05:00
|
|
|
|
2020-07-29 11:00:14 -05:00
|
|
|
return AdminCreateUser(c, cmd)
|
2020-04-15 04:11:45 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
sc.m.Post(routePattern, sc.defaultHandler)
|
|
|
|
|
|
|
|
fn(sc)
|
|
|
|
})
|
|
|
|
}
|