[MM-51554] Make LDAP sync job more resilient against errors (#25496)

Этот коммит содержится в:
Ben Schumacher
2024-04-25 12:38:19 +02:00
коммит произвёл GitHub
родитель 9ecb3e20c8
Коммит bac05a273d
5 изменённых файлов: 164 добавлений и 102 удалений

Просмотреть файл

@@ -4,13 +4,18 @@
package app
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/channels/store"
)
//nolint:govet // The setup code leads to a lot of variable shadowing.
func TestCreateDefaultMemberships(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
@@ -105,7 +110,7 @@ func TestCreateDefaultMemberships(t *testing.T) {
pErr := th.App.CreateDefaultMemberships(th.Context, model.CreateDefaultMembershipParams{Since: 0, ReAddRemovedMembers: false})
if pErr != nil {
t.Errorf("faild to populate syncables: %s", pErr.Error())
t.Errorf("failed to populate syncables: %s", pErr.Error())
}
// Singer should be in team and channel
@@ -175,7 +180,7 @@ func TestCreateDefaultMemberships(t *testing.T) {
// Sync everything after syncable was created (proving that team updates trigger re-sync)
pErr = th.App.CreateDefaultMemberships(th.Context, model.CreateDefaultMembershipParams{Since: scientistGroupMember.CreateAt + 1, ReAddRemovedMembers: false})
if pErr != nil {
t.Errorf("faild to populate syncables: %s", pErr.Error())
t.Errorf("failed to populate syncables: %s", pErr.Error())
}
// Scientist should be in team but not the channel
@@ -218,7 +223,7 @@ func TestCreateDefaultMemberships(t *testing.T) {
// Sync everything after syncable was created (proving that channel updates trigger re-sync)
pErr = th.App.CreateDefaultMemberships(th.Context, model.CreateDefaultMembershipParams{Since: scientistGroupMember.CreateAt + 1, ReAddRemovedMembers: false})
if pErr != nil {
t.Errorf("faild to populate syncables: %s", pErr.Error())
t.Errorf("failed to populate syncables: %s", pErr.Error())
}
expected = 1
@@ -243,7 +248,7 @@ func TestCreateDefaultMemberships(t *testing.T) {
// Even re-syncing from the beginning doesn't re-add to channel or team
pErr = th.App.CreateDefaultMemberships(th.Context, model.CreateDefaultMembershipParams{Since: 0, ReAddRemovedMembers: false})
if pErr != nil {
t.Errorf("faild to populate syncables: %s", pErr.Error())
t.Errorf("failed to populate syncables: %s", pErr.Error())
}
// Singer should not be in team or channel
@@ -284,7 +289,7 @@ func TestCreateDefaultMemberships(t *testing.T) {
pErr = th.App.CreateDefaultMemberships(th.Context, model.CreateDefaultMembershipParams{Since: 0, ReAddRemovedMembers: false})
if pErr != nil {
t.Errorf("faild to populate syncables: %s", pErr.Error())
t.Errorf("failed to populate syncables: %s", pErr.Error())
}
timeBeforeLeaving := model.GetMillis()
@@ -482,6 +487,46 @@ func TestCreateDefaultMemberships(t *testing.T) {
t.Errorf("expected 2 channel member on team1Channel1, got %d", len(team1Channel1Members))
}
})
t.Run("error should contain a information about all users that failed", func(t *testing.T) {
user1 := th.CreateUser()
_, err = th.App.UpsertGroupMember(scienceGroup.Id, user1.Id)
require.Nil(t, err)
user2 := th.CreateUser()
_, err = th.App.UpsertGroupMember(scienceGroup.Id, user2.Id)
require.Nil(t, err)
store := &mockStore{
Store: th.App.Srv().Store(),
us: &mokeUserStore{
UserStore: th.App.Srv().Store().User(),
},
}
require.Nil(t, err)
th.App.Srv().platform.Store = store
nErr = th.App.CreateDefaultMemberships(th.Context, model.CreateDefaultMembershipParams{Since: 0, ReAddRemovedMembers: false})
require.Error(t, nErr)
assert.ErrorContains(t, nErr, "failed to add team member for default team membership")
assert.ErrorContains(t, nErr, user1.Id)
assert.ErrorContains(t, nErr, user2.Id)
})
}
type mockStore struct {
store.Store
us store.UserStore
}
func (fk *mockStore) User() store.UserStore { return fk.us }
type mokeUserStore struct {
store.UserStore
}
func (us *mokeUserStore) Get(_ context.Context, id string) (*model.User, error) {
return nil, fmt.Errorf("some error for %s", id)
}
func TestDeleteGroupMemberships(t *testing.T) {
@@ -601,10 +646,10 @@ func TestSyncSyncableRoles(t *testing.T) {
_, err = th.App.UpdateGroupSyncable(channelSyncable)
require.Nil(t, err)
err = th.App.SyncSyncableRoles(channel.Id, model.GroupSyncableTypeChannel)
err = th.App.SyncSyncableRoles(th.Context, channel.Id, model.GroupSyncableTypeChannel)
require.Nil(t, err)
err = th.App.SyncSyncableRoles(team.Id, model.GroupSyncableTypeTeam)
err = th.App.SyncSyncableRoles(th.Context, team.Id, model.GroupSyncableTypeTeam)
require.Nil(t, err)
for _, user := range []*model.User{user1, user2} {