0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

test: integration tests for users (#1640)

This commit is contained in:
Xiao Yijun 2022-07-22 10:50:21 +08:00 committed by GitHub
parent dec85378de
commit 37e7807b4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 95 additions and 17 deletions

View file

@ -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<User>();
};

View file

@ -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<User>();
};
import api from '@/api';
import { createUser } from '@/helper';
import { generatePassword } from '@/utils';
describe('api `/me`', () => {
it('should get user info successfully', async () => {

View file

@ -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<User>();
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<User[]>();
expect(users.length).not.toBeLessThan(1);
});
it('should update userinfo successfully', async () => {
const user = await createUser();
const newUserData: Partial<User> = {
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<User>();
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);
});
});