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