[MM-17213] Fix domain check when inviting guests and update user checks (#11793)

* Add update user check for guests and tests

* [MM-17213] Fix domain check when inviting guests

* Fix i18n strings

* Fix tests

* Add missing translation

* Adds test case for non guest user
Этот коммит содержится в:
Miguel de la Cruz
2019-08-14 16:07:58 +02:00
коммит произвёл GitHub
родитель da768d677e
Коммит b36eb415d1
5 изменённых файлов: 74 добавлений и 40 удалений

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

@@ -2305,10 +2305,12 @@ func TestInviteGuestsToTeam(t *testing.T) {
enableEmailInvitations := *th.App.Config().ServiceSettings.EnableEmailInvitations
restrictCreationToDomains := th.App.Config().TeamSettings.RestrictCreationToDomains
guestRestrictCreationToDomains := th.App.Config().GuestAccountsSettings.RestrictCreationToDomains
enableGuestAccounts := *th.App.Config().GuestAccountsSettings.Enable
defer func() {
th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableEmailInvitations = &enableEmailInvitations })
th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.RestrictCreationToDomains = restrictCreationToDomains })
th.App.UpdateConfig(func(cfg *model.Config) { cfg.GuestAccountsSettings.RestrictCreationToDomains = guestRestrictCreationToDomains })
th.App.UpdateConfig(func(cfg *model.Config) { cfg.GuestAccountsSettings.Enable = &enableGuestAccounts })
}()
@@ -2378,45 +2380,26 @@ func TestInviteGuestsToTeam(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.RestrictCreationToDomains = "@global.com,@common.com" })
t.Run("restricted domains", func(t *testing.T) {
t.Run("team domain restrictions should not affect inviting guests", func(t *testing.T) {
err := th.App.InviteGuestsToChannels(th.BasicTeam.Id, &model.GuestsInvite{Emails: emailList, Channels: []string{th.BasicChannel.Id}, Message: "test message"}, th.BasicUser.Id)
if err == nil {
t.Fatal("Adding users with non-restricted domains was allowed")
}
if err.Where != "InviteGuestsToChannels" || err.Id != "api.team.invite_members.invalid_email.app_error" {
t.Log(err)
t.Fatal("Got wrong error message!")
}
require.Nil(t, err, "guest user invites should not be affected by team restrictions")
})
t.Run("override restricted domains", func(t *testing.T) {
th.BasicTeam.AllowedDomains = "invalid.com,common.com"
if _, err := th.App.UpdateTeam(th.BasicTeam); err == nil {
t.Fatal("Should not update the team")
}
t.Run("guest restrictions should affect guest users", func(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.RestrictCreationToDomains = "@guest.com" })
th.BasicTeam.AllowedDomains = "common.com"
if _, err := th.App.UpdateTeam(th.BasicTeam); err != nil {
t.Log(err)
t.Fatal("Should update the team")
}
err := th.App.InviteGuestsToChannels(th.BasicTeam.Id, &model.GuestsInvite{Emails: []string{"guest1@invalid.com"}, Channels: []string{th.BasicChannel.Id}, Message: "test message"}, th.BasicUser.Id)
require.NotNil(t, err, "guest user invites should be affected by the guest domain restrictions")
if err := th.App.InviteGuestsToChannels(th.BasicTeam.Id, &model.GuestsInvite{Emails: []string{"test@global.com"}, Channels: []string{th.BasicChannel.Id}, Message: "test message"}, th.BasicUser.Id); err == nil || err.Where != "InviteGuestsToChannels" {
t.Log(err)
t.Fatal("Per team restriction should take precedence over the global restriction")
}
err = th.App.InviteGuestsToChannels(th.BasicTeam.Id, &model.GuestsInvite{Emails: []string{"guest1@guest.com"}, Channels: []string{th.BasicChannel.Id}, Message: "test message"}, th.BasicUser.Id)
require.Nil(t, err, "whitelisted guest user email should be allowed by the guest domain restrictions")
})
if err := th.App.InviteGuestsToChannels(th.BasicTeam.Id, &model.GuestsInvite{Emails: []string{"test@common.com"}, Channels: []string{th.BasicChannel.Id}, Message: "test message"}, th.BasicUser.Id); err != nil {
t.Log(err)
t.Fatal("Failed to invite user which was common between team and global domain restriction")
}
if err := th.App.InviteGuestsToChannels(th.BasicTeam.Id, &model.GuestsInvite{Emails: []string{"test@invalid.com"}, Channels: []string{th.BasicChannel.Id}, Message: "test message"}, th.BasicUser.Id); err == nil {
t.Log(err)
t.Fatal("Should not invite user")
}
t.Run("guest restrictions should not affect inviting new team members", func(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.RestrictCreationToDomains = "@guest.com" })
err := th.App.InviteNewUsersToTeam([]string{"user@global.com"}, th.BasicTeam.Id, th.BasicUser.Id)
require.Nil(t, err, "non guest user invites should not be affected by the guest domain restrictions")
})
}