github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/sqlstore/team_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 TestTeamStore(t *testing.T) {
    17  	StoreTest(t, storetest.TestTeamStore)
    18  }
    19  
    20  func TestTeamStoreInternalDataTypes(t *testing.T) {
    21  	t.Run("NewTeamMemberFromModel", func(t *testing.T) { testNewTeamMemberFromModel(t) })
    22  	t.Run("TeamMemberWithSchemeRolesToModel", func(t *testing.T) { testTeamMemberWithSchemeRolesToModel(t) })
    23  }
    24  
    25  func testNewTeamMemberFromModel(t *testing.T) {
    26  	m := model.TeamMember{
    27  		TeamId:        model.NewId(),
    28  		UserId:        model.NewId(),
    29  		Roles:         "team_user team_admin custom_role",
    30  		DeleteAt:      12345,
    31  		SchemeUser:    true,
    32  		SchemeAdmin:   true,
    33  		ExplicitRoles: "custom_role",
    34  	}
    35  
    36  	db := NewTeamMemberFromModel(&m)
    37  
    38  	assert.Equal(t, m.TeamId, db.TeamId)
    39  	assert.Equal(t, m.UserId, db.UserId)
    40  	assert.Equal(t, m.DeleteAt, db.DeleteAt)
    41  	assert.Equal(t, true, db.SchemeUser.Valid)
    42  	assert.Equal(t, true, db.SchemeAdmin.Valid)
    43  	assert.Equal(t, m.SchemeUser, db.SchemeUser.Bool)
    44  	assert.Equal(t, m.SchemeAdmin, db.SchemeAdmin.Bool)
    45  	assert.Equal(t, m.ExplicitRoles, db.Roles)
    46  }
    47  
    48  func testTeamMemberWithSchemeRolesToModel(t *testing.T) {
    49  	// Test all the non-role-related properties here.
    50  	t.Run("BasicProperties", func(t *testing.T) {
    51  		db := teamMemberWithSchemeRoles{
    52  			TeamId:                     model.NewId(),
    53  			UserId:                     model.NewId(),
    54  			Roles:                      "custom_role",
    55  			DeleteAt:                   12345,
    56  			SchemeUser:                 sql.NullBool{Valid: true, Bool: true},
    57  			SchemeAdmin:                sql.NullBool{Valid: true, Bool: true},
    58  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: false},
    59  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: false},
    60  		}
    61  
    62  		m := db.ToModel()
    63  
    64  		assert.Equal(t, db.TeamId, m.TeamId)
    65  		assert.Equal(t, db.UserId, m.UserId)
    66  		assert.Equal(t, "custom_role team_user team_admin", m.Roles)
    67  		assert.Equal(t, db.DeleteAt, m.DeleteAt)
    68  		assert.Equal(t, db.SchemeUser.Bool, m.SchemeUser)
    69  		assert.Equal(t, db.SchemeAdmin.Bool, m.SchemeAdmin)
    70  		assert.Equal(t, db.Roles, m.ExplicitRoles)
    71  	})
    72  
    73  	// Example data *before* the Phase 2 migration has taken place.
    74  	t.Run("Unmigrated_NoScheme_User", func(t *testing.T) {
    75  		db := teamMemberWithSchemeRoles{
    76  			Roles:                      "team_user",
    77  			SchemeUser:                 sql.NullBool{Valid: false, Bool: false},
    78  			SchemeAdmin:                sql.NullBool{Valid: false, Bool: false},
    79  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: false},
    80  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: false},
    81  		}
    82  
    83  		m := db.ToModel()
    84  
    85  		assert.Equal(t, "team_user", m.Roles)
    86  		assert.Equal(t, true, m.SchemeUser)
    87  		assert.Equal(t, false, m.SchemeAdmin)
    88  		assert.Equal(t, "", m.ExplicitRoles)
    89  	})
    90  
    91  	t.Run("Unmigrated_NoScheme_Admin", func(t *testing.T) {
    92  		db := teamMemberWithSchemeRoles{
    93  			Roles:                      "team_user team_admin",
    94  			SchemeUser:                 sql.NullBool{Valid: false, Bool: false},
    95  			SchemeAdmin:                sql.NullBool{Valid: false, Bool: false},
    96  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: false},
    97  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: false},
    98  		}
    99  
   100  		m := db.ToModel()
   101  
   102  		assert.Equal(t, "team_user team_admin", m.Roles)
   103  		assert.Equal(t, true, m.SchemeUser)
   104  		assert.Equal(t, true, m.SchemeAdmin)
   105  		assert.Equal(t, "", m.ExplicitRoles)
   106  	})
   107  
   108  	t.Run("Unmigrated_NoScheme_CustomRole", func(t *testing.T) {
   109  		db := teamMemberWithSchemeRoles{
   110  			Roles:                      "custom_role",
   111  			SchemeUser:                 sql.NullBool{Valid: false, Bool: false},
   112  			SchemeAdmin:                sql.NullBool{Valid: false, Bool: false},
   113  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: false},
   114  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: false},
   115  		}
   116  
   117  		m := db.ToModel()
   118  
   119  		assert.Equal(t, "custom_role", m.Roles)
   120  		assert.Equal(t, false, m.SchemeUser)
   121  		assert.Equal(t, false, m.SchemeAdmin)
   122  		assert.Equal(t, "custom_role", m.ExplicitRoles)
   123  	})
   124  
   125  	t.Run("Unmigrated_NoScheme_UserAndCustomRole", func(t *testing.T) {
   126  		db := teamMemberWithSchemeRoles{
   127  			Roles:                      "team_user custom_role",
   128  			SchemeUser:                 sql.NullBool{Valid: false, Bool: false},
   129  			SchemeAdmin:                sql.NullBool{Valid: false, Bool: false},
   130  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: false},
   131  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: false},
   132  		}
   133  
   134  		m := db.ToModel()
   135  
   136  		assert.Equal(t, "team_user custom_role", m.Roles)
   137  		assert.Equal(t, true, m.SchemeUser)
   138  		assert.Equal(t, false, m.SchemeAdmin)
   139  		assert.Equal(t, "custom_role", m.ExplicitRoles)
   140  	})
   141  
   142  	t.Run("Unmigrated_NoScheme_AdminAndCustomRole", func(t *testing.T) {
   143  		db := teamMemberWithSchemeRoles{
   144  			Roles:                      "team_user team_admin custom_role",
   145  			SchemeUser:                 sql.NullBool{Valid: false, Bool: false},
   146  			SchemeAdmin:                sql.NullBool{Valid: false, Bool: false},
   147  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: false},
   148  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: false},
   149  		}
   150  
   151  		m := db.ToModel()
   152  
   153  		assert.Equal(t, "team_user team_admin custom_role", m.Roles)
   154  		assert.Equal(t, true, m.SchemeUser)
   155  		assert.Equal(t, true, m.SchemeAdmin)
   156  		assert.Equal(t, "custom_role", m.ExplicitRoles)
   157  	})
   158  
   159  	t.Run("Unmigrated_NoScheme_NoRoles", func(t *testing.T) {
   160  		db := teamMemberWithSchemeRoles{
   161  			Roles:                      "",
   162  			SchemeUser:                 sql.NullBool{Valid: false, Bool: false},
   163  			SchemeAdmin:                sql.NullBool{Valid: false, Bool: false},
   164  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: false},
   165  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: false},
   166  		}
   167  
   168  		m := db.ToModel()
   169  
   170  		assert.Equal(t, "", m.Roles)
   171  		assert.Equal(t, false, m.SchemeUser)
   172  		assert.Equal(t, false, m.SchemeAdmin)
   173  		assert.Equal(t, "", m.ExplicitRoles)
   174  	})
   175  
   176  	// Example data *after* the Phase 2 migration has taken place.
   177  	t.Run("Migrated_NoScheme_User", func(t *testing.T) {
   178  		db := teamMemberWithSchemeRoles{
   179  			Roles:                      "",
   180  			SchemeUser:                 sql.NullBool{Valid: true, Bool: true},
   181  			SchemeAdmin:                sql.NullBool{Valid: true, Bool: false},
   182  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: false},
   183  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: false},
   184  		}
   185  
   186  		m := db.ToModel()
   187  
   188  		assert.Equal(t, "team_user", m.Roles)
   189  		assert.Equal(t, true, m.SchemeUser)
   190  		assert.Equal(t, false, m.SchemeAdmin)
   191  		assert.Equal(t, "", m.ExplicitRoles)
   192  	})
   193  
   194  	t.Run("Migrated_NoScheme_Admin", func(t *testing.T) {
   195  		db := teamMemberWithSchemeRoles{
   196  			Roles:                      "",
   197  			SchemeUser:                 sql.NullBool{Valid: true, Bool: true},
   198  			SchemeAdmin:                sql.NullBool{Valid: true, Bool: true},
   199  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: false},
   200  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: false},
   201  		}
   202  
   203  		m := db.ToModel()
   204  
   205  		assert.Equal(t, "team_user team_admin", m.Roles)
   206  		assert.Equal(t, true, m.SchemeUser)
   207  		assert.Equal(t, true, m.SchemeAdmin)
   208  		assert.Equal(t, "", m.ExplicitRoles)
   209  	})
   210  
   211  	t.Run("Migrated_NoScheme_CustomRole", func(t *testing.T) {
   212  		db := teamMemberWithSchemeRoles{
   213  			Roles:                      "custom_role",
   214  			SchemeUser:                 sql.NullBool{Valid: true, Bool: false},
   215  			SchemeAdmin:                sql.NullBool{Valid: true, Bool: false},
   216  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: false},
   217  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: false},
   218  		}
   219  
   220  		m := db.ToModel()
   221  
   222  		assert.Equal(t, "custom_role", m.Roles)
   223  		assert.Equal(t, false, m.SchemeUser)
   224  		assert.Equal(t, false, m.SchemeAdmin)
   225  		assert.Equal(t, "custom_role", m.ExplicitRoles)
   226  	})
   227  
   228  	t.Run("Migrated_NoScheme_UserAndCustomRole", func(t *testing.T) {
   229  		db := teamMemberWithSchemeRoles{
   230  			Roles:                      "custom_role",
   231  			SchemeUser:                 sql.NullBool{Valid: true, Bool: true},
   232  			SchemeAdmin:                sql.NullBool{Valid: true, Bool: false},
   233  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: false},
   234  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: false},
   235  		}
   236  
   237  		m := db.ToModel()
   238  
   239  		assert.Equal(t, "custom_role team_user", m.Roles)
   240  		assert.Equal(t, true, m.SchemeUser)
   241  		assert.Equal(t, false, m.SchemeAdmin)
   242  		assert.Equal(t, "custom_role", m.ExplicitRoles)
   243  	})
   244  
   245  	t.Run("Migrated_NoScheme_AdminAndCustomRole", func(t *testing.T) {
   246  		db := teamMemberWithSchemeRoles{
   247  			Roles:                      "custom_role",
   248  			SchemeUser:                 sql.NullBool{Valid: true, Bool: true},
   249  			SchemeAdmin:                sql.NullBool{Valid: true, Bool: true},
   250  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: false},
   251  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: false},
   252  		}
   253  
   254  		m := db.ToModel()
   255  
   256  		assert.Equal(t, "custom_role team_user team_admin", m.Roles)
   257  		assert.Equal(t, true, m.SchemeUser)
   258  		assert.Equal(t, true, m.SchemeAdmin)
   259  		assert.Equal(t, "custom_role", m.ExplicitRoles)
   260  	})
   261  
   262  	t.Run("Migrated_NoScheme_NoRoles", func(t *testing.T) {
   263  		db := teamMemberWithSchemeRoles{
   264  			Roles:                      "",
   265  			SchemeUser:                 sql.NullBool{Valid: true, Bool: false},
   266  			SchemeAdmin:                sql.NullBool{Valid: true, Bool: false},
   267  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: false},
   268  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: false},
   269  		}
   270  
   271  		m := db.ToModel()
   272  
   273  		assert.Equal(t, "", m.Roles)
   274  		assert.Equal(t, false, m.SchemeUser)
   275  		assert.Equal(t, false, m.SchemeAdmin)
   276  		assert.Equal(t, "", m.ExplicitRoles)
   277  	})
   278  
   279  	// Example data with a team scheme.
   280  	t.Run("Migrated_TeamScheme_User", func(t *testing.T) {
   281  		db := teamMemberWithSchemeRoles{
   282  			Roles:                      "",
   283  			SchemeUser:                 sql.NullBool{Valid: true, Bool: true},
   284  			SchemeAdmin:                sql.NullBool{Valid: true, Bool: false},
   285  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: true, String: "tscheme_user"},
   286  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_admin"},
   287  		}
   288  
   289  		m := db.ToModel()
   290  
   291  		assert.Equal(t, "tscheme_user", m.Roles)
   292  		assert.Equal(t, true, m.SchemeUser)
   293  		assert.Equal(t, false, m.SchemeAdmin)
   294  		assert.Equal(t, "", m.ExplicitRoles)
   295  	})
   296  
   297  	t.Run("Migrated_TeamScheme_Admin", func(t *testing.T) {
   298  		db := teamMemberWithSchemeRoles{
   299  			Roles:                      "",
   300  			SchemeUser:                 sql.NullBool{Valid: true, Bool: true},
   301  			SchemeAdmin:                sql.NullBool{Valid: true, Bool: true},
   302  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: true, String: "tscheme_user"},
   303  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_admin"},
   304  		}
   305  
   306  		m := db.ToModel()
   307  
   308  		assert.Equal(t, "tscheme_user tscheme_admin", m.Roles)
   309  		assert.Equal(t, true, m.SchemeUser)
   310  		assert.Equal(t, true, m.SchemeAdmin)
   311  		assert.Equal(t, "", m.ExplicitRoles)
   312  	})
   313  
   314  	t.Run("Migrated_TeamScheme_CustomRole", func(t *testing.T) {
   315  		db := teamMemberWithSchemeRoles{
   316  			Roles:                      "custom_role",
   317  			SchemeUser:                 sql.NullBool{Valid: true, Bool: false},
   318  			SchemeAdmin:                sql.NullBool{Valid: true, Bool: false},
   319  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: true, String: "tscheme_user"},
   320  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_admin"},
   321  		}
   322  
   323  		m := db.ToModel()
   324  
   325  		assert.Equal(t, "custom_role", m.Roles)
   326  		assert.Equal(t, false, m.SchemeUser)
   327  		assert.Equal(t, false, m.SchemeAdmin)
   328  		assert.Equal(t, "custom_role", m.ExplicitRoles)
   329  	})
   330  
   331  	t.Run("Migrated_TeamScheme_UserAndCustomRole", func(t *testing.T) {
   332  		db := teamMemberWithSchemeRoles{
   333  			Roles:                      "custom_role",
   334  			SchemeUser:                 sql.NullBool{Valid: true, Bool: true},
   335  			SchemeAdmin:                sql.NullBool{Valid: true, Bool: false},
   336  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: true, String: "tscheme_user"},
   337  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_admin"},
   338  		}
   339  
   340  		m := db.ToModel()
   341  
   342  		assert.Equal(t, "custom_role tscheme_user", m.Roles)
   343  		assert.Equal(t, true, m.SchemeUser)
   344  		assert.Equal(t, false, m.SchemeAdmin)
   345  		assert.Equal(t, "custom_role", m.ExplicitRoles)
   346  	})
   347  
   348  	t.Run("Migrated_TeamScheme_AdminAndCustomRole", func(t *testing.T) {
   349  		db := teamMemberWithSchemeRoles{
   350  			Roles:                      "custom_role",
   351  			SchemeUser:                 sql.NullBool{Valid: true, Bool: true},
   352  			SchemeAdmin:                sql.NullBool{Valid: true, Bool: true},
   353  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: true, String: "tscheme_user"},
   354  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_admin"},
   355  		}
   356  
   357  		m := db.ToModel()
   358  
   359  		assert.Equal(t, "custom_role tscheme_user tscheme_admin", m.Roles)
   360  		assert.Equal(t, true, m.SchemeUser)
   361  		assert.Equal(t, true, m.SchemeAdmin)
   362  		assert.Equal(t, "custom_role", m.ExplicitRoles)
   363  	})
   364  
   365  	t.Run("Migrated_TeamScheme_NoRoles", func(t *testing.T) {
   366  		db := teamMemberWithSchemeRoles{
   367  			Roles:                      "",
   368  			SchemeUser:                 sql.NullBool{Valid: true, Bool: false},
   369  			SchemeAdmin:                sql.NullBool{Valid: true, Bool: false},
   370  			TeamSchemeDefaultUserRole:  sql.NullString{Valid: true, String: "tscheme_user"},
   371  			TeamSchemeDefaultAdminRole: sql.NullString{Valid: true, String: "tscheme_admin"},
   372  		}
   373  
   374  		m := db.ToModel()
   375  
   376  		assert.Equal(t, "", m.Roles)
   377  		assert.Equal(t, false, m.SchemeUser)
   378  		assert.Equal(t, false, m.SchemeAdmin)
   379  		assert.Equal(t, "", m.ExplicitRoles)
   380  	})
   381  }