Skip to content

Commit

Permalink
chore(api): Refactor user e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Apr 23, 2024
1 parent e9d710d commit b38d45a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 46 deletions.
28 changes: 15 additions & 13 deletions apps/api/src/user/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,22 @@ export class UserService {
}

private async deleteUserById(userId: User['id']) {
// Delete the default workspace of this user
await this.prisma.workspace.deleteMany({
where: {
ownerId: userId,
isDefault: true
}
})
await this.prisma.$transaction([
// Delete the user
this.prisma.user.delete({
where: {
id: userId
}
}),

// Delete the user
await this.prisma.user.delete({
where: {
id: userId
}
})
// Delete the default workspace of this user
this.prisma.workspace.deleteMany({
where: {
ownerId: userId,
isDefault: true
}
})
])

this.log.log(`Deleted user ${userId}`)
}
Expand Down
90 changes: 58 additions & 32 deletions apps/api/src/user/user.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('User Controller Tests', () => {

let adminUser: User
let regularUser: User
let janeDoeId: string

beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
Expand All @@ -38,21 +37,23 @@ describe('User Controller Tests', () => {
await app.getHttpAdapter().getInstance().ready()

await cleanUp(prisma)
})

beforeEach(async () => {
adminUser = await userService.createUser({
email: 'admin@keyshade.xyz',
name: 'Admin',
isActive: true,
isAdmin: true,
isOnboardingFinished: false
isOnboardingFinished: true
})

regularUser = await userService.createUser({
email: 'john@keyshade.xyz',
name: 'John',
isActive: true,
isAdmin: false,
isOnboardingFinished: false
isOnboardingFinished: true
})

// @ts-expect-error - We don't need the default workspace for these tests
Expand All @@ -61,6 +62,13 @@ describe('User Controller Tests', () => {
delete adminUser.defaultWorkspace
})

afterEach(async () => {
// Delete the users
await prisma.user.deleteMany()
// Delete the workspaces
await prisma.workspace.deleteMany()
})

it('should be defined', () => {
expect(app).toBeDefined()
expect(prisma).toBeDefined()
Expand Down Expand Up @@ -126,6 +134,16 @@ describe('User Controller Tests', () => {
})

test('regular user should not be able to access other routes if onboarding is not finished', async () => {
// Flip the user's onboarding status to false
await prisma.user.update({
where: {
email: regularUser.email
},
data: {
isOnboardingFinished: false
}
})

const result = await app.inject({
method: 'DELETE',
url: '/user',
Expand All @@ -137,6 +155,16 @@ describe('User Controller Tests', () => {
})

test('admin user should not be able to access other routes if onboarding is not finished', async () => {
// Flip the user's onboarding status to false
await prisma.user.update({
where: {
email: adminUser.email
},
data: {
isOnboardingFinished: false
}
})

const result = await app.inject({
method: 'DELETE',
url: '/user',
Expand Down Expand Up @@ -232,7 +260,7 @@ describe('User Controller Tests', () => {
}
})
expect(result.statusCode).toEqual(200)
expect(JSON.parse(result.body).length).toEqual(3)
expect(JSON.parse(result.body).length).toEqual(2)
})

test('admin should be able to update any user', async () => {
Expand All @@ -253,8 +281,6 @@ describe('User Controller Tests', () => {
name: 'John Doe',
isOnboardingFinished: true
})

regularUser = JSON.parse(result.body)
})

test('admin should be able to create new users', async () => {
Expand All @@ -281,44 +307,44 @@ describe('User Controller Tests', () => {
profilePictureUrl: null,
defaultWorkspace: expect.any(Object)
})

janeDoeId = JSON.parse(result.body).id
})

test('admin should be able to delete any user', async () => {
const result = await app.inject({
method: 'DELETE',
url: `/user/${janeDoeId}`,
url: `/user/${regularUser.id}`,
headers: {
'x-e2e-user-email': adminUser.email
}
})
expect(result.statusCode).toEqual(204)
})

test('user should be able to delete their own account', async () => {
const result = await app.inject({
method: 'DELETE',
url: `/user`,
headers: {
'x-e2e-user-email': regularUser.email
}
})

expect(result.statusCode).toEqual(204)
})

it('should have deleted the default workspace', async () => {
// Fetching the user who's account has the default workspace
const user = await prisma.user.findFirst({
where: {
email: 'jane@keyshade.xyz'
}
})

// Delete the user
await userService.deleteUser(user.id)
})
// test('user should be able to delete their own account', async () => {
// const result = await app.inject({
// method: 'DELETE',
// url: `/user`,
// headers: {
// 'x-e2e-user-email': regularUser.email
// }
// })
// expect(result.statusCode).toEqual(204)
// })

// it('should delete the default workspace on user deletion', async () => {
// // Delete the user
// await userService.deleteUser(regularUser.id)

// // Try fetching the workspace related to the user
// const workspace = await prisma.workspace.findFirst({
// where: {
// ownerId: regularUser.id,
// isDefault: true
// }
// })

// expect(workspace).toBeNull()
// })

afterAll(async () => {
await cleanUp(prisma)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"prepare": "husky install",
"sourcemaps:api": "turbo run sourcemaps --filter=api"
},
"packageManager": "pnpm@8.6.10",
"packageManager": "pnpm@9.0.5",
"devDependencies": {
"@sentry/cli": "^2.28.6",
"@sentry/webpack-plugin": "^2.14.2",
Expand Down

0 comments on commit b38d45a

Please sign in to comment.