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", () => { 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/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..49f10e0655 --- /dev/null +++ b/apps/studio/src/server/modules/user/__tests__/user.service.test.ts @@ -0,0 +1,40 @@ +import { resetTables } from "tests/integration/helpers/db" +import { setupUser } from "tests/integration/helpers/seed" + +import { isUserDeleted } from "../user.service" + +describe("user.service", () => { + beforeAll(async () => { + await resetTables("User") + }) + + it("should return false if user is not deleted", async () => { + // Arrange + const email = "active@example.com" + // Setup active user + await setupUser({ + email: email, + isDeleted: false, + }) + + // 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" + // Setup deleted user + await setupUser({ + email: email, + isDeleted: true, + }) + + // 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..f67b9d2c63 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() } @@ -439,3 +442,29 @@ export const setUpWhitelist = async ({ .returningAll() .executeTakeFirstOrThrow() } + +export const setupUser = async ({ + name = "Test User", + userId = nanoid(), + 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() +}