From 04ba181a64e8cc11f1ed5da6208a21aed780a1da Mon Sep 17 00:00:00 2001 From: "IceHe.xyz" Date: Wed, 23 Mar 2022 18:54:26 +0800 Subject: [PATCH] fix(core): missing some commits (#444) --- .../src/queries/sign-in-experience.test.ts | 41 ++++++++++--------- .../core/src/queries/sign-in-experience.ts | 9 ++-- .../src/routes/sign-in-experience.test.ts | 12 +++--- .../core/src/routes/sign-in-experience.ts | 14 ++----- 4 files changed, 37 insertions(+), 39 deletions(-) diff --git a/packages/core/src/queries/sign-in-experience.test.ts b/packages/core/src/queries/sign-in-experience.test.ts index d81f6b234..9ac66d87c 100644 --- a/packages/core/src/queries/sign-in-experience.test.ts +++ b/packages/core/src/queries/sign-in-experience.test.ts @@ -1,11 +1,9 @@ -import { SignInExperiences } from '@logto/schemas'; -import { createMockPool, createMockQueryResult, sql } from 'slonik'; +import { createMockPool, createMockQueryResult } from 'slonik'; -import { convertToIdentifiers } from '@/database/utils'; import { mockSignInExperience } from '@/utils/mock'; import { expectSqlAssert, QueryType } from '@/utils/test-utils'; -import { findDefaultSignInExperience, updateSignInExperienceById } from './sign-in-experience'; +import { findDefaultSignInExperience, updateDefaultSignInExperience } from './sign-in-experience'; const mockQuery: jest.MockedFunction = jest.fn(); @@ -18,7 +16,8 @@ jest.mock('@/database/pool', () => ); describe('sign-in-experience query', () => { - const { table, fields } = convertToIdentifiers(SignInExperiences); + const id = 'default'; + const dbvalue = { ...mockSignInExperience, branding: JSON.stringify(mockSignInExperience.branding), @@ -29,14 +28,17 @@ describe('sign-in-experience query', () => { }; it('findDefaultSignInExperience', async () => { - const expectSql = sql` - select ${sql.join(Object.values(fields), sql`, `)} - from ${table} + /* eslint-disable sql/no-unsafe-query */ + const expectSql = ` + select "id", "branding", "language_info", "terms_of_use", "forget_password_enabled", "sign_in_methods", "social_sign_in_connector_ids" + from "sign_in_experiences" + where "id" = $1 `; + /* eslint-enable sql/no-unsafe-query */ mockQuery.mockImplementationOnce(async (sql, values) => { - expectSqlAssert(sql, expectSql.sql); - expect(values).toEqual([]); + expectSqlAssert(sql, expectSql); + expect(values).toEqual([id]); return createMockQueryResult([dbvalue]); }); @@ -44,28 +46,29 @@ describe('sign-in-experience query', () => { await expect(findDefaultSignInExperience()).resolves.toEqual(dbvalue); }); - it('updateSignInExperienceById', async () => { - const id = 'foo'; + it('updateDefaultSignInExperience', async () => { const termsOfUse = { enabled: false, }; - const expectSql = sql` - update ${table} + /* eslint-disable sql/no-unsafe-query */ + const expectSql = ` + update "sign_in_experiences" set - ${fields.termsOfUse}= - coalesce(${fields.termsOfUse},'{}'::jsonb)|| $1 - where ${fields.id}=$2 + "terms_of_use"= + coalesce("terms_of_use",'{}'::jsonb)|| $1 + where "id"=$2 returning * `; + /* eslint-enable sql/no-unsafe-query */ mockQuery.mockImplementationOnce(async (sql, values) => { - expectSqlAssert(sql, expectSql.sql); + expectSqlAssert(sql, expectSql); expect(values).toEqual([JSON.stringify(termsOfUse), id]); return createMockQueryResult([dbvalue]); }); - await expect(updateSignInExperienceById(id, { termsOfUse })).resolves.toEqual(dbvalue); + await expect(updateDefaultSignInExperience({ termsOfUse })).resolves.toEqual(dbvalue); }); }); diff --git a/packages/core/src/queries/sign-in-experience.ts b/packages/core/src/queries/sign-in-experience.ts index 362812670..2b6a3a5b6 100644 --- a/packages/core/src/queries/sign-in-experience.ts +++ b/packages/core/src/queries/sign-in-experience.ts @@ -13,13 +13,14 @@ const updateSignInExperience = buildUpdateWhere -) => updateSignInExperience({ set, where: { id } }); +const id = 'default'; + +export const updateDefaultSignInExperience = async (set: Partial) => + updateSignInExperience({ set, where: { id } }); export const findDefaultSignInExperience = async () => pool.one(sql` select ${sql.join(Object.values(fields), sql`, `)} from ${table} + where ${fields.id} = ${id} `); diff --git a/packages/core/src/routes/sign-in-experience.test.ts b/packages/core/src/routes/sign-in-experience.test.ts index 7043c93f5..0fe5872bc 100644 --- a/packages/core/src/routes/sign-in-experience.test.ts +++ b/packages/core/src/routes/sign-in-experience.test.ts @@ -12,8 +12,8 @@ jest.mock('@/connectors', () => ({ jest.mock('@/queries/sign-in-experience', () => ({ findDefaultSignInExperience: jest.fn(async (): Promise => mockSignInExperience), - updateSignInExperienceById: jest.fn( - async (_, data: Partial): Promise => ({ + updateDefaultSignInExperience: jest.fn( + async (data: Partial): Promise => ({ ...mockSignInExperience, ...data, }) @@ -29,7 +29,7 @@ describe('signInExperiences routes', () => { expect(response.body).toEqual(mockSignInExperience); }); - it('PATCH /sign-in-exp/:id', async () => { + it('PATCH /sign-in-exp', async () => { const branding: Branding = { primaryColor: '#000', backgroundColor: '#fff', @@ -43,7 +43,7 @@ describe('signInExperiences routes', () => { const socialSignInConnectorIds = ['abc', 'def']; - const response = await signInExperienceRequester.patch('/sign-in-exp/default').send({ + const response = await signInExperienceRequester.patch('/sign-in-exp').send({ branding, socialSignInConnectorIds, }); @@ -56,10 +56,10 @@ describe('signInExperiences routes', () => { }); }); - it('PATCH /sign-in-exp/:id should throw with invalid inputs', async () => { + it('PATCH /sign-in-exp should throw with invalid inputs', async () => { const socialSignInConnectorIds = [123, 456]; - const response = await signInExperienceRequester.patch('/sign-in-exp/default').send({ + const response = await signInExperienceRequester.patch('/sign-in-exp').send({ socialSignInConnectorIds, }); expect(response.status).toEqual(400); diff --git a/packages/core/src/routes/sign-in-experience.ts b/packages/core/src/routes/sign-in-experience.ts index bd50da15b..09e08f9b2 100644 --- a/packages/core/src/routes/sign-in-experience.ts +++ b/packages/core/src/routes/sign-in-experience.ts @@ -1,11 +1,10 @@ import { SignInExperiences } from '@logto/schemas'; -import { object, string } from 'zod'; import { getEnabledSocialConnectorIds } from '@/connectors'; import koaGuard from '@/middleware/koa-guard'; import { findDefaultSignInExperience, - updateSignInExperienceById, + updateDefaultSignInExperience, } from '@/queries/sign-in-experience'; import { AuthedRouter } from './types'; @@ -36,20 +35,15 @@ export default function signInExperiencesRoutes(router: return next(); }); - // TODO: LOG-1403 need to find a way to validate SignInMethod input router.patch( - '/sign-in-exp/:id', + '/sign-in-exp', koaGuard({ - params: object({ id: string().min(1) }), body: SignInExperiences.createGuard.omit({ id: true }).partial(), }), async (ctx, next) => { - const { - params: { id }, - body, - } = ctx.guard; + const { body } = ctx.guard; - ctx.body = await updateSignInExperienceById(id, { + ctx.body = await updateDefaultSignInExperience({ ...body, });