0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00

feat(core): check username (#2290)

This commit is contained in:
wangsijie 2022-11-01 16:52:57 +08:00 committed by GitHub
parent a1ffc2491f
commit 386e021e76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 1 deletions

View file

@ -9,6 +9,7 @@ import { createRequester } from '@/utils/test-utils';
import passwordRoutes, { registerRoute, signInRoute } from './password';
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 updateUserById = jest.fn(async (..._args: unknown[]) => ({ id: 'id' }));
const hasActiveUsers = jest.fn(async () => true);
@ -20,7 +21,7 @@ jest.mock('@/queries/user', () => ({
findUserByPhone: async () => ({ id: 'id' }),
findUserByEmail: async () => ({ id: 'id' }),
updateUserById: async (...args: unknown[]) => updateUserById(...args),
hasUser: async (username: string) => username === 'username1',
hasUser: async (username: string) => hasUser(username),
hasUserWithIdentity: async (connectorId: string, userId: string) =>
connectorId === 'connectorId' && userId === 'id',
hasUserWithPhone: async (phone: string) => phone === '13000000000',
@ -243,4 +244,48 @@ describe('session -> password routes', () => {
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);
});
});
});

View file

@ -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(
`${registerRoute}/username`,
koaGuard({