@@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker'
22import { describe , expect , it } from 'vitest'
33import type { AdminRole , User } from '@prisma/client'
44import prisma from '../../__mocks__/prisma.js'
5- import { BadRequest400 } from '../../utils/errors.ts'
5+ import { BadRequest400 , Forbidden403 } from '../../utils/errors.ts'
66import { countRolesMembers , createRole , deleteRole , listRoles , patchRoles } from './business.ts'
77
88describe ( 'test admin-role business' , ( ) => {
@@ -14,11 +14,12 @@ describe('test admin-role business', () => {
1414 permissions : 4n ,
1515 position : 0 ,
1616 oidcGroup : '' ,
17+ type : 'custom' ,
1718 }
1819
1920 prisma . adminRole . findMany . mockResolvedValueOnce ( [ dbRole ] )
2021 const response = await listRoles ( )
21- expect ( response ) . toContainEqual ( expect . objectContaining ( { permissions : '4' } ) )
22+ expect ( response ) . toContainEqual ( expect . objectContaining ( { permissions : '4' , type : 'custom' } ) )
2223 } )
2324 } )
2425
@@ -30,6 +31,7 @@ describe('test admin-role business', () => {
3031 permissions : 4n ,
3132 position : 0 ,
3233 oidcGroup : '' ,
34+ type : 'custom' ,
3335 }
3436
3537 prisma . adminRole . findFirst . mockResolvedValueOnce ( dbRole )
@@ -47,6 +49,7 @@ describe('test admin-role business', () => {
4749 permissions : 4n ,
4850 position : 50 ,
4951 oidcGroup : '' ,
52+ type : 'custom' ,
5053 }
5154
5255 prisma . adminRole . findFirst . mockResolvedValueOnce ( dbRole )
@@ -64,6 +67,7 @@ describe('test admin-role business', () => {
6467 permissions : 4n ,
6568 position : 50 ,
6669 oidcGroup : '' ,
70+ type : 'custom' ,
6771 }
6872
6973 prisma . adminRole . findFirst . mockResolvedValueOnce ( null )
@@ -105,6 +109,7 @@ describe('test admin-role business', () => {
105109 permissions : 4n ,
106110 position : 50 ,
107111 oidcGroup : '' ,
112+ type : 'custom' ,
108113 }
109114
110115 prisma . user . findMany . mockResolvedValueOnce ( users )
@@ -126,12 +131,14 @@ describe('test admin-role business', () => {
126131 oidcGroup : '' ,
127132 permissions : faker . number . bigInt ( { min : 0n , max : 50000n } ) ,
128133 position : 0 ,
134+ type : 'custom' ,
129135 } , {
130136 id : faker . string . uuid ( ) ,
131137 name : faker . string . alphanumeric ( ) ,
132138 oidcGroup : '' ,
133139 permissions : faker . number . bigInt ( { min : 0n , max : 50000n } ) ,
134140 position : 1 ,
141+ type : 'custom' ,
135142 } ] as const satisfies AdminRole [ ]
136143
137144 const users = [ {
@@ -170,14 +177,36 @@ describe('test admin-role business', () => {
170177 oidcGroup : '' ,
171178 permissions : faker . number . bigInt ( { min : 0n , max : 50000n } ) ,
172179 position : 0 ,
180+ type : 'custom' ,
173181 } , {
174182 id : faker . string . uuid ( ) ,
175183 name : faker . string . alphanumeric ( ) ,
176184 oidcGroup : '' ,
177185 permissions : faker . number . bigInt ( { min : 0n , max : 50000n } ) ,
178186 position : 1 ,
187+ type : 'custom' ,
179188 } ]
180189
190+ it ( 'should throw Forbidden403 when renaming a system role' , async ( ) => {
191+ const systemRole : AdminRole = {
192+ id : faker . string . uuid ( ) ,
193+ name : 'Admin' ,
194+ permissions : 10n ,
195+ position : 0 ,
196+ oidcGroup : 'admin-group' ,
197+ type : 'system' ,
198+ }
199+ prisma . adminRole . findMany . mockResolvedValue ( [ systemRole ] )
200+
201+ const updateRoles = [ {
202+ id : systemRole . id ,
203+ name : 'New Admin Name' ,
204+ } ]
205+
206+ await expect ( patchRoles ( updateRoles ) ) . rejects . toThrow ( Forbidden403 )
207+ expect ( prisma . adminRole . update ) . toHaveBeenCalledTimes ( 0 )
208+ } )
209+
181210 it ( 'should do nothing' , async ( ) => {
182211 prisma . adminRole . findMany . mockResolvedValue ( [ ] )
183212 await patchRoles ( [ ] )
@@ -233,6 +262,7 @@ describe('test admin-role business', () => {
233262 oidcGroup : dbRoles [ 1 ] . oidcGroup ,
234263 permissions : 0n ,
235264 position : 1 ,
265+ type : 'custom' ,
236266 } ,
237267 where : {
238268 id : dbRoles [ 1 ] . id ,
0 commit comments