UpdateRolesPanel.utils.test.ts438 lines · main
| 1 | import { describe, expect, test } from 'vitest' |
| 2 | |
| 3 | import { deriveRoleChangeActions } from './UpdateRolesPanel.utils' |
| 4 | |
| 5 | // [Joshen] This is specifically testing the project scope changes |
| 6 | describe('UpdateRolesPanel.utils.ts:deriveRoleChangeActions', () => { |
| 7 | test('Should update from org scope to project scope correctly', () => { |
| 8 | const existingRoles = [ |
| 9 | { |
| 10 | id: 3, |
| 11 | name: 'Developer', |
| 12 | description: null, |
| 13 | projects: [], |
| 14 | base_role_id: 6, |
| 15 | }, |
| 16 | ] |
| 17 | const changesToRoles = { |
| 18 | removed: [], |
| 19 | added: [ |
| 20 | { ref: 'ref_1', roleId: 3 }, |
| 21 | { ref: 'ref_2', roleId: 3 }, |
| 22 | { ref: 'ref_3', roleId: 3 }, |
| 23 | ], |
| 24 | updated: [], |
| 25 | } |
| 26 | |
| 27 | const { toAssign, toRemove, toUpdate } = deriveRoleChangeActions(existingRoles, changesToRoles) |
| 28 | expect(toRemove).toStrictEqual([3]) |
| 29 | expect(toAssign).toStrictEqual([{ roleId: 3, refs: ['ref_1', 'ref_2', 'ref_3'] }]) |
| 30 | expect(toUpdate).toStrictEqual([]) |
| 31 | }) |
| 32 | test('Should update role for a single project (Project 1, Developer to Admin)', () => { |
| 33 | const existingRoles = [ |
| 34 | { |
| 35 | id: 29, |
| 36 | name: 'Developer_qggwcbikivagivlidfhw', |
| 37 | description: '', |
| 38 | projects: [{ ref: 'ref_1', name: 'Project 1' }], |
| 39 | base_role_id: 3, |
| 40 | }, |
| 41 | ] |
| 42 | const changesToRoles = { |
| 43 | removed: [], |
| 44 | added: [], |
| 45 | updated: [ |
| 46 | { |
| 47 | ref: 'ref_1', |
| 48 | originalRole: 29, |
| 49 | originalBaseRole: 3, |
| 50 | updatedRole: 1, |
| 51 | }, |
| 52 | ], |
| 53 | } |
| 54 | |
| 55 | const { toAssign, toRemove, toUpdate } = deriveRoleChangeActions(existingRoles, changesToRoles) |
| 56 | expect(toRemove).toStrictEqual([29]) |
| 57 | expect(toAssign).toStrictEqual([{ roleId: 1, refs: ['ref_1'] }]) |
| 58 | expect(toUpdate).toStrictEqual([]) |
| 59 | }) |
| 60 | test('Should remove role if role changes completely involve either removing role or updating role to another', () => { |
| 61 | // Developer of projects 1, 2, 3 |
| 62 | // Remove developer role for projects 1, 2 |
| 63 | // Update developer to admin for project 3 |
| 64 | const existingRoles = [ |
| 65 | { |
| 66 | id: 29, |
| 67 | name: 'Developer_qggwcbikivagivlidfhw', |
| 68 | description: '', |
| 69 | base_role_id: 3, |
| 70 | projects: [ |
| 71 | { ref: 'ref_1', name: 'Project 1' }, |
| 72 | { ref: 'ref_2', name: 'Project 2' }, |
| 73 | { ref: 'ref_3', name: 'Project 3' }, |
| 74 | ], |
| 75 | }, |
| 76 | ] |
| 77 | const changesToRoles = { |
| 78 | removed: [ |
| 79 | { |
| 80 | ref: 'ref_1', |
| 81 | roleId: 29, |
| 82 | baseRoleId: 3, |
| 83 | }, |
| 84 | { |
| 85 | ref: 'ref_2', |
| 86 | roleId: 29, |
| 87 | baseRoleId: 3, |
| 88 | }, |
| 89 | ], |
| 90 | added: [], |
| 91 | updated: [ |
| 92 | { |
| 93 | ref: 'ref_3', |
| 94 | originalRole: 29, |
| 95 | originalBaseRole: 3, |
| 96 | updatedRole: 1, |
| 97 | }, |
| 98 | ], |
| 99 | } |
| 100 | |
| 101 | const { toAssign, toRemove, toUpdate } = deriveRoleChangeActions(existingRoles, changesToRoles) |
| 102 | expect(toRemove).toStrictEqual([29]) |
| 103 | expect(toAssign).toStrictEqual([{ roleId: 1, refs: ['ref_3'] }]) |
| 104 | expect(toUpdate).toStrictEqual([]) |
| 105 | }) |
| 106 | test('Should not remove role if role changes partially involve either removing role or updating role to another', () => { |
| 107 | const existingRoles = [ |
| 108 | { |
| 109 | id: 29, |
| 110 | name: 'Developer_qggwcbikivagivlidfhw', |
| 111 | description: '', |
| 112 | base_role_id: 3, |
| 113 | projects: [ |
| 114 | { ref: 'ref_1', name: 'Project 1' }, |
| 115 | { ref: 'ref_2', name: 'Project 2' }, |
| 116 | { ref: 'ref_3', name: 'Project 3' }, |
| 117 | ], |
| 118 | }, |
| 119 | ] |
| 120 | const changesToRoles = { |
| 121 | removed: [ |
| 122 | { |
| 123 | ref: 'ref_1', |
| 124 | roleId: 29, |
| 125 | baseRoleId: 3, |
| 126 | }, |
| 127 | ], |
| 128 | added: [], |
| 129 | updated: [ |
| 130 | { |
| 131 | ref: 'ref_3', |
| 132 | originalRole: 29, |
| 133 | originalBaseRole: 3, |
| 134 | updatedRole: 1, |
| 135 | }, |
| 136 | ], |
| 137 | } |
| 138 | |
| 139 | const { toRemove, toUpdate } = deriveRoleChangeActions(existingRoles, changesToRoles) |
| 140 | expect(toRemove).toStrictEqual([]) |
| 141 | expect(toUpdate).toStrictEqual([{ roleId: 29, refs: ['ref_2'] }]) |
| 142 | }) |
| 143 | test('Should update role if newly added role(s) has the same base_role_id, and if removed role has same base_role_id', () => { |
| 144 | const existingRoles = [ |
| 145 | { |
| 146 | id: 29, |
| 147 | name: 'Developer_qggwcbikivagivlidfhw', |
| 148 | description: '', |
| 149 | base_role_id: 3, |
| 150 | projects: [ |
| 151 | { ref: 'ref_1', name: 'Project 1' }, |
| 152 | { ref: 'ref_2', name: 'Project 2' }, |
| 153 | { ref: 'ref_3', name: 'Project 3' }, |
| 154 | ], |
| 155 | }, |
| 156 | ] |
| 157 | const changesToRoles = { |
| 158 | removed: [ |
| 159 | { |
| 160 | ref: 'ref_1', |
| 161 | roleId: 29, |
| 162 | baseRoleId: 3, |
| 163 | }, |
| 164 | ], |
| 165 | added: [ |
| 166 | { ref: 'ref_4', roleId: 3 }, |
| 167 | { ref: 'ref_5', roleId: 3 }, |
| 168 | ], |
| 169 | updated: [], |
| 170 | } |
| 171 | |
| 172 | const { toAssign, toRemove, toUpdate } = deriveRoleChangeActions(existingRoles, changesToRoles) |
| 173 | expect(toUpdate).toStrictEqual([{ roleId: 29, refs: ['ref_2', 'ref_3', 'ref_4', 'ref_5'] }]) |
| 174 | expect(toAssign).toStrictEqual([]) |
| 175 | expect(toRemove).toStrictEqual([]) |
| 176 | }) |
| 177 | test('Should assign new role if newly added role(s) has no base_role_id as any existing role', () => { |
| 178 | const existingRoles = [ |
| 179 | { |
| 180 | id: 29, |
| 181 | name: 'Developer_qggwcbikivagivlidfhw', |
| 182 | description: '', |
| 183 | base_role_id: 3, |
| 184 | projects: [ |
| 185 | { ref: 'ref_1', name: 'Project 1' }, |
| 186 | { ref: 'ref_2', name: 'Project 2' }, |
| 187 | { ref: 'ref_3', name: 'Project 3' }, |
| 188 | ], |
| 189 | }, |
| 190 | ] |
| 191 | const changesToRoles = { |
| 192 | removed: [], |
| 193 | added: [ |
| 194 | { ref: 'ref_4', roleId: 1 }, |
| 195 | { ref: 'ref_5', roleId: 1 }, |
| 196 | ], |
| 197 | updated: [], |
| 198 | } |
| 199 | |
| 200 | const { toAssign, toRemove, toUpdate } = deriveRoleChangeActions(existingRoles, changesToRoles) |
| 201 | expect(toAssign).toStrictEqual([{ roleId: 1, refs: ['ref_4', 'ref_5'] }]) |
| 202 | expect(toUpdate).toStrictEqual([]) |
| 203 | expect(toRemove).toStrictEqual([]) |
| 204 | }) |
| 205 | test('Should assign project read only role from org developer role correctly', () => { |
| 206 | const existingRoles = [ |
| 207 | { |
| 208 | id: 3, |
| 209 | name: 'Developer', |
| 210 | description: null, |
| 211 | base_role_id: 6, |
| 212 | projects: [], |
| 213 | }, |
| 214 | ] |
| 215 | const changesToRoles = { |
| 216 | removed: [{ ref: undefined, roleId: 3 }], |
| 217 | added: [{ ref: 'ref_1', roleId: 6 }], |
| 218 | updated: [], |
| 219 | } |
| 220 | |
| 221 | const { toAssign, toRemove, toUpdate } = deriveRoleChangeActions(existingRoles, changesToRoles) |
| 222 | expect(toAssign).toStrictEqual([{ roleId: 6, refs: ['ref_1'] }]) |
| 223 | expect(toUpdate).toStrictEqual([]) |
| 224 | expect(toRemove).toStrictEqual([3]) |
| 225 | }) |
| 226 | test('Myriad of updates to stress test function logic (I)', () => { |
| 227 | // Given 6 projects 1, 2, 3, 4, 5, 6 |
| 228 | // Existing has developer role for 1, 2, 3, admin role for 4, owner role for 5 |
| 229 | const existingRoles = [ |
| 230 | { |
| 231 | id: 30, |
| 232 | name: 'Developer_qggwcbikivagivlidfhw', |
| 233 | description: '', |
| 234 | base_role_id: 3, // Developer |
| 235 | projects: [ |
| 236 | { ref: 'ref_1', name: 'Project 1' }, |
| 237 | { ref: 'ref_2', name: 'Project 2' }, |
| 238 | { ref: 'ref_3', name: 'Project 3' }, |
| 239 | ], |
| 240 | }, |
| 241 | { |
| 242 | id: 31, |
| 243 | name: 'Administrator_qggwcbikivagivlidfhw', |
| 244 | description: '', |
| 245 | base_role_id: 1, // Admin, |
| 246 | projects: [{ ref: 'ref_4', name: 'Project 4' }], |
| 247 | }, |
| 248 | { |
| 249 | id: 32, |
| 250 | name: 'Owner_qggwcbikivagivlidfhw', |
| 251 | description: '', |
| 252 | base_role_id: 5, // Owner, |
| 253 | projects: [{ ref: 'ref_5', name: 'Project 5' }], |
| 254 | }, |
| 255 | ] |
| 256 | |
| 257 | // Removing developer for 1 |
| 258 | // Updating developer -> admin for 2 |
| 259 | // Updating developer -> owner for 3 |
| 260 | // Updating administrator -> owner for 4 |
| 261 | // Adding administrator for 6 |
| 262 | const changesToRoles = { |
| 263 | removed: [ |
| 264 | { |
| 265 | ref: 'ref_1', |
| 266 | roleId: 30, |
| 267 | baseRoleId: 3, |
| 268 | }, |
| 269 | ], |
| 270 | added: [{ ref: 'ref_6', roleId: 1 }], |
| 271 | updated: [ |
| 272 | { |
| 273 | ref: 'ref_2', |
| 274 | originalRole: 30, |
| 275 | originalBaseRole: 3, |
| 276 | updatedRole: 1, |
| 277 | }, |
| 278 | { |
| 279 | ref: 'ref_3', |
| 280 | originalRole: 30, |
| 281 | originalBaseRole: 3, |
| 282 | updatedRole: 5, |
| 283 | }, |
| 284 | { |
| 285 | ref: 'ref_4', |
| 286 | originalRole: 31, |
| 287 | originalBaseRole: 1, |
| 288 | updatedRole: 5, |
| 289 | }, |
| 290 | ], |
| 291 | } |
| 292 | |
| 293 | const { toAssign, toRemove, toUpdate } = deriveRoleChangeActions(existingRoles, changesToRoles) |
| 294 | expect(toAssign).toStrictEqual([]) |
| 295 | expect(toUpdate).toStrictEqual([ |
| 296 | { roleId: 31, refs: ['ref_2', 'ref_6'] }, |
| 297 | { roleId: 32, refs: ['ref_3', 'ref_4', 'ref_5'] }, |
| 298 | ]) |
| 299 | expect(toRemove).toStrictEqual([30]) |
| 300 | }) |
| 301 | test('Myriad of updates to stress test function logic (II)', () => { |
| 302 | // Given 5 projects 1, 2, 3, 4, 5 |
| 303 | // Existing has developer role for 1, admin role for 2 |
| 304 | const existingRoles = [ |
| 305 | { |
| 306 | id: 30, |
| 307 | name: 'Developer_qggwcbikivagivlidfhw', |
| 308 | description: '', |
| 309 | base_role_id: 3, // Developer |
| 310 | projects: [{ ref: 'ref_1', name: 'Project 1' }], |
| 311 | }, |
| 312 | { |
| 313 | id: 31, |
| 314 | name: 'Administrator_qggwcbikivagivlidfhw', |
| 315 | description: '', |
| 316 | base_role_id: 1, // Admin, |
| 317 | projects: [{ ref: 'ref_2', name: 'Project 2' }], |
| 318 | }, |
| 319 | ] |
| 320 | |
| 321 | // Adding developer for 4 |
| 322 | // Updating developer -> admin for 1 |
| 323 | // Updating admin -> developer for 2 |
| 324 | // Adding owner for 5 |
| 325 | const changesToRoles = { |
| 326 | removed: [], |
| 327 | added: [ |
| 328 | { ref: 'ref_4', roleId: 3 }, |
| 329 | { ref: 'ref_5', roleId: 5 }, |
| 330 | ], |
| 331 | updated: [ |
| 332 | { |
| 333 | ref: 'ref_1', |
| 334 | originalRole: 30, |
| 335 | originalBaseRole: 3, |
| 336 | updatedRole: 1, |
| 337 | }, |
| 338 | { |
| 339 | ref: 'ref_2', |
| 340 | originalRole: 31, |
| 341 | originalBaseRole: 1, |
| 342 | updatedRole: 3, |
| 343 | }, |
| 344 | ], |
| 345 | } |
| 346 | |
| 347 | const { toAssign, toRemove, toUpdate } = deriveRoleChangeActions(existingRoles, changesToRoles) |
| 348 | expect(toAssign).toStrictEqual([{ roleId: 5, refs: ['ref_5'] }]) |
| 349 | expect(toUpdate).toStrictEqual([ |
| 350 | { roleId: 30, refs: ['ref_2', 'ref_4'] }, |
| 351 | { roleId: 31, refs: ['ref_1'] }, |
| 352 | ]) |
| 353 | expect(toRemove).toStrictEqual([]) |
| 354 | }) |
| 355 | test('Myriad of updates to stress test function logic (III)', () => { |
| 356 | // Given 10 projects 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 |
| 357 | // Existing has developer role for 1, 2, 3, admin role for 4, 5, 6, owner for 7 |
| 358 | const existingRoles = [ |
| 359 | { |
| 360 | id: 30, |
| 361 | name: 'Developer_qggwcbikivagivlidfhw', |
| 362 | description: '', |
| 363 | base_role_id: 3, // Developer |
| 364 | projects: [ |
| 365 | { ref: 'ref_1', name: 'Project 1' }, |
| 366 | { ref: 'ref_2', name: 'Project 2' }, |
| 367 | { ref: 'ref_3', name: 'Project 3' }, |
| 368 | ], |
| 369 | }, |
| 370 | { |
| 371 | id: 31, |
| 372 | name: 'Administrator_qggwcbikivagivlidfhw', |
| 373 | description: '', |
| 374 | base_role_id: 1, // Admin, |
| 375 | projects: [ |
| 376 | { ref: 'ref_4', name: 'Project 4' }, |
| 377 | { ref: 'ref_5', name: 'Project 5' }, |
| 378 | { ref: 'ref_6', name: 'Project 6' }, |
| 379 | ], |
| 380 | }, |
| 381 | { |
| 382 | id: 32, |
| 383 | name: 'Owner_qggwcbikivagivlidfhw', |
| 384 | description: '', |
| 385 | base_role_id: 5, // Owner |
| 386 | projects: [{ ref: 'ref_7', name: 'Project 7' }], |
| 387 | }, |
| 388 | ] |
| 389 | |
| 390 | // Remove access from projects 2, 7 |
| 391 | // Update developer -> owner for project 3 |
| 392 | // Update admin -> owner for project 5 |
| 393 | // Add admin for project 8 |
| 394 | // Add developer for project 9 and 10 |
| 395 | const changesToRoles = { |
| 396 | removed: [ |
| 397 | { |
| 398 | ref: 'ref_2', |
| 399 | roleId: 30, |
| 400 | baseRoleId: 3, |
| 401 | }, |
| 402 | { |
| 403 | ref: 'ref_7', |
| 404 | roleId: 32, |
| 405 | baseRoleId: 5, |
| 406 | }, |
| 407 | ], |
| 408 | added: [ |
| 409 | { ref: 'ref_8', roleId: 1 }, |
| 410 | { ref: 'ref_9', roleId: 3 }, |
| 411 | { ref: 'ref_10', roleId: 3 }, |
| 412 | ], |
| 413 | updated: [ |
| 414 | { |
| 415 | ref: 'ref_3', |
| 416 | originalRole: 30, |
| 417 | originalBaseRole: 3, |
| 418 | updatedRole: 5, |
| 419 | }, |
| 420 | { |
| 421 | ref: 'ref_5', |
| 422 | originalRole: 31, |
| 423 | originalBaseRole: 1, |
| 424 | updatedRole: 5, |
| 425 | }, |
| 426 | ], |
| 427 | } |
| 428 | |
| 429 | const { toAssign, toRemove, toUpdate } = deriveRoleChangeActions(existingRoles, changesToRoles) |
| 430 | expect(toAssign).toStrictEqual([]) |
| 431 | expect(toRemove).toStrictEqual([]) |
| 432 | expect(toUpdate).toStrictEqual([ |
| 433 | { roleId: 30, refs: ['ref_1', 'ref_10', 'ref_9'] }, |
| 434 | { roleId: 31, refs: ['ref_4', 'ref_6', 'ref_8'] }, |
| 435 | { roleId: 32, refs: ['ref_3', 'ref_5'] }, |
| 436 | ]) |
| 437 | }) |
| 438 | }) |