mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
test(core): fix unit tests
This commit is contained in:
parent
b07252f9b2
commit
52c2222f4a
13 changed files with 56 additions and 22 deletions
|
@ -1,6 +1,8 @@
|
|||
import en from '@logto/phrases-ui/lib/locales/en.js';
|
||||
import { CustomPhrase } from '@logto/schemas';
|
||||
|
||||
import { mockId } from '#src/test-utils/nanoid.js';
|
||||
|
||||
export const enTag = 'en';
|
||||
export const trTrTag = 'tr-TR';
|
||||
export const zhCnTag = 'zh-CN';
|
||||
|
@ -8,7 +10,7 @@ export const zhHkTag = 'zh-HK';
|
|||
|
||||
export const mockEnCustomPhrase = {
|
||||
tenantId: 'fake_tenant',
|
||||
id: 'fake_id',
|
||||
id: mockId,
|
||||
languageTag: enTag,
|
||||
translation: {
|
||||
input: {
|
||||
|
@ -23,7 +25,7 @@ export const mockEnCustomPhrase = {
|
|||
|
||||
export const mockEnPhrase = {
|
||||
tenantId: 'fake_tenant',
|
||||
id: 'fake_id',
|
||||
id: mockId,
|
||||
languageTag: enTag,
|
||||
translation: {
|
||||
...en.translation,
|
||||
|
@ -33,7 +35,7 @@ export const mockEnPhrase = {
|
|||
|
||||
export const mockTrTrCustomPhrase = {
|
||||
tenantId: 'fake_tenant',
|
||||
id: 'fake_id',
|
||||
id: mockId,
|
||||
languageTag: trTrTag,
|
||||
translation: {
|
||||
input: {
|
||||
|
@ -48,7 +50,7 @@ export const mockTrTrCustomPhrase = {
|
|||
|
||||
export const mockZhCnCustomPhrase = {
|
||||
tenantId: 'fake_tenant',
|
||||
id: 'fake_id',
|
||||
id: mockId,
|
||||
languageTag: zhCnTag,
|
||||
translation: {
|
||||
input: {
|
||||
|
@ -63,7 +65,7 @@ export const mockZhCnCustomPhrase = {
|
|||
|
||||
export const mockZhHkCustomPhrase = {
|
||||
tenantId: 'fake_tenant',
|
||||
id: 'fake_id',
|
||||
id: mockId,
|
||||
languageTag: zhHkTag,
|
||||
translation: {
|
||||
input: {
|
||||
|
|
|
@ -73,6 +73,8 @@ it('should ignore empty string values from the custom phrase', async () => {
|
|||
findCustomPhraseByLanguageTag.mockResolvedValueOnce(mockEnCustomPhraseWithEmptyStringValues);
|
||||
await expect(getPhrases(enTag, [enTag])).resolves.toEqual(
|
||||
deepmerge(englishBuiltInPhrase, {
|
||||
id: 'fake_id',
|
||||
tenantId: 'fake_tenant',
|
||||
languageTag: enTag,
|
||||
translation: {
|
||||
input: {
|
||||
|
|
|
@ -191,7 +191,8 @@ describe('connector queries', () => {
|
|||
return createMockQueryResult([connector]);
|
||||
});
|
||||
|
||||
await expect(insertConnector(mockConnector)).resolves.toEqual(connector);
|
||||
const { tenantId, ...data } = mockConnector;
|
||||
await expect(insertConnector(data)).resolves.toEqual(connector);
|
||||
});
|
||||
|
||||
it('updateConnector (with id)', async () => {
|
||||
|
|
|
@ -125,7 +125,7 @@ describe('roles query', () => {
|
|||
const keys = excludeAutoSetFields(Roles.fieldKeys);
|
||||
|
||||
const expectSql = `
|
||||
insert into "roles" ("id", "name", "description")
|
||||
insert into "roles" ("tenant_id", "id", "name", "description")
|
||||
values (${keys.map((_, index) => `$${index + 1}`).join(', ')})
|
||||
returning *
|
||||
`;
|
||||
|
|
|
@ -21,7 +21,7 @@ const { findDefaultSignInExperience, updateDefaultSignInExperience } =
|
|||
describe('sign-in-experience query', () => {
|
||||
const id = 'default';
|
||||
|
||||
const dbvalue = {
|
||||
const databaseValue = {
|
||||
...mockSignInExperience,
|
||||
color: JSON.stringify(mockSignInExperience.color),
|
||||
branding: JSON.stringify(mockSignInExperience.branding),
|
||||
|
@ -35,7 +35,7 @@ describe('sign-in-experience query', () => {
|
|||
it('findDefaultSignInExperience', async () => {
|
||||
/* eslint-disable sql/no-unsafe-query */
|
||||
const expectSql = `
|
||||
select "id", "color", "branding", "language_info", "terms_of_use_url", "sign_in", "sign_up", "social_sign_in_connector_targets", "sign_in_mode"
|
||||
select "tenant_id", "id", "color", "branding", "language_info", "terms_of_use_url", "sign_in", "sign_up", "social_sign_in_connector_targets", "sign_in_mode"
|
||||
from "sign_in_experiences"
|
||||
where "id"=$1
|
||||
`;
|
||||
|
@ -45,10 +45,10 @@ describe('sign-in-experience query', () => {
|
|||
expectSqlAssert(sql, expectSql);
|
||||
expect(values).toEqual([id]);
|
||||
|
||||
return createMockQueryResult([dbvalue]);
|
||||
return createMockQueryResult([databaseValue]);
|
||||
});
|
||||
|
||||
await expect(findDefaultSignInExperience()).resolves.toEqual(dbvalue);
|
||||
await expect(findDefaultSignInExperience()).resolves.toEqual(databaseValue);
|
||||
});
|
||||
|
||||
it('updateDefaultSignInExperience', async () => {
|
||||
|
@ -67,9 +67,9 @@ describe('sign-in-experience query', () => {
|
|||
expectSqlAssert(sql, expectSql);
|
||||
expect(values).toEqual([termsOfUseUrl, id]);
|
||||
|
||||
return createMockQueryResult([dbvalue]);
|
||||
return createMockQueryResult([databaseValue]);
|
||||
});
|
||||
|
||||
await expect(updateDefaultSignInExperience({ termsOfUseUrl })).resolves.toEqual(dbvalue);
|
||||
await expect(updateDefaultSignInExperience({ termsOfUseUrl })).resolves.toEqual(databaseValue);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
import { pickDefault } from '@logto/shared/esm';
|
||||
|
||||
import { mockRole, mockUser } from '#src/__mocks__/index.js';
|
||||
import { mockId, mockStandardId } from '#src/test-utils/nanoid.js';
|
||||
import { MockTenant } from '#src/test-utils/tenant.js';
|
||||
import { createRequester } from '#src/utils/test-utils.js';
|
||||
|
||||
const { jest } = import.meta;
|
||||
|
||||
await mockStandardId();
|
||||
|
||||
const users = { findUserById: jest.fn() };
|
||||
|
||||
const roles = {
|
||||
|
@ -14,7 +17,6 @@ const roles = {
|
|||
countRoles: jest.fn(async () => ({ count: 1 })),
|
||||
findRoles: jest.fn(async () => [mockRole]),
|
||||
};
|
||||
const { findRolesByRoleIds } = roles;
|
||||
|
||||
const usersRoles = {
|
||||
findUsersRolesByUserId: jest.fn(),
|
||||
|
@ -43,7 +45,9 @@ describe('user role routes', () => {
|
|||
roleIds: [mockRole.id],
|
||||
});
|
||||
expect(response.status).toEqual(201);
|
||||
expect(insertUsersRoles).toHaveBeenCalledWith([{ userId: mockUser.id, roleId: mockRole.id }]);
|
||||
expect(insertUsersRoles).toHaveBeenCalledWith([
|
||||
{ id: mockId, userId: mockUser.id, roleId: mockRole.id },
|
||||
]);
|
||||
});
|
||||
|
||||
it('DELETE /users/:id/roles/:roleId', async () => {
|
||||
|
|
|
@ -6,12 +6,15 @@ import { pickDefault, createMockUtils } from '@logto/shared/esm';
|
|||
import { mockZhCnCustomPhrase, trTrTag, zhCnTag } from '#src/__mocks__/custom-phrase.js';
|
||||
import { mockSignInExperience } from '#src/__mocks__/index.js';
|
||||
import RequestError from '#src/errors/RequestError/index.js';
|
||||
import { mockId, mockStandardId } from '#src/test-utils/nanoid.js';
|
||||
import { MockTenant } from '#src/test-utils/tenant.js';
|
||||
import { createRequester } from '#src/utils/test-utils.js';
|
||||
|
||||
const { jest } = import.meta;
|
||||
const { mockEsm } = createMockUtils(jest);
|
||||
|
||||
await mockStandardId();
|
||||
|
||||
const mockLanguageTag = zhCnTag;
|
||||
const mockPhrase = mockZhCnCustomPhrase;
|
||||
const mockCustomPhrases: Record<string, CustomPhrase> = {
|
||||
|
@ -126,6 +129,7 @@ describe('customPhraseRoutes', () => {
|
|||
input: { ...inputTranslation, password: '' },
|
||||
});
|
||||
expect(upsertCustomPhrase).toBeCalledWith({
|
||||
id: mockId,
|
||||
languageTag: mockLanguageTag,
|
||||
translation: { input: inputTranslation },
|
||||
});
|
||||
|
@ -146,7 +150,9 @@ describe('customPhraseRoutes', () => {
|
|||
|
||||
it('should call upsertCustomPhrase with specified language tag', async () => {
|
||||
await customPhraseRequest.put(`/custom-phrases/${mockLanguageTag}`).send(translation);
|
||||
expect(upsertCustomPhrase).toBeCalledWith(mockCustomPhrases[mockLanguageTag]);
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
const { tenantId, ...phrase } = mockCustomPhrases[mockLanguageTag]!;
|
||||
expect(upsertCustomPhrase).toBeCalledWith(phrase);
|
||||
});
|
||||
|
||||
it('should return custom phrase after upserting', async () => {
|
||||
|
|
|
@ -90,6 +90,7 @@ describe('resource routes', () => {
|
|||
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.body).toEqual({
|
||||
tenantId: 'fake_tenant',
|
||||
id: 'randomId',
|
||||
name,
|
||||
indicator,
|
||||
|
|
|
@ -2,11 +2,14 @@ import type { Role } from '@logto/schemas';
|
|||
import { pickDefault } from '@logto/shared/esm';
|
||||
|
||||
import { mockRole, mockScope, mockResource } from '#src/__mocks__/index.js';
|
||||
import { mockId, mockStandardId } from '#src/test-utils/nanoid.js';
|
||||
import { MockTenant } from '#src/test-utils/tenant.js';
|
||||
import { createRequester } from '#src/utils/test-utils.js';
|
||||
|
||||
const { jest } = import.meta;
|
||||
|
||||
await mockStandardId();
|
||||
|
||||
const roles = {
|
||||
findRoles: jest.fn(async (): Promise<Role[]> => [mockRole]),
|
||||
countRoles: jest.fn(async () => ({ count: 10 })),
|
||||
|
@ -77,7 +80,7 @@ describe('role scope routes', () => {
|
|||
});
|
||||
expect(response.status).toEqual(200);
|
||||
expect(insertRolesScopes).toHaveBeenCalledWith([
|
||||
{ roleId: mockRole.id, scopeId: mockScope.id },
|
||||
{ id: mockId, roleId: mockRole.id, scopeId: mockScope.id },
|
||||
]);
|
||||
});
|
||||
|
||||
|
|
|
@ -2,11 +2,14 @@ import type { Role } from '@logto/schemas';
|
|||
import { pickDefault } from '@logto/shared/esm';
|
||||
|
||||
import { mockRole, mockScope, mockUser, mockResource } from '#src/__mocks__/index.js';
|
||||
import { mockId, mockStandardId } from '#src/test-utils/nanoid.js';
|
||||
import { MockTenant } from '#src/test-utils/tenant.js';
|
||||
import { createRequester } from '#src/utils/test-utils.js';
|
||||
|
||||
const { jest } = import.meta;
|
||||
|
||||
await mockStandardId();
|
||||
|
||||
const roles = {
|
||||
findRoles: jest.fn(async (): Promise<Role[]> => [mockRole]),
|
||||
countRoles: jest.fn(async () => ({ count: 10 })),
|
||||
|
@ -15,12 +18,14 @@ const roles = {
|
|||
insertRole: jest.fn(async (data) => ({
|
||||
...data,
|
||||
id: mockRole.id,
|
||||
tenantId: 'fake_tenant',
|
||||
})),
|
||||
deleteRoleById: jest.fn(),
|
||||
findRoleById: jest.fn(),
|
||||
updateRoleById: jest.fn(async (id, data) => ({
|
||||
...mockRole,
|
||||
...data,
|
||||
tenantId: 'fake_tenant',
|
||||
})),
|
||||
findRolesByRoleIds: jest.fn(),
|
||||
};
|
||||
|
@ -172,7 +177,9 @@ describe('role routes', () => {
|
|||
userIds: [mockUser.id],
|
||||
});
|
||||
expect(response.status).toEqual(201);
|
||||
expect(insertUsersRoles).toHaveBeenCalledWith([{ userId: mockUser.id, roleId: mockRole.id }]);
|
||||
expect(insertUsersRoles).toHaveBeenCalledWith([
|
||||
{ id: mockId, userId: mockUser.id, roleId: mockRole.id },
|
||||
]);
|
||||
});
|
||||
|
||||
it('DELETE /roles/:id/users/:userId', async () => {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { buildIdGenerator, generateStandardId } from '@logto/core-kit';
|
||||
import { generateStandardId } from '@logto/core-kit';
|
||||
import type { RoleResponse } from '@logto/schemas';
|
||||
import { userInfoSelectFields, Roles } from '@logto/schemas';
|
||||
import { tryThat } from '@logto/shared';
|
||||
|
@ -13,8 +13,6 @@ import { parseSearchParamsForSearch } from '#src/utils/search.js';
|
|||
|
||||
import type { AuthedRouter, RouterInitArgs } from './types.js';
|
||||
|
||||
const roleId = buildIdGenerator(21);
|
||||
|
||||
export default function roleRoutes<T extends AuthedRouter>(
|
||||
...[router, { queries }]: RouterInitArgs<T>
|
||||
) {
|
||||
|
@ -108,7 +106,7 @@ export default function roleRoutes<T extends AuthedRouter>(
|
|||
|
||||
const role = await insertRole({
|
||||
...roleBody,
|
||||
id: roleId(),
|
||||
id: generateStandardId(),
|
||||
});
|
||||
|
||||
if (scopeIds) {
|
||||
|
|
|
@ -37,6 +37,7 @@ describe('settings routes', () => {
|
|||
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.body).toEqual({
|
||||
tenantId: 'fake_tenant',
|
||||
adminConsole,
|
||||
});
|
||||
});
|
||||
|
|
9
packages/core/src/test-utils/nanoid.ts
Normal file
9
packages/core/src/test-utils/nanoid.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { createMockUtils } from '@logto/shared/esm';
|
||||
|
||||
const { mockEsmWithActual } = createMockUtils(import.meta.jest);
|
||||
|
||||
export const mockId = 'mockId';
|
||||
export const mockStandardId = async () =>
|
||||
mockEsmWithActual('@logto/core-kit', () => ({
|
||||
generateStandardId: () => mockId,
|
||||
}));
|
Loading…
Reference in a new issue