Skip to content

Commit

Permalink
add tests and handling for deleted users (#953)
Browse files Browse the repository at this point in the history
* add tests for soft delete

* add tests for list site

* add test for login

* fix pr comments

* default values for non-essential fields
  • Loading branch information
harishv7 committed Jan 23, 2025
1 parent aca4afa commit bcf9ed1
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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", () => {
Expand Down
26 changes: 26 additions & 0 deletions apps/studio/src/server/modules/site/__tests__/site.router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
40 changes: 40 additions & 0 deletions apps/studio/src/server/modules/user/__tests__/user.service.test.ts
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"
// 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 = "[email protected]"
// Setup deleted user
await setupUser({
email: email,
isDeleted: true,
})

// Act
const result = await isUserDeleted(email)
// Assert
expect(result).toBe(true)
})
})
29 changes: 29 additions & 0 deletions apps/studio/tests/integration/helpers/seed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -22,6 +24,7 @@ export const setupAdminPermissions = async ({
siteId,
role: RoleType.Admin,
resourceId: null,
deletedAt: isDeleted ? new Date() : null,
})
.execute()
}
Expand Down Expand Up @@ -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()
}

0 comments on commit bcf9ed1

Please sign in to comment.