From 05cb6ac91e7ad5d302465378c14a97fcb0eebc2b Mon Sep 17 00:00:00 2001 From: Harish V Date: Fri, 20 Dec 2024 23:25:19 +0800 Subject: [PATCH 1/5] add tests for soft delete --- .../user/__tests__/user.service.test.ts | 47 +++++++++++++++++++ .../tests/integration/helpers/seed/index.ts | 26 ++++++++++ 2 files changed, 73 insertions(+) create mode 100644 apps/studio/src/server/modules/user/__tests__/user.service.test.ts diff --git a/apps/studio/src/server/modules/user/__tests__/user.service.test.ts b/apps/studio/src/server/modules/user/__tests__/user.service.test.ts new file mode 100644 index 0000000000..b2ce1a12db --- /dev/null +++ b/apps/studio/src/server/modules/user/__tests__/user.service.test.ts @@ -0,0 +1,47 @@ +import { resetTables } from "tests/integration/helpers/db" +import { setupUser, setUpWhitelist } from "tests/integration/helpers/seed" + +import { isUserDeleted } from "../user.service" + +describe("user.service", () => { + beforeAll(async () => { + await resetTables("User") + await setUpWhitelist({ email: "@example.com" }) + + // Setup active user + await setupUser({ + name: "Active User", + userId: "active123", + email: "active@example.com", + phone: "12345678", + isDeleted: false, + }) + + // Setup deleted user + await setupUser({ + name: "Deleted User", + userId: "deleted123", + email: "deleted@example.com", + phone: "12345678", + isDeleted: true, + }) + }) + + it("should return false if user is not deleted", async () => { + // Arrange + const email = "active@example.com" + // Act + const result = await isUserDeleted(email) + // Assert + expect(result).toBe(false) + }) + + it("should return true if user is deleted", async () => { + // Arrange + const email = "deleted@example.com" + // Act + const result = await isUserDeleted(email) + // Assert + expect(result).toBe(true) + }) +}) diff --git a/apps/studio/tests/integration/helpers/seed/index.ts b/apps/studio/tests/integration/helpers/seed/index.ts index a72cf877a8..eeb70c3dc8 100644 --- a/apps/studio/tests/integration/helpers/seed/index.ts +++ b/apps/studio/tests/integration/helpers/seed/index.ts @@ -439,3 +439,29 @@ export const setUpWhitelist = async ({ .returningAll() .executeTakeFirstOrThrow() } + +export const setupUser = async ({ + name, + userId, + email, + phone, + isDeleted, +}: { + name: string + userId: string + email: string + phone?: string + isDeleted: boolean +}) => { + return db + .insertInto("User") + .values({ + id: userId, + name, + email, + phone: phone ?? "", + deletedAt: isDeleted ? new Date() : null, + }) + .returningAll() + .executeTakeFirstOrThrow() +} From b2b72bc6a8c8e57ca70bb86823a58054977f435c Mon Sep 17 00:00:00 2001 From: Harish V Date: Fri, 20 Dec 2024 23:33:03 +0800 Subject: [PATCH 2/5] add tests for list site --- .../site/__tests__/site.router.test.ts | 26 +++++++++++++++++++ .../tests/integration/helpers/seed/index.ts | 3 +++ 2 files changed, 29 insertions(+) diff --git a/apps/studio/src/server/modules/site/__tests__/site.router.test.ts b/apps/studio/src/server/modules/site/__tests__/site.router.test.ts index 31ee44b986..249809a182 100644 --- a/apps/studio/src/server/modules/site/__tests__/site.router.test.ts +++ b/apps/studio/src/server/modules/site/__tests__/site.router.test.ts @@ -128,6 +128,32 @@ describe("site.router", async () => { }, ]) }) + + it("should only return sites if the permissions are not deleted for the site", async () => { + const { site: site1 } = await setupSite() + const { site: site2 } = await setupSite() + await setupAdminPermissions({ + userId: session.userId, + siteId: site1.id, + isDeleted: true, + }) + await setupAdminPermissions({ + userId: session.userId, + siteId: site2.id, + }) + + // Act + const result = await caller.list() + + // Assert + expect(result).toEqual([ + { + id: site2.id, + name: site2.name, + config: site2.config, + }, + ]) + }) }) describe("getSiteName", () => { diff --git a/apps/studio/tests/integration/helpers/seed/index.ts b/apps/studio/tests/integration/helpers/seed/index.ts index eeb70c3dc8..eaa3b028ab 100644 --- a/apps/studio/tests/integration/helpers/seed/index.ts +++ b/apps/studio/tests/integration/helpers/seed/index.ts @@ -9,9 +9,11 @@ import { nanoid } from "nanoid" export const setupAdminPermissions = async ({ userId, siteId, + isDeleted = false, }: { userId?: string siteId: number + isDeleted?: boolean }) => { if (!userId) throw new Error("userId is a required field") @@ -22,6 +24,7 @@ export const setupAdminPermissions = async ({ siteId, role: RoleType.Admin, resourceId: null, + deletedAt: isDeleted ? new Date() : null, }) .execute() } From ec1a0a87dfb12c0bf29f9a26b5334e0c9f045ad4 Mon Sep 17 00:00:00 2001 From: Harish V Date: Fri, 20 Dec 2024 23:37:37 +0800 Subject: [PATCH 3/5] add test for login --- .../auth/email/__tests__/email.router.test.ts | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/apps/studio/src/server/modules/auth/email/__tests__/email.router.test.ts b/apps/studio/src/server/modules/auth/email/__tests__/email.router.test.ts index 55fdb7617a..f5f0ece67c 100644 --- a/apps/studio/src/server/modules/auth/email/__tests__/email.router.test.ts +++ b/apps/studio/src/server/modules/auth/email/__tests__/email.router.test.ts @@ -3,7 +3,7 @@ import { applySession, createMockRequest, } from "tests/integration/helpers/iron-session" -import { setUpWhitelist } from "tests/integration/helpers/seed" +import { setupUser, setUpWhitelist } from "tests/integration/helpers/seed" import { describe, expect, it } from "vitest" import { env } from "~/env.mjs" @@ -71,6 +71,25 @@ describe("auth.email", () => { }) expect(result).toEqual(expectedReturn) }) + + it("should throw if user is deleted", async () => { + // Arrange + await setupUser({ + name: "Deleted", + userId: "deleted123", + email: TEST_VALID_EMAIL, + phone: "123", + isDeleted: true, + }) + + // Act + const result = caller.login({ email: TEST_VALID_EMAIL }) + + // Assert + await expect(result).rejects.toThrowError( + "Unauthorized. Contact Isomer support.", + ) + }) }) describe("verifyOtp", () => { From 70ea755a4960e5ea3c7946d17e9b719f05e6a916 Mon Sep 17 00:00:00 2001 From: Harish V Date: Wed, 15 Jan 2025 15:29:46 +0800 Subject: [PATCH 4/5] fix pr comments --- .../user/__tests__/user.service.test.ts | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/apps/studio/src/server/modules/user/__tests__/user.service.test.ts b/apps/studio/src/server/modules/user/__tests__/user.service.test.ts index b2ce1a12db..b6bc675663 100644 --- a/apps/studio/src/server/modules/user/__tests__/user.service.test.ts +++ b/apps/studio/src/server/modules/user/__tests__/user.service.test.ts @@ -1,35 +1,25 @@ import { resetTables } from "tests/integration/helpers/db" -import { setupUser, setUpWhitelist } from "tests/integration/helpers/seed" +import { setupUser } from "tests/integration/helpers/seed" import { isUserDeleted } from "../user.service" describe("user.service", () => { beforeAll(async () => { await resetTables("User") - await setUpWhitelist({ email: "@example.com" }) + }) + it("should return false if user is not deleted", async () => { + // Arrange + const email = "active@example.com" // Setup active user await setupUser({ name: "Active User", userId: "active123", - email: "active@example.com", + email: email, phone: "12345678", isDeleted: false, }) - // Setup deleted user - await setupUser({ - name: "Deleted User", - userId: "deleted123", - email: "deleted@example.com", - phone: "12345678", - isDeleted: true, - }) - }) - - it("should return false if user is not deleted", async () => { - // Arrange - const email = "active@example.com" // Act const result = await isUserDeleted(email) // Assert @@ -39,6 +29,15 @@ describe("user.service", () => { it("should return true if user is deleted", async () => { // Arrange const email = "deleted@example.com" + // Setup deleted user + await setupUser({ + name: "Deleted User", + userId: "deleted123", + email: email, + phone: "12345678", + isDeleted: true, + }) + // Act const result = await isUserDeleted(email) // Assert From e6460664e05c38be770c0afb60444ffea3de9305 Mon Sep 17 00:00:00 2001 From: Harish V Date: Wed, 15 Jan 2025 15:37:17 +0800 Subject: [PATCH 5/5] default values for non-essential fields --- .../modules/user/__tests__/user.service.test.ts | 6 ------ apps/studio/tests/integration/helpers/seed/index.ts | 12 ++++++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/apps/studio/src/server/modules/user/__tests__/user.service.test.ts b/apps/studio/src/server/modules/user/__tests__/user.service.test.ts index b6bc675663..49f10e0655 100644 --- a/apps/studio/src/server/modules/user/__tests__/user.service.test.ts +++ b/apps/studio/src/server/modules/user/__tests__/user.service.test.ts @@ -13,10 +13,7 @@ describe("user.service", () => { const email = "active@example.com" // Setup active user await setupUser({ - name: "Active User", - userId: "active123", email: email, - phone: "12345678", isDeleted: false, }) @@ -31,10 +28,7 @@ describe("user.service", () => { const email = "deleted@example.com" // Setup deleted user await setupUser({ - name: "Deleted User", - userId: "deleted123", email: email, - phone: "12345678", isDeleted: true, }) diff --git a/apps/studio/tests/integration/helpers/seed/index.ts b/apps/studio/tests/integration/helpers/seed/index.ts index eaa3b028ab..f67b9d2c63 100644 --- a/apps/studio/tests/integration/helpers/seed/index.ts +++ b/apps/studio/tests/integration/helpers/seed/index.ts @@ -444,14 +444,14 @@ export const setUpWhitelist = async ({ } export const setupUser = async ({ - name, - userId, + name = "Test User", + userId = nanoid(), email, - phone, + phone = "", isDeleted, }: { - name: string - userId: string + name?: string + userId?: string email: string phone?: string isDeleted: boolean @@ -462,7 +462,7 @@ export const setupUser = async ({ id: userId, name, email, - phone: phone ?? "", + phone: phone, deletedAt: isDeleted ? new Date() : null, }) .returningAll()