Skip to content

Commit

Permalink
Adding New Conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
vr-varad committed Aug 16, 2024
1 parent 40b4eee commit 06e7b6b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 82 deletions.
2 changes: 1 addition & 1 deletion packages/api-client/src/controllers/variable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { APIClient } from 'src/core/client'
import { APIClient } from '../core/client'
import { parseResponse } from '../core/response-parser'
import { ClientResponse } from '../types/index.types'
import {
Expand Down
50 changes: 26 additions & 24 deletions packages/api-client/src/types/variable.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,30 @@ export interface GetAllVariablesOfProjectRequest {
search?: string
}

export interface GetAllVariablesOfProjectResponse {
variable: {
id: string
name: string
createdAt: string
updatedAt: string
note: string | null
lastUpdatedById: string
projectId: string
lastUpdatedBy: {
export interface GetAllVariablesOfProjectResponse
extends Page<{
variable: {
id: string
name: string
createdAt: string
updatedAt: string
note: string | null
lastUpdatedById: string
projectId: string
lastUpdatedBy: {
id: string
name: string
}
}
}
values: {
environment: {
id: string
name: string
values: {
environment: {
id: string
name: string
}
value: string
version: number
}
value: string
version: number
}
}
}> {}

export interface GetAllVariablesOfEnvironmentRequest {
projectId: string
Expand All @@ -113,8 +114,9 @@ export interface GetAllVariablesOfEnvironmentRequest {
search?: string
}

export interface GetAllVariablesOfEnvironmentResponse {
name: string
value: string
isPlaintext: boolean
}
export interface GetAllVariablesOfEnvironmentResponse
extends Page<{
name: string
value: string
isPlaintext: boolean
}> {}
127 changes: 70 additions & 57 deletions packages/api-client/tests/variable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,59 @@
import client from '@package/client'
import VariableController from '@package/controllers/variable/variable'
import VariableController from '../src/controllers/variable'
import { APIClient } from '../src/core/client'

describe('Get Variable Tests', () => {
const backendUrl = process.env.BACKEND_URL

const client = new APIClient(backendUrl)
const variableController = new VariableController(backendUrl)
const email = 'johndoe@example.com'
let workspaceId: string | null
let projectId: string | null
let environment: any
let variableId: string | null

beforeAll(async () => {
const workspaceResponse = (await client.post(
'/api/workspace',
{
name: 'My Workspace'
},
{
'x-e2e-user-email': email
}
)) as any
const workspaceResponse = (await (
await client.post(
'/api/workspace',
{
name: 'My Workspace'
},
{
'x-e2e-user-email': email
}
)
).json()) as any

workspaceId = workspaceResponse.id

const projectResponse = (await client.post(
`/api/project/${workspaceId}`,
{
name: 'Project',
storePrivateKey: true
},
{
'x-e2e-user-email': email
}
)) as any
// Create a project
const projectResponse = (await (
await client.post(
`/api/project/${workspaceId}`,
{
name: 'Project',
storePrivateKey: true
},
{
'x-e2e-user-email': email
}
)
).json()) as any

projectId = projectResponse.id

const createEnvironmentResponse = await client.post(
`/api/environment/${projectId}`,
{
name: 'Dev'
},
{
'x-e2e-user-email': email
}
)
const createEnvironmentResponse = (await (
await client.post(
`/api/environment/${projectId}`,
{
name: 'Dev'
},
{
'x-e2e-user-email': email
}
)
).json()) as any

environment = createEnvironmentResponse
})
Expand All @@ -54,7 +65,7 @@ describe('Get Variable Tests', () => {

// Create a variable
it('should create a variable', async () => {
const variable = await VariableController.createVariable(
const variable = await variableController.createVariable(
{
projectId,
name: 'Variable 1',
Expand All @@ -64,18 +75,18 @@ describe('Get Variable Tests', () => {
'x-e2e-user-email': email
}
)
expect(variable.name).toBe('Variable 1')
expect(variable.projectId).toBe(projectId)
expect(variable.project.workspaceId).toBe(workspaceId)
expect(variable.versions.length).toBe(1)
expect(variable.versions[0].value).toBe('Variable 1 value')
expect(variable.versions[0].environmentId).toBe(environment.id)
variableId = variable.id
expect(variable.data.name).toBe('Variable 1')
expect(variable.data.projectId).toBe(projectId)
expect(variable.data.project.workspaceId).toBe(workspaceId)
expect(variable.data.versions.length).toBe(1)
expect(variable.data.versions[0].value).toBe('Variable 1 value')
expect(variable.data.versions[0].environmentId).toBe(environment.id)
variableId = variable.data.id
})

// Update Name of the Variable
it('should update the name a variable', async () => {
const updatedVariable = await VariableController.updateVariable(
const updatedVariable = await variableController.updateVariable(
{
name: 'UpdatedVariable 1',
variableId
Expand All @@ -84,13 +95,13 @@ describe('Get Variable Tests', () => {
'x-e2e-user-email': email
}
)
expect(updatedVariable.variable.name).toBe('UpdatedVariable 1')
expect(updatedVariable.variable.id).toBe(variableId)
expect(updatedVariable.data.variable.name).toBe('UpdatedVariable 1')
expect(updatedVariable.data.variable.id).toBe(variableId)
})

// Create a new version of Variable
it('should add version to a variable', async () => {
const updateVariable = await VariableController.updateVariable(
const updateVariable = await variableController.updateVariable(
{
entries: [
{
Expand All @@ -102,32 +113,34 @@ describe('Get Variable Tests', () => {
},
{ 'x-e2e-user-email': email }
)
expect(updateVariable.updatedVersions.length).toBe(1)
expect(updateVariable.updatedVersions[0].value).toBe('1234')
expect(updateVariable.updatedVersions[0].environmentId).toBe(environment.id)
expect(updateVariable.data.updatedVersions.length).toBe(1)
expect(updateVariable.data.updatedVersions[0].value).toBe('1234')
expect(updateVariable.data.updatedVersions[0].environmentId).toBe(
environment.id
)
})

// Roll back a variable
it('should rollback a variable', async () => {
const rolledBackVariable: any = await VariableController.rollbackVariable(
const rolledBackVariable: any = await variableController.rollbackVariable(
{
variableId,
version: 1,
environmentId: environment.id
},
{ 'x-e2e-user-email': email }
)
expect(rolledBackVariable.count).toBe(1)
expect(rolledBackVariable.data.count).toBe(1)
})

// Get all the variables of project
it('should get all variables of project', async () => {
const response: any = await VariableController.getAllVariablesOfProject(
const response: any = await variableController.getAllVariablesOfProject(
{ projectId },
{ 'x-e2e-user-email': email }
)
expect(response.length).toBe(1)
const variable1 = response[0]
expect(response.data.items.length).toBe(1)
const variable1 = response.data.items [0]
const variable = variable1.variable
const values = variable1.values
expect(variable).toHaveProperty('id')
Expand Down Expand Up @@ -177,16 +190,16 @@ describe('Get Variable Tests', () => {
// Get all variables for an environment
it('should get all variables for an environment', async () => {
const variables: any =
await VariableController.getAllVariablesOfEnvironment(
await variableController.getAllVariablesOfEnvironment(
{
environmentId: environment.id,
projectId
},
{ 'x-e2e-user-email': email }
)

expect(variables.length).toBe(1)
variables.forEach((variable) => {
expect(variables.data.length).toBe(1)
variables.data.forEach((variable) => {
expect(variable).toHaveProperty('name')
expect(typeof variable.name).toBe('string')

Expand All @@ -196,22 +209,22 @@ describe('Get Variable Tests', () => {
expect(variable).toHaveProperty('isPlaintext')
expect(typeof variable.isPlaintext).toBe('boolean')
})
const variable1 = variables[0]
const variable1 = variables.data[0]
expect(variable1.name).toBe('UpdatedVariable 1')
expect(variable1.value).toBe('Variable 1 value')
expect(variable1.isPlaintext).toBe(true)
})

// Delete a variable
it('should delete variable', async () => {
await VariableController.deleteVariable(
await variableController.deleteVariable(
{ variableId },
{ 'x-e2e-user-email': email }
)
const variables: any = await VariableController.getAllVariablesOfProject(
const variables: any = await variableController.getAllVariablesOfProject(
{ projectId },
{ 'x-e2e-user-email': email }
)
expect(variables.length).toBe(0)
expect(variables.data.items.length).toBe(0)
})
})

0 comments on commit 06e7b6b

Please sign in to comment.