From ec3fa4715dc1261b06b74d6ab051b931cd1e6f2c Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Mon, 26 Sep 2022 11:28:18 +0800 Subject: [PATCH] chore(core): remove unnecessary updateLastSignInAt (#1982) --- packages/core/package.json | 2 +- packages/core/src/lib/user.test.ts | 4 ++-- packages/core/src/lib/user.ts | 5 +---- .../core/src/routes/session/passwordless.test.ts | 1 - packages/core/src/routes/session/passwordless.ts | 13 ++++++------- packages/core/src/routes/session/social.test.ts | 1 - packages/core/src/routes/session/social.ts | 8 ++++---- .../src/routes/session/username-password.test.ts | 5 +++++ .../core/src/routes/session/username-password.ts | 7 +++---- 9 files changed, 22 insertions(+), 24 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 92e818b79..86f0d49ee 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -17,7 +17,7 @@ "add-connector": "node build/cli/add-connector.js", "add-official-connectors": "node build/cli/add-official-connectors.js", "migration-deploy": "node build/cli/migration-deploy.js", - "test": "jest", + "test": "jest --testPathIgnorePatterns=/core/connectors/", "test:coverage": "jest --coverage --silent", "test:report": "codecov -F core" }, diff --git a/packages/core/src/lib/user.test.ts b/packages/core/src/lib/user.test.ts index c631671b1..75be699c2 100644 --- a/packages/core/src/lib/user.test.ts +++ b/packages/core/src/lib/user.test.ts @@ -2,7 +2,7 @@ import { UsersPasswordEncryptionMethod } from '@logto/schemas'; import { hasUserWithId, updateUserById } from '@/queries/user'; -import { encryptUserPassword, generateUserId, updateLastSignInAt } from './user'; +import { encryptUserPassword, generateUserId } from './user'; jest.mock('@/queries/user'); @@ -66,7 +66,7 @@ describe('updateLastSignIn()', () => { }); it('calls updateUserById with current timestamp', async () => { - await updateLastSignInAt('user-id'); + await updateUserById('user-id', { lastSignInAt: Date.now() }); expect(updateUserById).toHaveBeenCalledWith( 'user-id', expect.objectContaining({ lastSignInAt: new Date('2020-01-01').getTime() }) diff --git a/packages/core/src/lib/user.ts b/packages/core/src/lib/user.ts index ea79c1d9e..0ba447b55 100644 --- a/packages/core/src/lib/user.ts +++ b/packages/core/src/lib/user.ts @@ -5,7 +5,7 @@ import pRetry from 'p-retry'; import { buildInsertInto } from '@/database/insert-into'; import envSet from '@/env-set'; import { findRolesByRoleNames, insertRoles } from '@/queries/roles'; -import { findUserByUsername, hasUserWithId, updateUserById } from '@/queries/user'; +import { findUserByUsername, hasUserWithId } from '@/queries/user'; import assertThat from '@/utils/assert-that'; import { buildIdGenerator } from '@/utils/id'; import { encryptPassword } from '@/utils/password'; @@ -59,9 +59,6 @@ export const findUserByUsernameAndPassword = async ( return user; }; -export const updateLastSignInAt = async (userId: string) => - updateUserById(userId, { lastSignInAt: Date.now() }); - const insertUserQuery = buildInsertInto(Users, { returning: true, }); diff --git a/packages/core/src/routes/session/passwordless.test.ts b/packages/core/src/routes/session/passwordless.test.ts index a2cedc0c8..afca44857 100644 --- a/packages/core/src/routes/session/passwordless.test.ts +++ b/packages/core/src/routes/session/passwordless.test.ts @@ -13,7 +13,6 @@ const updateUserById = jest.fn(async (..._args: unknown[]) => ({ id: 'id' })); jest.mock('@/lib/user', () => ({ generateUserId: () => 'user1', - updateLastSignInAt: async (...args: unknown[]) => updateUserById(...args), insertUser: async (...args: unknown[]) => insertUser(...args), })); diff --git a/packages/core/src/routes/session/passwordless.ts b/packages/core/src/routes/session/passwordless.ts index 01583bf48..ec7b9a0d2 100644 --- a/packages/core/src/routes/session/passwordless.ts +++ b/packages/core/src/routes/session/passwordless.ts @@ -6,9 +6,10 @@ import { object, string } from 'zod'; import RequestError from '@/errors/RequestError'; import { createPasscode, sendPasscode, verifyPasscode } from '@/lib/passcode'; import { assignInteractionResults } from '@/lib/session'; -import { generateUserId, insertUser, updateLastSignInAt } from '@/lib/user'; +import { generateUserId, insertUser } from '@/lib/user'; import koaGuard from '@/middleware/koa-guard'; import { + updateUserById, hasUserWithEmail, hasUserWithPhone, findUserByEmail, @@ -67,7 +68,7 @@ export default function passwordlessRoutes( const { id } = await findUserByPhone(phone); ctx.log(type, { userId: id }); - await updateLastSignInAt(id); + await updateUserById(id, { lastSignInAt: Date.now() }); await assignInteractionResults(ctx, provider, { login: { accountId: id } }, true); return next(); @@ -115,7 +116,7 @@ export default function passwordlessRoutes( const { id } = await findUserByEmail(email); ctx.log(type, { userId: id }); - await updateLastSignInAt(id); + await updateUserById(id, { lastSignInAt: Date.now() }); await assignInteractionResults(ctx, provider, { login: { accountId: id } }, true); return next(); @@ -163,8 +164,7 @@ export default function passwordlessRoutes( const id = await generateUserId(); ctx.log(type, { userId: id }); - await insertUser({ id, primaryPhone: phone }); - await updateLastSignInAt(id); + await insertUser({ id, primaryPhone: phone, lastSignInAt: Date.now() }); await assignInteractionResults(ctx, provider, { login: { accountId: id } }); return next(); @@ -212,8 +212,7 @@ export default function passwordlessRoutes( const id = await generateUserId(); ctx.log(type, { userId: id }); - await insertUser({ id, primaryEmail: email }); - await updateLastSignInAt(id); + await insertUser({ id, primaryEmail: email, lastSignInAt: Date.now() }); await assignInteractionResults(ctx, provider, { login: { accountId: id } }); return next(); diff --git a/packages/core/src/routes/session/social.test.ts b/packages/core/src/routes/session/social.test.ts index 28c5ff837..0724b771f 100644 --- a/packages/core/src/routes/session/social.test.ts +++ b/packages/core/src/routes/session/social.test.ts @@ -46,7 +46,6 @@ jest.mock('@/queries/user', () => ({ jest.mock('@/lib/user', () => ({ generateUserId: () => 'user1', - updateLastSignInAt: async (...args: unknown[]) => updateUserById(...args), insertUser: async (...args: unknown[]) => insertUser(...args), })); diff --git a/packages/core/src/routes/session/social.ts b/packages/core/src/routes/session/social.ts index e61951fba..f5a8ceed1 100644 --- a/packages/core/src/routes/session/social.ts +++ b/packages/core/src/routes/session/social.ts @@ -12,7 +12,7 @@ import { getUserInfoByAuthCode, getUserInfoFromInteractionResult, } from '@/lib/social'; -import { generateUserId, insertUser, updateLastSignInAt } from '@/lib/user'; +import { generateUserId, insertUser } from '@/lib/user'; import koaGuard from '@/middleware/koa-guard'; import { hasUserWithIdentity, @@ -98,8 +98,8 @@ export default function socialRoutes(router: T, provi // Update social connector's user info await updateUserById(id, { identities: { ...identities, [target]: { userId: userInfo.id, details: userInfo } }, + lastSignInAt: Date.now(), }); - await updateLastSignInAt(id); await assignInteractionResults(ctx, provider, { login: { accountId: id } }); return next(); @@ -133,8 +133,8 @@ export default function socialRoutes(router: T, provi await updateUserById(id, { identities: { ...identities, [target]: { userId: userInfo.id, details: userInfo } }, + lastSignInAt: Date.now(), }); - await updateLastSignInAt(id); await assignInteractionResults(ctx, provider, { login: { accountId: id } }); return next(); @@ -177,10 +177,10 @@ export default function socialRoutes(router: T, provi details: userInfo, }, }, + lastSignInAt: Date.now(), }); ctx.log(type, { userId: id }); - await updateLastSignInAt(id); await assignInteractionResults(ctx, provider, { login: { accountId: id } }); return next(); diff --git a/packages/core/src/routes/session/username-password.test.ts b/packages/core/src/routes/session/username-password.test.ts index e5491883e..4684c5eb2 100644 --- a/packages/core/src/routes/session/username-password.test.ts +++ b/packages/core/src/routes/session/username-password.test.ts @@ -139,6 +139,9 @@ describe('sessionRoutes', () => { it('assign result and redirect', async () => { interactionDetails.mockResolvedValueOnce({ params: {} }); + const fakeTime = Date.now(); + jest.useFakeTimers().setSystemTime(fakeTime); + const response = await sessionRequest .post(registerRoute) .send({ username: 'username', password: 'password' }); @@ -149,6 +152,7 @@ describe('sessionRoutes', () => { passwordEncrypted: 'password_user1', passwordEncryptionMethod: 'Argon2i', roleNames: [], + lastSignInAt: fakeTime, }) ); expect(response.body).toHaveProperty('redirectTo'); @@ -158,6 +162,7 @@ describe('sessionRoutes', () => { expect.objectContaining({ login: { accountId: 'user1' } }), expect.anything() ); + jest.useRealTimers(); }); it('register user with admin role for admin console if no active user found', async () => { diff --git a/packages/core/src/routes/session/username-password.ts b/packages/core/src/routes/session/username-password.ts index eba0d7a9c..8fd6db9b3 100644 --- a/packages/core/src/routes/session/username-password.ts +++ b/packages/core/src/routes/session/username-password.ts @@ -10,11 +10,10 @@ import { encryptUserPassword, generateUserId, findUserByUsernameAndPassword, - updateLastSignInAt, insertUser, } from '@/lib/user'; import koaGuard from '@/middleware/koa-guard'; -import { hasUser, hasActiveUsers } from '@/queries/user'; +import { hasUser, hasActiveUsers, updateUserById } from '@/queries/user'; import assertThat from '@/utils/assert-that'; import { AnonymousRouter } from '../types'; @@ -46,7 +45,7 @@ export default function usernamePasswordRoutes( const { id } = await findUserByUsernameAndPassword(username, password); ctx.log(type, { userId: id }); - await updateLastSignInAt(id); + await updateUserById(id, { lastSignInAt: Date.now() }); await assignInteractionResults(ctx, provider, { login: { accountId: id } }, true); return next(); @@ -94,8 +93,8 @@ export default function usernamePasswordRoutes( passwordEncrypted, passwordEncryptionMethod, roleNames, + lastSignInAt: Date.now(), }); - await updateLastSignInAt(id); await assignInteractionResults(ctx, provider, { login: { accountId: id } }); return next();