Chore: Remove bus from admin users (#44869)
* Chore: Remove bus from admin users * Mock authinfoservice * Update user id * attempt to fix the tests in admin users api * fix type cast * revert skipped tests Co-authored-by: Serge Zaitsev <serge.zaitsev@grafana.com>
This commit is contained in:
+43
-49
@@ -28,6 +28,20 @@ const (
|
||||
existingTestLogin = "existing@example.com"
|
||||
)
|
||||
|
||||
type mockAuthInfoService struct {
|
||||
LatestUserID int64
|
||||
ExpectedError error
|
||||
}
|
||||
|
||||
func (m *mockAuthInfoService) LookupAndUpdate(ctx context.Context, query *models.GetUserByAuthInfoQuery) (*models.User, error) {
|
||||
m.LatestUserID = query.UserId
|
||||
return nil, m.ExpectedError
|
||||
}
|
||||
func (m *mockAuthInfoService) GetAuthInfo(ctx context.Context, query *models.GetAuthInfoQuery) error {
|
||||
m.LatestUserID = query.UserId
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
func TestAdminAPIEndpoint(t *testing.T) {
|
||||
const role = models.ROLE_ADMIN
|
||||
|
||||
@@ -96,17 +110,9 @@ func TestAdminAPIEndpoint(t *testing.T) {
|
||||
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(ctx context.Context, cmd *models.GetAuthInfoQuery) error {
|
||||
return models.ErrUserNotFound
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(ctx context.Context, cmd *models.DisableUserCommand) error {
|
||||
userID = cmd.UserId
|
||||
isDisabled = cmd.IsDisabled
|
||||
return models.ErrUserNotFound
|
||||
})
|
||||
store := sc.sqlStore.(*mockstore.SQLStoreMock)
|
||||
sc.authInfoService.ExpectedError = models.ErrUserNotFound
|
||||
store.ExpectedError = models.ErrUserNotFound
|
||||
|
||||
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
||||
|
||||
@@ -116,23 +122,14 @@ func TestAdminAPIEndpoint(t *testing.T) {
|
||||
|
||||
assert.Equal(t, "user not found", respJSON.Get("message").MustString())
|
||||
|
||||
assert.Equal(t, int64(42), userID)
|
||||
assert.Equal(t, false, isDisabled)
|
||||
assert.Equal(t, int64(42), store.LatestUserId)
|
||||
})
|
||||
|
||||
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(ctx context.Context, cmd *models.GetAuthInfoQuery) error {
|
||||
return models.ErrUserNotFound
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(ctx context.Context, cmd *models.DisableUserCommand) error {
|
||||
userID = cmd.UserId
|
||||
isDisabled = cmd.IsDisabled
|
||||
return models.ErrUserNotFound
|
||||
})
|
||||
store := sc.sqlStore.(*mockstore.SQLStoreMock)
|
||||
sc.authInfoService.ExpectedError = models.ErrUserNotFound
|
||||
store.ExpectedError = models.ErrUserNotFound
|
||||
|
||||
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
||||
|
||||
@@ -142,20 +139,13 @@ func TestAdminAPIEndpoint(t *testing.T) {
|
||||
|
||||
assert.Equal(t, "user not found", respJSON.Get("message").MustString())
|
||||
|
||||
assert.Equal(t, int64(42), userID)
|
||||
assert.Equal(t, true, isDisabled)
|
||||
assert.Equal(t, int64(42), store.LatestUserId)
|
||||
})
|
||||
})
|
||||
|
||||
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(ctx context.Context, cmd *models.GetAuthInfoQuery) error {
|
||||
userID = cmd.UserId
|
||||
return nil
|
||||
})
|
||||
|
||||
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
||||
assert.Equal(t, 500, sc.resp.Code)
|
||||
|
||||
@@ -163,17 +153,11 @@ func TestAdminAPIEndpoint(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "Could not disable external user", respJSON.Get("message").MustString())
|
||||
|
||||
assert.Equal(t, int64(42), userID)
|
||||
assert.Equal(t, int64(42), sc.authInfoService.LatestUserID)
|
||||
})
|
||||
|
||||
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(ctx context.Context, cmd *models.GetAuthInfoQuery) error {
|
||||
userID = cmd.UserId
|
||||
return nil
|
||||
})
|
||||
|
||||
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
||||
assert.Equal(t, 500, sc.resp.Code)
|
||||
|
||||
@@ -181,6 +165,7 @@ func TestAdminAPIEndpoint(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "Could not enable external user", respJSON.Get("message").MustString())
|
||||
|
||||
userID := sc.authInfoService.LatestUserID
|
||||
assert.Equal(t, int64(42), userID)
|
||||
})
|
||||
})
|
||||
@@ -188,13 +173,10 @@ func TestAdminAPIEndpoint(t *testing.T) {
|
||||
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(ctx context.Context, cmd *models.DeleteUserCommand) error {
|
||||
userID = cmd.UserId
|
||||
return models.ErrUserNotFound
|
||||
})
|
||||
|
||||
sc.sqlStore.(*mockstore.SQLStoreMock).ExpectedError = models.ErrUserNotFound
|
||||
sc.authInfoService.ExpectedError = models.ErrUserNotFound
|
||||
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
|
||||
userID := sc.sqlStore.(*mockstore.SQLStoreMock).LatestUserId
|
||||
|
||||
assert.Equal(t, 404, sc.resp.Code)
|
||||
|
||||
@@ -291,8 +273,9 @@ func putAdminScenario(t *testing.T, desc string, url string, routePattern string
|
||||
t.Cleanup(bus.ClearBusHandlers)
|
||||
|
||||
hs := &HTTPServer{
|
||||
Cfg: setting.NewCfg(),
|
||||
SQLStore: sqlStore,
|
||||
Cfg: setting.NewCfg(),
|
||||
SQLStore: sqlStore,
|
||||
authInfoService: &mockAuthInfoService{},
|
||||
}
|
||||
|
||||
sc := setupScenarioContext(t, url)
|
||||
@@ -405,18 +388,24 @@ func adminDisableUserScenario(t *testing.T, desc string, action string, url stri
|
||||
|
||||
fakeAuthTokenService := auth.NewFakeUserAuthTokenService()
|
||||
|
||||
authInfoService := &mockAuthInfoService{}
|
||||
|
||||
hs := HTTPServer{
|
||||
Bus: bus.GetBus(),
|
||||
SQLStore: mockstore.NewSQLStoreMock(),
|
||||
AuthTokenService: fakeAuthTokenService,
|
||||
authInfoService: authInfoService,
|
||||
}
|
||||
|
||||
sc := setupScenarioContext(t, url)
|
||||
sc.sqlStore = hs.SQLStore
|
||||
sc.authInfoService = authInfoService
|
||||
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
|
||||
sc.context = c
|
||||
sc.context.UserId = testUserID
|
||||
|
||||
if action == "enable" {
|
||||
return AdminEnableUser(c)
|
||||
return hs.AdminEnableUser(c)
|
||||
}
|
||||
|
||||
return hs.AdminDisableUser(c)
|
||||
@@ -429,15 +418,20 @@ func adminDisableUserScenario(t *testing.T, desc string, action string, url stri
|
||||
}
|
||||
|
||||
func adminDeleteUserScenario(t *testing.T, desc string, url string, routePattern string, fn scenarioFunc) {
|
||||
hs := HTTPServer{
|
||||
SQLStore: mockstore.NewSQLStoreMock(),
|
||||
}
|
||||
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
||||
t.Cleanup(bus.ClearBusHandlers)
|
||||
|
||||
sc := setupScenarioContext(t, url)
|
||||
sc.sqlStore = hs.SQLStore
|
||||
sc.authInfoService = &mockAuthInfoService{}
|
||||
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
|
||||
sc.context = c
|
||||
sc.context.UserId = testUserID
|
||||
|
||||
return AdminDeleteUser(c)
|
||||
return hs.AdminDeleteUser(c)
|
||||
})
|
||||
|
||||
sc.m.Delete(routePattern, sc.defaultHandler)
|
||||
|
||||
Reference in New Issue
Block a user