Skip to content

Commit

Permalink
test: Add event E2E tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Feb 12, 2024
1 parent 71c75a7 commit d3bd851
Show file tree
Hide file tree
Showing 14 changed files with 470 additions and 157 deletions.
7 changes: 2 additions & 5 deletions apps/api/src/api-key/api-key.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { MockMailService } from '../mail/services/mock.service'
import { AppModule } from '../app/app.module'
import { Test } from '@nestjs/testing'
import { ApiKey, User } from '@prisma/client'
import cleanUp from '../common/cleanup'

describe('Api Key Role Controller Tests', () => {
let app: NestFastifyApplication
Expand Down Expand Up @@ -194,10 +195,6 @@ describe('Api Key Role Controller Tests', () => {
})

afterAll(async () => {
await prisma.apiKey.deleteMany()
await prisma.user.deleteMany()

await prisma.$disconnect()
await app.close()
await cleanUp(prisma)
})
})
16 changes: 16 additions & 0 deletions apps/api/src/common/cleanup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { PrismaClient } from '@prisma/client'

export default async function cleanUp(prisma: PrismaClient) {
await prisma.$transaction([
prisma.workspaceMemberRoleAssociation.deleteMany(),
prisma.workspaceMember.deleteMany(),
prisma.workspaceRole.deleteMany(),
prisma.workspace.deleteMany(),
prisma.secret.deleteMany(),
prisma.environment.deleteMany(),
prisma.project.deleteMany(),
prisma.user.deleteMany(),
prisma.event.deleteMany(),
prisma.apiKey.deleteMany()
])
}
127 changes: 32 additions & 95 deletions apps/api/src/common/create-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
Secret,
User,
Workspace,
WorkspaceMember,
WorkspaceRole
} from '@prisma/client'
import { JsonObject } from '@prisma/client/runtime/library'
Expand All @@ -20,14 +19,7 @@ export default async function createEvent(
triggerer?: EventTriggerer
severity?: EventSeverity
triggeredBy?: User
entity?:
| Workspace
| Project
| Environment
| WorkspaceRole
| WorkspaceMember
| ApiKey
| Secret
entity?: Workspace | Project | Environment | WorkspaceRole | ApiKey | Secret
type: EventType
source: EventSource
title: string
Expand All @@ -40,126 +32,71 @@ export default async function createEvent(
throw new Error('User must be provided for non-system events')

Check warning on line 32 in apps/api/src/common/create-event.ts

View check run for this annotation

Codecov / codecov/patch

apps/api/src/common/create-event.ts#L32

Added line #L32 was not covered by tests
}

const baseData = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const baseData: any = {
triggerer: data.triggerer ?? EventTriggerer.USER,
severity: data.severity ?? EventSeverity.INFO,
type: data.type,
source: data.source,
title: data.title,
description: data.description,
metadata: data.metadata,
sourceUser: data.triggeredBy.id
? {
connect: {
id: data.triggeredBy.id
}
}
: undefined
description: data.description ?? '',
metadata: data.metadata
}

if (data.triggeredBy) {
baseData.sourceUserId = data.triggeredBy.id
}

try {
switch (data.source) {
case EventSource.WORKSPACE: {
const entity = data.entity as Workspace
await prisma.event.create({
data: {
...baseData,
sourceWorkspace: data.entity
? {
connect: {
id: entity.id
}
}
: undefined
}
})
if (data.entity) {
baseData.sourceWorkspaceId = data.entity.id
}
break
}
case EventSource.PROJECT: {
const entity = data.entity as Project
await prisma.event.create({
data: {
...baseData,
sourceProject: data.entity
? { connect: { id: entity.id } }
: undefined
}
})
if (data.entity) {
baseData.sourceProjectId = data.entity.id
}
break
}
case EventSource.ENVIRONMENT: {
const entity = data.entity as Environment
await prisma.event.create({
data: {
...baseData,
sourceEnvironment: data.entity
? { connect: { id: entity.id } }
: undefined
}
})
if (data.entity) {
baseData.sourceEnvironmentId = data.entity.id
}
break
}
case EventSource.WORKSPACE_ROLE: {
const entity = data.entity as WorkspaceRole
await prisma.event.create({
data: {
...baseData,
sourceWorkspaceRole: data.entity
? { connect: { id: entity.id } }
: undefined
}
})
break
}
case EventSource.WORKSPACE_MEMBER: {
const entity = data.entity as WorkspaceMember
await prisma.event.create({
data: {
...baseData,
sourceWorkspaceMembership: data.entity
? { connect: { id: entity.id } }
: undefined
}
})
if (data.entity) {
baseData.sourceWorkspaceRoleId = data.entity.id
}
break
}
case EventSource.API_KEY: {
const entity = data.entity as ApiKey
await prisma.event.create({
data: {
...baseData,
sourceApiKey: data.entity
? { connect: { id: entity.id } }
: undefined
}
})
if (data.entity) {
baseData.sourceApiKeyId = data.entity.id
}
break
}
case EventSource.SECRET: {
const entity = data.entity as Secret
await prisma.event.create({
data: {
...baseData,
sourceSecret: data.entity
? { connect: { id: entity.id } }
: undefined
}
})
if (data.entity) {
baseData.sourceSecretId = data.entity.id
}
break
}
case EventSource.USER: {
await prisma.event.create({
data: {
...baseData
}
})
break
}
default: {
throw new Error('Invalid event source')

Check warning on line 92 in apps/api/src/common/create-event.ts

View check run for this annotation

Codecov / codecov/patch

apps/api/src/common/create-event.ts#L91-L92

Added lines #L91 - L92 were not covered by tests
}
}
} catch (error) {
console.error('Error creating event', error)
console.error('Error creating event', data, error)

Check warning on line 96 in apps/api/src/common/create-event.ts

View check run for this annotation

Codecov / codecov/patch

apps/api/src/common/create-event.ts#L96

Added line #L96 was not covered by tests
}

await prisma.event.create({
data: baseData
})
}
6 changes: 2 additions & 4 deletions apps/api/src/event/controller/event.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export class EventController {
@Query('environmentId') environmentId: string,
@Query('secretId') secretId: string,
@Query('apiKeyId') apiKeyId: string,
@Query('workspaceRoleId') workspaceRoleId: string,
@Query('workspaceMemberId') workspaceMemberId: string
@Query('workspaceRoleId') workspaceRoleId: string
) {
return this.eventService.getEvents(
user,
Expand All @@ -34,8 +33,7 @@ export class EventController {
environmentId,
secretId,
apiKeyId,
workspaceRoleId,
workspaceMemberId
workspaceRoleId
},
page,
limit,
Expand Down
Loading

0 comments on commit d3bd851

Please sign in to comment.