mirror of
https://github.com/grafana/grafana.git
synced 2025-02-20 11:48:34 -06:00
* Remove user from preferences, stars, orguser, team member * Fix lint * Add Delete user from org and dashboard acl * Delete user from user auth * Add DeleteUser to quota * Add test files and adjust user auth store * Rename package in wire for user auth * Import Quota Service interface in other services * do the same in tests * fix lint tests * Fix tests * Add some tests * Rename InsertUser and DeleteUser to InsertOrgUser and DeleteOrgUser * Rename DeleteUser to DeleteByUser in quota * changing a method name in few additional places * Fix in other places * Fix lint * Fix tests * Chore: Split Delete User method * Add fakes for userauth * Add mock for access control Delete User permossion, use interface * Use interface for ream guardian * Add simple fake for dashboard acl * Add go routines, clean up, use interfaces * fix lint * Update pkg/services/user/userimpl/user_test.go Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com> * Update pkg/services/user/userimpl/user_test.go Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com> * Update pkg/services/user/userimpl/user_test.go Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com> * Add wrapper for not service account error * fix indentation * Use fmt for error wrapper Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com>
108 lines
3.5 KiB
Go
108 lines
3.5 KiB
Go
package userimpl
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol/mock"
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
|
"github.com/grafana/grafana/pkg/services/org/orgtest"
|
|
"github.com/grafana/grafana/pkg/services/preference/preftest"
|
|
"github.com/grafana/grafana/pkg/services/quota/quotatest"
|
|
"github.com/grafana/grafana/pkg/services/star/startest"
|
|
"github.com/grafana/grafana/pkg/services/teamguardian/manager"
|
|
"github.com/grafana/grafana/pkg/services/user"
|
|
"github.com/grafana/grafana/pkg/services/userauth/userauthtest"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestUserService(t *testing.T) {
|
|
userStore := newUserStoreFake()
|
|
orgService := orgtest.NewOrgServiceFake()
|
|
starService := startest.NewStarServiceFake()
|
|
dashboardService := dashboards.NewFakeDashboardService(t)
|
|
preferenceService := preftest.NewPreferenceServiceFake()
|
|
teamMemberService := manager.NewTeamGuardianMock()
|
|
userAuthService := userauthtest.NewFakeUserAuthService()
|
|
quotaService := quotatest.NewQuotaServiceFake()
|
|
accessControlStore := mock.New()
|
|
userService := Service{
|
|
store: userStore,
|
|
orgService: orgService,
|
|
starService: starService,
|
|
dashboardService: dashboardService,
|
|
preferenceService: preferenceService,
|
|
teamMemberService: teamMemberService,
|
|
userAuthService: userAuthService,
|
|
quotaService: quotaService,
|
|
accessControlStore: accessControlStore,
|
|
}
|
|
|
|
t.Run("create user", func(t *testing.T) {
|
|
_, err := userService.Create(context.Background(), &user.CreateUserCommand{})
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
t.Run("delete user store returns error", func(t *testing.T) {
|
|
userStore.ExpectedDeleteUserError = models.ErrUserNotFound
|
|
t.Cleanup(func() {
|
|
userStore.ExpectedDeleteUserError = nil
|
|
})
|
|
err := userService.Delete(context.Background(), &user.DeleteUserCommand{UserID: 1})
|
|
require.Error(t, err, models.ErrUserNotFound)
|
|
})
|
|
|
|
t.Run("delete user returns from team", func(t *testing.T) {
|
|
teamMemberService.ExpectedError = errors.New("some error")
|
|
t.Cleanup(func() {
|
|
teamMemberService.ExpectedError = nil
|
|
})
|
|
err := userService.Delete(context.Background(), &user.DeleteUserCommand{UserID: 1})
|
|
require.Error(t, err)
|
|
})
|
|
|
|
t.Run("delete user returns from team and pref", func(t *testing.T) {
|
|
teamMemberService.ExpectedError = errors.New("some error")
|
|
preferenceService.ExpectedError = errors.New("some error 2")
|
|
t.Cleanup(func() {
|
|
teamMemberService.ExpectedError = nil
|
|
preferenceService.ExpectedError = nil
|
|
})
|
|
err := userService.Delete(context.Background(), &user.DeleteUserCommand{UserID: 1})
|
|
require.Error(t, err)
|
|
})
|
|
|
|
t.Run("delete user successfully", func(t *testing.T) {
|
|
err := userService.Delete(context.Background(), &user.DeleteUserCommand{UserID: 1})
|
|
require.NoError(t, err)
|
|
})
|
|
}
|
|
|
|
type FakeUserStore struct {
|
|
ExpectedUser *user.User
|
|
ExpectedError error
|
|
ExpectedDeleteUserError error
|
|
}
|
|
|
|
func newUserStoreFake() *FakeUserStore {
|
|
return &FakeUserStore{}
|
|
}
|
|
|
|
func (f *FakeUserStore) Get(ctx context.Context, query *user.User) (*user.User, error) {
|
|
return f.ExpectedUser, f.ExpectedError
|
|
}
|
|
|
|
func (f *FakeUserStore) Insert(ctx context.Context, query *user.User) (int64, error) {
|
|
return 0, f.ExpectedError
|
|
}
|
|
|
|
func (f *FakeUserStore) Delete(ctx context.Context, userID int64) error {
|
|
return f.ExpectedDeleteUserError
|
|
}
|
|
|
|
func (f *FakeUserStore) GetNotServiceAccount(ctx context.Context, userID int64) (*user.User, error) {
|
|
return f.ExpectedUser, f.ExpectedError
|
|
}
|