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

test: Refactor environment e2e tests #252

Merged
merged 2 commits into from
May 31, 2024
Merged
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
9 changes: 3 additions & 6 deletions apps/api/src/auth/auth.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { MAIL_SERVICE } from '../mail/services/interface.service'
import { MockMailService } from '../mail/services/mock.service'
import { AppModule } from '../app/app.module'
import { Otp } from '@prisma/client'
import cleanUp from '../common/cleanup'

describe('Auth Controller Tests', () => {
let app: NestFastifyApplication
Expand All @@ -32,8 +31,10 @@ describe('Auth Controller Tests', () => {

await app.init()
await app.getHttpAdapter().getInstance().ready()
})

await cleanUp(prisma)
afterEach(async () => {
await prisma.user.deleteMany()
})

it('should be defined', async () => {
Expand Down Expand Up @@ -125,8 +126,4 @@ describe('Auth Controller Tests', () => {

expect(response.statusCode).toBe(401)
})

afterAll(async () => {
await cleanUp(prisma)
})
})
185 changes: 109 additions & 76 deletions apps/api/src/environment/environment.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
NestFastifyApplication
} from '@nestjs/platform-fastify'
import { PrismaService } from '../prisma/prisma.service'
import cleanUp from '../common/cleanup'
import {
Environment,
EventSeverity,
Expand All @@ -20,20 +19,21 @@ import {
User,
Workspace
} from '@prisma/client'
import { v4 } from 'uuid'
import fetchEvents from '../common/fetch-events'
import { ProjectModule } from '../project/project.module'
import { WorkspaceService } from '../workspace/service/workspace.service'
import { ProjectService } from '../project/service/project.service'
import { EventModule } from '../event/event.module'
import { WorkspaceModule } from '../workspace/workspace.module'
import { EventService } from '../event/service/event.service'
import { EnvironmentService } from './service/environment.service'
import { UserModule } from '../user/user.module'
import { UserService } from '../user/service/user.service'

describe('Environment Controller Tests', () => {
let app: NestFastifyApplication
let prisma: PrismaService
let projectService: ProjectService
let workspaceService: WorkspaceService
let environmentService: EnvironmentService
let userService: UserService
let eventService: EventService

let user1: User, user2: User
Expand All @@ -46,9 +46,9 @@ describe('Environment Controller Tests', () => {
imports: [
AppModule,
EventModule,
WorkspaceModule,
ProjectModule,
EnvironmentModule
EnvironmentModule,
UserModule
]
})
.overrideProvider(MAIL_SERVICE)
Expand All @@ -60,53 +60,77 @@ describe('Environment Controller Tests', () => {
)
prisma = moduleRef.get(PrismaService)
projectService = moduleRef.get(ProjectService)
workspaceService = moduleRef.get(WorkspaceService)
eventService = moduleRef.get(EventService)
environmentService = moduleRef.get(EnvironmentService)
userService = moduleRef.get(UserService)

await app.init()
await app.getHttpAdapter().getInstance().ready()
})

await cleanUp(prisma)

const user1Id = v4()
const user2Id = v4()
beforeEach(async () => {
const createUser1 = await userService.createUser({
email: '[email protected]',
name: 'John Doe',
isOnboardingFinished: true
})

user1 = await prisma.user.create({
data: {
id: user1Id,
email: '[email protected]',
name: 'John Doe',
isOnboardingFinished: true
}
const createUser2 = await userService.createUser({
email: '[email protected]',
name: 'Jane Doe',
isOnboardingFinished: true
})

user2 = await prisma.user.create({
data: {
id: user2Id,
email: '[email protected]',
name: 'Jane Doe',
isOnboardingFinished: true
workspace1 = createUser1.defaultWorkspace

delete createUser1.defaultWorkspace
delete createUser2.defaultWorkspace

user1 = createUser1
user2 = createUser2

project1 = (await projectService.createProject(user1, workspace1.id, {
name: 'Project 1',
description: 'Project 1 description',
storePrivateKey: true,
environments: [
{
name: 'Environment 1',
description: 'Default environment',
isDefault: true
},
{
name: 'Environment 2',
description: 'Environment 2 description'
}
],
accessLevel: ProjectAccessLevel.PRIVATE
})) as Project

environment1 = await prisma.environment.findUnique({
where: {
projectId_name: {
projectId: project1.id,
name: 'Environment 1'
}
}
})

workspace1 = await workspaceService.createWorkspace(user1, {
name: 'Workspace 1',
description: 'Workspace 1 description',
approvalEnabled: false
environment2 = await prisma.environment.findUnique({
where: {
projectId_name: {
projectId: project1.id,
name: 'Environment 2'
}
}
})
})

project1 = (await projectService.createProject(
user1,
workspace1.id,
{
name: 'Project 1',
description: 'Project 1 description',
storePrivateKey: true,
environments: [],
accessLevel: ProjectAccessLevel.PRIVATE
},
''
)) as Project
afterEach(async () => {
await prisma.$transaction([
prisma.user.deleteMany(),
prisma.workspace.deleteMany()
])
})

it('should be defined', () => {
Expand All @@ -119,8 +143,8 @@ describe('Environment Controller Tests', () => {
method: 'POST',
url: `/environment/${project1.id}`,
payload: {
name: 'Environment 1',
description: 'Environment 1 description',
name: 'Environment 3',
description: 'Environment 3 description',
isDefault: true
},
headers: {
Expand All @@ -129,20 +153,17 @@ describe('Environment Controller Tests', () => {
})

expect(response.statusCode).toBe(201)
expect(response.json()).toEqual({
id: expect.any(String),
name: 'Environment 1',
description: 'Environment 1 description',
isDefault: true,
projectId: project1.id,
lastUpdatedById: user1.id,
createdAt: expect.any(String),
updatedAt: expect.any(String),
pendingCreation: false,
project: expect.any(Object)
expect(response.json().name).toBe('Environment 3')
expect(response.json().description).toBe('Environment 3 description')
expect(response.json().isDefault).toBe(true)

const environmentFromDb = await prisma.environment.findUnique({
where: {
id: response.json().id
}
})

environment1 = response.json()
expect(environmentFromDb).toBeDefined()
})

it('should ensure there is only one default environment per project', async () => {
Expand All @@ -152,7 +173,6 @@ describe('Environment Controller Tests', () => {
}
})

expect(environments.length).toBe(2)
expect(environments.filter((e) => e.isDefault).length).toBe(1)
})

Expand Down Expand Up @@ -219,8 +239,8 @@ describe('Environment Controller Tests', () => {
method: 'POST',
url: `/environment/${project1.id}`,
payload: {
name: 'Environment 2',
description: 'Environment 2 description',
name: 'Environment 3',
description: 'Environment 3 description',
isDefault: false
},
headers: {
Expand All @@ -229,12 +249,10 @@ describe('Environment Controller Tests', () => {
})

expect(response.statusCode).toBe(201)
expect(response.json().name).toBe('Environment 2')
expect(response.json().description).toBe('Environment 2 description')
expect(response.json().name).toBe('Environment 3')
expect(response.json().description).toBe('Environment 3 description')
expect(response.json().isDefault).toBe(false)

environment2 = response.json()

const environments = await prisma.environment.findMany({
where: {
projectId: project1.id
Expand All @@ -246,6 +264,15 @@ describe('Environment Controller Tests', () => {
})

it('should have created a ENVIRONMENT_ADDED event', async () => {
// Create an environment
await environmentService.createEnvironment(
user1,
{
name: 'Environment 4'
},
project1.id
)

const response = await fetchEvents(
eventService,
user1,
Expand Down Expand Up @@ -351,6 +378,15 @@ describe('Environment Controller Tests', () => {
})

it('should create a ENVIRONMENT_UPDATED event', async () => {
// Update an environment
await environmentService.updateEnvironment(
user1,
{
name: 'Environment 1 Updated'
},
environment1.id
)

const response = await fetchEvents(
eventService,
user1,
Expand Down Expand Up @@ -395,7 +431,7 @@ describe('Environment Controller Tests', () => {
}
})

expect(environments.length).toBe(3)
expect(environments.length).toBe(2)
expect(environments.filter((e) => e.isDefault).length).toBe(1)

environment2 = response.json()
Expand All @@ -412,11 +448,9 @@ describe('Environment Controller Tests', () => {
})

expect(response.statusCode).toBe(200)
expect(response.json().name).toBe('Environment 1 Updated')
expect(response.json().description).toBe(
'Environment 1 description updated'
)
expect(response.json().isDefault).toBe(false)
expect(response.json().name).toBe('Environment 1')
expect(response.json().description).toBe('Default environment')
expect(response.json().isDefault).toBe(true)
})

it('should not be able to fetch an environment that does not exist', async () => {
Expand Down Expand Up @@ -490,7 +524,7 @@ describe('Environment Controller Tests', () => {
it('should be able to delete an environment', async () => {
const response = await app.inject({
method: 'DELETE',
url: `/environment/${environment1.id}`,
url: `/environment/${environment2.id}`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -500,6 +534,9 @@ describe('Environment Controller Tests', () => {
})

it('should have created a ENVIRONMENT_DELETED event', async () => {
// Delete an environment
await environmentService.deleteEnvironment(user1, environment2.id)

const response = await fetchEvents(
eventService,
user1,
Expand Down Expand Up @@ -548,7 +585,7 @@ describe('Environment Controller Tests', () => {
it('should not be able to delete the default environment of a project', async () => {
const response = await app.inject({
method: 'DELETE',
url: `/environment/${environment2.id}`,
url: `/environment/${environment1.id}`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -564,13 +601,13 @@ describe('Environment Controller Tests', () => {
await prisma.environment.deleteMany({
where: {
projectId: project1.id,
name: 'Default'
isDefault: false
}
})

const response = await app.inject({
method: 'PUT',
url: `/environment/${environment2.id}`,
url: `/environment/${environment1.id}`,
payload: {
isDefault: false
},
Expand All @@ -584,8 +621,4 @@ describe('Environment Controller Tests', () => {
'Cannot make the last environment non-default'
)
})

afterAll(async () => {
await cleanUp(prisma)
})
})
Loading
Loading