@@ -110,20 +110,20 @@ describe('API Keys', (ctx) => {
110110 const orgGroupsResponse = await client . getOrganizationGroups ( { } ) ;
111111 expect ( orgGroupsResponse . response ?. code ) . toBe ( EnumStatusCode . OK ) ;
112112
113- const adminGroup = orgGroupsResponse . groups . find ( ( g ) => g . name === 'admin' ) ! ;
114113 const developerGroup = orgGroupsResponse . groups . find ( ( g ) => g . name === 'developer' ) ! ;
114+ const viewerGroup = orgGroupsResponse . groups . find ( ( g ) => g . name === 'viewer' ) ! ;
115115
116116 authenticator . changeUserWithSuppliedContext ( {
117117 ...users [ TestUser . adminAliceCompanyA ] ,
118118 rbac : createTestRBACEvaluator ( createTestGroup ( { role : role as OrganizationRole } ) ) ,
119119 } ) ;
120120
121- // Create the API key with the `admin ` group
121+ // Create the API key with the `viewer ` group
122122 const apiKeyName = uid ( ) ;
123123 const createApiKeyResponse = await client . createAPIKey ( {
124124 name : apiKeyName ,
125125 expires : ExpiresAt . NEVER ,
126- groupId : adminGroup . groupId ,
126+ groupId : viewerGroup . groupId ,
127127 } ) ;
128128
129129 expect ( createApiKeyResponse . response ?. code ) . toBe ( EnumStatusCode . OK ) ;
@@ -136,7 +136,7 @@ describe('API Keys', (ctx) => {
136136
137137 expect ( updateApiKeyResponse . response ?. code ) . toBe ( EnumStatusCode . OK ) ;
138138
139- // Ensure that the API key have the correct group
139+ // Ensure that the API key has the correct group
140140 let getApiKeysResponse = await client . getAPIKeys ( { } ) ;
141141 let apiKey = getApiKeysResponse . apiKeys ?. find ( ( k ) => k . name === apiKeyName ) ;
142142
@@ -150,7 +150,7 @@ describe('API Keys', (ctx) => {
150150
151151 expect ( deleteApiKeyResponse . response ?. code ) . toBe ( EnumStatusCode . OK ) ;
152152
153- // Ensure the API key have been deleted
153+ // Ensure the API key has been deleted
154154 getApiKeysResponse = await client . getAPIKeys ( { } ) ;
155155 apiKey = getApiKeysResponse . apiKeys ?. find ( ( k ) => k . name === apiKeyName ) ;
156156
@@ -160,6 +160,104 @@ describe('API Keys', (ctx) => {
160160 await server . close ( ) ;
161161 } ) ;
162162
163+ test ( 'that an "organization-apikey-manager" cannot create API keys with admin role' , async ( ) => {
164+ const { client, server, users, authenticator } = await SetupTest ( { dbname, enableMultiUsers : true } ) ;
165+
166+ const orgGroupsResponse = await client . getOrganizationGroups ( { } ) ;
167+ expect ( orgGroupsResponse . response ?. code ) . toBe ( EnumStatusCode . OK ) ;
168+
169+ const adminGroup = orgGroupsResponse . groups . find ( ( g ) => g . name === 'admin' ) ! ;
170+
171+ authenticator . changeUserWithSuppliedContext ( {
172+ ...users [ TestUser . adminAliceCompanyA ] ,
173+ rbac : createTestRBACEvaluator ( createTestGroup ( { role : 'organization-apikey-manager' } ) ) ,
174+ } ) ;
175+
176+ // Create the API key with the `admin` group
177+ const apiKeyName = uid ( ) ;
178+ const createApiKeyResponse = await client . createAPIKey ( {
179+ name : apiKeyName ,
180+ expires : ExpiresAt . NEVER ,
181+ groupId : adminGroup . groupId ,
182+ } ) ;
183+
184+ expect ( createApiKeyResponse . response ?. code ) . toBe ( EnumStatusCode . ERR ) ;
185+ expect ( createApiKeyResponse . response ?. details ) . toBe ( `You don't have access to create an API key with the group "admin"` ) ;
186+
187+ await server . close ( ) ;
188+ } ) ;
189+
190+ test ( 'that an "organization-apikey-manager" cannot update API keys with admin role' , async ( ) => {
191+ const { client, server, users, authenticator } = await SetupTest ( { dbname, enableMultiUsers : true } ) ;
192+
193+ const orgGroupsResponse = await client . getOrganizationGroups ( { } ) ;
194+ expect ( orgGroupsResponse . response ?. code ) . toBe ( EnumStatusCode . OK ) ;
195+
196+ const adminGroup = orgGroupsResponse . groups . find ( ( g ) => g . name === 'admin' ) ! ;
197+ const viewerGroup = orgGroupsResponse . groups . find ( ( g ) => g . name === 'viewer' ) ! ;
198+
199+ authenticator . changeUserWithSuppliedContext ( {
200+ ...users [ TestUser . adminAliceCompanyA ] ,
201+ rbac : createTestRBACEvaluator ( createTestGroup ( { role : 'organization-apikey-manager' } ) ) ,
202+ } ) ;
203+
204+ // Create the API key with the `viewer` group
205+ const apiKeyName = uid ( ) ;
206+ const createApiKeyResponse = await client . createAPIKey ( {
207+ name : apiKeyName ,
208+ expires : ExpiresAt . NEVER ,
209+ groupId : viewerGroup . groupId ,
210+ } ) ;
211+
212+ expect ( createApiKeyResponse . response ?. code ) . toBe ( EnumStatusCode . OK ) ;
213+
214+ // Update the API key to the `admin` group
215+ const updateApiKeyResponse = await client . updateAPIKey ( {
216+ name : apiKeyName ,
217+ groupId : adminGroup . groupId ,
218+ } ) ;
219+
220+ expect ( updateApiKeyResponse . response ?. code ) . toBe ( EnumStatusCode . ERR ) ;
221+ expect ( updateApiKeyResponse . response ?. details ) . toBe ( `You don't have access to update the API key group to "admin"` ) ;
222+
223+ await server . close ( ) ;
224+ } ) ;
225+
226+ test ( 'that an "organization-apikey-manager" cannot delete an API key with admin role' , async ( ) => {
227+ const { client, server, users, authenticator } = await SetupTest ( { dbname, enableMultiUsers : true } ) ;
228+
229+ const orgGroupsResponse = await client . getOrganizationGroups ( { } ) ;
230+ expect ( orgGroupsResponse . response ?. code ) . toBe ( EnumStatusCode . OK ) ;
231+
232+ const adminGroup = orgGroupsResponse . groups . find ( ( g ) => g . name === 'admin' ) ! ;
233+ authenticator . changeUserWithSuppliedContext ( {
234+ ...users [ TestUser . adminAliceCompanyA ] ,
235+ } ) ;
236+
237+ // Create the API key with the `admin` group
238+ const apiKeyName = uid ( ) ;
239+ const createApiKeyResponse = await client . createAPIKey ( {
240+ name : apiKeyName ,
241+ expires : ExpiresAt . NEVER ,
242+ groupId : adminGroup . groupId ,
243+ } ) ;
244+
245+ expect ( createApiKeyResponse . response ?. code ) . toBe ( EnumStatusCode . OK ) ;
246+
247+ // Try to delete the API key
248+ authenticator . changeUserWithSuppliedContext ( {
249+ ...users [ TestUser . adminAliceCompanyA ] ,
250+ rbac : createTestRBACEvaluator ( createTestGroup ( { role : 'organization-apikey-manager' } ) ) ,
251+ } ) ;
252+
253+ const deleteApiKeyResponse = await client . deleteAPIKey ( { name : apiKeyName } ) ;
254+
255+ expect ( deleteApiKeyResponse . response ?. code ) . toBe ( EnumStatusCode . ERR ) ;
256+ expect ( deleteApiKeyResponse . response ?. details ) . toBe ( `You don't have access to remove the API key "${ apiKeyName } "` ) ;
257+
258+ await server . close ( ) ;
259+ } ) ;
260+
163261 test . each ( [
164262 'organization-developer' ,
165263 'organization-viewer' ,
0 commit comments