github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/store/storetest/role_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 TestRoleStore(t *testing.T, ss store.Store) {
    16  	t.Run("Save", func(t *testing.T) { testRoleStoreSave(t, ss) })
    17  	t.Run("Get", func(t *testing.T) { testRoleStoreGet(t, ss) })
    18  	t.Run("GetByName", func(t *testing.T) { testRoleStoreGetByName(t, ss) })
    19  	t.Run("GetNames", func(t *testing.T) { testRoleStoreGetByNames(t, ss) })
    20  	t.Run("Delete", func(t *testing.T) { testRoleStoreDelete(t, ss) })
    21  	t.Run("PermanentDeleteAll", func(t *testing.T) { testRoleStorePermanentDeleteAll(t, ss) })
    22  }
    23  
    24  func testRoleStoreSave(t *testing.T, ss store.Store) {
    25  	// Save a new role.
    26  	r1 := &model.Role{
    27  		Name:        model.NewId(),
    28  		DisplayName: model.NewId(),
    29  		Description: model.NewId(),
    30  		Permissions: []string{
    31  			"invite_user",
    32  			"create_public_channel",
    33  			"add_user_to_team",
    34  		},
    35  		SchemeManaged: false,
    36  	}
    37  
    38  	res1 := <-ss.Role().Save(r1)
    39  	assert.Nil(t, res1.Err)
    40  	d1 := res1.Data.(*model.Role)
    41  	assert.Len(t, d1.Id, 26)
    42  	assert.Equal(t, r1.Name, d1.Name)
    43  	assert.Equal(t, r1.DisplayName, d1.DisplayName)
    44  	assert.Equal(t, r1.Description, d1.Description)
    45  	assert.Equal(t, r1.Permissions, d1.Permissions)
    46  	assert.Equal(t, r1.SchemeManaged, d1.SchemeManaged)
    47  
    48  	// Change the role permissions and update.
    49  	d1.Permissions = []string{
    50  		"invite_user",
    51  		"add_user_to_team",
    52  		"delete_public_channel",
    53  	}
    54  
    55  	res2 := <-ss.Role().Save(d1)
    56  	assert.Nil(t, res2.Err)
    57  	d2 := res2.Data.(*model.Role)
    58  	assert.Len(t, d2.Id, 26)
    59  	assert.Equal(t, r1.Name, d2.Name)
    60  	assert.Equal(t, r1.DisplayName, d2.DisplayName)
    61  	assert.Equal(t, r1.Description, d2.Description)
    62  	assert.Equal(t, d1.Permissions, d2.Permissions)
    63  	assert.Equal(t, r1.SchemeManaged, d2.SchemeManaged)
    64  
    65  	// Try saving one with an invalid ID set.
    66  	r3 := &model.Role{
    67  		Id:          model.NewId(),
    68  		Name:        model.NewId(),
    69  		DisplayName: model.NewId(),
    70  		Description: model.NewId(),
    71  		Permissions: []string{
    72  			"invite_user",
    73  			"create_public_channel",
    74  			"add_user_to_team",
    75  		},
    76  		SchemeManaged: false,
    77  	}
    78  
    79  	res3 := <-ss.Role().Save(r3)
    80  	assert.NotNil(t, res3.Err)
    81  
    82  	// Try saving one with a duplicate "name" field.
    83  	r4 := &model.Role{
    84  		Name:        r1.Name,
    85  		DisplayName: model.NewId(),
    86  		Description: model.NewId(),
    87  		Permissions: []string{
    88  			"invite_user",
    89  			"create_public_channel",
    90  			"add_user_to_team",
    91  		},
    92  		SchemeManaged: false,
    93  	}
    94  
    95  	res4 := <-ss.Role().Save(r4)
    96  	assert.NotNil(t, res4.Err)
    97  }
    98  
    99  func testRoleStoreGet(t *testing.T, ss store.Store) {
   100  	// Save a role to test with.
   101  	r1 := &model.Role{
   102  		Name:        model.NewId(),
   103  		DisplayName: model.NewId(),
   104  		Description: model.NewId(),
   105  		Permissions: []string{
   106  			"invite_user",
   107  			"create_public_channel",
   108  			"add_user_to_team",
   109  		},
   110  		SchemeManaged: false,
   111  	}
   112  
   113  	res1 := <-ss.Role().Save(r1)
   114  	assert.Nil(t, res1.Err)
   115  	d1 := res1.Data.(*model.Role)
   116  	assert.Len(t, d1.Id, 26)
   117  
   118  	// Get a valid role
   119  	res2 := <-ss.Role().Get(d1.Id)
   120  	assert.Nil(t, res2.Err)
   121  	d2 := res1.Data.(*model.Role)
   122  	assert.Equal(t, d1.Id, d2.Id)
   123  	assert.Equal(t, r1.Name, d2.Name)
   124  	assert.Equal(t, r1.DisplayName, d2.DisplayName)
   125  	assert.Equal(t, r1.Description, d2.Description)
   126  	assert.Equal(t, r1.Permissions, d2.Permissions)
   127  	assert.Equal(t, r1.SchemeManaged, d2.SchemeManaged)
   128  
   129  	// Get an invalid role
   130  	res3 := <-ss.Role().Get(model.NewId())
   131  	assert.NotNil(t, res3.Err)
   132  }
   133  
   134  func testRoleStoreGetByName(t *testing.T, ss store.Store) {
   135  	// Save a role to test with.
   136  	r1 := &model.Role{
   137  		Name:        model.NewId(),
   138  		DisplayName: model.NewId(),
   139  		Description: model.NewId(),
   140  		Permissions: []string{
   141  			"invite_user",
   142  			"create_public_channel",
   143  			"add_user_to_team",
   144  		},
   145  		SchemeManaged: false,
   146  	}
   147  
   148  	res1 := <-ss.Role().Save(r1)
   149  	assert.Nil(t, res1.Err)
   150  	d1 := res1.Data.(*model.Role)
   151  	assert.Len(t, d1.Id, 26)
   152  
   153  	// Get a valid role
   154  	res2 := <-ss.Role().GetByName(d1.Name)
   155  	assert.Nil(t, res2.Err)
   156  	d2 := res1.Data.(*model.Role)
   157  	assert.Equal(t, d1.Id, d2.Id)
   158  	assert.Equal(t, r1.Name, d2.Name)
   159  	assert.Equal(t, r1.DisplayName, d2.DisplayName)
   160  	assert.Equal(t, r1.Description, d2.Description)
   161  	assert.Equal(t, r1.Permissions, d2.Permissions)
   162  	assert.Equal(t, r1.SchemeManaged, d2.SchemeManaged)
   163  
   164  	// Get an invalid role
   165  	res3 := <-ss.Role().GetByName(model.NewId())
   166  	assert.NotNil(t, res3.Err)
   167  }
   168  
   169  func testRoleStoreGetByNames(t *testing.T, ss store.Store) {
   170  	// Save some roles to test with.
   171  	r1 := &model.Role{
   172  		Name:        model.NewId(),
   173  		DisplayName: model.NewId(),
   174  		Description: model.NewId(),
   175  		Permissions: []string{
   176  			"invite_user",
   177  			"create_public_channel",
   178  			"add_user_to_team",
   179  		},
   180  		SchemeManaged: false,
   181  	}
   182  	r2 := &model.Role{
   183  		Name:        model.NewId(),
   184  		DisplayName: model.NewId(),
   185  		Description: model.NewId(),
   186  		Permissions: []string{
   187  			"read_channel",
   188  			"create_public_channel",
   189  			"add_user_to_team",
   190  		},
   191  		SchemeManaged: false,
   192  	}
   193  	r3 := &model.Role{
   194  		Name:        model.NewId(),
   195  		DisplayName: model.NewId(),
   196  		Description: model.NewId(),
   197  		Permissions: []string{
   198  			"invite_user",
   199  			"delete_private_channel",
   200  			"add_user_to_team",
   201  		},
   202  		SchemeManaged: false,
   203  	}
   204  
   205  	res1 := <-ss.Role().Save(r1)
   206  	assert.Nil(t, res1.Err)
   207  	d1 := res1.Data.(*model.Role)
   208  	assert.Len(t, d1.Id, 26)
   209  
   210  	res2 := <-ss.Role().Save(r2)
   211  	assert.Nil(t, res2.Err)
   212  	d2 := res2.Data.(*model.Role)
   213  	assert.Len(t, d2.Id, 26)
   214  
   215  	res3 := <-ss.Role().Save(r3)
   216  	assert.Nil(t, res3.Err)
   217  	d3 := res3.Data.(*model.Role)
   218  	assert.Len(t, d3.Id, 26)
   219  
   220  	// Get two valid roles.
   221  	n4 := []string{r1.Name, r2.Name}
   222  	res4 := <-ss.Role().GetByNames(n4)
   223  	assert.Nil(t, res4.Err)
   224  	roles4 := res4.Data.([]*model.Role)
   225  	assert.Len(t, roles4, 2)
   226  	assert.Contains(t, roles4, d1)
   227  	assert.Contains(t, roles4, d2)
   228  	assert.NotContains(t, roles4, d3)
   229  
   230  	// Get two invalid roles.
   231  	n5 := []string{model.NewId(), model.NewId()}
   232  	res5 := <-ss.Role().GetByNames(n5)
   233  	assert.Nil(t, res5.Err)
   234  	roles5 := res5.Data.([]*model.Role)
   235  	assert.Len(t, roles5, 0)
   236  
   237  	// Get one valid one and one invalid one.
   238  	n6 := []string{r1.Name, model.NewId()}
   239  	res6 := <-ss.Role().GetByNames(n6)
   240  	assert.Nil(t, res6.Err)
   241  	roles6 := res6.Data.([]*model.Role)
   242  	assert.Len(t, roles6, 1)
   243  	assert.Contains(t, roles6, d1)
   244  	assert.NotContains(t, roles6, d2)
   245  	assert.NotContains(t, roles6, d3)
   246  }
   247  
   248  func testRoleStoreDelete(t *testing.T, ss store.Store) {
   249  	// Save a role to test with.
   250  	r1 := &model.Role{
   251  		Name:        model.NewId(),
   252  		DisplayName: model.NewId(),
   253  		Description: model.NewId(),
   254  		Permissions: []string{
   255  			"invite_user",
   256  			"create_public_channel",
   257  			"add_user_to_team",
   258  		},
   259  		SchemeManaged: false,
   260  	}
   261  
   262  	res1 := <-ss.Role().Save(r1)
   263  	assert.Nil(t, res1.Err)
   264  	d1 := res1.Data.(*model.Role)
   265  	assert.Len(t, d1.Id, 26)
   266  
   267  	// Check the role is there.
   268  	res2 := <-ss.Role().Get(d1.Id)
   269  	assert.Nil(t, res2.Err)
   270  
   271  	// Delete the role.
   272  	res3 := <-ss.Role().Delete(d1.Id)
   273  	assert.Nil(t, res3.Err)
   274  
   275  	// Check the role is deleted there.
   276  	res4 := <-ss.Role().Get(d1.Id)
   277  	assert.Nil(t, res4.Err)
   278  	d2 := res4.Data.(*model.Role)
   279  	assert.NotZero(t, d2.DeleteAt)
   280  
   281  	res5 := <-ss.Role().GetByName(d1.Name)
   282  	assert.Nil(t, res5.Err)
   283  	d3 := res5.Data.(*model.Role)
   284  	assert.NotZero(t, d3.DeleteAt)
   285  
   286  	// Try and delete a role that does not exist.
   287  	res6 := <-ss.Role().Delete(model.NewId())
   288  	assert.NotNil(t, res6.Err)
   289  }
   290  
   291  func testRoleStorePermanentDeleteAll(t *testing.T, ss store.Store) {
   292  	r1 := &model.Role{
   293  		Name:        model.NewId(),
   294  		DisplayName: model.NewId(),
   295  		Description: model.NewId(),
   296  		Permissions: []string{
   297  			"invite_user",
   298  			"create_public_channel",
   299  			"add_user_to_team",
   300  		},
   301  		SchemeManaged: false,
   302  	}
   303  
   304  	r2 := &model.Role{
   305  		Name:        model.NewId(),
   306  		DisplayName: model.NewId(),
   307  		Description: model.NewId(),
   308  		Permissions: []string{
   309  			"read_channel",
   310  			"create_public_channel",
   311  			"add_user_to_team",
   312  		},
   313  		SchemeManaged: false,
   314  	}
   315  
   316  	store.Must(ss.Role().Save(r1))
   317  	store.Must(ss.Role().Save(r2))
   318  
   319  	res1 := <-ss.Role().GetByNames([]string{r1.Name, r2.Name})
   320  	assert.Nil(t, res1.Err)
   321  	assert.Len(t, res1.Data.([]*model.Role), 2)
   322  
   323  	res2 := <-ss.Role().PermanentDeleteAll()
   324  	assert.Nil(t, res2.Err)
   325  
   326  	res3 := <-ss.Role().GetByNames([]string{r1.Name, r2.Name})
   327  	assert.Nil(t, res3.Err)
   328  	assert.Len(t, res3.Data.([]*model.Role), 0)
   329  }