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