github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/storetest/role_store.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package storetest 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "github.com/mattermost/mattermost-server/v5/model" 14 "github.com/mattermost/mattermost-server/v5/store" 15 ) 16 17 func TestRoleStore(t *testing.T, ss store.Store, s SqlStore) { 18 t.Run("Save", func(t *testing.T) { testRoleStoreSave(t, ss) }) 19 t.Run("Get", func(t *testing.T) { testRoleStoreGet(t, ss) }) 20 t.Run("GetAll", func(t *testing.T) { testRoleStoreGetAll(t, ss) }) 21 t.Run("GetByName", func(t *testing.T) { testRoleStoreGetByName(t, ss) }) 22 t.Run("GetNames", func(t *testing.T) { testRoleStoreGetByNames(t, ss) }) 23 t.Run("Delete", func(t *testing.T) { testRoleStoreDelete(t, ss) }) 24 t.Run("PermanentDeleteAll", func(t *testing.T) { testRoleStorePermanentDeleteAll(t, ss) }) 25 t.Run("LowerScopedChannelSchemeRoles_AllChannelSchemeRoles", func(t *testing.T) { testRoleStoreLowerScopedChannelSchemeRoles(t, ss) }) 26 t.Run("ChannelHigherScopedPermissionsBlankTeamSchemeChannelGuest", func(t *testing.T) { testRoleStoreChannelHigherScopedPermissionsBlankTeamSchemeChannelGuest(t, ss, s) }) 27 } 28 29 func testRoleStoreSave(t *testing.T, ss store.Store) { 30 // Save a new role. 31 r1 := &model.Role{ 32 Name: model.NewId(), 33 DisplayName: model.NewId(), 34 Description: model.NewId(), 35 Permissions: []string{ 36 "invite_user", 37 "create_public_channel", 38 "add_user_to_team", 39 }, 40 SchemeManaged: false, 41 } 42 43 d1, err := ss.Role().Save(r1) 44 assert.NoError(t, err) 45 assert.Len(t, d1.Id, 26) 46 assert.Equal(t, r1.Name, d1.Name) 47 assert.Equal(t, r1.DisplayName, d1.DisplayName) 48 assert.Equal(t, r1.Description, d1.Description) 49 assert.Equal(t, r1.Permissions, d1.Permissions) 50 assert.Equal(t, r1.SchemeManaged, d1.SchemeManaged) 51 52 // Change the role permissions and update. 53 d1.Permissions = []string{ 54 "invite_user", 55 "add_user_to_team", 56 "delete_public_channel", 57 } 58 59 d2, err := ss.Role().Save(d1) 60 assert.NoError(t, err) 61 assert.Len(t, d2.Id, 26) 62 assert.Equal(t, r1.Name, d2.Name) 63 assert.Equal(t, r1.DisplayName, d2.DisplayName) 64 assert.Equal(t, r1.Description, d2.Description) 65 assert.Equal(t, d1.Permissions, d2.Permissions) 66 assert.Equal(t, r1.SchemeManaged, d2.SchemeManaged) 67 68 // Try saving one with an invalid ID set. 69 r3 := &model.Role{ 70 Id: model.NewId(), 71 Name: model.NewId(), 72 DisplayName: model.NewId(), 73 Description: model.NewId(), 74 Permissions: []string{ 75 "invite_user", 76 "create_public_channel", 77 "add_user_to_team", 78 }, 79 SchemeManaged: false, 80 } 81 82 _, err = ss.Role().Save(r3) 83 assert.Error(t, err) 84 85 // Try saving one with a duplicate "name" field. 86 r4 := &model.Role{ 87 Name: r1.Name, 88 DisplayName: model.NewId(), 89 Description: model.NewId(), 90 Permissions: []string{ 91 "invite_user", 92 "create_public_channel", 93 "add_user_to_team", 94 }, 95 SchemeManaged: false, 96 } 97 98 _, err = ss.Role().Save(r4) 99 assert.Error(t, err) 100 } 101 102 func testRoleStoreGetAll(t *testing.T, ss store.Store) { 103 prev, err := ss.Role().GetAll() 104 require.NoError(t, err) 105 prevCount := len(prev) 106 107 // Save a role to test with. 108 r1 := &model.Role{ 109 Name: model.NewId(), 110 DisplayName: model.NewId(), 111 Description: model.NewId(), 112 Permissions: []string{ 113 "invite_user", 114 "create_public_channel", 115 "add_user_to_team", 116 }, 117 SchemeManaged: false, 118 } 119 120 _, err = ss.Role().Save(r1) 121 require.NoError(t, err) 122 123 r2 := &model.Role{ 124 Name: model.NewId(), 125 DisplayName: model.NewId(), 126 Description: model.NewId(), 127 Permissions: []string{ 128 "invite_user", 129 "create_public_channel", 130 "add_user_to_team", 131 }, 132 SchemeManaged: false, 133 } 134 _, err = ss.Role().Save(r2) 135 require.NoError(t, err) 136 137 data, err := ss.Role().GetAll() 138 require.NoError(t, err) 139 assert.Len(t, data, prevCount+2) 140 } 141 142 func testRoleStoreGet(t *testing.T, ss store.Store) { 143 // Save a role to test with. 144 r1 := &model.Role{ 145 Name: model.NewId(), 146 DisplayName: model.NewId(), 147 Description: model.NewId(), 148 Permissions: []string{ 149 "invite_user", 150 "create_public_channel", 151 "add_user_to_team", 152 }, 153 SchemeManaged: false, 154 } 155 156 d1, err := ss.Role().Save(r1) 157 assert.NoError(t, err) 158 assert.Len(t, d1.Id, 26) 159 160 // Get a valid role 161 d2, err := ss.Role().Get(d1.Id) 162 assert.NoError(t, err) 163 assert.Equal(t, d1.Id, d2.Id) 164 assert.Equal(t, r1.Name, d2.Name) 165 assert.Equal(t, r1.DisplayName, d2.DisplayName) 166 assert.Equal(t, r1.Description, d2.Description) 167 assert.Equal(t, r1.Permissions, d2.Permissions) 168 assert.Equal(t, r1.SchemeManaged, d2.SchemeManaged) 169 170 // Get an invalid role 171 _, err = ss.Role().Get(model.NewId()) 172 assert.Error(t, err) 173 } 174 175 func testRoleStoreGetByName(t *testing.T, ss store.Store) { 176 // Save a role to test with. 177 r1 := &model.Role{ 178 Name: model.NewId(), 179 DisplayName: model.NewId(), 180 Description: model.NewId(), 181 Permissions: []string{ 182 "invite_user", 183 "create_public_channel", 184 "add_user_to_team", 185 }, 186 SchemeManaged: false, 187 } 188 189 d1, err := ss.Role().Save(r1) 190 assert.NoError(t, err) 191 assert.Len(t, d1.Id, 26) 192 193 // Get a valid role 194 d2, err := ss.Role().GetByName(d1.Name) 195 assert.NoError(t, err) 196 assert.Equal(t, d1.Id, d2.Id) 197 assert.Equal(t, r1.Name, d2.Name) 198 assert.Equal(t, r1.DisplayName, d2.DisplayName) 199 assert.Equal(t, r1.Description, d2.Description) 200 assert.Equal(t, r1.Permissions, d2.Permissions) 201 assert.Equal(t, r1.SchemeManaged, d2.SchemeManaged) 202 203 // Get an invalid role 204 _, err = ss.Role().GetByName(model.NewId()) 205 assert.Error(t, err) 206 } 207 208 func testRoleStoreGetByNames(t *testing.T, ss store.Store) { 209 // Save some roles to test with. 210 r1 := &model.Role{ 211 Name: model.NewId(), 212 DisplayName: model.NewId(), 213 Description: model.NewId(), 214 Permissions: []string{ 215 "invite_user", 216 "create_public_channel", 217 "add_user_to_team", 218 }, 219 SchemeManaged: false, 220 } 221 r2 := &model.Role{ 222 Name: model.NewId(), 223 DisplayName: model.NewId(), 224 Description: model.NewId(), 225 Permissions: []string{ 226 "read_channel", 227 "create_public_channel", 228 "add_user_to_team", 229 }, 230 SchemeManaged: false, 231 } 232 r3 := &model.Role{ 233 Name: model.NewId(), 234 DisplayName: model.NewId(), 235 Description: model.NewId(), 236 Permissions: []string{ 237 "invite_user", 238 "delete_private_channel", 239 "add_user_to_team", 240 }, 241 SchemeManaged: false, 242 } 243 244 d1, err := ss.Role().Save(r1) 245 assert.NoError(t, err) 246 assert.Len(t, d1.Id, 26) 247 248 d2, err := ss.Role().Save(r2) 249 assert.NoError(t, err) 250 assert.Len(t, d2.Id, 26) 251 252 d3, err := ss.Role().Save(r3) 253 assert.NoError(t, err) 254 assert.Len(t, d3.Id, 26) 255 256 // Get two valid roles. 257 n4 := []string{r1.Name, r2.Name} 258 roles4, err := ss.Role().GetByNames(n4) 259 assert.NoError(t, err) 260 assert.Len(t, roles4, 2) 261 assert.Contains(t, roles4, d1) 262 assert.Contains(t, roles4, d2) 263 assert.NotContains(t, roles4, d3) 264 265 // Get two invalid roles. 266 n5 := []string{model.NewId(), model.NewId()} 267 roles5, err := ss.Role().GetByNames(n5) 268 assert.NoError(t, err) 269 assert.Empty(t, roles5) 270 271 // Get one valid one and one invalid one. 272 n6 := []string{r1.Name, model.NewId()} 273 roles6, err := ss.Role().GetByNames(n6) 274 assert.NoError(t, err) 275 assert.Len(t, roles6, 1) 276 assert.Contains(t, roles6, d1) 277 assert.NotContains(t, roles6, d2) 278 assert.NotContains(t, roles6, d3) 279 } 280 281 func testRoleStoreDelete(t *testing.T, ss store.Store) { 282 // Save a role to test with. 283 r1 := &model.Role{ 284 Name: model.NewId(), 285 DisplayName: model.NewId(), 286 Description: model.NewId(), 287 Permissions: []string{ 288 "invite_user", 289 "create_public_channel", 290 "add_user_to_team", 291 }, 292 SchemeManaged: false, 293 } 294 295 d1, err := ss.Role().Save(r1) 296 assert.NoError(t, err) 297 assert.Len(t, d1.Id, 26) 298 299 // Check the role is there. 300 _, err = ss.Role().Get(d1.Id) 301 assert.NoError(t, err) 302 303 // Delete the role. 304 _, err = ss.Role().Delete(d1.Id) 305 assert.NoError(t, err) 306 307 // Check the role is deleted there. 308 d2, err := ss.Role().Get(d1.Id) 309 assert.NoError(t, err) 310 assert.NotZero(t, d2.DeleteAt) 311 312 d3, err := ss.Role().GetByName(d1.Name) 313 assert.NoError(t, err) 314 assert.NotZero(t, d3.DeleteAt) 315 316 // Try and delete a role that does not exist. 317 _, err = ss.Role().Delete(model.NewId()) 318 assert.Error(t, err) 319 } 320 321 func testRoleStorePermanentDeleteAll(t *testing.T, ss store.Store) { 322 r1 := &model.Role{ 323 Name: model.NewId(), 324 DisplayName: model.NewId(), 325 Description: model.NewId(), 326 Permissions: []string{ 327 "invite_user", 328 "create_public_channel", 329 "add_user_to_team", 330 }, 331 SchemeManaged: false, 332 } 333 334 r2 := &model.Role{ 335 Name: model.NewId(), 336 DisplayName: model.NewId(), 337 Description: model.NewId(), 338 Permissions: []string{ 339 "read_channel", 340 "create_public_channel", 341 "add_user_to_team", 342 }, 343 SchemeManaged: false, 344 } 345 346 _, err := ss.Role().Save(r1) 347 require.NoError(t, err) 348 _, err = ss.Role().Save(r2) 349 require.NoError(t, err) 350 351 roles, err := ss.Role().GetByNames([]string{r1.Name, r2.Name}) 352 assert.NoError(t, err) 353 assert.Len(t, roles, 2) 354 355 err = ss.Role().PermanentDeleteAll() 356 assert.NoError(t, err) 357 358 roles, err = ss.Role().GetByNames([]string{r1.Name, r2.Name}) 359 assert.NoError(t, err) 360 assert.Empty(t, roles) 361 } 362 363 func testRoleStoreLowerScopedChannelSchemeRoles(t *testing.T, ss store.Store) { 364 createDefaultRoles(ss) 365 366 teamScheme1 := &model.Scheme{ 367 DisplayName: model.NewId(), 368 Name: model.NewId(), 369 Description: model.NewId(), 370 Scope: model.SCHEME_SCOPE_TEAM, 371 } 372 teamScheme1, err := ss.Scheme().Save(teamScheme1) 373 require.NoError(t, err) 374 defer ss.Scheme().Delete(teamScheme1.Id) 375 376 teamScheme2 := &model.Scheme{ 377 DisplayName: model.NewId(), 378 Name: model.NewId(), 379 Description: model.NewId(), 380 Scope: model.SCHEME_SCOPE_TEAM, 381 } 382 teamScheme2, err = ss.Scheme().Save(teamScheme2) 383 require.NoError(t, err) 384 defer ss.Scheme().Delete(teamScheme2.Id) 385 386 channelScheme1 := &model.Scheme{ 387 DisplayName: model.NewId(), 388 Name: model.NewId(), 389 Description: model.NewId(), 390 Scope: model.SCHEME_SCOPE_CHANNEL, 391 } 392 channelScheme1, err = ss.Scheme().Save(channelScheme1) 393 require.NoError(t, err) 394 defer ss.Scheme().Delete(channelScheme1.Id) 395 396 channelScheme2 := &model.Scheme{ 397 DisplayName: model.NewId(), 398 Name: model.NewId(), 399 Description: model.NewId(), 400 Scope: model.SCHEME_SCOPE_CHANNEL, 401 } 402 channelScheme2, err = ss.Scheme().Save(channelScheme2) 403 require.NoError(t, err) 404 defer ss.Scheme().Delete(channelScheme1.Id) 405 406 team1 := &model.Team{ 407 DisplayName: "Name", 408 Name: "zz" + model.NewId(), 409 Email: MakeEmail(), 410 Type: model.TEAM_OPEN, 411 SchemeId: &teamScheme1.Id, 412 } 413 team1, err = ss.Team().Save(team1) 414 require.NoError(t, err) 415 defer ss.Team().PermanentDelete(team1.Id) 416 417 team2 := &model.Team{ 418 DisplayName: "Name", 419 Name: "zz" + model.NewId(), 420 Email: MakeEmail(), 421 Type: model.TEAM_OPEN, 422 SchemeId: &teamScheme2.Id, 423 } 424 team2, err = ss.Team().Save(team2) 425 require.NoError(t, err) 426 defer ss.Team().PermanentDelete(team2.Id) 427 428 channel1 := &model.Channel{ 429 TeamId: team1.Id, 430 DisplayName: "Display " + model.NewId(), 431 Name: "zz" + model.NewId() + "b", 432 Type: model.CHANNEL_OPEN, 433 SchemeId: &channelScheme1.Id, 434 } 435 channel1, nErr := ss.Channel().Save(channel1, -1) 436 require.NoError(t, nErr) 437 defer ss.Channel().Delete(channel1.Id, 0) 438 439 channel2 := &model.Channel{ 440 TeamId: team2.Id, 441 DisplayName: "Display " + model.NewId(), 442 Name: "zz" + model.NewId() + "b", 443 Type: model.CHANNEL_OPEN, 444 SchemeId: &channelScheme2.Id, 445 } 446 channel2, nErr = ss.Channel().Save(channel2, -1) 447 require.NoError(t, nErr) 448 defer ss.Channel().Delete(channel2.Id, 0) 449 450 t.Run("ChannelRolesUnderTeamRole", func(t *testing.T) { 451 t.Run("guest role for the right team's channels are returned", func(t *testing.T) { 452 actualRoles, err := ss.Role().ChannelRolesUnderTeamRole(teamScheme1.DefaultChannelGuestRole) 453 require.NoError(t, err) 454 455 var actualRoleNames []string 456 for _, role := range actualRoles { 457 actualRoleNames = append(actualRoleNames, role.Name) 458 } 459 460 require.Contains(t, actualRoleNames, channelScheme1.DefaultChannelGuestRole) 461 require.NotContains(t, actualRoleNames, channelScheme2.DefaultChannelGuestRole) 462 }) 463 464 t.Run("user role for the right team's channels are returned", func(t *testing.T) { 465 actualRoles, err := ss.Role().ChannelRolesUnderTeamRole(teamScheme1.DefaultChannelUserRole) 466 require.NoError(t, err) 467 468 var actualRoleNames []string 469 for _, role := range actualRoles { 470 actualRoleNames = append(actualRoleNames, role.Name) 471 } 472 473 require.Contains(t, actualRoleNames, channelScheme1.DefaultChannelUserRole) 474 require.NotContains(t, actualRoleNames, channelScheme2.DefaultChannelUserRole) 475 }) 476 477 t.Run("admin role for the right team's channels are returned", func(t *testing.T) { 478 actualRoles, err := ss.Role().ChannelRolesUnderTeamRole(teamScheme1.DefaultChannelAdminRole) 479 require.NoError(t, err) 480 481 var actualRoleNames []string 482 for _, role := range actualRoles { 483 actualRoleNames = append(actualRoleNames, role.Name) 484 } 485 486 require.Contains(t, actualRoleNames, channelScheme1.DefaultChannelAdminRole) 487 require.NotContains(t, actualRoleNames, channelScheme2.DefaultChannelAdminRole) 488 }) 489 }) 490 491 t.Run("AllChannelSchemeRoles", func(t *testing.T) { 492 t.Run("guest role for the right team's channels are returned", func(t *testing.T) { 493 actualRoles, err := ss.Role().AllChannelSchemeRoles() 494 require.NoError(t, err) 495 496 var actualRoleNames []string 497 for _, role := range actualRoles { 498 actualRoleNames = append(actualRoleNames, role.Name) 499 } 500 501 allRoleNames := []string{ 502 channelScheme1.DefaultChannelGuestRole, 503 channelScheme2.DefaultChannelGuestRole, 504 505 channelScheme1.DefaultChannelUserRole, 506 channelScheme2.DefaultChannelUserRole, 507 508 channelScheme1.DefaultChannelAdminRole, 509 channelScheme2.DefaultChannelAdminRole, 510 } 511 512 for _, roleName := range allRoleNames { 513 require.Contains(t, actualRoleNames, roleName) 514 } 515 }) 516 }) 517 } 518 519 func testRoleStoreChannelHigherScopedPermissionsBlankTeamSchemeChannelGuest(t *testing.T, ss store.Store, s SqlStore) { 520 teamScheme := &model.Scheme{ 521 DisplayName: model.NewId(), 522 Name: model.NewId(), 523 Description: model.NewId(), 524 Scope: model.SCHEME_SCOPE_TEAM, 525 } 526 teamScheme, err := ss.Scheme().Save(teamScheme) 527 require.NoError(t, err) 528 defer ss.Scheme().Delete(teamScheme.Id) 529 530 channelScheme := &model.Scheme{ 531 DisplayName: model.NewId(), 532 Name: model.NewId(), 533 Description: model.NewId(), 534 Scope: model.SCHEME_SCOPE_CHANNEL, 535 } 536 channelScheme, err = ss.Scheme().Save(channelScheme) 537 require.NoError(t, err) 538 defer ss.Scheme().Delete(channelScheme.Id) 539 540 team := &model.Team{ 541 DisplayName: "Name", 542 Name: "zz" + model.NewId(), 543 Email: MakeEmail(), 544 Type: model.TEAM_OPEN, 545 SchemeId: &teamScheme.Id, 546 } 547 team, err = ss.Team().Save(team) 548 require.NoError(t, err) 549 defer ss.Team().PermanentDelete(team.Id) 550 551 channel := &model.Channel{ 552 TeamId: team.Id, 553 DisplayName: "Display " + model.NewId(), 554 Name: "zz" + model.NewId() + "b", 555 Type: model.CHANNEL_OPEN, 556 SchemeId: &channelScheme.Id, 557 } 558 channel, nErr := ss.Channel().Save(channel, -1) 559 require.NoError(t, nErr) 560 defer ss.Channel().Delete(channel.Id, 0) 561 562 channelSchemeUserRole, err := ss.Role().GetByName(channelScheme.DefaultChannelUserRole) 563 require.NoError(t, err) 564 channelSchemeUserRole.Permissions = []string{} 565 _, err = ss.Role().Save(channelSchemeUserRole) 566 require.NoError(t, err) 567 568 teamSchemeUserRole, err := ss.Role().GetByName(teamScheme.DefaultChannelUserRole) 569 require.NoError(t, err) 570 teamSchemeUserRole.Permissions = []string{model.PERMISSION_UPLOAD_FILE.Id} 571 _, err = ss.Role().Save(teamSchemeUserRole) 572 require.NoError(t, err) 573 574 // get the channel scheme user role again and ensure that it has the permission inherited from the team 575 // scheme user role 576 roleMapBefore, err := ss.Role().ChannelHigherScopedPermissions([]string{channelSchemeUserRole.Name}) 577 require.NoError(t, err) 578 579 // blank-out the guest role to simulate an old team scheme, ensure it's blank 580 result, sqlErr := s.GetMaster().Exec(fmt.Sprintf("UPDATE Schemes SET DefaultChannelGuestRole = '' WHERE Id = '%s'", teamScheme.Id)) 581 require.NoError(t, sqlErr) 582 rows, serr := result.RowsAffected() 583 require.NoError(t, serr) 584 require.Equal(t, int64(1), rows) 585 teamScheme, err = ss.Scheme().Get(teamScheme.Id) 586 require.NoError(t, err) 587 require.Equal(t, "", teamScheme.DefaultChannelGuestRole) 588 589 // trigger a cache clear 590 _, err = ss.Role().Save(channelSchemeUserRole) 591 require.NoError(t, err) 592 593 roleMapAfter, err := ss.Role().ChannelHigherScopedPermissions([]string{channelSchemeUserRole.Name}) 594 require.NoError(t, err) 595 596 require.Equal(t, len(roleMapBefore), len(roleMapAfter)) 597 }