@@ -206,7 +206,8 @@ func ProvideService(plugCtxProvider *plugincontext.Provider, cfg *setting.Cfg, r
|
||||
// This can be unreasonable to have in production scenario with many
|
||||
// organizations.
|
||||
orgQuery := &models.SearchOrgsQuery{}
|
||||
err := sqlstore.SearchOrgs(context.Background(), orgQuery)
|
||||
|
||||
err := sqlStore.SearchOrgs(context.Background(), orgQuery)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't get org list: %w", err)
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ var (
|
||||
logger = log.New("login.ext_user")
|
||||
)
|
||||
|
||||
func ProvideService(sqlStore *sqlstore.SQLStore, bus bus.Bus, quotaService *quota.QuotaService, authInfoService login.AuthInfoService) *Implementation {
|
||||
func ProvideService(sqlStore sqlstore.Store, bus bus.Bus, quotaService *quota.QuotaService, authInfoService login.AuthInfoService) *Implementation {
|
||||
s := &Implementation{
|
||||
SQLStore: sqlStore,
|
||||
Bus: bus,
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package loginservice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/login"
|
||||
)
|
||||
|
||||
type LoginServiceMock struct {
|
||||
login.Service
|
||||
ExpectedUserForm dtos.AdminCreateUserForm
|
||||
NoExistingOrgId int64
|
||||
AlreadyExitingLogin string
|
||||
GeneratedUserId int64
|
||||
}
|
||||
|
||||
func (s LoginServiceMock) CreateUser(cmd models.CreateUserCommand) (*models.User, error) {
|
||||
if cmd.OrgId == s.NoExistingOrgId {
|
||||
return nil, models.ErrOrgNotFound
|
||||
}
|
||||
|
||||
if cmd.Login == s.AlreadyExitingLogin {
|
||||
return nil, models.ErrUserAlreadyExists
|
||||
}
|
||||
|
||||
if s.ExpectedUserForm.Login == cmd.Login && s.ExpectedUserForm.Email == cmd.Email &&
|
||||
s.ExpectedUserForm.Password == cmd.Password && s.ExpectedUserForm.Name == cmd.Name && s.ExpectedUserForm.OrgId == cmd.OrgId {
|
||||
return &models.User{Id: s.GeneratedUserId}, nil
|
||||
}
|
||||
|
||||
return nil, errors.New("unexpected cmd")
|
||||
}
|
||||
|
||||
func (s LoginServiceMock) UpsertUser(ctx context.Context, cmd *models.UpsertUserCommand) error {
|
||||
return nil
|
||||
}
|
||||
@@ -16,7 +16,6 @@ type Store interface {
|
||||
SetAuthInfo(ctx context.Context, cmd *models.SetAuthInfoCommand) error
|
||||
UpdateAuthInfo(ctx context.Context, cmd *models.UpdateAuthInfoCommand) error
|
||||
DeleteAuthInfo(ctx context.Context, cmd *models.DeleteAuthInfoCommand) error
|
||||
|
||||
GetUserById(id int64) (bool, *models.User, error)
|
||||
GetUser(user *models.User) (bool, error)
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ type SQLStoreMock struct {
|
||||
ExpectedOrgListResponse OrgListResponse
|
||||
ExpectedDashboardSnapshot *models.DashboardSnapshot
|
||||
ExpectedTeamsByUser []*models.TeamDTO
|
||||
ExpectedSearchOrgList []*models.OrgDTO
|
||||
ExpectedError error
|
||||
}
|
||||
|
||||
@@ -628,3 +629,8 @@ func (m *SQLStoreMock) ExpireOldUserInvites(ctx context.Context, cmd *models.Exp
|
||||
func (m *SQLStoreMock) GetDBHealthQuery(ctx context.Context, query *models.GetDBHealthQuery) error {
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
func (m *SQLStoreMock) SearchOrgs(ctx context.Context, query *models.SearchOrgsQuery) error {
|
||||
query.Result = m.ExpectedSearchOrgList
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
@@ -21,11 +21,11 @@ func (ss *SQLStore) addOrgQueryAndCommandHandlers() {
|
||||
bus.AddHandler("sql", ss.UpdateOrg)
|
||||
bus.AddHandler("sql", ss.UpdateOrgAddress)
|
||||
bus.AddHandler("sql", GetOrgByName)
|
||||
bus.AddHandler("sql", SearchOrgs)
|
||||
bus.AddHandler("sql", ss.SearchOrgs)
|
||||
bus.AddHandler("sql", ss.DeleteOrg)
|
||||
}
|
||||
|
||||
func SearchOrgs(ctx context.Context, query *models.SearchOrgsQuery) error {
|
||||
func (ss *SQLStore) SearchOrgs(ctx context.Context, query *models.SearchOrgsQuery) error {
|
||||
query.Result = make([]*models.OrgDTO, 0)
|
||||
sess := x.Table("org")
|
||||
if query.Query != "" {
|
||||
|
||||
@@ -32,7 +32,7 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
}
|
||||
|
||||
query := &models.SearchOrgsQuery{Ids: ids}
|
||||
err = SearchOrgs(context.Background(), query)
|
||||
err = sqlStore.SearchOrgs(context.Background(), query)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(query.Result), 3)
|
||||
@@ -48,7 +48,7 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
|
||||
t.Run("Should be able to search with defaults", func(t *testing.T) {
|
||||
query := &models.SearchOrgsQuery{}
|
||||
err := SearchOrgs(context.Background(), query)
|
||||
err := sqlStore.SearchOrgs(context.Background(), query)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(query.Result), 3)
|
||||
@@ -56,7 +56,7 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
|
||||
t.Run("Should be able to limit search", func(t *testing.T) {
|
||||
query := &models.SearchOrgsQuery{Limit: 1}
|
||||
err := SearchOrgs(context.Background(), query)
|
||||
err := sqlStore.SearchOrgs(context.Background(), query)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(query.Result), 1)
|
||||
@@ -64,7 +64,7 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
|
||||
t.Run("Should be able to limit and paginate search", func(t *testing.T) {
|
||||
query := &models.SearchOrgsQuery{Limit: 2, Page: 1}
|
||||
err := SearchOrgs(context.Background(), query)
|
||||
err := sqlStore.SearchOrgs(context.Background(), query)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(query.Result), 1)
|
||||
|
||||
@@ -151,4 +151,5 @@ type Store interface {
|
||||
GetTempUserByCode(ctx context.Context, query *models.GetTempUserByCodeQuery) error
|
||||
ExpireOldUserInvites(ctx context.Context, cmd *models.ExpireTempUsersCommand) error
|
||||
GetDBHealthQuery(ctx context.Context, query *models.GetDBHealthQuery) error
|
||||
SearchOrgs(ctx context.Context, query *models.SearchOrgsQuery) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user