github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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  }
    24  
    25  func createDefaultRoles(t *testing.T, ss store.Store) {
    26  	<-ss.Role().Save(&model.Role{
    27  		Name:        model.TEAM_ADMIN_ROLE_ID,
    28  		DisplayName: model.TEAM_ADMIN_ROLE_ID,
    29  		Permissions: []string{
    30  			model.PERMISSION_EDIT_OTHERS_POSTS.Id,
    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{"edit_others_posts", "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 testSchemeStoreGetAllPage(t *testing.T, ss store.Store) {
   183  	// Save a scheme to test with.
   184  	schemes := []*model.Scheme{
   185  		{
   186  			DisplayName: model.NewId(),
   187  			Name:        model.NewId(),
   188  			Description: model.NewId(),
   189  			Scope:       model.SCHEME_SCOPE_TEAM,
   190  		},
   191  		{
   192  			DisplayName: model.NewId(),
   193  			Name:        model.NewId(),
   194  			Description: model.NewId(),
   195  			Scope:       model.SCHEME_SCOPE_CHANNEL,
   196  		},
   197  		{
   198  			DisplayName: model.NewId(),
   199  			Name:        model.NewId(),
   200  			Description: model.NewId(),
   201  			Scope:       model.SCHEME_SCOPE_TEAM,
   202  		},
   203  		{
   204  			DisplayName: model.NewId(),
   205  			Name:        model.NewId(),
   206  			Description: model.NewId(),
   207  			Scope:       model.SCHEME_SCOPE_CHANNEL,
   208  		},
   209  	}
   210  
   211  	for _, scheme := range schemes {
   212  		store.Must(ss.Scheme().Save(scheme))
   213  	}
   214  
   215  	r1 := <-ss.Scheme().GetAllPage("", 0, 2)
   216  	assert.Nil(t, r1.Err)
   217  	s1 := r1.Data.([]*model.Scheme)
   218  	assert.Len(t, s1, 2)
   219  
   220  	r2 := <-ss.Scheme().GetAllPage("", 2, 2)
   221  	assert.Nil(t, r2.Err)
   222  	s2 := r2.Data.([]*model.Scheme)
   223  	assert.Len(t, s2, 2)
   224  	assert.NotEqual(t, s1[0].DisplayName, s2[0].DisplayName)
   225  	assert.NotEqual(t, s1[0].DisplayName, s2[1].DisplayName)
   226  	assert.NotEqual(t, s1[1].DisplayName, s2[0].DisplayName)
   227  	assert.NotEqual(t, s1[1].DisplayName, s2[1].DisplayName)
   228  	assert.NotEqual(t, s1[0].Name, s2[0].Name)
   229  	assert.NotEqual(t, s1[0].Name, s2[1].Name)
   230  	assert.NotEqual(t, s1[1].Name, s2[0].Name)
   231  	assert.NotEqual(t, s1[1].Name, s2[1].Name)
   232  
   233  	r3 := <-ss.Scheme().GetAllPage("team", 0, 1000)
   234  	assert.Nil(t, r3.Err)
   235  	s3 := r3.Data.([]*model.Scheme)
   236  	assert.NotZero(t, len(s3))
   237  	for _, s := range s3 {
   238  		assert.Equal(t, "team", s.Scope)
   239  	}
   240  
   241  	r4 := <-ss.Scheme().GetAllPage("channel", 0, 1000)
   242  	assert.Nil(t, r4.Err)
   243  	s4 := r4.Data.([]*model.Scheme)
   244  	assert.NotZero(t, len(s4))
   245  	for _, s := range s4 {
   246  		assert.Equal(t, "channel", s.Scope)
   247  	}
   248  }
   249  
   250  func testSchemeStoreDelete(t *testing.T, ss store.Store) {
   251  	// Save a new scheme.
   252  	s1 := &model.Scheme{
   253  		DisplayName: model.NewId(),
   254  		Name:        model.NewId(),
   255  		Description: model.NewId(),
   256  		Scope:       model.SCHEME_SCOPE_TEAM,
   257  	}
   258  
   259  	// Check all fields saved correctly.
   260  	res1 := <-ss.Scheme().Save(s1)
   261  	assert.Nil(t, res1.Err)
   262  	d1 := res1.Data.(*model.Scheme)
   263  	assert.Len(t, d1.Id, 26)
   264  	assert.Equal(t, s1.DisplayName, d1.DisplayName)
   265  	assert.Equal(t, s1.Name, d1.Name)
   266  	assert.Equal(t, s1.Description, d1.Description)
   267  	assert.NotZero(t, d1.CreateAt)
   268  	assert.NotZero(t, d1.UpdateAt)
   269  	assert.Zero(t, d1.DeleteAt)
   270  	assert.Equal(t, s1.Scope, d1.Scope)
   271  	assert.Len(t, d1.DefaultTeamAdminRole, 26)
   272  	assert.Len(t, d1.DefaultTeamUserRole, 26)
   273  	assert.Len(t, d1.DefaultChannelAdminRole, 26)
   274  	assert.Len(t, d1.DefaultChannelUserRole, 26)
   275  
   276  	// Check the default roles were created correctly.
   277  	roleRes1 := <-ss.Role().GetByName(d1.DefaultTeamAdminRole)
   278  	assert.Nil(t, roleRes1.Err)
   279  	role1 := roleRes1.Data.(*model.Role)
   280  	assert.Equal(t, role1.Permissions, []string{"edit_others_posts", "delete_others_posts"})
   281  	assert.True(t, role1.SchemeManaged)
   282  
   283  	roleRes2 := <-ss.Role().GetByName(d1.DefaultTeamUserRole)
   284  	assert.Nil(t, roleRes2.Err)
   285  	role2 := roleRes2.Data.(*model.Role)
   286  	assert.Equal(t, role2.Permissions, []string{"view_team", "add_user_to_team"})
   287  	assert.True(t, role2.SchemeManaged)
   288  
   289  	roleRes3 := <-ss.Role().GetByName(d1.DefaultChannelAdminRole)
   290  	assert.Nil(t, roleRes3.Err)
   291  	role3 := roleRes3.Data.(*model.Role)
   292  	assert.Equal(t, role3.Permissions, []string{"manage_public_channel_members", "manage_private_channel_members"})
   293  	assert.True(t, role3.SchemeManaged)
   294  
   295  	roleRes4 := <-ss.Role().GetByName(d1.DefaultChannelUserRole)
   296  	assert.Nil(t, roleRes4.Err)
   297  	role4 := roleRes4.Data.(*model.Role)
   298  	assert.Equal(t, role4.Permissions, []string{"read_channel", "create_post"})
   299  	assert.True(t, role4.SchemeManaged)
   300  
   301  	// Delete the scheme.
   302  	res2 := <-ss.Scheme().Delete(d1.Id)
   303  	if !assert.Nil(t, res2.Err) {
   304  		t.Fatal(res2.Err)
   305  	}
   306  	d2 := res2.Data.(*model.Scheme)
   307  	assert.NotZero(t, d2.DeleteAt)
   308  
   309  	// Check that the roles are deleted too.
   310  	roleRes5 := <-ss.Role().GetByName(d1.DefaultTeamAdminRole)
   311  	assert.Nil(t, roleRes5.Err)
   312  	role5 := roleRes5.Data.(*model.Role)
   313  	assert.NotZero(t, role5.DeleteAt)
   314  
   315  	roleRes6 := <-ss.Role().GetByName(d1.DefaultTeamUserRole)
   316  	assert.Nil(t, roleRes6.Err)
   317  	role6 := roleRes6.Data.(*model.Role)
   318  	assert.NotZero(t, role6.DeleteAt)
   319  
   320  	roleRes7 := <-ss.Role().GetByName(d1.DefaultChannelAdminRole)
   321  	assert.Nil(t, roleRes7.Err)
   322  	role7 := roleRes7.Data.(*model.Role)
   323  	assert.NotZero(t, role7.DeleteAt)
   324  
   325  	roleRes8 := <-ss.Role().GetByName(d1.DefaultChannelUserRole)
   326  	assert.Nil(t, roleRes8.Err)
   327  	role8 := roleRes8.Data.(*model.Role)
   328  	assert.NotZero(t, role8.DeleteAt)
   329  
   330  	// Try deleting a scheme that does not exist.
   331  	res3 := <-ss.Scheme().Delete(model.NewId())
   332  	assert.NotNil(t, res3.Err)
   333  
   334  	// Try deleting a team scheme that's in use.
   335  	s4 := &model.Scheme{
   336  		DisplayName: model.NewId(),
   337  		Name:        model.NewId(),
   338  		Description: model.NewId(),
   339  		Scope:       model.SCHEME_SCOPE_TEAM,
   340  	}
   341  	res4 := <-ss.Scheme().Save(s4)
   342  	assert.Nil(t, res4.Err)
   343  	d4 := res4.Data.(*model.Scheme)
   344  
   345  	t4 := &model.Team{
   346  		Name:        model.NewId(),
   347  		DisplayName: model.NewId(),
   348  		Email:       model.NewId() + "@nowhere.com",
   349  		Type:        model.TEAM_OPEN,
   350  		SchemeId:    &d4.Id,
   351  	}
   352  	tres4 := <-ss.Team().Save(t4)
   353  	assert.Nil(t, tres4.Err)
   354  	t4 = tres4.Data.(*model.Team)
   355  
   356  	sres4 := <-ss.Scheme().Delete(d4.Id)
   357  	assert.Nil(t, sres4.Err)
   358  
   359  	tres5 := <-ss.Team().Get(t4.Id)
   360  	assert.Nil(t, tres5.Err)
   361  	t5 := tres5.Data.(*model.Team)
   362  	assert.Equal(t, "", *t5.SchemeId)
   363  
   364  	// Try deleting a channel scheme that's in use.
   365  	s5 := &model.Scheme{
   366  		DisplayName: model.NewId(),
   367  		Name:        model.NewId(),
   368  		Description: model.NewId(),
   369  		Scope:       model.SCHEME_SCOPE_CHANNEL,
   370  	}
   371  	res5 := <-ss.Scheme().Save(s5)
   372  	assert.Nil(t, res5.Err)
   373  	d5 := res5.Data.(*model.Scheme)
   374  
   375  	c5 := &model.Channel{
   376  		TeamId:      model.NewId(),
   377  		DisplayName: model.NewId(),
   378  		Name:        model.NewId(),
   379  		Type:        model.CHANNEL_OPEN,
   380  		SchemeId:    &d5.Id,
   381  	}
   382  	cres5 := <-ss.Channel().Save(c5, -1)
   383  	assert.Nil(t, cres5.Err)
   384  	c5 = cres5.Data.(*model.Channel)
   385  
   386  	sres5 := <-ss.Scheme().Delete(d5.Id)
   387  	assert.Nil(t, sres5.Err)
   388  
   389  	cres6 := <-ss.Channel().Get(c5.Id, true)
   390  	assert.Nil(t, cres6.Err)
   391  	c6 := cres6.Data.(*model.Channel)
   392  	assert.Equal(t, "", *c6.SchemeId)
   393  }
   394  
   395  func testSchemeStorePermanentDeleteAll(t *testing.T, ss store.Store) {
   396  	s1 := &model.Scheme{
   397  		Name:        model.NewId(),
   398  		DisplayName: model.NewId(),
   399  		Description: model.NewId(),
   400  		Scope:       model.SCHEME_SCOPE_TEAM,
   401  	}
   402  
   403  	s2 := &model.Scheme{
   404  		Name:        model.NewId(),
   405  		DisplayName: model.NewId(),
   406  		Description: model.NewId(),
   407  		Scope:       model.SCHEME_SCOPE_CHANNEL,
   408  	}
   409  
   410  	s1 = (<-ss.Scheme().Save(s1)).Data.(*model.Scheme)
   411  	s2 = (<-ss.Scheme().Save(s2)).Data.(*model.Scheme)
   412  
   413  	res := <-ss.Scheme().PermanentDeleteAll()
   414  	assert.Nil(t, res.Err)
   415  
   416  	res1 := <-ss.Scheme().Get(s1.Id)
   417  	assert.NotNil(t, res1.Err)
   418  
   419  	res2 := <-ss.Scheme().Get(s2.Id)
   420  	assert.NotNil(t, res2.Err)
   421  
   422  	res3 := <-ss.Scheme().GetAllPage("", 0, 100000)
   423  	assert.Nil(t, res3.Err)
   424  	assert.Len(t, res3.Data.([]*model.Scheme), 0)
   425  }