Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests and handling for deleted users #953

Open
wants to merge 5 commits into
base: 12-20-soft_delete_for_user_and_perms
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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()
}
Loading