1+ import { faker } from '@faker-js/faker'
12import { describe , expect , it } from 'vitest'
23import type { AdminRole , User } from '@prisma/client'
3- import { faker } from '@faker-js/faker'
44import prisma from '../../__mocks__/prisma.js'
55import { BadRequest400 } from '../../utils/errors.ts'
66import { countRolesMembers , createRole , deleteRole , listRoles , patchRoles } from './business.ts'
77
88describe ( 'test admin-role business' , ( ) => {
99 describe ( 'listRoles' , ( ) => {
1010 it ( 'should stringify bigint' , async ( ) => {
11- const partialRole : Partial < AdminRole > = {
11+ const dbRole : AdminRole = {
12+ id : faker . string . uuid ( ) ,
13+ name : faker . string . alphanumeric ( ) ,
1214 permissions : 4n ,
15+ position : 0 ,
16+ oidcGroup : '' ,
1317 }
1418
15- prisma . adminRole . findMany . mockResolvedValueOnce ( [ partialRole ] )
19+ prisma . adminRole . findMany . mockResolvedValueOnce ( [ dbRole ] )
1620 const response = await listRoles ( )
17- expect ( response ) . toEqual ( [ { permissions : '4' } ] )
21+ expect ( response ) . toContainEqual ( expect . objectContaining ( { permissions : '4' } ) )
1822 } )
1923 } )
2024
2125 describe ( 'createRole' , ( ) => {
2226 it ( 'should create role with incremented position when position 0 is the highest' , async ( ) => {
23- const dbRole : Partial < AdminRole > = {
27+ const dbRole : AdminRole = {
28+ id : faker . string . uuid ( ) ,
29+ name : faker . string . alphanumeric ( ) ,
2430 permissions : 4n ,
2531 position : 0 ,
32+ oidcGroup : '' ,
2633 }
2734
2835 prisma . adminRole . findFirst . mockResolvedValueOnce ( dbRole )
2936 prisma . adminRole . findMany . mockResolvedValueOnce ( [ dbRole ] )
30- prisma . adminRole . create . mockResolvedValue ( null )
37+ prisma . adminRole . create . mockResolvedValue ( dbRole )
3138 await createRole ( { name : 'test' } )
3239
3340 expect ( prisma . adminRole . create ) . toHaveBeenCalledWith ( { data : { name : 'test' , permissions : 0n , position : 1 } } )
3441 } )
3542
3643 it ( 'should create role with incremented position with bigger position' , async ( ) => {
37- const dbRole : Partial < AdminRole > = {
44+ const dbRole : AdminRole = {
45+ id : faker . string . uuid ( ) ,
46+ name : faker . string . alphanumeric ( ) ,
3847 permissions : 4n ,
3948 position : 50 ,
49+ oidcGroup : '' ,
4050 }
4151
4252 prisma . adminRole . findFirst . mockResolvedValueOnce ( dbRole )
4353 prisma . adminRole . findMany . mockResolvedValueOnce ( [ dbRole ] )
44- prisma . adminRole . create . mockResolvedValue ( null )
54+ prisma . adminRole . create . mockResolvedValue ( dbRole )
4555 await createRole ( { name : 'test' } )
4656
4757 expect ( prisma . adminRole . create ) . toHaveBeenCalledWith ( { data : { name : 'test' , permissions : 0n , position : 51 } } )
4858 } )
4959
5060 it ( 'should create role with incremented position with no role in db' , async ( ) => {
51- const dbRole : Partial < AdminRole > = {
61+ const dbRole : AdminRole = {
62+ id : faker . string . uuid ( ) ,
63+ name : faker . string . alphanumeric ( ) ,
5264 permissions : 4n ,
5365 position : 50 ,
66+ oidcGroup : '' ,
5467 }
5568
56- prisma . adminRole . findFirst . mockResolvedValueOnce ( undefined )
69+ prisma . adminRole . findFirst . mockResolvedValueOnce ( null )
5770 prisma . adminRole . findMany . mockResolvedValueOnce ( [ dbRole ] )
58- prisma . adminRole . create . mockResolvedValue ( null )
71+ prisma . adminRole . create . mockResolvedValue ( dbRole )
5972 await createRole ( { name : 'test' } )
6073
6174 expect ( prisma . adminRole . create ) . toHaveBeenCalledWith ( { data : { name : 'test' , permissions : 0n , position : 0 } } )
@@ -65,16 +78,39 @@ describe('test admin-role business', () => {
6578 const roleId = faker . string . uuid ( )
6679 it ( 'should delete role and remove id from concerned users' , async ( ) => {
6780 const users = [ {
68- adminRoleIds : [ roleId ] ,
6981 id : faker . string . uuid ( ) ,
82+ type : 'human' ,
83+ firstName : faker . person . firstName ( ) ,
84+ lastName : faker . person . lastName ( ) ,
85+ email : faker . internet . email ( ) ,
86+ adminRoleIds : [ roleId ] ,
87+ createdAt : faker . date . past ( ) ,
88+ updatedAt : faker . date . recent ( ) ,
89+ lastLogin : faker . date . past ( ) ,
7090 } , {
71- adminRoleIds : [ roleId , faker . string . uuid ( ) ] ,
7291 id : faker . string . uuid ( ) ,
73- } ] as const satisfies Partial < User > [ ]
92+ type : 'human' ,
93+ firstName : faker . person . firstName ( ) ,
94+ lastName : faker . person . lastName ( ) ,
95+ email : faker . internet . email ( ) ,
96+ adminRoleIds : [ roleId , faker . string . uuid ( ) ] ,
97+ createdAt : faker . date . past ( ) ,
98+ updatedAt : faker . date . recent ( ) ,
99+ lastLogin : faker . date . past ( ) ,
100+ } ] as const satisfies User [ ]
101+
102+ const dbRole : AdminRole = {
103+ name : 'Admin' ,
104+ id : roleId ,
105+ permissions : 4n ,
106+ position : 50 ,
107+ oidcGroup : '' ,
108+ }
74109
75110 prisma . user . findMany . mockResolvedValueOnce ( users )
76111 prisma . adminRole . findMany . mockResolvedValueOnce ( [ ] )
77- prisma . adminRole . create . mockResolvedValue ( null )
112+ prisma . adminRole . findUnique . mockResolvedValueOnce ( dbRole )
113+ prisma . adminRole . create . mockResolvedValue ( dbRole )
78114 await deleteRole ( roleId )
79115
80116 expect ( prisma . user . update ) . toHaveBeenNthCalledWith ( 1 , { where : { id : users [ 0 ] . id } , data : { adminRoleIds : [ ] } } )
@@ -84,35 +120,59 @@ describe('test admin-role business', () => {
84120 } )
85121 describe ( 'countRolesMembers' , ( ) => {
86122 it ( 'should return aggregated role member counts' , async ( ) => {
87- const partialRoles = [ {
123+ const roles = [ {
88124 id : faker . string . uuid ( ) ,
125+ name : faker . string . alphanumeric ( ) ,
126+ oidcGroup : '' ,
127+ permissions : faker . number . bigInt ( { min : 0n , max : 50000n } ) ,
128+ position : 0 ,
89129 } , {
90130 id : faker . string . uuid ( ) ,
91- } ] as const satisfies Partial < AdminRole > [ ]
131+ name : faker . string . alphanumeric ( ) ,
132+ oidcGroup : '' ,
133+ permissions : faker . number . bigInt ( { min : 0n , max : 50000n } ) ,
134+ position : 1 ,
135+ } ] as const satisfies AdminRole [ ]
92136
93137 const users = [ {
94- adminRoleIds : [ partialRoles [ 0 ] . id , partialRoles [ 1 ] . id ] ,
138+ id : faker . string . uuid ( ) ,
139+ type : 'human' ,
140+ firstName : faker . person . firstName ( ) ,
141+ lastName : faker . person . lastName ( ) ,
142+ email : faker . internet . email ( ) ,
143+ adminRoleIds : [ roles [ 0 ] . id , roles [ 1 ] . id ] ,
144+ createdAt : faker . date . past ( ) ,
145+ updatedAt : faker . date . recent ( ) ,
146+ lastLogin : faker . date . past ( ) ,
95147 } , {
96- adminRoleIds : [ partialRoles [ 1 ] . id ] ,
97- } ] as const satisfies Partial < User > [ ]
98- prisma . adminRole . findMany . mockResolvedValue ( partialRoles )
148+ id : faker . string . uuid ( ) ,
149+ type : 'human' ,
150+ firstName : faker . person . firstName ( ) ,
151+ lastName : faker . person . lastName ( ) ,
152+ email : faker . internet . email ( ) ,
153+ adminRoleIds : [ roles [ 1 ] . id ] ,
154+ createdAt : faker . date . past ( ) ,
155+ updatedAt : faker . date . recent ( ) ,
156+ lastLogin : faker . date . past ( ) ,
157+ } ] as const satisfies User [ ]
158+ prisma . adminRole . findMany . mockResolvedValue ( roles )
99159 prisma . user . findMany . mockResolvedValue ( users )
100160
101161 const response = await countRolesMembers ( )
102162
103- expect ( response ) . toEqual ( { [ partialRoles [ 0 ] . id ] : 1 , [ partialRoles [ 1 ] . id ] : 2 } )
163+ expect ( response ) . toEqual ( { [ roles [ 0 ] . id ] : 1 , [ roles [ 1 ] . id ] : 2 } )
104164 } )
105165 } )
106166 describe ( 'patchRoles' , ( ) => {
107167 const dbRoles : AdminRole [ ] = [ {
108168 id : faker . string . uuid ( ) ,
109- name : faker . company . name ( ) ,
169+ name : faker . string . alphanumeric ( ) ,
110170 oidcGroup : '' ,
111171 permissions : faker . number . bigInt ( { min : 0n , max : 50000n } ) ,
112172 position : 0 ,
113173 } , {
114174 id : faker . string . uuid ( ) ,
115- name : faker . company . name ( ) ,
175+ name : faker . string . alphanumeric ( ) ,
116176 oidcGroup : '' ,
117177 permissions : faker . number . bigInt ( { min : 0n , max : 50000n } ) ,
118178 position : 1 ,
@@ -125,7 +185,7 @@ describe('test admin-role business', () => {
125185 } )
126186
127187 it ( 'should return 400 if incoherent positions' , async ( ) => {
128- const updateRoles : Pick < AdminRole , 'id' | 'position' > = [
188+ const updateRoles : Pick < AdminRole , 'id' | 'position' > [ ] = [
129189 { id : dbRoles [ 0 ] . id , position : 1 } ,
130190 { id : dbRoles [ 1 ] . id , position : 1 } ,
131191 ]
@@ -137,7 +197,7 @@ describe('test admin-role business', () => {
137197 expect ( prisma . adminRole . update ) . toHaveBeenCalledTimes ( 0 )
138198 } )
139199 it ( 'should return 400 if incoherent positions (missing roles)' , async ( ) => {
140- const updateRoles : Pick < AdminRole , 'id' | 'position' > = [
200+ const updateRoles : Pick < AdminRole , 'id' | 'position' > [ ] = [
141201 { id : dbRoles [ 1 ] . id , position : 1 } ,
142202 ]
143203 prisma . adminRole . findMany . mockResolvedValue ( dbRoles )
@@ -148,7 +208,7 @@ describe('test admin-role business', () => {
148208 expect ( prisma . adminRole . update ) . toHaveBeenCalledTimes ( 0 )
149209 } )
150210 it ( 'should update positions' , async ( ) => {
151- const updateRoles : Pick < AdminRole , 'id' | 'position' > = [
211+ const updateRoles : Pick < AdminRole , 'id' | 'position' > [ ] = [
152212 { id : dbRoles [ 0 ] . id , position : 1 } ,
153213 { id : dbRoles [ 1 ] . id , position : 0 } ,
154214 ]
@@ -159,7 +219,7 @@ describe('test admin-role business', () => {
159219 expect ( prisma . adminRole . update ) . toHaveBeenCalledTimes ( 2 )
160220 } )
161221 it ( 'should update permissions' , async ( ) => {
162- const updateRoles : Pick < AdminRole , 'id' | 'position' > = [
222+ const updateRoles : ( Pick < AdminRole , 'id' > & { permissions ?: string } ) [ ] = [
163223 { id : dbRoles [ 1 ] . id , permissions : '0' } ,
164224 ]
165225 prisma . adminRole . findMany . mockResolvedValue ( dbRoles )
0 commit comments