0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-24 22:41:28 -05:00

test: add it for admin-user roles (#2881)

This commit is contained in:
wangsijie 2023-01-11 15:34:39 +08:00 committed by GitHub
parent dee3ca0891
commit fe14f0563c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 3 deletions

View file

@ -1,10 +1,11 @@
import type { UsersRole } from '@logto/schemas';
import { UsersRoles } from '@logto/schemas';
import { RolesScopes, UsersRoles } from '@logto/schemas';
import { conditionalSql, convertToIdentifiers } from '@logto/shared';
import type { CommonQueryMethods } from 'slonik';
import { sql } from 'slonik';
import envSet from '#src/env-set/index.js';
import { DeletionError } from '#src/errors/SlonikError/index.js';
const { table, fields } = convertToIdentifiers(UsersRoles);
@ -52,10 +53,14 @@ export const createUsersRolesQueries = (pool: CommonQueryMethods) => {
`);
const deleteUsersRolesByUserIdAndRoleId = async (userId: string, roleId: string) => {
await pool.query(sql`
const { rowCount } = await pool.query(sql`
delete from ${table}
where ${fields.userId} = ${userId} and ${fields.roleId} = ${roleId}
`);
if (rowCount < 1) {
throw new DeletionError(RolesScopes.table);
}
};
return {

View file

@ -70,6 +70,9 @@ const mockedQueries = {
async (): Promise<Role[]> => [{ id: 'role_id', name: 'admin', description: 'none' }]
),
},
usersRoles: {
deleteUsersRolesByUserIdAndRoleId: jest.fn(),
},
} satisfies Partial2<Queries>;
const mockHasUser = jest.fn(async () => false);

View file

@ -1,4 +1,4 @@
import type { User } from '@logto/schemas';
import type { Role, User } from '@logto/schemas';
import { authedAdminApi } from './api.js';
@ -42,3 +42,12 @@ export const updateUserPassword = (userId: string, password: string) =>
export const deleteUserIdentity = (userId: string, connectorTarget: string) =>
authedAdminApi.delete(`users/${userId}/identities/${connectorTarget}`);
export const assignRolesToUser = (userId: string, roleIds: string[]) =>
authedAdminApi.post(`users/${userId}/roles`, { json: { roleIds } });
export const getUserRoles = (userId: string) =>
authedAdminApi.get(`users/${userId}/roles`).json<Role[]>();
export const deleteRoleFromUser = (userId: string, roleId: string) =>
authedAdminApi.delete(`users/${userId}/roles/${roleId}`);

View file

@ -0,0 +1,41 @@
import { adminConsoleAdminRoleId } from '@logto/schemas';
import { HTTPError } from 'got';
import { assignRolesToUser, getUserRoles, deleteRoleFromUser } from '#src/api/index.js';
import { createUserByAdmin } from '#src/helpers.js';
describe('admin console user management (roles)', () => {
it('should get empty list successfully', async () => {
const user = await createUserByAdmin();
const roles = await getUserRoles(user.id);
expect(roles.length).toBe(0);
});
it('should assign role to user and get list successfully', async () => {
const user = await createUserByAdmin();
await assignRolesToUser(user.id, [adminConsoleAdminRoleId]);
const roles = await getUserRoles(user.id);
expect(roles[0]).toHaveProperty('id', adminConsoleAdminRoleId);
});
it('should delete role from user successfully', async () => {
const user = await createUserByAdmin();
await assignRolesToUser(user.id, [adminConsoleAdminRoleId]);
await deleteRoleFromUser(user.id, adminConsoleAdminRoleId);
const roles = await getUserRoles(user.id);
expect(roles.length).toBe(0);
});
it('should delete non-exist-role from user failed', async () => {
const user = await createUserByAdmin();
const response = await deleteRoleFromUser(user.id, adminConsoleAdminRoleId).catch(
(error: unknown) => error
);
expect(response instanceof HTTPError && response.response.statusCode === 404).toBe(true);
});
});