mirror of
https://github.com/logto-io/logto.git
synced 2025-03-24 22:41:28 -05:00
test: add IT for scopes (#2895)
This commit is contained in:
parent
836a147194
commit
375a8f4842
3 changed files with 116 additions and 0 deletions
38
packages/integration-tests/src/api/scope.ts
Normal file
38
packages/integration-tests/src/api/scope.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import type { Scope, CreateScope } from '@logto/schemas';
|
||||
import type { OptionsOfTextResponseBody } from 'got';
|
||||
|
||||
import { generateScopeName } from '#src/utils.js';
|
||||
|
||||
import { authedAdminApi } from './api.js';
|
||||
|
||||
export const getScopes = (resourceId: string, options?: OptionsOfTextResponseBody) =>
|
||||
authedAdminApi.get(`resources/${resourceId}/scopes`, options).json<Scope[]>();
|
||||
|
||||
export const createScope = (resourceId: string, name?: string) => {
|
||||
const scopeName = name ?? generateScopeName();
|
||||
|
||||
return authedAdminApi
|
||||
.post(`resources/${resourceId}/scopes`, {
|
||||
json: {
|
||||
name: scopeName,
|
||||
description: scopeName,
|
||||
},
|
||||
})
|
||||
.json<Scope>();
|
||||
};
|
||||
|
||||
export const updateScope = (
|
||||
resourceId: string,
|
||||
scopeId: string,
|
||||
payload: Partial<Omit<CreateScope, 'id' | 'resourceId'>>
|
||||
) =>
|
||||
authedAdminApi
|
||||
.patch(`resources/${resourceId}/scopes/${scopeId}`, {
|
||||
json: {
|
||||
...payload,
|
||||
},
|
||||
})
|
||||
.json<Scope>();
|
||||
|
||||
export const deleteScope = (resourceId: string, scopeId: string) =>
|
||||
authedAdminApi.delete(`resources/${resourceId}/scopes/${scopeId}`);
|
|
@ -0,0 +1,77 @@
|
|||
import { managementResource, managementResourceScope } from '@logto/schemas';
|
||||
import { HTTPError } from 'got';
|
||||
|
||||
import { createResource } from '#src/api/index.js';
|
||||
import { createScope, deleteScope, getScopes, updateScope } from '#src/api/scope.js';
|
||||
import { generateScopeName } from '#src/utils.js';
|
||||
|
||||
describe('admin console api resources', () => {
|
||||
it('should get management api resource scopes successfully', async () => {
|
||||
const scopes = await getScopes(managementResource.id);
|
||||
|
||||
expect(scopes[0]).toMatchObject(managementResourceScope);
|
||||
});
|
||||
|
||||
it('should create scope successfully', async () => {
|
||||
const resource = await createResource();
|
||||
const scopeName = generateScopeName();
|
||||
const createdScope = await createScope(resource.id, scopeName);
|
||||
|
||||
expect(createdScope.name).toBe(scopeName);
|
||||
|
||||
const scopes = await getScopes(resource.id);
|
||||
|
||||
expect(scopes.some(({ name }) => name === scopeName)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should fail when create scope with name conflict', async () => {
|
||||
const resource = await createResource();
|
||||
const createdScope = await createScope(resource.id);
|
||||
const response = await createScope(resource.id, createdScope.name).catch(
|
||||
(error: unknown) => error
|
||||
);
|
||||
expect(response instanceof HTTPError && response.response.statusCode === 422).toBe(true);
|
||||
});
|
||||
|
||||
it('should update scope successfully', async () => {
|
||||
const resource = await createResource();
|
||||
const scope = await createScope(resource.id);
|
||||
|
||||
expect(scope).toBeTruthy();
|
||||
|
||||
const newScopeName = `new_${scope.name}`;
|
||||
const newScopeDescription = `new_${scope.description ?? ''}`;
|
||||
|
||||
const updatesScope = await updateScope(resource.id, scope.id, {
|
||||
name: newScopeName,
|
||||
description: newScopeDescription,
|
||||
});
|
||||
|
||||
expect(updatesScope.id).toBe(scope.id);
|
||||
expect(updatesScope.name).toBe(newScopeName);
|
||||
expect(updatesScope.description).toBe(newScopeDescription);
|
||||
});
|
||||
|
||||
it('should fail when update scope with name conflict', async () => {
|
||||
const resource = await createResource();
|
||||
const createdScope = await createScope(resource.id);
|
||||
const createdScope2 = await createScope(resource.id);
|
||||
const response = await updateScope(resource.id, createdScope2.id, {
|
||||
name: createdScope.name,
|
||||
}).catch((error: unknown) => error);
|
||||
expect(response instanceof HTTPError && response.response.statusCode === 422).toBe(true);
|
||||
});
|
||||
|
||||
it('should delete scope successfully', async () => {
|
||||
const resource = await createResource();
|
||||
const scope = await createScope(resource.id);
|
||||
|
||||
expect(scope).toBeTruthy();
|
||||
|
||||
await deleteScope(resource.id, scope.id);
|
||||
|
||||
const scopes = await getScopes(resource.id);
|
||||
|
||||
expect(scopes.some(({ name }) => name === scope.name)).toBeFalsy();
|
||||
});
|
||||
});
|
|
@ -6,6 +6,7 @@ export const generatePassword = () => `pwd_${crypto.randomUUID()}`;
|
|||
export const generateResourceName = () => `res_${crypto.randomUUID()}`;
|
||||
export const generateResourceIndicator = () => `https://${crypto.randomUUID()}.logto.io`;
|
||||
export const generateEmail = () => `${crypto.randomUUID().toLowerCase()}@logto.io`;
|
||||
export const generateScopeName = () => `sc:${crypto.randomUUID()}`;
|
||||
|
||||
export const generatePhone = () => {
|
||||
const array = new Uint32Array(1);
|
||||
|
|
Loading…
Add table
Reference in a new issue