@@ -7,11 +7,64 @@ import (
77 "testing"
88
99 "github.com/stretchr/testify/assert"
10+ "github.com/stretchr/testify/mock"
11+ "github.com/stretchr/testify/require"
1012
1113 "github.com/mattermost/mattermost/server/public/model"
1214 "github.com/mattermost/mattermost/server/v8/channels/store/sqlstore"
15+ "github.com/mattermost/mattermost/server/v8/channels/store/storetest/mocks"
1316)
1417
18+ func TestRestoreManageOAuthPermissionMigration (t * testing.T ) {
19+ mainHelper .Parallel (t )
20+
21+ th := SetupWithStoreMock (t )
22+
23+ migrationMap , err := th .App .getRestoreManageOAuthPermissionMigration ()
24+ require .NoError (t , err )
25+
26+ systemAdminRole := & model.Role {
27+ Name : model .SystemAdminRoleId ,
28+ Permissions : []string {model .PermissionManageSystemWideOAuth .Id },
29+ }
30+ systemUserRole := & model.Role {
31+ Name : model .SystemUserRoleId ,
32+ Permissions : []string {model .PermissionCreateDirectChannel .Id },
33+ }
34+ roles := []* model.Role {systemAdminRole , systemUserRole }
35+
36+ mockStore := th .App .Srv ().Store ().(* mocks.Store )
37+ roleStore := mocks.RoleStore {}
38+ systemStore := mocks.SystemStore {}
39+
40+ mockStore .On ("Role" ).Return (& roleStore )
41+ mockStore .On ("System" ).Return (& systemStore )
42+
43+ systemStore .On ("GetByName" , model .MigrationKeyRestoreManageOAuthPermission ).
44+ Return (nil , model .NewAppError ("test" , "missing" , nil , "" , 404 )).Once ()
45+ systemStore .On ("GetByName" , model .MigrationKeyRestoreManageOAuthPermission ).
46+ Return (& model.System {Name : model .MigrationKeyRestoreManageOAuthPermission , Value : "true" }, nil ).Once ()
47+ systemStore .On ("SaveOrUpdate" , mock .MatchedBy (func (system * model.System ) bool {
48+ return system .Name == model .MigrationKeyRestoreManageOAuthPermission && system .Value == "true"
49+ })).Return (nil ).Once ()
50+
51+ roleStore .On ("Save" , mock .AnythingOfType ("*model.Role" )).
52+ Return (func (role * model.Role ) * model.Role { return role }, nil ).Twice ()
53+
54+ appErr := th .App .Srv ().doPermissionsMigration (model .MigrationKeyRestoreManageOAuthPermission , migrationMap , roles )
55+ require .Nil (t , appErr )
56+ assert .Contains (t , systemAdminRole .Permissions , model .PermissionManageOAuth .Id )
57+ assert .NotContains (t , systemUserRole .Permissions , model .PermissionManageOAuth .Id )
58+ assert .Len (t , systemAdminRole .Permissions , 2 )
59+
60+ appErr = th .App .Srv ().doPermissionsMigration (model .MigrationKeyRestoreManageOAuthPermission , migrationMap , roles )
61+ require .Nil (t , appErr )
62+ assert .Len (t , systemAdminRole .Permissions , 2 )
63+
64+ roleStore .AssertNumberOfCalls (t , "Save" , 2 )
65+ systemStore .AssertNumberOfCalls (t , "SaveOrUpdate" , 1 )
66+ }
67+
1568func TestApplyPermissionsMap (t * testing.T ) {
1669 mainHelper .Parallel (t )
1770 tt := []struct {
0 commit comments