diff --git a/packages/core/src/__mocks__/custom-phrase.ts b/packages/core/src/__mocks__/custom-phrase.ts index b1e7cf467..c367babd2 100644 --- a/packages/core/src/__mocks__/custom-phrase.ts +++ b/packages/core/src/__mocks__/custom-phrase.ts @@ -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: { diff --git a/packages/core/src/libraries/phrase.test.ts b/packages/core/src/libraries/phrase.test.ts index e3100490f..f9faea10f 100644 --- a/packages/core/src/libraries/phrase.test.ts +++ b/packages/core/src/libraries/phrase.test.ts @@ -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: { diff --git a/packages/core/src/queries/connector.test.ts b/packages/core/src/queries/connector.test.ts index bb82d241a..8a66f13f0 100644 --- a/packages/core/src/queries/connector.test.ts +++ b/packages/core/src/queries/connector.test.ts @@ -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 () => { diff --git a/packages/core/src/queries/roles.test.ts b/packages/core/src/queries/roles.test.ts index e7376dbeb..f1e7fffe1 100644 --- a/packages/core/src/queries/roles.test.ts +++ b/packages/core/src/queries/roles.test.ts @@ -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 * `; diff --git a/packages/core/src/queries/sign-in-experience.test.ts b/packages/core/src/queries/sign-in-experience.test.ts index 1f64c06d2..51e5048ac 100644 --- a/packages/core/src/queries/sign-in-experience.test.ts +++ b/packages/core/src/queries/sign-in-experience.test.ts @@ -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); }); }); diff --git a/packages/core/src/routes/admin-user-role.test.ts b/packages/core/src/routes/admin-user-role.test.ts index 2b1853a74..6122a767a 100644 --- a/packages/core/src/routes/admin-user-role.test.ts +++ b/packages/core/src/routes/admin-user-role.test.ts @@ -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 () => { diff --git a/packages/core/src/routes/custom-phrase.test.ts b/packages/core/src/routes/custom-phrase.test.ts index 87b07ff14..03760168b 100644 --- a/packages/core/src/routes/custom-phrase.test.ts +++ b/packages/core/src/routes/custom-phrase.test.ts @@ -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 = { @@ -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 () => { diff --git a/packages/core/src/routes/resource.test.ts b/packages/core/src/routes/resource.test.ts index d72b064eb..1829f3aac 100644 --- a/packages/core/src/routes/resource.test.ts +++ b/packages/core/src/routes/resource.test.ts @@ -90,6 +90,7 @@ describe('resource routes', () => { expect(response.status).toEqual(200); expect(response.body).toEqual({ + tenantId: 'fake_tenant', id: 'randomId', name, indicator, diff --git a/packages/core/src/routes/role.scope.test.ts b/packages/core/src/routes/role.scope.test.ts index 50f7ef108..5a9056401 100644 --- a/packages/core/src/routes/role.scope.test.ts +++ b/packages/core/src/routes/role.scope.test.ts @@ -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 => [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 }, ]); }); diff --git a/packages/core/src/routes/role.test.ts b/packages/core/src/routes/role.test.ts index cbbc7e703..5c64dafe5 100644 --- a/packages/core/src/routes/role.test.ts +++ b/packages/core/src/routes/role.test.ts @@ -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 => [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 () => { diff --git a/packages/core/src/routes/role.ts b/packages/core/src/routes/role.ts index 45453d548..2e51cfd4e 100644 --- a/packages/core/src/routes/role.ts +++ b/packages/core/src/routes/role.ts @@ -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( ...[router, { queries }]: RouterInitArgs ) { @@ -108,7 +106,7 @@ export default function roleRoutes( const role = await insertRole({ ...roleBody, - id: roleId(), + id: generateStandardId(), }); if (scopeIds) { diff --git a/packages/core/src/routes/setting.test.ts b/packages/core/src/routes/setting.test.ts index 3065c29d7..216b5fb1b 100644 --- a/packages/core/src/routes/setting.test.ts +++ b/packages/core/src/routes/setting.test.ts @@ -37,6 +37,7 @@ describe('settings routes', () => { expect(response.status).toEqual(200); expect(response.body).toEqual({ + tenantId: 'fake_tenant', adminConsole, }); }); diff --git a/packages/core/src/test-utils/nanoid.ts b/packages/core/src/test-utils/nanoid.ts new file mode 100644 index 000000000..4e719e802 --- /dev/null +++ b/packages/core/src/test-utils/nanoid.ts @@ -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, + }));