github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/sqlstore/channel_store_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package sqlstore 5 6 import ( 7 "database/sql" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 12 "github.com/mattermost/mattermost-server/model" 13 "github.com/mattermost/mattermost-server/store/storetest" 14 ) 15 16 func TestChannelStore(t *testing.T) { 17 StoreTestWithSqlSupplier(t, storetest.TestChannelStore) 18 } 19 20 func TestChannelStoreInternalDataTypes(t *testing.T) { 21 t.Run("NewChannelMemberFromModel", func(t *testing.T) { testNewChannelMemberFromModel(t) }) 22 t.Run("ChannelMemberWithSchemeRolesToModel", func(t *testing.T) { testChannelMemberWithSchemeRolesToModel(t) }) 23 t.Run("AllChannelMemberProcess", func(t *testing.T) { testAllChannelMemberProcess(t) }) 24 } 25 26 func testNewChannelMemberFromModel(t *testing.T) { 27 m := model.ChannelMember{ 28 ChannelId: model.NewId(), 29 UserId: model.NewId(), 30 Roles: "channel_user channel_admin custom_role", 31 LastViewedAt: 12345, 32 MsgCount: 2, 33 MentionCount: 1, 34 NotifyProps: model.StringMap{"key": "value"}, 35 LastUpdateAt: 54321, 36 SchemeUser: true, 37 SchemeAdmin: true, 38 ExplicitRoles: "custom_role", 39 } 40 41 db := NewChannelMemberFromModel(&m) 42 43 assert.Equal(t, m.ChannelId, db.ChannelId) 44 assert.Equal(t, m.UserId, db.UserId) 45 assert.Equal(t, m.LastViewedAt, db.LastViewedAt) 46 assert.Equal(t, m.MsgCount, db.MsgCount) 47 assert.Equal(t, m.MentionCount, db.MentionCount) 48 assert.Equal(t, m.NotifyProps, db.NotifyProps) 49 assert.Equal(t, m.LastUpdateAt, db.LastUpdateAt) 50 assert.Equal(t, true, db.SchemeUser.Valid) 51 assert.Equal(t, true, db.SchemeAdmin.Valid) 52 assert.Equal(t, m.SchemeUser, db.SchemeUser.Bool) 53 assert.Equal(t, m.SchemeAdmin, db.SchemeAdmin.Bool) 54 assert.Equal(t, m.ExplicitRoles, db.Roles) 55 } 56 57 func testChannelMemberWithSchemeRolesToModel(t *testing.T) { 58 t.Run("BasicProperties", func(t *testing.T) { 59 // Test all the non-roles properties here. 60 db := channelMemberWithSchemeRoles{ 61 ChannelId: model.NewId(), 62 UserId: model.NewId(), 63 Roles: "custom_role", 64 LastViewedAt: 12345, 65 MsgCount: 2, 66 MentionCount: 1, 67 NotifyProps: model.StringMap{"key": "value"}, 68 LastUpdateAt: 54321, 69 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 70 SchemeAdmin: sql.NullBool{Valid: true, Bool: true}, 71 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 72 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 73 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 74 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 75 } 76 77 m := db.ToModel() 78 79 assert.Equal(t, db.ChannelId, m.ChannelId) 80 assert.Equal(t, db.UserId, m.UserId) 81 assert.Equal(t, "custom_role channel_user channel_admin", m.Roles) 82 assert.Equal(t, db.LastViewedAt, m.LastViewedAt) 83 assert.Equal(t, db.MsgCount, m.MsgCount) 84 assert.Equal(t, db.MentionCount, m.MentionCount) 85 assert.Equal(t, db.NotifyProps, m.NotifyProps) 86 assert.Equal(t, db.LastUpdateAt, m.LastUpdateAt) 87 assert.Equal(t, db.SchemeUser.Bool, m.SchemeUser) 88 assert.Equal(t, db.SchemeAdmin.Bool, m.SchemeAdmin) 89 assert.Equal(t, db.Roles, m.ExplicitRoles) 90 }) 91 92 // Example data *before* the Phase 2 migration has taken place. 93 t.Run("Unmigrated_NoScheme_User", func(t *testing.T) { 94 db := channelMemberWithSchemeRoles{ 95 Roles: "channel_user", 96 SchemeUser: sql.NullBool{Valid: false, Bool: false}, 97 SchemeAdmin: sql.NullBool{Valid: false, Bool: false}, 98 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 99 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 100 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 101 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 102 } 103 104 cm := db.ToModel() 105 106 assert.Equal(t, "channel_user", cm.Roles) 107 assert.Equal(t, true, cm.SchemeUser) 108 assert.Equal(t, false, cm.SchemeAdmin) 109 assert.Equal(t, "", cm.ExplicitRoles) 110 }) 111 112 t.Run("Unmigrated_NoScheme_Admin", func(t *testing.T) { 113 db := channelMemberWithSchemeRoles{ 114 Roles: "channel_admin channel_user", 115 SchemeUser: sql.NullBool{Valid: false, Bool: false}, 116 SchemeAdmin: sql.NullBool{Valid: false, Bool: false}, 117 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 118 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 119 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 120 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 121 } 122 123 cm := db.ToModel() 124 125 assert.Equal(t, "channel_admin channel_user", cm.Roles) 126 assert.Equal(t, true, cm.SchemeUser) 127 assert.Equal(t, true, cm.SchemeAdmin) 128 assert.Equal(t, "", cm.ExplicitRoles) 129 }) 130 131 t.Run("Unmigrated_NoScheme_CustomRole", func(t *testing.T) { 132 db := channelMemberWithSchemeRoles{ 133 Roles: "custom_role", 134 SchemeUser: sql.NullBool{Valid: false, Bool: false}, 135 SchemeAdmin: sql.NullBool{Valid: false, Bool: false}, 136 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 137 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 138 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 139 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 140 } 141 142 cm := db.ToModel() 143 144 assert.Equal(t, "custom_role", cm.Roles) 145 assert.Equal(t, false, cm.SchemeUser) 146 assert.Equal(t, false, cm.SchemeAdmin) 147 assert.Equal(t, "custom_role", cm.ExplicitRoles) 148 }) 149 150 t.Run("Unmigrated_NoScheme_UserAndCustomRole", func(t *testing.T) { 151 db := channelMemberWithSchemeRoles{ 152 Roles: "channel_user custom_role", 153 SchemeUser: sql.NullBool{Valid: false, Bool: false}, 154 SchemeAdmin: sql.NullBool{Valid: false, Bool: false}, 155 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 156 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 157 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 158 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 159 } 160 161 cm := db.ToModel() 162 163 assert.Equal(t, "channel_user custom_role", cm.Roles) 164 assert.Equal(t, true, cm.SchemeUser) 165 assert.Equal(t, false, cm.SchemeAdmin) 166 assert.Equal(t, "custom_role", cm.ExplicitRoles) 167 }) 168 169 t.Run("Unmigrated_NoScheme_AdminAndCustomRole", func(t *testing.T) { 170 db := channelMemberWithSchemeRoles{ 171 Roles: "channel_user channel_admin custom_role", 172 SchemeUser: sql.NullBool{Valid: false, Bool: false}, 173 SchemeAdmin: sql.NullBool{Valid: false, Bool: false}, 174 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 175 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 176 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 177 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 178 } 179 180 cm := db.ToModel() 181 182 assert.Equal(t, "channel_user channel_admin custom_role", cm.Roles) 183 assert.Equal(t, true, cm.SchemeUser) 184 assert.Equal(t, true, cm.SchemeAdmin) 185 assert.Equal(t, "custom_role", cm.ExplicitRoles) 186 }) 187 188 t.Run("Unmigrated_NoScheme_NoRoles", func(t *testing.T) { 189 db := channelMemberWithSchemeRoles{ 190 Roles: "", 191 SchemeUser: sql.NullBool{Valid: false, Bool: false}, 192 SchemeAdmin: sql.NullBool{Valid: false, Bool: false}, 193 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 194 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 195 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 196 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 197 } 198 199 cm := db.ToModel() 200 201 assert.Equal(t, "", cm.Roles) 202 assert.Equal(t, false, cm.SchemeUser) 203 assert.Equal(t, false, cm.SchemeAdmin) 204 assert.Equal(t, "", cm.ExplicitRoles) 205 }) 206 207 // Example data *after* the Phase 2 migration has taken place. 208 t.Run("Migrated_NoScheme_User", func(t *testing.T) { 209 db := channelMemberWithSchemeRoles{ 210 Roles: "", 211 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 212 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 213 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 214 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 215 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 216 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 217 } 218 219 cm := db.ToModel() 220 221 assert.Equal(t, "channel_user", cm.Roles) 222 assert.Equal(t, true, cm.SchemeUser) 223 assert.Equal(t, false, cm.SchemeAdmin) 224 assert.Equal(t, "", cm.ExplicitRoles) 225 }) 226 227 t.Run("Migrated_NoScheme_Admin", func(t *testing.T) { 228 db := channelMemberWithSchemeRoles{ 229 Roles: "", 230 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 231 SchemeAdmin: sql.NullBool{Valid: true, Bool: true}, 232 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 233 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 234 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 235 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 236 } 237 238 cm := db.ToModel() 239 240 assert.Equal(t, "channel_user channel_admin", cm.Roles) 241 assert.Equal(t, true, cm.SchemeUser) 242 assert.Equal(t, true, cm.SchemeAdmin) 243 assert.Equal(t, "", cm.ExplicitRoles) 244 }) 245 246 t.Run("Migrated_NoScheme_CustomRole", func(t *testing.T) { 247 db := channelMemberWithSchemeRoles{ 248 Roles: "custom_role", 249 SchemeUser: sql.NullBool{Valid: true, Bool: false}, 250 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 251 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 252 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 253 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 254 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 255 } 256 257 cm := db.ToModel() 258 259 assert.Equal(t, "custom_role", cm.Roles) 260 assert.Equal(t, false, cm.SchemeUser) 261 assert.Equal(t, false, cm.SchemeAdmin) 262 assert.Equal(t, "custom_role", cm.ExplicitRoles) 263 }) 264 265 t.Run("Migrated_NoScheme_UserAndCustomRole", func(t *testing.T) { 266 db := channelMemberWithSchemeRoles{ 267 Roles: "custom_role", 268 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 269 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 270 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 271 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 272 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 273 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 274 } 275 276 cm := db.ToModel() 277 278 assert.Equal(t, "custom_role channel_user", cm.Roles) 279 assert.Equal(t, true, cm.SchemeUser) 280 assert.Equal(t, false, cm.SchemeAdmin) 281 assert.Equal(t, "custom_role", cm.ExplicitRoles) 282 }) 283 284 t.Run("Migrated_NoScheme_AdminAndCustomRole", func(t *testing.T) { 285 db := channelMemberWithSchemeRoles{ 286 Roles: "custom_role", 287 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 288 SchemeAdmin: sql.NullBool{Valid: true, Bool: true}, 289 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 290 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 291 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 292 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 293 } 294 295 cm := db.ToModel() 296 297 assert.Equal(t, "custom_role channel_user channel_admin", cm.Roles) 298 assert.Equal(t, true, cm.SchemeUser) 299 assert.Equal(t, true, cm.SchemeAdmin) 300 assert.Equal(t, "custom_role", cm.ExplicitRoles) 301 }) 302 303 t.Run("Migrated_NoScheme_NoRoles", func(t *testing.T) { 304 db := channelMemberWithSchemeRoles{ 305 Roles: "", 306 SchemeUser: sql.NullBool{Valid: true, Bool: false}, 307 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 308 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 309 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 310 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 311 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 312 } 313 314 cm := db.ToModel() 315 316 assert.Equal(t, "", cm.Roles) 317 assert.Equal(t, false, cm.SchemeUser) 318 assert.Equal(t, false, cm.SchemeAdmin) 319 assert.Equal(t, "", cm.ExplicitRoles) 320 }) 321 322 // Example data with a channel scheme. 323 t.Run("Migrated_ChannelScheme_User", func(t *testing.T) { 324 db := channelMemberWithSchemeRoles{ 325 Roles: "", 326 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 327 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 328 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 329 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 330 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 331 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 332 } 333 334 cm := db.ToModel() 335 336 assert.Equal(t, "cscheme_user", cm.Roles) 337 assert.Equal(t, true, cm.SchemeUser) 338 assert.Equal(t, false, cm.SchemeAdmin) 339 assert.Equal(t, "", cm.ExplicitRoles) 340 }) 341 342 t.Run("Migrated_ChannelScheme_Admin", func(t *testing.T) { 343 db := channelMemberWithSchemeRoles{ 344 Roles: "", 345 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 346 SchemeAdmin: sql.NullBool{Valid: true, Bool: true}, 347 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 348 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 349 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 350 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 351 } 352 353 cm := db.ToModel() 354 355 assert.Equal(t, "cscheme_user cscheme_admin", cm.Roles) 356 assert.Equal(t, true, cm.SchemeUser) 357 assert.Equal(t, true, cm.SchemeAdmin) 358 assert.Equal(t, "", cm.ExplicitRoles) 359 }) 360 361 t.Run("Migrated_ChannelScheme_CustomRole", func(t *testing.T) { 362 db := channelMemberWithSchemeRoles{ 363 Roles: "custom_role", 364 SchemeUser: sql.NullBool{Valid: true, Bool: false}, 365 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 366 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 367 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 368 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 369 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 370 } 371 372 cm := db.ToModel() 373 374 assert.Equal(t, "custom_role", cm.Roles) 375 assert.Equal(t, false, cm.SchemeUser) 376 assert.Equal(t, false, cm.SchemeAdmin) 377 assert.Equal(t, "custom_role", cm.ExplicitRoles) 378 }) 379 380 t.Run("Migrated_ChannelScheme_UserAndCustomRole", func(t *testing.T) { 381 db := channelMemberWithSchemeRoles{ 382 Roles: "custom_role", 383 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 384 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 385 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 386 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 387 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 388 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 389 } 390 391 cm := db.ToModel() 392 393 assert.Equal(t, "custom_role cscheme_user", cm.Roles) 394 assert.Equal(t, true, cm.SchemeUser) 395 assert.Equal(t, false, cm.SchemeAdmin) 396 assert.Equal(t, "custom_role", cm.ExplicitRoles) 397 }) 398 399 t.Run("Migrated_ChannelScheme_AdminAndCustomRole", func(t *testing.T) { 400 db := channelMemberWithSchemeRoles{ 401 Roles: "custom_role", 402 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 403 SchemeAdmin: sql.NullBool{Valid: true, Bool: true}, 404 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 405 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 406 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 407 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 408 } 409 410 cm := db.ToModel() 411 412 assert.Equal(t, "custom_role cscheme_user cscheme_admin", cm.Roles) 413 assert.Equal(t, true, cm.SchemeUser) 414 assert.Equal(t, true, cm.SchemeAdmin) 415 assert.Equal(t, "custom_role", cm.ExplicitRoles) 416 }) 417 418 t.Run("Migrated_ChannelScheme_NoRoles", func(t *testing.T) { 419 db := channelMemberWithSchemeRoles{ 420 Roles: "", 421 SchemeUser: sql.NullBool{Valid: true, Bool: false}, 422 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 423 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 424 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 425 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 426 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 427 } 428 429 cm := db.ToModel() 430 431 assert.Equal(t, "", cm.Roles) 432 assert.Equal(t, false, cm.SchemeUser) 433 assert.Equal(t, false, cm.SchemeAdmin) 434 assert.Equal(t, "", cm.ExplicitRoles) 435 }) 436 437 // Example data with a team scheme. 438 t.Run("Migrated_TeamScheme_User", func(t *testing.T) { 439 db := channelMemberWithSchemeRoles{ 440 Roles: "", 441 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 442 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 443 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 444 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 445 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 446 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 447 } 448 449 cm := db.ToModel() 450 451 assert.Equal(t, "tscheme_channeluser", cm.Roles) 452 assert.Equal(t, true, cm.SchemeUser) 453 assert.Equal(t, false, cm.SchemeAdmin) 454 assert.Equal(t, "", cm.ExplicitRoles) 455 }) 456 457 t.Run("Migrated_TeamScheme_Admin", func(t *testing.T) { 458 db := channelMemberWithSchemeRoles{ 459 Roles: "", 460 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 461 SchemeAdmin: sql.NullBool{Valid: true, Bool: true}, 462 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 463 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 464 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 465 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 466 } 467 468 cm := db.ToModel() 469 470 assert.Equal(t, "tscheme_channeluser tscheme_channeladmin", cm.Roles) 471 assert.Equal(t, true, cm.SchemeUser) 472 assert.Equal(t, true, cm.SchemeAdmin) 473 assert.Equal(t, "", cm.ExplicitRoles) 474 }) 475 476 t.Run("Migrated_TeamScheme_CustomRole", func(t *testing.T) { 477 db := channelMemberWithSchemeRoles{ 478 Roles: "custom_role", 479 SchemeUser: sql.NullBool{Valid: true, Bool: false}, 480 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 481 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 482 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 483 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 484 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 485 } 486 487 cm := db.ToModel() 488 489 assert.Equal(t, "custom_role", cm.Roles) 490 assert.Equal(t, false, cm.SchemeUser) 491 assert.Equal(t, false, cm.SchemeAdmin) 492 assert.Equal(t, "custom_role", cm.ExplicitRoles) 493 }) 494 495 t.Run("Migrated_TeamScheme_UserAndCustomRole", func(t *testing.T) { 496 db := channelMemberWithSchemeRoles{ 497 Roles: "custom_role", 498 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 499 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 500 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 501 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 502 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 503 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 504 } 505 506 cm := db.ToModel() 507 508 assert.Equal(t, "custom_role tscheme_channeluser", cm.Roles) 509 assert.Equal(t, true, cm.SchemeUser) 510 assert.Equal(t, false, cm.SchemeAdmin) 511 assert.Equal(t, "custom_role", cm.ExplicitRoles) 512 }) 513 514 t.Run("Migrated_TeamScheme_AdminAndCustomRole", func(t *testing.T) { 515 db := channelMemberWithSchemeRoles{ 516 Roles: "custom_role", 517 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 518 SchemeAdmin: sql.NullBool{Valid: true, Bool: true}, 519 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 520 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 521 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 522 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 523 } 524 525 cm := db.ToModel() 526 527 assert.Equal(t, "custom_role tscheme_channeluser tscheme_channeladmin", cm.Roles) 528 assert.Equal(t, true, cm.SchemeUser) 529 assert.Equal(t, true, cm.SchemeAdmin) 530 assert.Equal(t, "custom_role", cm.ExplicitRoles) 531 }) 532 533 t.Run("Migrated_TeamScheme_NoRoles", func(t *testing.T) { 534 db := channelMemberWithSchemeRoles{ 535 Roles: "", 536 SchemeUser: sql.NullBool{Valid: true, Bool: false}, 537 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 538 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 539 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 540 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 541 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 542 } 543 544 cm := db.ToModel() 545 546 assert.Equal(t, "", cm.Roles) 547 assert.Equal(t, false, cm.SchemeUser) 548 assert.Equal(t, false, cm.SchemeAdmin) 549 assert.Equal(t, "", cm.ExplicitRoles) 550 }) 551 552 // Example data with a team and channel scheme. 553 t.Run("Migrated_TeamAndChannelScheme_User", func(t *testing.T) { 554 db := channelMemberWithSchemeRoles{ 555 Roles: "", 556 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 557 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 558 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 559 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 560 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 561 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 562 } 563 564 cm := db.ToModel() 565 566 assert.Equal(t, "cscheme_user", cm.Roles) 567 assert.Equal(t, true, cm.SchemeUser) 568 assert.Equal(t, false, cm.SchemeAdmin) 569 assert.Equal(t, "", cm.ExplicitRoles) 570 }) 571 572 t.Run("Migrated_TeamAndChannelScheme_Admin", func(t *testing.T) { 573 db := channelMemberWithSchemeRoles{ 574 Roles: "", 575 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 576 SchemeAdmin: sql.NullBool{Valid: true, Bool: true}, 577 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 578 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 579 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 580 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 581 } 582 583 cm := db.ToModel() 584 585 assert.Equal(t, "cscheme_user cscheme_admin", cm.Roles) 586 assert.Equal(t, true, cm.SchemeUser) 587 assert.Equal(t, true, cm.SchemeAdmin) 588 assert.Equal(t, "", cm.ExplicitRoles) 589 }) 590 591 t.Run("Migrated_TeamAndChannelScheme_CustomRole", func(t *testing.T) { 592 db := channelMemberWithSchemeRoles{ 593 Roles: "custom_role", 594 SchemeUser: sql.NullBool{Valid: true, Bool: false}, 595 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 596 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 597 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 598 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 599 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 600 } 601 602 cm := db.ToModel() 603 604 assert.Equal(t, "custom_role", cm.Roles) 605 assert.Equal(t, false, cm.SchemeUser) 606 assert.Equal(t, false, cm.SchemeAdmin) 607 assert.Equal(t, "custom_role", cm.ExplicitRoles) 608 }) 609 610 t.Run("Migrated_TeamAndChannelScheme_UserAndCustomRole", func(t *testing.T) { 611 db := channelMemberWithSchemeRoles{ 612 Roles: "custom_role", 613 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 614 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 615 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 616 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 617 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 618 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 619 } 620 621 cm := db.ToModel() 622 623 assert.Equal(t, "custom_role cscheme_user", cm.Roles) 624 assert.Equal(t, true, cm.SchemeUser) 625 assert.Equal(t, false, cm.SchemeAdmin) 626 assert.Equal(t, "custom_role", cm.ExplicitRoles) 627 }) 628 629 t.Run("Migrated_TeamAndChannelScheme_AdminAndCustomRole", func(t *testing.T) { 630 db := channelMemberWithSchemeRoles{ 631 Roles: "custom_role", 632 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 633 SchemeAdmin: sql.NullBool{Valid: true, Bool: true}, 634 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 635 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 636 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 637 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 638 } 639 640 cm := db.ToModel() 641 642 assert.Equal(t, "custom_role cscheme_user cscheme_admin", cm.Roles) 643 assert.Equal(t, true, cm.SchemeUser) 644 assert.Equal(t, true, cm.SchemeAdmin) 645 assert.Equal(t, "custom_role", cm.ExplicitRoles) 646 }) 647 648 t.Run("Migrated_TeamAndChannelScheme_NoRoles", func(t *testing.T) { 649 db := channelMemberWithSchemeRoles{ 650 Roles: "", 651 SchemeUser: sql.NullBool{Valid: true, Bool: false}, 652 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 653 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 654 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 655 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 656 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 657 } 658 659 cm := db.ToModel() 660 661 assert.Equal(t, "", cm.Roles) 662 assert.Equal(t, false, cm.SchemeUser) 663 assert.Equal(t, false, cm.SchemeAdmin) 664 assert.Equal(t, "", cm.ExplicitRoles) 665 }) 666 } 667 668 func testAllChannelMemberProcess(t *testing.T) { 669 t.Run("Unmigrated_User", func(t *testing.T) { 670 db := allChannelMember{ 671 Roles: "channel_user", 672 SchemeUser: sql.NullBool{Valid: false, Bool: false}, 673 SchemeAdmin: sql.NullBool{Valid: false, Bool: false}, 674 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 675 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 676 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 677 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 678 } 679 680 _, roles := db.Process() 681 682 assert.Equal(t, "channel_user", roles) 683 }) 684 685 t.Run("Unmigrated_Admin", func(t *testing.T) { 686 db := allChannelMember{ 687 Roles: "channel_user channel_admin", 688 SchemeUser: sql.NullBool{Valid: false, Bool: false}, 689 SchemeAdmin: sql.NullBool{Valid: false, Bool: false}, 690 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 691 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 692 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 693 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 694 } 695 696 _, roles := db.Process() 697 698 assert.Equal(t, "channel_user channel_admin", roles) 699 }) 700 701 t.Run("Unmigrated_Neither", func(t *testing.T) { 702 db := allChannelMember{ 703 Roles: "", 704 SchemeUser: sql.NullBool{Valid: false, Bool: false}, 705 SchemeAdmin: sql.NullBool{Valid: false, Bool: false}, 706 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 707 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 708 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 709 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 710 } 711 712 _, roles := db.Process() 713 714 assert.Equal(t, "", roles) 715 }) 716 717 t.Run("Unmigrated_Custom", func(t *testing.T) { 718 db := allChannelMember{ 719 Roles: "custom", 720 SchemeUser: sql.NullBool{Valid: false, Bool: false}, 721 SchemeAdmin: sql.NullBool{Valid: false, Bool: false}, 722 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 723 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 724 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 725 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 726 } 727 728 _, roles := db.Process() 729 730 assert.Equal(t, "custom", roles) 731 }) 732 733 t.Run("MigratedNoScheme_User", func(t *testing.T) { 734 db := allChannelMember{ 735 Roles: "", 736 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 737 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 738 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 739 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 740 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 741 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 742 } 743 744 _, roles := db.Process() 745 746 assert.Equal(t, "channel_user", roles) 747 }) 748 749 t.Run("MigratedNoScheme_Admin", func(t *testing.T) { 750 db := allChannelMember{ 751 Roles: "", 752 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 753 SchemeAdmin: sql.NullBool{Valid: true, Bool: true}, 754 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 755 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 756 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 757 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 758 } 759 760 _, roles := db.Process() 761 762 assert.Equal(t, "channel_user channel_admin", roles) 763 }) 764 765 t.Run("MigratedNoScheme_Neither", func(t *testing.T) { 766 db := allChannelMember{ 767 Roles: "", 768 SchemeUser: sql.NullBool{Valid: true, Bool: false}, 769 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 770 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 771 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 772 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 773 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 774 } 775 776 _, roles := db.Process() 777 778 assert.Equal(t, "", roles) 779 }) 780 781 t.Run("MigratedChannelScheme_User", func(t *testing.T) { 782 db := allChannelMember{ 783 Roles: "", 784 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 785 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 786 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 787 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 788 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 789 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 790 } 791 792 _, roles := db.Process() 793 794 assert.Equal(t, "cscheme_user", roles) 795 }) 796 797 t.Run("MigratedChannelScheme_Admin", func(t *testing.T) { 798 db := allChannelMember{ 799 Roles: "", 800 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 801 SchemeAdmin: sql.NullBool{Valid: true, Bool: true}, 802 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 803 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 804 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 805 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 806 } 807 808 _, roles := db.Process() 809 810 assert.Equal(t, "cscheme_user cscheme_admin", roles) 811 }) 812 813 t.Run("MigratedChannelScheme_Neither", func(t *testing.T) { 814 db := allChannelMember{ 815 Roles: "", 816 SchemeUser: sql.NullBool{Valid: true, Bool: false}, 817 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 818 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 819 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 820 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 821 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 822 } 823 824 _, roles := db.Process() 825 826 assert.Equal(t, "", roles) 827 }) 828 829 t.Run("MigratedTeamScheme_User", func(t *testing.T) { 830 db := allChannelMember{ 831 Roles: "", 832 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 833 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 834 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 835 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 836 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 837 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 838 } 839 840 _, roles := db.Process() 841 842 assert.Equal(t, "tscheme_channeluser", roles) 843 }) 844 845 t.Run("MigratedTeamScheme_Admin", func(t *testing.T) { 846 db := allChannelMember{ 847 Roles: "", 848 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 849 SchemeAdmin: sql.NullBool{Valid: true, Bool: true}, 850 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 851 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 852 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 853 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 854 } 855 856 _, roles := db.Process() 857 858 assert.Equal(t, "tscheme_channeluser tscheme_channeladmin", roles) 859 }) 860 861 t.Run("MigratedTeamScheme_Neither", func(t *testing.T) { 862 db := allChannelMember{ 863 Roles: "", 864 SchemeUser: sql.NullBool{Valid: true, Bool: false}, 865 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 866 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 867 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 868 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 869 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 870 } 871 872 _, roles := db.Process() 873 874 assert.Equal(t, "", roles) 875 }) 876 877 t.Run("MigratedTeamAndChannelScheme_User", func(t *testing.T) { 878 db := allChannelMember{ 879 Roles: "", 880 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 881 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 882 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 883 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 884 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 885 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 886 } 887 888 _, roles := db.Process() 889 890 assert.Equal(t, "cscheme_user", roles) 891 }) 892 893 t.Run("MigratedTeamAndChannelScheme_Admin", func(t *testing.T) { 894 db := allChannelMember{ 895 Roles: "", 896 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 897 SchemeAdmin: sql.NullBool{Valid: true, Bool: true}, 898 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 899 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 900 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 901 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 902 } 903 904 _, roles := db.Process() 905 906 assert.Equal(t, "cscheme_user cscheme_admin", roles) 907 }) 908 909 t.Run("MigratedTeamAndChannelScheme_Neither", func(t *testing.T) { 910 db := allChannelMember{ 911 Roles: "", 912 SchemeUser: sql.NullBool{Valid: true, Bool: false}, 913 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 914 TeamSchemeDefaultUserRole: sql.NullString{Valid: true, String: "tscheme_channeluser"}, 915 TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_channeladmin"}, 916 ChannelSchemeDefaultUserRole: sql.NullString{Valid: true, String: "cscheme_user"}, 917 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "cscheme_admin"}, 918 } 919 920 _, roles := db.Process() 921 922 assert.Equal(t, "", roles) 923 }) 924 925 t.Run("DeduplicationCheck", func(t *testing.T) { 926 db := allChannelMember{ 927 Roles: "channel_user", 928 SchemeUser: sql.NullBool{Valid: true, Bool: true}, 929 SchemeAdmin: sql.NullBool{Valid: true, Bool: false}, 930 TeamSchemeDefaultUserRole: sql.NullString{Valid: false}, 931 TeamSchemeDefaultAdminRole: sql.NullString{Valid: false}, 932 ChannelSchemeDefaultUserRole: sql.NullString{Valid: false}, 933 ChannelSchemeDefaultAdminRole: sql.NullString{Valid: false}, 934 } 935 936 _, roles := db.Process() 937 938 assert.Equal(t, "channel_user", roles) 939 }) 940 }