@@ -4,8 +4,11 @@ import { generateStandardId } from '@logto/shared';
4
4
import { isKeyInObject , pick } from '@silverhand/essentials' ;
5
5
import { HTTPError } from 'ky' ;
6
6
7
+ import { createResource } from '#src/api/index.js' ;
8
+ import { createScope } from '#src/api/scope.js' ;
7
9
import { OrganizationRoleApiTest , OrganizationScopeApiTest } from '#src/helpers/organization.js' ;
8
10
import { ScopeApiTest } from '#src/helpers/resource.js' ;
11
+ import { generateScopeName } from '#src/utils.js' ;
9
12
10
13
const randomId = ( ) => generateStandardId ( 4 ) ;
11
14
@@ -179,6 +182,85 @@ describe('organization role APIs', () => {
179
182
const response = await roleApi . delete ( '0' ) . catch ( ( error : unknown ) => error ) ;
180
183
expect ( response instanceof HTTPError && response . response . status ) . toBe ( 404 ) ;
181
184
} ) ;
185
+
186
+ it ( 'should fail when creating a role with invalid organization scope IDs' , async ( ) => {
187
+ const invalidScopeId = generateStandardId ( ) ;
188
+ const response = await roleApi
189
+ . create ( {
190
+ name : 'test' + randomId ( ) ,
191
+ organizationScopeIds : [ invalidScopeId ] ,
192
+ } )
193
+ . catch ( ( error : unknown ) => error ) ;
194
+
195
+ assert ( response instanceof HTTPError ) ;
196
+ const body : unknown = await response . response . json ( ) ;
197
+ expect ( response . response . status ) . toBe ( 422 ) ;
198
+ expect ( body ) . toMatchObject (
199
+ expect . objectContaining ( {
200
+ code : 'organization.roles.invalid_scope_ids' ,
201
+ } )
202
+ ) ;
203
+
204
+ const roles = await roleApi . getList ( ) ;
205
+ expect ( roles ) . toHaveLength ( 0 ) ;
206
+ } ) ;
207
+
208
+ it ( 'should fail when creating a role with invalid resource scope IDs' , async ( ) => {
209
+ const invalidScopeId = generateStandardId ( ) ;
210
+ const response = await roleApi
211
+ . create ( {
212
+ name : 'test' + randomId ( ) ,
213
+ resourceScopeIds : [ invalidScopeId ] ,
214
+ } )
215
+ . catch ( ( error : unknown ) => error ) ;
216
+
217
+ assert ( response instanceof HTTPError ) ;
218
+ const body : unknown = await response . response . json ( ) ;
219
+ expect ( response . response . status ) . toBe ( 422 ) ;
220
+ expect ( body ) . toMatchObject (
221
+ expect . objectContaining ( {
222
+ code : 'organization.roles.invalid_resource_scope_ids' ,
223
+ } )
224
+ ) ;
225
+
226
+ const roles = await roleApi . getList ( ) ;
227
+ expect ( roles ) . toHaveLength ( 0 ) ;
228
+ } ) ;
229
+
230
+ it ( 'should successfully create a role with scope IDs are provided' , async ( ) => {
231
+ const resource = await createResource ( ) ;
232
+ const scopeName = generateScopeName ( ) ;
233
+ const createdScope = await createScope ( resource . id , scopeName ) ;
234
+
235
+ const [ scope1 , scope2 ] = await Promise . all ( [
236
+ scopeApi . create ( { name : 'test' + randomId ( ) } ) ,
237
+ scopeApi . create ( { name : 'test' + randomId ( ) } ) ,
238
+ ] ) ;
239
+ const createdRole = await roleApi . create ( {
240
+ name : 'test' + randomId ( ) ,
241
+ organizationScopeIds : [ scope1 . id , scope2 . id ] ,
242
+ resourceScopeIds : [ createdScope . id ] ,
243
+ } ) ;
244
+
245
+ expect ( createdRole ) . toHaveProperty ( 'id' ) ;
246
+ expect ( createdRole ) . toHaveProperty ( 'name' ) ;
247
+
248
+ const scopes = await roleApi . getScopes ( createdRole . id ) ;
249
+ expect ( scopes ) . toContainEqual (
250
+ expect . objectContaining ( {
251
+ name : scope1 . name ,
252
+ } )
253
+ ) ;
254
+ expect ( scopes ) . toContainEqual (
255
+ expect . objectContaining ( {
256
+ name : scope2 . name ,
257
+ } )
258
+ ) ;
259
+
260
+ const { resourceScopes } = await roleApi . get ( createdRole . id ) ;
261
+ expect ( resourceScopes . length ) . toBe ( 1 ) ;
262
+ expect ( resourceScopes [ 0 ] ) . toHaveProperty ( 'name' , scopeName ) ;
263
+ } ) ;
182
264
} ) ;
183
265
184
266
describe ( 'organization role - scope relations' , ( ) => {
0 commit comments