123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763 |
- // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
- // See LICENSE.txt for license information.
- package api4
- import (
- "strings"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "github.com/mattermost/mattermost-server/v5/model"
- )
- func TestCreateScheme(t *testing.T) {
- th := Setup(t)
- defer th.TearDown()
- th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
- th.App.SetPhase2PermissionsMigrationStatus(true)
- // Basic test of creating a team scheme.
- scheme1 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_TEAM,
- }
- s1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
- CheckNoError(t, r1)
- assert.Equal(t, s1.DisplayName, scheme1.DisplayName)
- assert.Equal(t, s1.Name, scheme1.Name)
- assert.Equal(t, s1.Description, scheme1.Description)
- assert.NotZero(t, s1.CreateAt)
- assert.Equal(t, s1.CreateAt, s1.UpdateAt)
- assert.Zero(t, s1.DeleteAt)
- assert.Equal(t, s1.Scope, scheme1.Scope)
- assert.NotZero(t, len(s1.DefaultTeamAdminRole))
- assert.NotZero(t, len(s1.DefaultTeamUserRole))
- assert.NotZero(t, len(s1.DefaultTeamGuestRole))
- assert.NotZero(t, len(s1.DefaultChannelAdminRole))
- assert.NotZero(t, len(s1.DefaultChannelUserRole))
- assert.NotZero(t, len(s1.DefaultChannelGuestRole))
- // Check the default roles have been created.
- _, roleRes1 := th.SystemAdminClient.GetRoleByName(s1.DefaultTeamAdminRole)
- CheckNoError(t, roleRes1)
- _, roleRes2 := th.SystemAdminClient.GetRoleByName(s1.DefaultTeamUserRole)
- CheckNoError(t, roleRes2)
- _, roleRes3 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelAdminRole)
- CheckNoError(t, roleRes3)
- _, roleRes4 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelUserRole)
- CheckNoError(t, roleRes4)
- _, roleRes5 := th.SystemAdminClient.GetRoleByName(s1.DefaultTeamGuestRole)
- CheckNoError(t, roleRes5)
- _, roleRes6 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelGuestRole)
- CheckNoError(t, roleRes6)
- // Basic Test of a Channel scheme.
- scheme2 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_CHANNEL,
- }
- s2, r2 := th.SystemAdminClient.CreateScheme(scheme2)
- CheckNoError(t, r2)
- assert.Equal(t, s2.DisplayName, scheme2.DisplayName)
- assert.Equal(t, s2.Name, scheme2.Name)
- assert.Equal(t, s2.Description, scheme2.Description)
- assert.NotZero(t, s2.CreateAt)
- assert.Equal(t, s2.CreateAt, s2.UpdateAt)
- assert.Zero(t, s2.DeleteAt)
- assert.Equal(t, s2.Scope, scheme2.Scope)
- assert.Zero(t, len(s2.DefaultTeamAdminRole))
- assert.Zero(t, len(s2.DefaultTeamUserRole))
- assert.Zero(t, len(s2.DefaultTeamGuestRole))
- assert.NotZero(t, len(s2.DefaultChannelAdminRole))
- assert.NotZero(t, len(s2.DefaultChannelUserRole))
- assert.NotZero(t, len(s2.DefaultChannelGuestRole))
- // Check the default roles have been created.
- _, roleRes7 := th.SystemAdminClient.GetRoleByName(s2.DefaultChannelAdminRole)
- CheckNoError(t, roleRes7)
- _, roleRes8 := th.SystemAdminClient.GetRoleByName(s2.DefaultChannelUserRole)
- CheckNoError(t, roleRes8)
- _, roleRes9 := th.SystemAdminClient.GetRoleByName(s2.DefaultChannelGuestRole)
- CheckNoError(t, roleRes9)
- // Try and create a scheme with an invalid scope.
- scheme3 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.NewId(),
- }
- _, r3 := th.SystemAdminClient.CreateScheme(scheme3)
- CheckBadRequestStatus(t, r3)
- // Try and create a scheme with an invalid display name.
- scheme4 := &model.Scheme{
- DisplayName: strings.Repeat(model.NewId(), 100),
- Name: "Name",
- Description: model.NewId(),
- Scope: model.NewId(),
- }
- _, r4 := th.SystemAdminClient.CreateScheme(scheme4)
- CheckBadRequestStatus(t, r4)
- // Try and create a scheme with an invalid name.
- scheme8 := &model.Scheme{
- DisplayName: "DisplayName",
- Name: strings.Repeat(model.NewId(), 100),
- Description: model.NewId(),
- Scope: model.NewId(),
- }
- _, r8 := th.SystemAdminClient.CreateScheme(scheme8)
- CheckBadRequestStatus(t, r8)
- // Try and create a scheme without the appropriate permissions.
- scheme5 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_TEAM,
- }
- _, r5 := th.Client.CreateScheme(scheme5)
- CheckForbiddenStatus(t, r5)
- // Try and create a scheme without a license.
- th.App.Srv().SetLicense(nil)
- scheme6 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_TEAM,
- }
- _, r6 := th.SystemAdminClient.CreateScheme(scheme6)
- CheckNotImplementedStatus(t, r6)
- th.App.SetPhase2PermissionsMigrationStatus(false)
- th.LoginSystemAdmin()
- th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
- scheme7 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_TEAM,
- }
- _, r7 := th.SystemAdminClient.CreateScheme(scheme7)
- CheckNotImplementedStatus(t, r7)
- }
- func TestGetScheme(t *testing.T) {
- th := Setup(t).InitBasic()
- defer th.TearDown()
- th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
- // Basic test of creating a team scheme.
- scheme1 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_TEAM,
- }
- th.App.SetPhase2PermissionsMigrationStatus(true)
- s1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
- CheckNoError(t, r1)
- assert.Equal(t, s1.DisplayName, scheme1.DisplayName)
- assert.Equal(t, s1.Name, scheme1.Name)
- assert.Equal(t, s1.Description, scheme1.Description)
- assert.NotZero(t, s1.CreateAt)
- assert.Equal(t, s1.CreateAt, s1.UpdateAt)
- assert.Zero(t, s1.DeleteAt)
- assert.Equal(t, s1.Scope, scheme1.Scope)
- assert.NotZero(t, len(s1.DefaultTeamAdminRole))
- assert.NotZero(t, len(s1.DefaultTeamUserRole))
- assert.NotZero(t, len(s1.DefaultTeamGuestRole))
- assert.NotZero(t, len(s1.DefaultChannelAdminRole))
- assert.NotZero(t, len(s1.DefaultChannelUserRole))
- assert.NotZero(t, len(s1.DefaultChannelGuestRole))
- s2, r2 := th.SystemAdminClient.GetScheme(s1.Id)
- CheckNoError(t, r2)
- assert.Equal(t, s1, s2)
- _, r3 := th.SystemAdminClient.GetScheme(model.NewId())
- CheckNotFoundStatus(t, r3)
- _, r4 := th.SystemAdminClient.GetScheme("12345")
- CheckBadRequestStatus(t, r4)
- th.SystemAdminClient.Logout()
- _, r5 := th.SystemAdminClient.GetScheme(s1.Id)
- CheckUnauthorizedStatus(t, r5)
- th.SystemAdminClient.Login(th.SystemAdminUser.Username, th.SystemAdminUser.Password)
- th.App.Srv().SetLicense(nil)
- _, r6 := th.SystemAdminClient.GetScheme(s1.Id)
- CheckNoError(t, r6)
- _, r7 := th.Client.GetScheme(s1.Id)
- CheckForbiddenStatus(t, r7)
- th.App.SetPhase2PermissionsMigrationStatus(false)
- _, r8 := th.SystemAdminClient.GetScheme(s1.Id)
- CheckNotImplementedStatus(t, r8)
- }
- func TestGetSchemes(t *testing.T) {
- th := Setup(t).InitBasic()
- defer th.TearDown()
- th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
- scheme1 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_TEAM,
- }
- scheme2 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_CHANNEL,
- }
- th.App.SetPhase2PermissionsMigrationStatus(true)
- _, r1 := th.SystemAdminClient.CreateScheme(scheme1)
- CheckNoError(t, r1)
- _, r2 := th.SystemAdminClient.CreateScheme(scheme2)
- CheckNoError(t, r2)
- l3, r3 := th.SystemAdminClient.GetSchemes("", 0, 100)
- CheckNoError(t, r3)
- assert.NotZero(t, len(l3))
- l4, r4 := th.SystemAdminClient.GetSchemes("team", 0, 100)
- CheckNoError(t, r4)
- for _, s := range l4 {
- assert.Equal(t, "team", s.Scope)
- }
- l5, r5 := th.SystemAdminClient.GetSchemes("channel", 0, 100)
- CheckNoError(t, r5)
- for _, s := range l5 {
- assert.Equal(t, "channel", s.Scope)
- }
- _, r6 := th.SystemAdminClient.GetSchemes("asdf", 0, 100)
- CheckBadRequestStatus(t, r6)
- th.Client.Logout()
- _, r7 := th.Client.GetSchemes("", 0, 100)
- CheckUnauthorizedStatus(t, r7)
- th.Client.Login(th.BasicUser.Username, th.BasicUser.Password)
- _, r8 := th.Client.GetSchemes("", 0, 100)
- CheckForbiddenStatus(t, r8)
- th.App.SetPhase2PermissionsMigrationStatus(false)
- _, r9 := th.SystemAdminClient.GetSchemes("", 0, 100)
- CheckNotImplementedStatus(t, r9)
- }
- func TestGetTeamsForScheme(t *testing.T) {
- th := Setup(t).InitBasic()
- defer th.TearDown()
- th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
- th.App.SetPhase2PermissionsMigrationStatus(true)
- scheme1 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_TEAM,
- }
- scheme1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
- CheckNoError(t, r1)
- team1 := &model.Team{
- Name: GenerateTestUsername(),
- DisplayName: "A Test Team",
- Type: model.TEAM_OPEN,
- }
- team1, err := th.App.Srv().Store.Team().Save(team1)
- require.Nil(t, err)
- l2, r2 := th.SystemAdminClient.GetTeamsForScheme(scheme1.Id, 0, 100)
- CheckNoError(t, r2)
- assert.Zero(t, len(l2))
- team1.SchemeId = &scheme1.Id
- team1, err = th.App.Srv().Store.Team().Update(team1)
- assert.Nil(t, err)
- l3, r3 := th.SystemAdminClient.GetTeamsForScheme(scheme1.Id, 0, 100)
- CheckNoError(t, r3)
- assert.Len(t, l3, 1)
- assert.Equal(t, team1.Id, l3[0].Id)
- team2 := &model.Team{
- Name: GenerateTestUsername(),
- DisplayName: "B Test Team",
- Type: model.TEAM_OPEN,
- SchemeId: &scheme1.Id,
- }
- team2, err = th.App.Srv().Store.Team().Save(team2)
- require.Nil(t, err)
- l4, r4 := th.SystemAdminClient.GetTeamsForScheme(scheme1.Id, 0, 100)
- CheckNoError(t, r4)
- assert.Len(t, l4, 2)
- assert.Equal(t, team1.Id, l4[0].Id)
- assert.Equal(t, team2.Id, l4[1].Id)
- l5, r5 := th.SystemAdminClient.GetTeamsForScheme(scheme1.Id, 1, 1)
- CheckNoError(t, r5)
- assert.Len(t, l5, 1)
- assert.Equal(t, team2.Id, l5[0].Id)
- // Check various error cases.
- _, ri1 := th.SystemAdminClient.GetTeamsForScheme(model.NewId(), 0, 100)
- CheckNotFoundStatus(t, ri1)
- _, ri2 := th.SystemAdminClient.GetTeamsForScheme("", 0, 100)
- CheckBadRequestStatus(t, ri2)
- th.Client.Logout()
- _, ri3 := th.Client.GetTeamsForScheme(model.NewId(), 0, 100)
- CheckUnauthorizedStatus(t, ri3)
- th.Client.Login(th.BasicUser.Username, th.BasicUser.Password)
- _, ri4 := th.Client.GetTeamsForScheme(model.NewId(), 0, 100)
- CheckForbiddenStatus(t, ri4)
- scheme2 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_CHANNEL,
- }
- scheme2, rs2 := th.SystemAdminClient.CreateScheme(scheme2)
- CheckNoError(t, rs2)
- _, ri5 := th.SystemAdminClient.GetTeamsForScheme(scheme2.Id, 0, 100)
- CheckBadRequestStatus(t, ri5)
- th.App.SetPhase2PermissionsMigrationStatus(false)
- _, ri6 := th.SystemAdminClient.GetTeamsForScheme(scheme1.Id, 0, 100)
- CheckNotImplementedStatus(t, ri6)
- }
- func TestGetChannelsForScheme(t *testing.T) {
- th := Setup(t).InitBasic()
- defer th.TearDown()
- th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
- th.App.SetPhase2PermissionsMigrationStatus(true)
- scheme1 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_CHANNEL,
- }
- scheme1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
- CheckNoError(t, r1)
- channel1 := &model.Channel{
- TeamId: model.NewId(),
- DisplayName: "A Name",
- Name: model.NewId(),
- Type: model.CHANNEL_OPEN,
- }
- channel1, errCh := th.App.Srv().Store.Channel().Save(channel1, 1000000)
- assert.Nil(t, errCh)
- l2, r2 := th.SystemAdminClient.GetChannelsForScheme(scheme1.Id, 0, 100)
- CheckNoError(t, r2)
- assert.Zero(t, len(l2))
- channel1.SchemeId = &scheme1.Id
- channel1, err := th.App.Srv().Store.Channel().Update(channel1)
- assert.Nil(t, err)
- l3, r3 := th.SystemAdminClient.GetChannelsForScheme(scheme1.Id, 0, 100)
- CheckNoError(t, r3)
- assert.Len(t, l3, 1)
- assert.Equal(t, channel1.Id, l3[0].Id)
- channel2 := &model.Channel{
- TeamId: model.NewId(),
- DisplayName: "B Name",
- Name: model.NewId(),
- Type: model.CHANNEL_OPEN,
- SchemeId: &scheme1.Id,
- }
- channel2, nErr := th.App.Srv().Store.Channel().Save(channel2, 1000000)
- assert.Nil(t, nErr)
- l4, r4 := th.SystemAdminClient.GetChannelsForScheme(scheme1.Id, 0, 100)
- CheckNoError(t, r4)
- assert.Len(t, l4, 2)
- assert.Equal(t, channel1.Id, l4[0].Id)
- assert.Equal(t, channel2.Id, l4[1].Id)
- l5, r5 := th.SystemAdminClient.GetChannelsForScheme(scheme1.Id, 1, 1)
- CheckNoError(t, r5)
- assert.Len(t, l5, 1)
- assert.Equal(t, channel2.Id, l5[0].Id)
- // Check various error cases.
- _, ri1 := th.SystemAdminClient.GetChannelsForScheme(model.NewId(), 0, 100)
- CheckNotFoundStatus(t, ri1)
- _, ri2 := th.SystemAdminClient.GetChannelsForScheme("", 0, 100)
- CheckBadRequestStatus(t, ri2)
- th.Client.Logout()
- _, ri3 := th.Client.GetChannelsForScheme(model.NewId(), 0, 100)
- CheckUnauthorizedStatus(t, ri3)
- th.Client.Login(th.BasicUser.Username, th.BasicUser.Password)
- _, ri4 := th.Client.GetChannelsForScheme(model.NewId(), 0, 100)
- CheckForbiddenStatus(t, ri4)
- scheme2 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_TEAM,
- }
- scheme2, rs2 := th.SystemAdminClient.CreateScheme(scheme2)
- CheckNoError(t, rs2)
- _, ri5 := th.SystemAdminClient.GetChannelsForScheme(scheme2.Id, 0, 100)
- CheckBadRequestStatus(t, ri5)
- th.App.SetPhase2PermissionsMigrationStatus(false)
- _, ri6 := th.SystemAdminClient.GetChannelsForScheme(scheme1.Id, 0, 100)
- CheckNotImplementedStatus(t, ri6)
- }
- func TestPatchScheme(t *testing.T) {
- th := Setup(t)
- defer th.TearDown()
- th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
- th.App.SetPhase2PermissionsMigrationStatus(true)
- // Basic test of creating a team scheme.
- scheme1 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_TEAM,
- }
- s1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
- CheckNoError(t, r1)
- assert.Equal(t, s1.DisplayName, scheme1.DisplayName)
- assert.Equal(t, s1.Name, scheme1.Name)
- assert.Equal(t, s1.Description, scheme1.Description)
- assert.NotZero(t, s1.CreateAt)
- assert.Equal(t, s1.CreateAt, s1.UpdateAt)
- assert.Zero(t, s1.DeleteAt)
- assert.Equal(t, s1.Scope, scheme1.Scope)
- assert.NotZero(t, len(s1.DefaultTeamAdminRole))
- assert.NotZero(t, len(s1.DefaultTeamUserRole))
- assert.NotZero(t, len(s1.DefaultTeamGuestRole))
- assert.NotZero(t, len(s1.DefaultChannelAdminRole))
- assert.NotZero(t, len(s1.DefaultChannelUserRole))
- assert.NotZero(t, len(s1.DefaultChannelGuestRole))
- s2, r2 := th.SystemAdminClient.GetScheme(s1.Id)
- CheckNoError(t, r2)
- assert.Equal(t, s1, s2)
- // Test with a valid patch.
- schemePatch := &model.SchemePatch{
- DisplayName: new(string),
- Name: new(string),
- Description: new(string),
- }
- *schemePatch.DisplayName = model.NewId()
- *schemePatch.Name = model.NewId()
- *schemePatch.Description = model.NewId()
- s3, r3 := th.SystemAdminClient.PatchScheme(s2.Id, schemePatch)
- CheckNoError(t, r3)
- assert.Equal(t, s3.Id, s2.Id)
- assert.Equal(t, s3.DisplayName, *schemePatch.DisplayName)
- assert.Equal(t, s3.Name, *schemePatch.Name)
- assert.Equal(t, s3.Description, *schemePatch.Description)
- s4, r4 := th.SystemAdminClient.GetScheme(s3.Id)
- CheckNoError(t, r4)
- assert.Equal(t, s3, s4)
- // Test with a partial patch.
- *schemePatch.Name = model.NewId()
- *schemePatch.DisplayName = model.NewId()
- schemePatch.Description = nil
- s5, r5 := th.SystemAdminClient.PatchScheme(s4.Id, schemePatch)
- CheckNoError(t, r5)
- assert.Equal(t, s5.Id, s4.Id)
- assert.Equal(t, s5.DisplayName, *schemePatch.DisplayName)
- assert.Equal(t, s5.Name, *schemePatch.Name)
- assert.Equal(t, s5.Description, s4.Description)
- s6, r6 := th.SystemAdminClient.GetScheme(s5.Id)
- CheckNoError(t, r6)
- assert.Equal(t, s5, s6)
- // Test with invalid patch.
- *schemePatch.Name = strings.Repeat(model.NewId(), 20)
- _, r7 := th.SystemAdminClient.PatchScheme(s6.Id, schemePatch)
- CheckBadRequestStatus(t, r7)
- // Test with unknown ID.
- *schemePatch.Name = model.NewId()
- _, r8 := th.SystemAdminClient.PatchScheme(model.NewId(), schemePatch)
- CheckNotFoundStatus(t, r8)
- // Test with invalid ID.
- _, r9 := th.SystemAdminClient.PatchScheme("12345", schemePatch)
- CheckBadRequestStatus(t, r9)
- // Test without required permissions.
- _, r10 := th.Client.PatchScheme(s6.Id, schemePatch)
- CheckForbiddenStatus(t, r10)
- // Test without license.
- th.App.Srv().SetLicense(nil)
- _, r11 := th.SystemAdminClient.PatchScheme(s6.Id, schemePatch)
- CheckNotImplementedStatus(t, r11)
- th.App.SetPhase2PermissionsMigrationStatus(false)
- th.LoginSystemAdmin()
- th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
- _, r12 := th.SystemAdminClient.PatchScheme(s6.Id, schemePatch)
- CheckNotImplementedStatus(t, r12)
- }
- func TestDeleteScheme(t *testing.T) {
- th := Setup(t)
- defer th.TearDown()
- t.Run("ValidTeamScheme", func(t *testing.T) {
- th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
- th.App.SetPhase2PermissionsMigrationStatus(true)
- // Create a team scheme.
- scheme1 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_TEAM,
- }
- s1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
- CheckNoError(t, r1)
- // Retrieve the roles and check they are not deleted.
- role1, roleRes1 := th.SystemAdminClient.GetRoleByName(s1.DefaultTeamAdminRole)
- CheckNoError(t, roleRes1)
- role2, roleRes2 := th.SystemAdminClient.GetRoleByName(s1.DefaultTeamUserRole)
- CheckNoError(t, roleRes2)
- role3, roleRes3 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelAdminRole)
- CheckNoError(t, roleRes3)
- role4, roleRes4 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelUserRole)
- CheckNoError(t, roleRes4)
- role5, roleRes5 := th.SystemAdminClient.GetRoleByName(s1.DefaultTeamGuestRole)
- CheckNoError(t, roleRes5)
- role6, roleRes6 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelGuestRole)
- CheckNoError(t, roleRes6)
- assert.Zero(t, role1.DeleteAt)
- assert.Zero(t, role2.DeleteAt)
- assert.Zero(t, role3.DeleteAt)
- assert.Zero(t, role4.DeleteAt)
- assert.Zero(t, role5.DeleteAt)
- assert.Zero(t, role6.DeleteAt)
- // Make sure this scheme is in use by a team.
- team, err := th.App.Srv().Store.Team().Save(&model.Team{
- Name: "zz" + model.NewId(),
- DisplayName: model.NewId(),
- Email: model.NewId() + "@nowhere.com",
- Type: model.TEAM_OPEN,
- SchemeId: &s1.Id,
- })
- require.Nil(t, err)
- // Delete the Scheme.
- _, r3 := th.SystemAdminClient.DeleteScheme(s1.Id)
- CheckNoError(t, r3)
- // Check the roles were deleted.
- role1, roleRes1 = th.SystemAdminClient.GetRoleByName(s1.DefaultTeamAdminRole)
- CheckNoError(t, roleRes1)
- role2, roleRes2 = th.SystemAdminClient.GetRoleByName(s1.DefaultTeamUserRole)
- CheckNoError(t, roleRes2)
- role3, roleRes3 = th.SystemAdminClient.GetRoleByName(s1.DefaultChannelAdminRole)
- CheckNoError(t, roleRes3)
- role4, roleRes4 = th.SystemAdminClient.GetRoleByName(s1.DefaultChannelUserRole)
- CheckNoError(t, roleRes4)
- role5, roleRes5 = th.SystemAdminClient.GetRoleByName(s1.DefaultTeamGuestRole)
- CheckNoError(t, roleRes5)
- role6, roleRes6 = th.SystemAdminClient.GetRoleByName(s1.DefaultChannelGuestRole)
- CheckNoError(t, roleRes6)
- assert.NotZero(t, role1.DeleteAt)
- assert.NotZero(t, role2.DeleteAt)
- assert.NotZero(t, role3.DeleteAt)
- assert.NotZero(t, role4.DeleteAt)
- assert.NotZero(t, role5.DeleteAt)
- assert.NotZero(t, role6.DeleteAt)
- // Check the team now uses the default scheme
- c2, resp := th.SystemAdminClient.GetTeam(team.Id, "")
- CheckNoError(t, resp)
- assert.Equal(t, "", *c2.SchemeId)
- })
- t.Run("ValidChannelScheme", func(t *testing.T) {
- th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
- th.App.SetPhase2PermissionsMigrationStatus(true)
- // Create a channel scheme.
- scheme1 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_CHANNEL,
- }
- s1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
- CheckNoError(t, r1)
- // Retrieve the roles and check they are not deleted.
- role3, roleRes3 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelAdminRole)
- CheckNoError(t, roleRes3)
- role4, roleRes4 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelUserRole)
- CheckNoError(t, roleRes4)
- role6, roleRes6 := th.SystemAdminClient.GetRoleByName(s1.DefaultChannelGuestRole)
- CheckNoError(t, roleRes6)
- assert.Zero(t, role3.DeleteAt)
- assert.Zero(t, role4.DeleteAt)
- assert.Zero(t, role6.DeleteAt)
- // Make sure this scheme is in use by a team.
- channel, err := th.App.Srv().Store.Channel().Save(&model.Channel{
- TeamId: model.NewId(),
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Type: model.CHANNEL_OPEN,
- SchemeId: &s1.Id,
- }, -1)
- assert.Nil(t, err)
- // Delete the Scheme.
- _, r3 := th.SystemAdminClient.DeleteScheme(s1.Id)
- CheckNoError(t, r3)
- // Check the roles were deleted.
- role3, roleRes3 = th.SystemAdminClient.GetRoleByName(s1.DefaultChannelAdminRole)
- CheckNoError(t, roleRes3)
- role4, roleRes4 = th.SystemAdminClient.GetRoleByName(s1.DefaultChannelUserRole)
- CheckNoError(t, roleRes4)
- role6, roleRes6 = th.SystemAdminClient.GetRoleByName(s1.DefaultChannelGuestRole)
- CheckNoError(t, roleRes6)
- assert.NotZero(t, role3.DeleteAt)
- assert.NotZero(t, role4.DeleteAt)
- assert.NotZero(t, role6.DeleteAt)
- // Check the channel now uses the default scheme
- c2, resp := th.SystemAdminClient.GetChannelByName(channel.Name, channel.TeamId, "")
- CheckNoError(t, resp)
- assert.Equal(t, "", *c2.SchemeId)
- })
- t.Run("FailureCases", func(t *testing.T) {
- th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
- th.App.SetPhase2PermissionsMigrationStatus(true)
- scheme1 := &model.Scheme{
- DisplayName: model.NewId(),
- Name: model.NewId(),
- Description: model.NewId(),
- Scope: model.SCHEME_SCOPE_CHANNEL,
- }
- s1, r1 := th.SystemAdminClient.CreateScheme(scheme1)
- CheckNoError(t, r1)
- // Test with unknown ID.
- _, r2 := th.SystemAdminClient.DeleteScheme(model.NewId())
- CheckNotFoundStatus(t, r2)
- // Test with invalid ID.
- _, r3 := th.SystemAdminClient.DeleteScheme("12345")
- CheckBadRequestStatus(t, r3)
- // Test without required permissions.
- _, r4 := th.Client.DeleteScheme(s1.Id)
- CheckForbiddenStatus(t, r4)
- // Test without license.
- th.App.Srv().SetLicense(nil)
- _, r5 := th.SystemAdminClient.DeleteScheme(s1.Id)
- CheckNotImplementedStatus(t, r5)
- th.App.SetPhase2PermissionsMigrationStatus(false)
- th.App.Srv().SetLicense(model.NewTestLicense("custom_permissions_schemes"))
- _, r6 := th.SystemAdminClient.DeleteScheme(s1.Id)
- CheckNotImplementedStatus(t, r6)
- })
- }
|