@@ -259,6 +259,26 @@ describe('Users tests', () => {
259259
260260 const domainSuffix = 'dev.linode-apl.net'
261261
262+ const platformAdminSession : SessionUser = {
263+ name : 'Platform Admin' ,
264+ email : `platform-admin@${ domainSuffix } ` ,
265+ isPlatformAdmin : true ,
266+ isTeamAdmin : false ,
267+ authz : { } ,
268+ teams : [ ] ,
269+ roles : [ ] ,
270+ sub : 'platform-admin' ,
271+ }
272+ const teamAdminSession : SessionUser = {
273+ name : 'Team Admin' ,
274+ email : `team-admin@${ domainSuffix } ` ,
275+ isPlatformAdmin : false ,
276+ isTeamAdmin : true ,
277+ authz : { } ,
278+ teams : [ 'team1' ] ,
279+ roles : [ ] ,
280+ sub : 'team-admin' ,
281+ }
262282 const sessionUser : SessionUser = {
263283 name : 'Session User' ,
264284 email : `session@${ domainSuffix } ` ,
@@ -269,7 +289,6 @@ describe('Users tests', () => {
269289 roles : [ ] ,
270290 sub : 'session-user' ,
271291 }
272-
273292 const defaultPlatformAdmin : User = {
274293 id : '1' ,
275294 email : `platform-admin@${ domainSuffix } ` ,
@@ -342,14 +361,75 @@ describe('Users tests', () => {
342361 test ( 'should not allow deleting the default platform admin user' , async ( ) => {
343362 await expect ( otomiStack . deleteUser ( '1' ) ) . rejects . toMatchObject ( {
344363 code : 403 ,
345- publicMessage : 'Cannot delete the default platform admin user ' ,
364+ publicMessage : 'Forbidden ' ,
346365 } )
347366 } )
348367
349368 test ( 'should allow deleting any other platform admin user' , async ( ) => {
350369 expect ( await otomiStack . deleteUser ( '2' ) ) . toBeUndefined ( )
351370 } )
352371
372+ describe ( 'User Retrieve Validation' , ( ) => {
373+ beforeEach ( async ( ) => {
374+ otomiStack = new OtomiStack ( )
375+ await otomiStack . init ( )
376+ otomiStack . git = mockDeep < Git > ( )
377+ await otomiStack . initRepo ( )
378+ otomiStack . repoService . createUser ( teamMember1 )
379+ } )
380+
381+ it ( 'should return full user for platform admin' , ( ) => {
382+ const result = otomiStack . getUser ( teamMember1 . id ! , platformAdminSession )
383+ expect ( result ) . toMatchObject ( teamMember1 )
384+ } )
385+
386+ it ( 'should return limited user info for team admin' , ( ) => {
387+ const result = otomiStack . getUser ( teamMember1 . id ! , teamAdminSession )
388+ expect ( result ) . toEqual ( {
389+ id : teamMember1 . id ,
390+ email : teamMember1 . email ,
391+ isPlatformAdmin : teamMember1 . isPlatformAdmin ,
392+ isTeamAdmin : teamMember1 . isTeamAdmin ,
393+ teams : teamMember1 . teams ,
394+ } )
395+ } )
396+
397+ it ( 'should throw 403 for regular user' , ( ) => {
398+ try {
399+ otomiStack . getUser ( teamMember1 . id ! , { ...sessionUser , isPlatformAdmin : false , isTeamAdmin : false } )
400+ fail ( 'Expected error was not thrown' )
401+ } catch ( err : any ) {
402+ expect ( err ) . toHaveProperty ( 'code' , 403 )
403+ }
404+ } )
405+
406+ it ( 'should return all users for platform admin in getAllUsers' , ( ) => {
407+ const users = otomiStack . getAllUsers ( platformAdminSession )
408+ expect ( users . some ( ( u ) => u . id === teamMember1 . id ) ) . toBe ( true )
409+ } )
410+
411+ it ( 'should return limited info for team admin in getAllUsers' , ( ) => {
412+ const users = otomiStack . getAllUsers ( teamAdminSession )
413+ expect ( users [ 0 ] ) . toHaveProperty ( 'id' )
414+ expect ( users [ 0 ] ) . toHaveProperty ( 'email' )
415+ expect ( users [ 0 ] ) . toHaveProperty ( 'isPlatformAdmin' )
416+ expect ( users [ 0 ] ) . toHaveProperty ( 'isTeamAdmin' )
417+ expect ( users [ 0 ] ) . toHaveProperty ( 'teams' )
418+ // Should not have firstName/lastName
419+ expect ( users [ 0 ] ) . not . toHaveProperty ( 'firstName' )
420+ expect ( users [ 0 ] ) . not . toHaveProperty ( 'lastName' )
421+ } )
422+
423+ it ( 'should throw 403 for regular user in getAllUsers' , ( ) => {
424+ try {
425+ otomiStack . getAllUsers ( { ...sessionUser , isPlatformAdmin : false , isTeamAdmin : false } )
426+ fail ( 'Expected error was not thrown' )
427+ } catch ( err : any ) {
428+ expect ( err ) . toHaveProperty ( 'code' , 403 )
429+ }
430+ } )
431+ } )
432+
353433 describe ( 'User Creation Validation' , ( ) => {
354434 describe ( 'Username Length Validation' , ( ) => {
355435 it ( 'should not create a user with less than 3 characters' , async ( ) => {
@@ -436,8 +516,6 @@ describe('Users tests', () => {
436516 await otomiStack . createUser ( user )
437517 const updated = { ...user , firstName : 'edited' }
438518 jest . spyOn ( otomiStack . repoService , 'updateUser' ) . mockReturnValue ( updated )
439- // Use a platform admin session user
440- const platformAdminSession = { ...sessionUser , isPlatformAdmin : true }
441519 const result = await otomiStack . editUser ( user . id , updated , platformAdminSession )
442520 expect ( result . firstName ) . toBe ( 'edited' )
443521 } )
@@ -536,8 +614,7 @@ describe('Users tests', () => {
536614 const data = [ { ...teamMember2 , teams : [ 'team3' ] } ]
537615 await expect ( otomiStack . editTeamUsers ( data , sessionUser ) ) . rejects . toMatchObject ( {
538616 code : 403 ,
539- publicMessage :
540- 'Team admins are permitted to add or remove users only within the teams they manage. However, they cannot remove themselves or other team admins from those teams.' ,
617+ publicMessage : 'Forbidden' ,
541618 } )
542619 } )
543620
@@ -569,7 +646,7 @@ describe('Users tests', () => {
569646 const data = [ { ...teamMember2 , teams : [ 'team1' ] } ]
570647 await expect ( otomiStack . editTeamUsers ( data , regularUser ) ) . rejects . toMatchObject ( {
571648 code : 403 ,
572- publicMessage : "Only platform admins or team admins can modify a user's team memberships." ,
649+ publicMessage : 'Forbidden' ,
573650 } )
574651 } )
575652 } )
0 commit comments