github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/storetest/scheme_store.go (about) 1 // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package storetest 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 11 "github.com/mattermost/mattermost-server/model" 12 "github.com/mattermost/mattermost-server/store" 13 ) 14 15 func TestSchemeStore(t *testing.T, ss store.Store) { 16 createDefaultRoles(t, ss) 17 18 t.Run("Save", func(t *testing.T) { testSchemeStoreSave(t, ss) }) 19 t.Run("Get", func(t *testing.T) { testSchemeStoreGet(t, ss) }) 20 t.Run("GetAllPage", func(t *testing.T) { testSchemeStoreGetAllPage(t, ss) }) 21 t.Run("Delete", func(t *testing.T) { testSchemeStoreDelete(t, ss) }) 22 t.Run("PermanentDeleteAll", func(t *testing.T) { testSchemeStorePermanentDeleteAll(t, ss) }) 23 t.Run("GetByName", func(t *testing.T) { testSchemeStoreGetByName(t, ss) }) 24 } 25 26 func createDefaultRoles(t *testing.T, ss store.Store) { 27 <-ss.Role().Save(&model.Role{ 28 Name: model.TEAM_ADMIN_ROLE_ID, 29 DisplayName: model.TEAM_ADMIN_ROLE_ID, 30 Permissions: []string{ 31 model.PERMISSION_DELETE_OTHERS_POSTS.Id, 32 }, 33 }) 34 35 <-ss.Role().Save(&model.Role{ 36 Name: model.TEAM_USER_ROLE_ID, 37 DisplayName: model.TEAM_USER_ROLE_ID, 38 Permissions: []string{ 39 model.PERMISSION_VIEW_TEAM.Id, 40 model.PERMISSION_ADD_USER_TO_TEAM.Id, 41 }, 42 }) 43 44 <-ss.Role().Save(&model.Role{ 45 Name: model.CHANNEL_ADMIN_ROLE_ID, 46 DisplayName: model.CHANNEL_ADMIN_ROLE_ID, 47 Permissions: []string{ 48 model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS.Id, 49 model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS.Id, 50 }, 51 }) 52 53 <-ss.Role().Save(&model.Role{ 54 Name: model.CHANNEL_USER_ROLE_ID, 55 DisplayName: model.CHANNEL_USER_ROLE_ID, 56 Permissions: []string{ 57 model.PERMISSION_READ_CHANNEL.Id, 58 model.PERMISSION_CREATE_POST.Id, 59 }, 60 }) 61 } 62 63 func testSchemeStoreSave(t *testing.T, ss store.Store) { 64 // Save a new scheme. 65 s1 := &model.Scheme{ 66 DisplayName: model.NewId(), 67 Name: model.NewId(), 68 Description: model.NewId(), 69 Scope: model.SCHEME_SCOPE_TEAM, 70 } 71 72 // Check all fields saved correctly. 73 res1 := <-ss.Scheme().Save(s1) 74 assert.Nil(t, res1.Err) 75 d1 := res1.Data.(*model.Scheme) 76 assert.Len(t, d1.Id, 26) 77 assert.Equal(t, s1.DisplayName, d1.DisplayName) 78 assert.Equal(t, s1.Name, d1.Name) 79 assert.Equal(t, s1.Description, d1.Description) 80 assert.NotZero(t, d1.CreateAt) 81 assert.NotZero(t, d1.UpdateAt) 82 assert.Zero(t, d1.DeleteAt) 83 assert.Equal(t, s1.Scope, d1.Scope) 84 assert.Len(t, d1.DefaultTeamAdminRole, 26) 85 assert.Len(t, d1.DefaultTeamUserRole, 26) 86 assert.Len(t, d1.DefaultChannelAdminRole, 26) 87 assert.Len(t, d1.DefaultChannelUserRole, 26) 88 89 // Check the default roles were created correctly. 90 roleRes1 := <-ss.Role().GetByName(d1.DefaultTeamAdminRole) 91 assert.Nil(t, roleRes1.Err) 92 role1 := roleRes1.Data.(*model.Role) 93 assert.Equal(t, role1.Permissions, []string{"delete_others_posts"}) 94 assert.True(t, role1.SchemeManaged) 95 96 roleRes2 := <-ss.Role().GetByName(d1.DefaultTeamUserRole) 97 assert.Nil(t, roleRes2.Err) 98 role2 := roleRes2.Data.(*model.Role) 99 assert.Equal(t, role2.Permissions, []string{"view_team", "add_user_to_team"}) 100 assert.True(t, role2.SchemeManaged) 101 102 roleRes3 := <-ss.Role().GetByName(d1.DefaultChannelAdminRole) 103 assert.Nil(t, roleRes3.Err) 104 role3 := roleRes3.Data.(*model.Role) 105 assert.Equal(t, role3.Permissions, []string{"manage_public_channel_members", "manage_private_channel_members"}) 106 assert.True(t, role3.SchemeManaged) 107 108 roleRes4 := <-ss.Role().GetByName(d1.DefaultChannelUserRole) 109 assert.Nil(t, roleRes4.Err) 110 role4 := roleRes4.Data.(*model.Role) 111 assert.Equal(t, role4.Permissions, []string{"read_channel", "create_post"}) 112 assert.True(t, role4.SchemeManaged) 113 114 // Change the scheme description and update. 115 d1.Description = model.NewId() 116 117 res2 := <-ss.Scheme().Save(d1) 118 assert.Nil(t, res2.Err) 119 d2 := res2.Data.(*model.Scheme) 120 assert.Equal(t, d1.Id, d2.Id) 121 assert.Equal(t, s1.DisplayName, d2.DisplayName) 122 assert.Equal(t, s1.Name, d2.Name) 123 assert.Equal(t, d1.Description, d2.Description) 124 assert.NotZero(t, d2.CreateAt) 125 assert.NotZero(t, d2.UpdateAt) 126 assert.Zero(t, d2.DeleteAt) 127 assert.Equal(t, s1.Scope, d2.Scope) 128 assert.Equal(t, d1.DefaultTeamAdminRole, d2.DefaultTeamAdminRole) 129 assert.Equal(t, d1.DefaultTeamUserRole, d2.DefaultTeamUserRole) 130 assert.Equal(t, d1.DefaultChannelAdminRole, d2.DefaultChannelAdminRole) 131 assert.Equal(t, d1.DefaultChannelUserRole, d2.DefaultChannelUserRole) 132 133 // Try saving one with an invalid ID set. 134 s3 := &model.Scheme{ 135 Id: model.NewId(), 136 DisplayName: model.NewId(), 137 Name: model.NewId(), 138 Description: model.NewId(), 139 Scope: model.SCHEME_SCOPE_TEAM, 140 } 141 142 res3 := <-ss.Scheme().Save(s3) 143 assert.NotNil(t, res3.Err) 144 } 145 146 func testSchemeStoreGet(t *testing.T, ss store.Store) { 147 // Save a scheme to test with. 148 s1 := &model.Scheme{ 149 DisplayName: model.NewId(), 150 Name: model.NewId(), 151 Description: model.NewId(), 152 Scope: model.SCHEME_SCOPE_TEAM, 153 } 154 155 res1 := <-ss.Scheme().Save(s1) 156 assert.Nil(t, res1.Err) 157 d1 := res1.Data.(*model.Scheme) 158 assert.Len(t, d1.Id, 26) 159 160 // Get a valid scheme 161 res2 := <-ss.Scheme().Get(d1.Id) 162 assert.Nil(t, res2.Err) 163 d2 := res1.Data.(*model.Scheme) 164 assert.Equal(t, d1.Id, d2.Id) 165 assert.Equal(t, s1.DisplayName, d2.DisplayName) 166 assert.Equal(t, s1.Name, d2.Name) 167 assert.Equal(t, d1.Description, d2.Description) 168 assert.NotZero(t, d2.CreateAt) 169 assert.NotZero(t, d2.UpdateAt) 170 assert.Zero(t, d2.DeleteAt) 171 assert.Equal(t, s1.Scope, d2.Scope) 172 assert.Equal(t, d1.DefaultTeamAdminRole, d2.DefaultTeamAdminRole) 173 assert.Equal(t, d1.DefaultTeamUserRole, d2.DefaultTeamUserRole) 174 assert.Equal(t, d1.DefaultChannelAdminRole, d2.DefaultChannelAdminRole) 175 assert.Equal(t, d1.DefaultChannelUserRole, d2.DefaultChannelUserRole) 176 177 // Get an invalid scheme 178 res3 := <-ss.Scheme().Get(model.NewId()) 179 assert.NotNil(t, res3.Err) 180 } 181 182 func testSchemeStoreGetByName(t *testing.T, ss store.Store) { 183 // Save a scheme to test with. 184 s1 := &model.Scheme{ 185 DisplayName: model.NewId(), 186 Name: model.NewId(), 187 Description: model.NewId(), 188 Scope: model.SCHEME_SCOPE_TEAM, 189 } 190 191 res1 := <-ss.Scheme().Save(s1) 192 assert.Nil(t, res1.Err) 193 d1 := res1.Data.(*model.Scheme) 194 assert.Len(t, d1.Id, 26) 195 196 // Get a valid scheme 197 res2 := <-ss.Scheme().GetByName(d1.Name) 198 assert.Nil(t, res2.Err) 199 d2 := res1.Data.(*model.Scheme) 200 assert.Equal(t, d1.Id, d2.Id) 201 assert.Equal(t, s1.DisplayName, d2.DisplayName) 202 assert.Equal(t, s1.Name, d2.Name) 203 assert.Equal(t, d1.Description, d2.Description) 204 assert.NotZero(t, d2.CreateAt) 205 assert.NotZero(t, d2.UpdateAt) 206 assert.Zero(t, d2.DeleteAt) 207 assert.Equal(t, s1.Scope, d2.Scope) 208 assert.Equal(t, d1.DefaultTeamAdminRole, d2.DefaultTeamAdminRole) 209 assert.Equal(t, d1.DefaultTeamUserRole, d2.DefaultTeamUserRole) 210 assert.Equal(t, d1.DefaultChannelAdminRole, d2.DefaultChannelAdminRole) 211 assert.Equal(t, d1.DefaultChannelUserRole, d2.DefaultChannelUserRole) 212 213 // Get an invalid scheme 214 res3 := <-ss.Scheme().GetByName(model.NewId()) 215 assert.NotNil(t, res3.Err) 216 } 217 218 func testSchemeStoreGetAllPage(t *testing.T, ss store.Store) { 219 // Save a scheme to test with. 220 schemes := []*model.Scheme{ 221 { 222 DisplayName: model.NewId(), 223 Name: model.NewId(), 224 Description: model.NewId(), 225 Scope: model.SCHEME_SCOPE_TEAM, 226 }, 227 { 228 DisplayName: model.NewId(), 229 Name: model.NewId(), 230 Description: model.NewId(), 231 Scope: model.SCHEME_SCOPE_CHANNEL, 232 }, 233 { 234 DisplayName: model.NewId(), 235 Name: model.NewId(), 236 Description: model.NewId(), 237 Scope: model.SCHEME_SCOPE_TEAM, 238 }, 239 { 240 DisplayName: model.NewId(), 241 Name: model.NewId(), 242 Description: model.NewId(), 243 Scope: model.SCHEME_SCOPE_CHANNEL, 244 }, 245 } 246 247 for _, scheme := range schemes { 248 store.Must(ss.Scheme().Save(scheme)) 249 } 250 251 r1 := <-ss.Scheme().GetAllPage("", 0, 2) 252 assert.Nil(t, r1.Err) 253 s1 := r1.Data.([]*model.Scheme) 254 assert.Len(t, s1, 2) 255 256 r2 := <-ss.Scheme().GetAllPage("", 2, 2) 257 assert.Nil(t, r2.Err) 258 s2 := r2.Data.([]*model.Scheme) 259 assert.Len(t, s2, 2) 260 assert.NotEqual(t, s1[0].DisplayName, s2[0].DisplayName) 261 assert.NotEqual(t, s1[0].DisplayName, s2[1].DisplayName) 262 assert.NotEqual(t, s1[1].DisplayName, s2[0].DisplayName) 263 assert.NotEqual(t, s1[1].DisplayName, s2[1].DisplayName) 264 assert.NotEqual(t, s1[0].Name, s2[0].Name) 265 assert.NotEqual(t, s1[0].Name, s2[1].Name) 266 assert.NotEqual(t, s1[1].Name, s2[0].Name) 267 assert.NotEqual(t, s1[1].Name, s2[1].Name) 268 269 r3 := <-ss.Scheme().GetAllPage("team", 0, 1000) 270 assert.Nil(t, r3.Err) 271 s3 := r3.Data.([]*model.Scheme) 272 assert.NotZero(t, len(s3)) 273 for _, s := range s3 { 274 assert.Equal(t, "team", s.Scope) 275 } 276 277 r4 := <-ss.Scheme().GetAllPage("channel", 0, 1000) 278 assert.Nil(t, r4.Err) 279 s4 := r4.Data.([]*model.Scheme) 280 assert.NotZero(t, len(s4)) 281 for _, s := range s4 { 282 assert.Equal(t, "channel", s.Scope) 283 } 284 } 285 286 func testSchemeStoreDelete(t *testing.T, ss store.Store) { 287 // Save a new scheme. 288 s1 := &model.Scheme{ 289 DisplayName: model.NewId(), 290 Name: model.NewId(), 291 Description: model.NewId(), 292 Scope: model.SCHEME_SCOPE_TEAM, 293 } 294 295 // Check all fields saved correctly. 296 res1 := <-ss.Scheme().Save(s1) 297 assert.Nil(t, res1.Err) 298 d1 := res1.Data.(*model.Scheme) 299 assert.Len(t, d1.Id, 26) 300 assert.Equal(t, s1.DisplayName, d1.DisplayName) 301 assert.Equal(t, s1.Name, d1.Name) 302 assert.Equal(t, s1.Description, d1.Description) 303 assert.NotZero(t, d1.CreateAt) 304 assert.NotZero(t, d1.UpdateAt) 305 assert.Zero(t, d1.DeleteAt) 306 assert.Equal(t, s1.Scope, d1.Scope) 307 assert.Len(t, d1.DefaultTeamAdminRole, 26) 308 assert.Len(t, d1.DefaultTeamUserRole, 26) 309 assert.Len(t, d1.DefaultChannelAdminRole, 26) 310 assert.Len(t, d1.DefaultChannelUserRole, 26) 311 312 // Check the default roles were created correctly. 313 roleRes1 := <-ss.Role().GetByName(d1.DefaultTeamAdminRole) 314 assert.Nil(t, roleRes1.Err) 315 role1 := roleRes1.Data.(*model.Role) 316 assert.Equal(t, role1.Permissions, []string{"delete_others_posts"}) 317 assert.True(t, role1.SchemeManaged) 318 319 roleRes2 := <-ss.Role().GetByName(d1.DefaultTeamUserRole) 320 assert.Nil(t, roleRes2.Err) 321 role2 := roleRes2.Data.(*model.Role) 322 assert.Equal(t, role2.Permissions, []string{"view_team", "add_user_to_team"}) 323 assert.True(t, role2.SchemeManaged) 324 325 roleRes3 := <-ss.Role().GetByName(d1.DefaultChannelAdminRole) 326 assert.Nil(t, roleRes3.Err) 327 role3 := roleRes3.Data.(*model.Role) 328 assert.Equal(t, role3.Permissions, []string{"manage_public_channel_members", "manage_private_channel_members"}) 329 assert.True(t, role3.SchemeManaged) 330 331 roleRes4 := <-ss.Role().GetByName(d1.DefaultChannelUserRole) 332 assert.Nil(t, roleRes4.Err) 333 role4 := roleRes4.Data.(*model.Role) 334 assert.Equal(t, role4.Permissions, []string{"read_channel", "create_post"}) 335 assert.True(t, role4.SchemeManaged) 336 337 // Delete the scheme. 338 res2 := <-ss.Scheme().Delete(d1.Id) 339 if !assert.Nil(t, res2.Err) { 340 t.Fatal(res2.Err) 341 } 342 d2 := res2.Data.(*model.Scheme) 343 assert.NotZero(t, d2.DeleteAt) 344 345 // Check that the roles are deleted too. 346 roleRes5 := <-ss.Role().GetByName(d1.DefaultTeamAdminRole) 347 assert.Nil(t, roleRes5.Err) 348 role5 := roleRes5.Data.(*model.Role) 349 assert.NotZero(t, role5.DeleteAt) 350 351 roleRes6 := <-ss.Role().GetByName(d1.DefaultTeamUserRole) 352 assert.Nil(t, roleRes6.Err) 353 role6 := roleRes6.Data.(*model.Role) 354 assert.NotZero(t, role6.DeleteAt) 355 356 roleRes7 := <-ss.Role().GetByName(d1.DefaultChannelAdminRole) 357 assert.Nil(t, roleRes7.Err) 358 role7 := roleRes7.Data.(*model.Role) 359 assert.NotZero(t, role7.DeleteAt) 360 361 roleRes8 := <-ss.Role().GetByName(d1.DefaultChannelUserRole) 362 assert.Nil(t, roleRes8.Err) 363 role8 := roleRes8.Data.(*model.Role) 364 assert.NotZero(t, role8.DeleteAt) 365 366 // Try deleting a scheme that does not exist. 367 res3 := <-ss.Scheme().Delete(model.NewId()) 368 assert.NotNil(t, res3.Err) 369 370 // Try deleting a team scheme that's in use. 371 s4 := &model.Scheme{ 372 DisplayName: model.NewId(), 373 Name: model.NewId(), 374 Description: model.NewId(), 375 Scope: model.SCHEME_SCOPE_TEAM, 376 } 377 res4 := <-ss.Scheme().Save(s4) 378 assert.Nil(t, res4.Err) 379 d4 := res4.Data.(*model.Scheme) 380 381 t4 := &model.Team{ 382 Name: model.NewId(), 383 DisplayName: model.NewId(), 384 Email: MakeEmail(), 385 Type: model.TEAM_OPEN, 386 SchemeId: &d4.Id, 387 } 388 tres4 := <-ss.Team().Save(t4) 389 assert.Nil(t, tres4.Err) 390 t4 = tres4.Data.(*model.Team) 391 392 sres4 := <-ss.Scheme().Delete(d4.Id) 393 assert.Nil(t, sres4.Err) 394 395 tres5 := <-ss.Team().Get(t4.Id) 396 assert.Nil(t, tres5.Err) 397 t5 := tres5.Data.(*model.Team) 398 assert.Equal(t, "", *t5.SchemeId) 399 400 // Try deleting a channel scheme that's in use. 401 s5 := &model.Scheme{ 402 DisplayName: model.NewId(), 403 Name: model.NewId(), 404 Description: model.NewId(), 405 Scope: model.SCHEME_SCOPE_CHANNEL, 406 } 407 res5 := <-ss.Scheme().Save(s5) 408 assert.Nil(t, res5.Err) 409 d5 := res5.Data.(*model.Scheme) 410 411 c5 := &model.Channel{ 412 TeamId: model.NewId(), 413 DisplayName: model.NewId(), 414 Name: model.NewId(), 415 Type: model.CHANNEL_OPEN, 416 SchemeId: &d5.Id, 417 } 418 cres5 := <-ss.Channel().Save(c5, -1) 419 assert.Nil(t, cres5.Err) 420 c5 = cres5.Data.(*model.Channel) 421 422 sres5 := <-ss.Scheme().Delete(d5.Id) 423 assert.Nil(t, sres5.Err) 424 425 cres6 := <-ss.Channel().Get(c5.Id, true) 426 assert.Nil(t, cres6.Err) 427 c6 := cres6.Data.(*model.Channel) 428 assert.Equal(t, "", *c6.SchemeId) 429 } 430 431 func testSchemeStorePermanentDeleteAll(t *testing.T, ss store.Store) { 432 s1 := &model.Scheme{ 433 Name: model.NewId(), 434 DisplayName: model.NewId(), 435 Description: model.NewId(), 436 Scope: model.SCHEME_SCOPE_TEAM, 437 } 438 439 s2 := &model.Scheme{ 440 Name: model.NewId(), 441 DisplayName: model.NewId(), 442 Description: model.NewId(), 443 Scope: model.SCHEME_SCOPE_CHANNEL, 444 } 445 446 s1 = (<-ss.Scheme().Save(s1)).Data.(*model.Scheme) 447 s2 = (<-ss.Scheme().Save(s2)).Data.(*model.Scheme) 448 449 res := <-ss.Scheme().PermanentDeleteAll() 450 assert.Nil(t, res.Err) 451 452 res1 := <-ss.Scheme().Get(s1.Id) 453 assert.NotNil(t, res1.Err) 454 455 res2 := <-ss.Scheme().Get(s2.Id) 456 assert.NotNil(t, res2.Err) 457 458 res3 := <-ss.Scheme().GetAllPage("", 0, 100000) 459 assert.Nil(t, res3.Err) 460 assert.Len(t, res3.Data.([]*model.Scheme), 0) 461 }