From 982aa918e69519eff25953a4e30e20bb9fc83e76 Mon Sep 17 00:00:00 2001 From: wangsijie Date: Sun, 31 Mar 2024 17:39:25 +0800 Subject: [PATCH] test: add tests for username case sensitive (#5597) --- .../api/admin-user.username-case.test.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 packages/integration-tests/src/tests/api/admin-user.username-case.test.ts diff --git a/packages/integration-tests/src/tests/api/admin-user.username-case.test.ts b/packages/integration-tests/src/tests/api/admin-user.username-case.test.ts new file mode 100644 index 000000000..a2f4f34f0 --- /dev/null +++ b/packages/integration-tests/src/tests/api/admin-user.username-case.test.ts @@ -0,0 +1,40 @@ +import { type User } from '@logto/schemas'; + +import { authedAdminApi, deleteUser } from '#src/api/index.js'; +import { createUserByAdmin, expectRejects } from '#src/helpers/index.js'; +import { generateUsername } from '#src/utils.js'; + +const getUsers = async ( + init: string[][] | Record | URLSearchParams +): Promise<{ headers: Headers; json: T }> => { + const response = await authedAdminApi.get('users', { + searchParams: new URLSearchParams(init), + }); + + return { headers: response.headers, json: (await response.json()) as T }; +}; + +describe('admin console user management (username case sensitive)', () => { + const username = generateUsername(); + + it('should handle usernames case-sensitively', async () => { + const user = await createUserByAdmin({ username: username.toLowerCase() }); + const user2 = await createUserByAdmin({ username: username.toUpperCase() }); + await expectRejects(createUserByAdmin({ username: username.toUpperCase() }), { + code: 'user.username_already_in_use', + status: 422, + }); + await deleteUser(user.id); + await deleteUser(user2.id); + }); + + it('should be able to search by case-insensitively', async () => { + const user = await createUserByAdmin({ username: username.toLowerCase() }); + const user2 = await createUserByAdmin({ username: username.toUpperCase() }); + const { json } = await getUsers([['search', `%${username.toLowerCase()}%`]]); + expect(json[0]).toHaveProperty('username', username.toUpperCase()); + expect(json[1]).toHaveProperty('username', username.toLowerCase()); + await deleteUser(user.id); + await deleteUser(user2.id); + }); +});