github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+incompatible/app/permissions_test.go (about) 1 package app 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strings" 7 "testing" 8 9 "github.com/mattermost/mattermost-server/model" 10 ) 11 12 type testWriter struct { 13 write func(p []byte) (int, error) 14 } 15 16 func (tw testWriter) Write(p []byte) (int, error) { 17 return tw.write(p) 18 } 19 20 func TestExportPermissions(t *testing.T) { 21 th := Setup().InitBasic() 22 defer th.TearDown() 23 24 var scheme *model.Scheme 25 var roles []*model.Role 26 withMigrationMarkedComplete(th, func() { 27 scheme, roles = th.CreateScheme() 28 }) 29 30 results := [][]byte{} 31 32 tw := testWriter{ 33 write: func(p []byte) (int, error) { 34 results = append(results, p) 35 return len(p), nil 36 }, 37 } 38 39 err := th.App.ExportPermissions(tw) 40 if err != nil { 41 t.Error(err) 42 } 43 44 if len(results) == 0 { 45 t.Error("Expected export to have returned something.") 46 } 47 48 firstResult := results[0] 49 50 var row map[string]interface{} 51 err = json.Unmarshal(firstResult, &row) 52 if err != nil { 53 t.Error(err) 54 } 55 56 getRoleByName := func(name string) string { 57 for _, role := range roles { 58 if role.Name == name { 59 return role.Name 60 } 61 } 62 return "" 63 } 64 65 expectations := map[string]func(str string) string{ 66 scheme.DisplayName: func(str string) string { return row["display_name"].(string) }, 67 scheme.Name: func(str string) string { return row["name"].(string) }, 68 scheme.Description: func(str string) string { return row["description"].(string) }, 69 scheme.Scope: func(str string) string { return row["scope"].(string) }, 70 scheme.DefaultTeamAdminRole: func(str string) string { return getRoleByName(str) }, 71 scheme.DefaultTeamUserRole: func(str string) string { return getRoleByName(str) }, 72 scheme.DefaultChannelAdminRole: func(str string) string { return getRoleByName(str) }, 73 scheme.DefaultChannelUserRole: func(str string) string { return getRoleByName(str) }, 74 } 75 76 for key, valF := range expectations { 77 expected := key 78 actual := valF(key) 79 if actual != expected { 80 t.Errorf("Expected %v but got %v.", expected, actual) 81 } 82 } 83 84 } 85 86 func TestImportPermissions(t *testing.T) { 87 th := Setup().InitBasic() 88 defer th.TearDown() 89 90 name := model.NewId() 91 displayName := model.NewId() 92 description := "my test description" 93 scope := model.SCHEME_SCOPE_CHANNEL 94 roleName1 := model.NewId() 95 roleName2 := model.NewId() 96 97 var results []*model.Scheme 98 var beforeCount int 99 withMigrationMarkedComplete(th, func() { 100 101 var appErr *model.AppError 102 results, appErr = th.App.GetSchemes(scope, 0, 100) 103 if appErr != nil { 104 panic(appErr) 105 } 106 beforeCount = len(results) 107 108 json := fmt.Sprintf(`{"display_name":"%v","name":"%v","description":"%v","scope":"%v","default_team_admin_role":"","default_team_user_role":"","default_channel_admin_role":"%v","default_channel_user_role":"%v","roles":[{"id":"yzfx3g9xjjfw8cqo6bpn33xr7o","name":"%v","display_name":"Channel Admin Role for Scheme my_scheme_1526475590","description":"","create_at":1526475589687,"update_at":1526475589687,"delete_at":0,"permissions":["manage_channel_roles"],"scheme_managed":true,"built_in":false},{"id":"a7s3cp4n33dfxbsrmyh9djao3a","name":"%v","display_name":"Channel User Role for Scheme my_scheme_1526475590","description":"","create_at":1526475589688,"update_at":1526475589688,"delete_at":0,"permissions":["read_channel","add_reaction","remove_reaction","manage_public_channel_members","upload_file","get_public_link","create_post","use_slash_commands","manage_private_channel_members","delete_post","edit_post"],"scheme_managed":true,"built_in":false}]}`, displayName, name, description, scope, roleName1, roleName2, roleName1, roleName2) 109 r := strings.NewReader(json) 110 111 err := th.App.ImportPermissions(r) 112 if err != nil { 113 t.Error(err) 114 } 115 results, appErr = th.App.GetSchemes(scope, 0, 100) 116 if appErr != nil { 117 panic(appErr) 118 } 119 120 }) 121 122 actual := len(results) 123 expected := beforeCount + 1 124 if actual != expected { 125 t.Errorf("Expected %v roles but got %v.", expected, actual) 126 } 127 128 newScheme := results[0] 129 130 channelAdminRole, appErr := th.App.GetRoleByName(newScheme.DefaultChannelAdminRole) 131 if appErr != nil { 132 t.Error(appErr) 133 } 134 135 channelUserRole, appErr := th.App.GetRoleByName(newScheme.DefaultChannelUserRole) 136 if appErr != nil { 137 t.Error(appErr) 138 } 139 140 expectations := map[string]string{ 141 newScheme.DisplayName: displayName, 142 newScheme.Name: name, 143 newScheme.Description: description, 144 newScheme.Scope: scope, 145 newScheme.DefaultTeamAdminRole: "", 146 newScheme.DefaultTeamUserRole: "", 147 channelAdminRole.Name: newScheme.DefaultChannelAdminRole, 148 channelUserRole.Name: newScheme.DefaultChannelUserRole, 149 } 150 151 for actual, expected := range expectations { 152 if actual != expected { 153 t.Errorf("Expected %v but got %v.", expected, actual) 154 } 155 } 156 157 } 158 159 func TestImportPermissions_idempotentScheme(t *testing.T) { 160 th := Setup().InitBasic() 161 defer th.TearDown() 162 163 name := model.NewId() 164 displayName := model.NewId() 165 description := "my test description" 166 scope := model.SCHEME_SCOPE_CHANNEL 167 roleName1 := model.NewId() 168 roleName2 := model.NewId() 169 170 json := fmt.Sprintf(`{"display_name":"%v","name":"%v","description":"%v","scope":"%v","default_team_admin_role":"","default_team_user_role":"","default_channel_admin_role":"%v","default_channel_user_role":"%v","roles":[{"id":"yzfx3g9xjjfw8cqo6bpn33xr7o","name":"%v","display_name":"Channel Admin Role for Scheme my_scheme_1526475590","description":"","create_at":1526475589687,"update_at":1526475589687,"delete_at":0,"permissions":["manage_channel_roles"],"scheme_managed":true,"built_in":false},{"id":"a7s3cp4n33dfxbsrmyh9djao3a","name":"%v","display_name":"Channel User Role for Scheme my_scheme_1526475590","description":"","create_at":1526475589688,"update_at":1526475589688,"delete_at":0,"permissions":["read_channel","add_reaction","remove_reaction","manage_public_channel_members","upload_file","get_public_link","create_post","use_slash_commands","manage_private_channel_members","delete_post","edit_post"],"scheme_managed":true,"built_in":false}]}`, displayName, name, description, scope, roleName1, roleName2, roleName1, roleName2) 171 jsonl := strings.Repeat(json+"\n", 4) 172 r := strings.NewReader(jsonl) 173 174 var results []*model.Scheme 175 var expected int 176 withMigrationMarkedComplete(th, func() { 177 var appErr *model.AppError 178 results, appErr = th.App.GetSchemes(model.SCHEME_SCOPE_CHANNEL, 0, 100) 179 if appErr != nil { 180 panic(appErr) 181 } 182 expected = len(results) 183 184 err := th.App.ImportPermissions(r) 185 if err == nil { 186 t.Error(err) 187 } 188 189 results, appErr = th.App.GetSchemes(model.SCHEME_SCOPE_CHANNEL, 0, 100) 190 if appErr != nil { 191 panic(appErr) 192 } 193 }) 194 actual := len(results) 195 196 if expected != actual { 197 t.Errorf("Expected count to be %v but got %v", expected, actual) 198 } 199 200 } 201 202 func TestImportPermissions_schemeDeletedOnRoleFailure(t *testing.T) { 203 th := Setup().InitBasic() 204 defer th.TearDown() 205 206 name := model.NewId() 207 displayName := model.NewId() 208 description := "my test description" 209 scope := "invalid scope" 210 roleName1 := model.NewId() 211 roleName2 := model.NewId() 212 213 jsonl := fmt.Sprintf(`{"display_name":"%v","name":"%v","description":"%v","scope":"%v","default_team_admin_role":"","default_team_user_role":"","default_channel_admin_role":"%v","default_channel_user_role":"%v","roles":[{"id":"yzfx3g9xjjfw8cqo6bpn33xr7o","name":"%v","display_name":"Channel Admin Role for Scheme my_scheme_1526475590","description":"","create_at":1526475589687,"update_at":1526475589687,"delete_at":0,"permissions":["manage_channel_roles"],"scheme_managed":true,"built_in":false},{"id":"a7s3cp4n33dfxbsrmyh9djao3a","name":"%v","display_name":"Channel User Role for Scheme my_scheme_1526475590","description":"","create_at":1526475589688,"update_at":1526475589688,"delete_at":0,"permissions":["read_channel","add_reaction","remove_reaction","manage_public_channel_members","upload_file","get_public_link","create_post","use_slash_commands","manage_private_channel_members","delete_post","edit_post"],"scheme_managed":true,"built_in":false}]}`, displayName, name, description, scope, roleName1, roleName2, roleName1, roleName2) 214 r := strings.NewReader(jsonl) 215 216 var results []*model.Scheme 217 var expected int 218 withMigrationMarkedComplete(th, func() { 219 var appErr *model.AppError 220 results, appErr = th.App.GetSchemes(model.SCHEME_SCOPE_CHANNEL, 0, 100) 221 if appErr != nil { 222 panic(appErr) 223 } 224 expected = len(results) 225 226 err := th.App.ImportPermissions(r) 227 if err == nil { 228 t.Error(err) 229 } 230 231 results, appErr = th.App.GetSchemes(model.SCHEME_SCOPE_CHANNEL, 0, 100) 232 if appErr != nil { 233 panic(appErr) 234 } 235 }) 236 actual := len(results) 237 238 if expected != actual { 239 t.Errorf("Expected count to be %v but got %v", expected, actual) 240 } 241 242 } 243 244 func withMigrationMarkedComplete(th *TestHelper, f func()) { 245 // Mark the migration as done. 246 <-th.App.Srv.Store.System().PermanentDeleteByName(model.MIGRATION_KEY_ADVANCED_PERMISSIONS_PHASE_2) 247 <-th.App.Srv.Store.System().Save(&model.System{Name: model.MIGRATION_KEY_ADVANCED_PERMISSIONS_PHASE_2, Value: "true"}) 248 // Un-mark the migration at the end of the test. 249 defer func() { 250 <-th.App.Srv.Store.System().PermanentDeleteByName(model.MIGRATION_KEY_ADVANCED_PERMISSIONS_PHASE_2) 251 }() 252 f() 253 }