mirror of
https://github.com/logto-io/logto.git
synced 2025-01-20 21:32:31 -05:00
feat(core): check username (#2290)
This commit is contained in:
parent
a1ffc2491f
commit
386e021e76
2 changed files with 79 additions and 1 deletions
|
@ -9,6 +9,7 @@ import { createRequester } from '@/utils/test-utils';
|
||||||
import passwordRoutes, { registerRoute, signInRoute } from './password';
|
import passwordRoutes, { registerRoute, signInRoute } from './password';
|
||||||
|
|
||||||
const insertUser = jest.fn(async (..._args: unknown[]) => ({ id: 'id' }));
|
const insertUser = jest.fn(async (..._args: unknown[]) => ({ id: 'id' }));
|
||||||
|
const hasUser = jest.fn(async (username: string) => username === 'username1');
|
||||||
const findUserById = jest.fn(async (): Promise<User> => mockUser);
|
const findUserById = jest.fn(async (): Promise<User> => mockUser);
|
||||||
const updateUserById = jest.fn(async (..._args: unknown[]) => ({ id: 'id' }));
|
const updateUserById = jest.fn(async (..._args: unknown[]) => ({ id: 'id' }));
|
||||||
const hasActiveUsers = jest.fn(async () => true);
|
const hasActiveUsers = jest.fn(async () => true);
|
||||||
|
@ -20,7 +21,7 @@ jest.mock('@/queries/user', () => ({
|
||||||
findUserByPhone: async () => ({ id: 'id' }),
|
findUserByPhone: async () => ({ id: 'id' }),
|
||||||
findUserByEmail: async () => ({ id: 'id' }),
|
findUserByEmail: async () => ({ id: 'id' }),
|
||||||
updateUserById: async (...args: unknown[]) => updateUserById(...args),
|
updateUserById: async (...args: unknown[]) => updateUserById(...args),
|
||||||
hasUser: async (username: string) => username === 'username1',
|
hasUser: async (username: string) => hasUser(username),
|
||||||
hasUserWithIdentity: async (connectorId: string, userId: string) =>
|
hasUserWithIdentity: async (connectorId: string, userId: string) =>
|
||||||
connectorId === 'connectorId' && userId === 'id',
|
connectorId === 'connectorId' && userId === 'id',
|
||||||
hasUserWithPhone: async (phone: string) => phone === '13000000000',
|
hasUserWithPhone: async (phone: string) => phone === '13000000000',
|
||||||
|
@ -243,4 +244,48 @@ describe('session -> password routes', () => {
|
||||||
expect(response.statusCode).toEqual(422);
|
expect(response.statusCode).toEqual(422);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('POST /session/register/password/check-username', () => {
|
||||||
|
it('check and return empty', async () => {
|
||||||
|
interactionDetails.mockResolvedValueOnce({ params: {} });
|
||||||
|
|
||||||
|
const response = await sessionRequest
|
||||||
|
.post(`${registerRoute}/check-username`)
|
||||||
|
.send({ username: 'username' });
|
||||||
|
expect(response.status).toEqual(204);
|
||||||
|
expect(hasUser).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throw error if username not valid', async () => {
|
||||||
|
const usernameStartedWithNumber = '1username';
|
||||||
|
const response = await sessionRequest
|
||||||
|
.post(`${registerRoute}/check-username`)
|
||||||
|
.send({ username: usernameStartedWithNumber, password: 'password' });
|
||||||
|
expect(response.statusCode).toEqual(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throw error if username exists', async () => {
|
||||||
|
const response = await sessionRequest
|
||||||
|
.post(`${registerRoute}/check-username`)
|
||||||
|
.send({ username: 'username1' });
|
||||||
|
expect(response.statusCode).toEqual(422);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws if sign up identifier is not username', async () => {
|
||||||
|
interactionDetails.mockResolvedValueOnce({ params: {} });
|
||||||
|
|
||||||
|
findDefaultSignInExperience.mockResolvedValueOnce({
|
||||||
|
...mockSignInExperience,
|
||||||
|
signUp: {
|
||||||
|
...mockSignInExperience.signUp,
|
||||||
|
identifier: SignUpIdentifier.Email,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await sessionRequest
|
||||||
|
.post(`${registerRoute}/check-username`)
|
||||||
|
.send({ username: 'username' });
|
||||||
|
expect(response.statusCode).toEqual(422);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -94,6 +94,39 @@ export default function passwordRoutes<T extends AnonymousRouter>(router: T, pro
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
router.post(
|
||||||
|
`${registerRoute}/check-username`,
|
||||||
|
koaGuard({
|
||||||
|
body: object({
|
||||||
|
username: string().regex(usernameRegEx),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
async (ctx, next) => {
|
||||||
|
const { username } = ctx.guard.body;
|
||||||
|
|
||||||
|
const signInExperience = await findDefaultSignInExperience();
|
||||||
|
assertThat(
|
||||||
|
signInExperience.signUp.identifier === SignUpIdentifier.Username,
|
||||||
|
new RequestError({
|
||||||
|
code: 'user.sign_up_method_not_enabled',
|
||||||
|
status: 422,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThat(
|
||||||
|
!(await hasUser(username)),
|
||||||
|
new RequestError({
|
||||||
|
code: 'user.username_exists_register',
|
||||||
|
status: 422,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
ctx.status = 204;
|
||||||
|
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
`${registerRoute}/username`,
|
`${registerRoute}/username`,
|
||||||
koaGuard({
|
koaGuard({
|
||||||
|
|
Loading…
Add table
Reference in a new issue