Commit 551f9608 by Tania B Committed by GitHub

Remove the bus from teamgroupsync (#29810)

* Add fake TeamSyncService

* Change SyncTeams dispatch to direct method call

* Remove SyncTeamsCommand struct

* Move TeamSyncService to a separate package

* Remove context from SyncTeams args

* Add comments to teamsyncgroup package

Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>

* Replace TeamSyncService with TeamSyncFunc

* Remove unnecessary error check

* Add a test

* Fix test input

Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
parent 12211e4a
...@@ -122,8 +122,3 @@ type GetTeamsForLDAPGroupCommand struct { ...@@ -122,8 +122,3 @@ type GetTeamsForLDAPGroupCommand struct {
Groups []string Groups []string
Result []TeamOrgGroupDTO Result []TeamOrgGroupDTO
} }
type SyncTeamsCommand struct {
ExternalUser *ExternalUserInfo
User *User
}
...@@ -18,9 +18,12 @@ var ( ...@@ -18,9 +18,12 @@ var (
logger = log.New("login.ext_user") logger = log.New("login.ext_user")
) )
type TeamSyncFunc func(user *models.User, externalUser *models.ExternalUserInfo) error
type LoginService struct { type LoginService struct {
Bus bus.Bus `inject:""` Bus bus.Bus `inject:""`
QuotaService *quota.QuotaService `inject:""` QuotaService *quota.QuotaService `inject:""`
TeamSync TeamSyncFunc
} }
func (ls *LoginService) Init() error { func (ls *LoginService) Init() error {
...@@ -109,12 +112,11 @@ func (ls *LoginService) UpsertUser(cmd *models.UpsertUserCommand) error { ...@@ -109,12 +112,11 @@ func (ls *LoginService) UpsertUser(cmd *models.UpsertUserCommand) error {
} }
} }
err := ls.Bus.Dispatch(&models.SyncTeamsCommand{ if ls.TeamSync != nil {
User: cmd.Result, err := ls.TeamSync(cmd.Result, extUser)
ExternalUser: extUser, if err != nil {
}) return err
if err != nil && !errors.Is(err, bus.ErrHandlerNotFound) { }
return err
} }
return nil return nil
......
package login package login
import ( import (
"errors"
"testing" "testing"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/quota"
log "github.com/inconshreveable/log15" log "github.com/inconshreveable/log15"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
...@@ -74,6 +76,60 @@ func Test_syncOrgRoles_whenTryingToRemoveLastOrgLogsError(t *testing.T) { ...@@ -74,6 +76,60 @@ func Test_syncOrgRoles_whenTryingToRemoveLastOrgLogsError(t *testing.T) {
assert.Contains(t, logs, models.ErrLastOrgAdmin.Error()) assert.Contains(t, logs, models.ErrLastOrgAdmin.Error())
} }
func Test_teamSync(t *testing.T) {
login := LoginService{
Bus: bus.New(),
QuotaService: &quota.QuotaService{},
}
upserCmd := &models.UpsertUserCommand{ExternalUser: &models.ExternalUserInfo{Email: "test_user@example.org"}}
expectedUser := &models.User{
Id: 1,
Email: "test_user@example.org",
Name: "test_user",
Login: "test_user",
}
bus.ClearBusHandlers()
t.Cleanup(func() { bus.ClearBusHandlers() })
bus.AddHandler("test", func(query *models.GetUserByAuthInfoQuery) error {
query.Result = expectedUser
return nil
})
var actualUser *models.User
var actualExternalUser *models.ExternalUserInfo
t.Run("login.TeamSync should not be called when nil", func(t *testing.T) {
err := login.UpsertUser(upserCmd)
require.Nil(t, err)
assert.Nil(t, actualUser)
assert.Nil(t, actualExternalUser)
t.Run("login.TeamSync should be called when not nil", func(t *testing.T) {
teamSyncFunc := func(user *models.User, externalUser *models.ExternalUserInfo) error {
actualUser = user
actualExternalUser = externalUser
return nil
}
login.TeamSync = teamSyncFunc
err := login.UpsertUser(upserCmd)
require.Nil(t, err)
assert.Equal(t, actualUser, expectedUser)
assert.Equal(t, actualExternalUser, upserCmd.ExternalUser)
})
t.Run("login.TeamSync should propagate its errors to the caller", func(t *testing.T) {
teamSyncFunc := func(user *models.User, externalUser *models.ExternalUserInfo) error {
return errors.New("teamsync test error")
}
login.TeamSync = teamSyncFunc
err := login.UpsertUser(upserCmd)
require.Error(t, err)
})
})
}
func createSimpleUser() models.User { func createSimpleUser() models.User {
user := models.User{ user := models.User{
Id: 1, Id: 1,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment