Skip to content

Commit

Permalink
test(api): added variable e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Feb 20, 2024
1 parent d0fedc2 commit b61803a
Show file tree
Hide file tree
Showing 8 changed files with 952 additions and 12 deletions.
3 changes: 2 additions & 1 deletion apps/api/src/common/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default async function cleanUp(prisma: PrismaClient) {
prisma.project.deleteMany(),
prisma.user.deleteMany(),
prisma.event.deleteMany(),
prisma.apiKey.deleteMany()
prisma.apiKey.deleteMany(),
prisma.variable.deleteMany()
])
}
2 changes: 2 additions & 0 deletions apps/api/src/event/controller/event.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class EventController {
@Query('projectId') projectId: string,
@Query('environmentId') environmentId: string,
@Query('secretId') secretId: string,
@Query('variableId') variableId: string,
@Query('apiKeyId') apiKeyId: string,
@Query('workspaceRoleId') workspaceRoleId: string
) {
Expand All @@ -32,6 +33,7 @@ export class EventController {
projectId,
environmentId,
secretId,
variableId,
apiKeyId,
workspaceRoleId
},
Expand Down
44 changes: 43 additions & 1 deletion apps/api/src/event/event.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import { EnvironmentModule } from '../environment/environment.module'
import { ApiKeyModule } from '../api-key/api-key.module'
import createEvent from '../common/create-event'
import fetchEvents from '../common/fetch-events'
import { VariableService } from '../variable/service/variable.service'
import { VariableModule } from '../variable/variable.module'

describe('Event Controller Tests', () => {
let app: NestFastifyApplication
Expand All @@ -48,6 +50,7 @@ describe('Event Controller Tests', () => {
let workspaceRoleService: WorkspaceRoleService
let projectService: ProjectService
let secretService: SecretService
let variableService: VariableService

let user: User
let workspace: Workspace
Expand All @@ -67,7 +70,8 @@ describe('Event Controller Tests', () => {
SecretModule,
ProjectModule,
EnvironmentModule,
ApiKeyModule
ApiKeyModule,
VariableModule
]
})
.overrideProvider(MAIL_SERVICE)
Expand All @@ -85,6 +89,7 @@ describe('Event Controller Tests', () => {
workspaceRoleService = moduleRef.get(WorkspaceRoleService)
projectService = moduleRef.get(ProjectService)
secretService = moduleRef.get(SecretService)
variableService = moduleRef.get(VariableService)

await app.init()
await app.getHttpAdapter().getInstance().ready()
Expand Down Expand Up @@ -294,6 +299,43 @@ describe('Event Controller Tests', () => {
expect(response.json()).toEqual([event])
})

it('should be able to fetch a variable event', async () => {
const newVariable = await variableService.createVariable(
user,
{
name: 'My variable',
value: 'My value',
environmentId: environment.id
},
project.id
)

expect(newVariable).toBeDefined()

const response = await fetchEvents(
app,
user,
`variableId=${newVariable.id}`
)

const event = {
id: expect.any(String),
title: expect.any(String),
description: expect.any(String),
source: EventSource.VARIABLE,
triggerer: EventTriggerer.USER,
severity: EventSeverity.INFO,
type: EventType.VARIABLE_ADDED,
timestamp: expect.any(String),
metadata: expect.any(Object)
}

totalEvents.push(event)

expect(response.statusCode).toBe(200)
expect(response.json()).toEqual([event])
})

it('should be able to fetch a workspace role event', async () => {
const newWorkspaceRole = await workspaceRoleService.createWorkspaceRole(
user,
Expand Down
10 changes: 10 additions & 0 deletions apps/api/src/event/service/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import getProjectWithAuthority from '../../common/get-project-with-authority'
import getEnvironmentWithAuthority from '../../common/get-environment-with-authority'
import getSecretWithAuthority from '../../common/get-secret-with-authority'
import { PrismaService } from '../../prisma/prisma.service'
import getVariableWithAuthority from '../../common/get-variable-with-authority'

@Injectable()
export class EventService {
Expand All @@ -17,6 +18,7 @@ export class EventService {
projectId?: string
environmentId?: string
secretId?: string
variableId?: string
apiKeyId?: string
workspaceRoleId?: string
},
Expand Down Expand Up @@ -69,6 +71,14 @@ export class EventService {
this.prisma
)
whereCondition['sourceSecretId'] = context.secretId
} else if (context.variableId) {
await getVariableWithAuthority(
user.id,
context.variableId,
Authority.READ_VARIABLE,
this.prisma
)
whereCondition['sourceVariableId'] = context.variableId
} else if (context.apiKeyId) {
whereCondition['sourceApiKeyId'] = context.apiKeyId
} else if (context.workspaceRoleId) {
Expand Down
12 changes: 12 additions & 0 deletions apps/api/src/variable/controller/variable.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { Test, TestingModule } from '@nestjs/testing'
import { VariableController } from './variable.controller'
import { PrismaService } from '../../prisma/prisma.service'
import { MAIL_SERVICE } from '../../mail/services/interface.service'
import { MockMailService } from '../../mail/services/mock.service'
import { VariableService } from '../service/variable.service'

describe('VariableController', () => {
let controller: VariableController

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
PrismaService,
{
provide: MAIL_SERVICE,
useClass: MockMailService
},
VariableService
],
controllers: [VariableController]
}).compile()

Expand Down
12 changes: 11 additions & 1 deletion apps/api/src/variable/service/variable.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { Test, TestingModule } from '@nestjs/testing'
import { VariableService } from './variable.service'
import { PrismaService } from '../../prisma/prisma.service'
import { MAIL_SERVICE } from '../../mail/services/interface.service'
import { MockMailService } from '../../mail/services/mock.service'

describe('VariableService', () => {
let service: VariableService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [VariableService]
providers: [
PrismaService,
{
provide: MAIL_SERVICE,
useClass: MockMailService
},
VariableService
]
}).compile()

service = module.get<VariableService>(VariableService)
Expand Down
16 changes: 7 additions & 9 deletions apps/api/src/variable/service/variable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Logger,
NotFoundException
} from '@nestjs/common'
import { SecretService } from '../../secret/service/secret.service'
import { PrismaService } from '../../prisma/prisma.service'
import {
Authority,
Expand All @@ -27,7 +26,7 @@ import getVariableWithAuthority from '../../common/get-variable-with-authority'

@Injectable()
export class VariableService {
private readonly logger = new Logger(SecretService.name)
private readonly logger = new Logger(VariableService.name)

constructor(private readonly prisma: PrismaService) {}

Expand Down Expand Up @@ -114,7 +113,7 @@ export class VariableService {
source: EventSource.VARIABLE,
title: `Variable created`,
metadata: {
secretId: variable.id,
variableId: variable.id,
name: variable.name,
projectId,
projectName: project.name,
Expand Down Expand Up @@ -171,7 +170,7 @@ export class VariableService {
take: 1
})

result = await this.prisma.secret.update({
result = await this.prisma.variable.update({
where: {
id: variableId
},
Expand Down Expand Up @@ -207,7 +206,7 @@ export class VariableService {
source: EventSource.VARIABLE,
title: `Variable updated`,
metadata: {
secretId: variable.id,
variableId: variable.id,
name: variable.name,
projectId: variable.projectId,
projectName: variable.project.name
Expand Down Expand Up @@ -283,7 +282,7 @@ export class VariableService {
source: EventSource.VARIABLE,
title: `Variable environment updated`,
metadata: {
secretId: variable.id,
variableId: variable.id,
name: variable.name,
projectId: variable.projectId,
projectName: variable.project.name,
Expand Down Expand Up @@ -338,7 +337,7 @@ export class VariableService {
source: EventSource.VARIABLE,
title: `Variable rolled back`,
metadata: {
secretId: variable.id,
variableId: variable.id,
name: variable.name,
projectId: variable.projectId,
projectName: variable.project.name,
Expand Down Expand Up @@ -370,12 +369,11 @@ export class VariableService {
createEvent(
{
triggeredBy: user,
entity: variable,
type: EventType.VARIABLE_DELETED,
source: EventSource.VARIABLE,
title: `Variable deleted`,
metadata: {
secretId: variable.id,
variableId: variable.id,
name: variable.name,
projectId: variable.projectId,
projectName: variable.project.name
Expand Down
Loading

0 comments on commit b61803a

Please sign in to comment.