diff --git a/packages/integration-tests/src/helper.ts b/packages/integration-tests/src/helper.ts new file mode 100644 index 000000000..3bb752191 --- /dev/null +++ b/packages/integration-tests/src/helper.ts @@ -0,0 +1,18 @@ +import { User } from '@logto/schemas'; + +import { authedAdminApi } from './api'; +import { generateUsername, generatePassword } from './utils'; + +export const createUser = () => { + const username = generateUsername(); + + return authedAdminApi + .post('users', { + json: { + username, + password: generatePassword(), + name: username, + }, + }) + .json(); +}; diff --git a/packages/integration-tests/tests/me.test.ts b/packages/integration-tests/tests/me.test.ts index d1fa4f027..ac4595c2a 100644 --- a/packages/integration-tests/tests/me.test.ts +++ b/packages/integration-tests/tests/me.test.ts @@ -1,21 +1,8 @@ -import { ArbitraryObject, User, UserInfo, userInfoSelectFields } from '@logto/schemas'; +import { ArbitraryObject, UserInfo, userInfoSelectFields } from '@logto/schemas'; -import api, { authedAdminApi } from '@/api'; -import { generatePassword, generateUsername } from '@/utils'; - -const createUser = async () => { - const username = generateUsername(); - - return authedAdminApi - .post('users', { - json: { - username, - password: generatePassword(), - name: username, - }, - }) - .json(); -}; +import api from '@/api'; +import { createUser } from '@/helper'; +import { generatePassword } from '@/utils'; describe('api `/me`', () => { it('should get user info successfully', async () => { diff --git a/packages/integration-tests/tests/users.test.ts b/packages/integration-tests/tests/users.test.ts new file mode 100644 index 000000000..a33cf4b32 --- /dev/null +++ b/packages/integration-tests/tests/users.test.ts @@ -0,0 +1,73 @@ +import { User } from '@logto/schemas'; + +import { authedAdminApi } from '@/api'; +import { createUser } from '@/helper'; + +describe('admin console user management', () => { + it('should create user successfully', async () => { + const user = await createUser(); + + const userDetails = await authedAdminApi.get(`users/${user.id}`).json(); + + expect(userDetails).toBeTruthy(); + expect(userDetails.id).toBe(user.id); + }); + + it('should get user list successfully', async () => { + await createUser(); + const users = await authedAdminApi.get('users').json(); + + expect(users.length).not.toBeLessThan(1); + }); + + it('should update userinfo successfully', async () => { + const user = await createUser(); + + const newUserData: Partial = { + name: 'new name', + avatar: 'https://new.avatar.com/avatar.png', + customData: { + level: 1, + }, + roleNames: ['admin'], + }; + + const updatedUser = await authedAdminApi + .patch(`users/${user.id}`, { + json: newUserData, + }) + .json(); + + expect(updatedUser).toMatchObject(newUserData); + }); + + it('should delete user successfully', async () => { + const user = await createUser(); + + const fetchedResponseBeforeDeletion = await authedAdminApi.get(`users/${user.id}`, { + throwHttpErrors: false, + }); + + expect(fetchedResponseBeforeDeletion.statusCode).toBe(200); + + await authedAdminApi.delete(`users/${user.id}`); + + const fetchedResponseAfterDeletion = await authedAdminApi.get(`users/${user.id}`, { + throwHttpErrors: false, + }); + + expect(fetchedResponseAfterDeletion.statusCode).toBe(404); + }); + + it('should update user password successfully', async () => { + const user = await createUser(); + const updatePasswordResponse = await authedAdminApi.patch(`users/${user.id}/password`, { + json: { + password: 'newPassword', + }, + throwHttpErrors: false, + }); + + expect(updatePasswordResponse.statusCode).toBe(200); + }); +});