From ace427bc36dfe1082220944290955aabb7c5c8ee Mon Sep 17 00:00:00 2001 From: vr-varad Date: Sun, 28 Jul 2024 11:05:57 +0530 Subject: [PATCH 1/5] Initialized Solution with structure --- packages/api-client/src/controllers/secret/secret.ts | 12 ++++++++++++ packages/api-client/src/types/secret.types.d.ts | 12 ++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 packages/api-client/src/controllers/secret/secret.ts create mode 100644 packages/api-client/src/types/secret.types.d.ts diff --git a/packages/api-client/src/controllers/secret/secret.ts b/packages/api-client/src/controllers/secret/secret.ts new file mode 100644 index 00000000..4d00e025 --- /dev/null +++ b/packages/api-client/src/controllers/secret/secret.ts @@ -0,0 +1,12 @@ +import client from '../../client' + +export default class SecretController { + private static apiClient = client + + static async createSecret() {} + static async updateSecret() {} + static async rollbackSecret() {} + static async deleteSecret() {} + static async getAllSecretsOfProject() {} + static async getAllSecretsOfEnvironment() {} +} diff --git a/packages/api-client/src/types/secret.types.d.ts b/packages/api-client/src/types/secret.types.d.ts new file mode 100644 index 00000000..31a1f111 --- /dev/null +++ b/packages/api-client/src/types/secret.types.d.ts @@ -0,0 +1,12 @@ +export interface CreateSecretRequest {} +export interface CreateSecretResponse {} +export interface UpdateSecretRequest {} +export interface UpdateSecretResponse {} +export interface DeleteSecretRequest {} +export interface DeleteSecretResponse {} +export interface RollBackSecretRequest {} +export interface RollBackSecretResponse {} +export interface getAllSecretsOfProjectRequest {} +export interface getAllSecretsOfProjectResponse {} +export interface getAllSecretsOfEnvironmentRequest {} +export interface getAllSecretsOfEnvironmentResponse {} From 2986d176fed33925de33480f4096c316e0dbc41b Mon Sep 17 00:00:00 2001 From: vr-varad Date: Sun, 28 Jul 2024 11:26:13 +0530 Subject: [PATCH 2/5] Added Controllers and Types of request and response --- .../src/controllers/secret/secret.ts | 80 +++++++++++++++++-- .../api-client/src/types/secret.types.d.ts | 56 +++++++++++-- 2 files changed, 121 insertions(+), 15 deletions(-) diff --git a/packages/api-client/src/controllers/secret/secret.ts b/packages/api-client/src/controllers/secret/secret.ts index 4d00e025..5ebafb84 100644 --- a/packages/api-client/src/controllers/secret/secret.ts +++ b/packages/api-client/src/controllers/secret/secret.ts @@ -1,12 +1,78 @@ -import client from '../../client' +import client from '@package/client' +import { + CreateSecretRequest, + CreateSecretResponse, + DeleteSecretRequest, + DeleteSecretResponse, + getAllSecretsOfEnvironmentRequest, + getAllSecretsOfEnvironmentResponse, + getAllSecretsOfProjectRequest, + getAllSecretsOfProjectResponse, + RollBackSecretRequest, + RollBackSecretResponse, + UpdateSecretRequest, + UpdateSecretResponse +} from '@package/types/secret.types' export default class SecretController { private static apiClient = client - static async createSecret() {} - static async updateSecret() {} - static async rollbackSecret() {} - static async deleteSecret() {} - static async getAllSecretsOfProject() {} - static async getAllSecretsOfEnvironment() {} + static async createSecret( + request: CreateSecretRequest, + headers?: Record + ): Promise { + return this.apiClient.post( + `/api/secret/${request.projectId}`, + request, + headers + ) + } + static async updateSecret( + request: UpdateSecretRequest, + headers?: Record + ): Promise { + return this.apiClient.put( + `/api/secret/${request.secretId}`, + request, + headers + ) + } + + static async rollbackSecret( + request: RollBackSecretRequest, + headers?: Record + ): Promise { + return this.apiClient.put( + `/api/secret/${request.secretId}/rollback/${request.version}?environmentId=${request.environmentId}`, + request, + headers + ) + } + + static async deleteSecret( + request: DeleteSecretRequest, + headers?: Record + ): Promise { + return this.apiClient.delete(`/api/secret/${request.secretId}`, headers) + } + + static async getAllSecretsOfProject( + request: getAllSecretsOfProjectRequest, + headers?: Record + ): Promise { + return this.apiClient.get( + `/api/secret/${request.projectId}?decryptValue=true`, + headers + ) + } + + static async getAllSecretsOfEnvironment( + request: getAllSecretsOfEnvironmentRequest, + headers?: Record + ): Promise { + return this.apiClient.get( + `}/api/secret/${request.projectId}/${request.environmentId}`, + headers + ) + } } diff --git a/packages/api-client/src/types/secret.types.d.ts b/packages/api-client/src/types/secret.types.d.ts index 31a1f111..ca15d257 100644 --- a/packages/api-client/src/types/secret.types.d.ts +++ b/packages/api-client/src/types/secret.types.d.ts @@ -1,12 +1,52 @@ -export interface CreateSecretRequest {} -export interface CreateSecretResponse {} -export interface UpdateSecretRequest {} -export interface UpdateSecretResponse {} -export interface DeleteSecretRequest {} +export interface CreateSecretRequest { + projectId: string + name: string + entries: Entries[] +} + +export interface Entries { + value: string + environmentId: string +} +export interface CreateSecretResponse { + id: string + name: string + createdAt: string + updatedAt: string + rotateAt: string | null + note: string | null + lastUpdatedById: string + projectId: string + project: Project + versions: Entries[] +} + +export interface Project { + workspaceId: string +} +export interface UpdateSecretRequest { + secretId: string +} +export interface UpdateSecretResponse { + secret: any + updatedVersions: any +} +export interface DeleteSecretRequest { + secretId: string +} export interface DeleteSecretResponse {} -export interface RollBackSecretRequest {} +export interface RollBackSecretRequest { + environmentId: string + version: number + secretId: string +} export interface RollBackSecretResponse {} -export interface getAllSecretsOfProjectRequest {} +export interface getAllSecretsOfProjectRequest { + projectId: string +} export interface getAllSecretsOfProjectResponse {} -export interface getAllSecretsOfEnvironmentRequest {} +export interface getAllSecretsOfEnvironmentRequest { + projectId: string + environmentId: string +} export interface getAllSecretsOfEnvironmentResponse {} From 5d99ef453759e4bf085929fc2c0622f42672419f Mon Sep 17 00:00:00 2001 From: vr-varad Date: Sun, 28 Jul 2024 13:20:52 +0530 Subject: [PATCH 3/5] Added Tests --- .../src/controllers/secret/secret.ts | 38 ++-- .../api-client/src/types/secret.types.d.ts | 104 +++++++++-- packages/api-client/tests/secret.spec.ts | 164 ++++++++++++++++++ 3 files changed, 275 insertions(+), 31 deletions(-) create mode 100644 packages/api-client/tests/secret.spec.ts diff --git a/packages/api-client/src/controllers/secret/secret.ts b/packages/api-client/src/controllers/secret/secret.ts index 5ebafb84..d08a4978 100644 --- a/packages/api-client/src/controllers/secret/secret.ts +++ b/packages/api-client/src/controllers/secret/secret.ts @@ -4,10 +4,10 @@ import { CreateSecretResponse, DeleteSecretRequest, DeleteSecretResponse, - getAllSecretsOfEnvironmentRequest, - getAllSecretsOfEnvironmentResponse, - getAllSecretsOfProjectRequest, - getAllSecretsOfProjectResponse, + GetAllSecretsOfEnvironmentRequest, + GetAllSecretsOfEnvironmentResponse, + GetAllSecretsOfProjectRequest, + GetAllSecretsOfProjectResponse, RollBackSecretRequest, RollBackSecretResponse, UpdateSecretRequest, @@ -57,22 +57,28 @@ export default class SecretController { } static async getAllSecretsOfProject( - request: getAllSecretsOfProjectRequest, + request: GetAllSecretsOfProjectRequest, headers?: Record - ): Promise { - return this.apiClient.get( - `/api/secret/${request.projectId}?decryptValue=true`, - headers - ) + ): Promise { + let url = `/api/secret/${request.projectId}?decryptValue=true` + request.page && (url += `page=${request.page}&`) + request.limit && (url += `limit=${request.limit}&`) + request.sort && (url += `sort=${request.sort}&`) + request.order && (url += `order=${request.order}&`) + request.search && (url += `search=${request.search}&`) + return this.apiClient.get(url, headers) } static async getAllSecretsOfEnvironment( - request: getAllSecretsOfEnvironmentRequest, + request: GetAllSecretsOfEnvironmentRequest, headers?: Record - ): Promise { - return this.apiClient.get( - `}/api/secret/${request.projectId}/${request.environmentId}`, - headers - ) + ): Promise { + let url = `/api/secret/${request.projectId}/${request.environmentId}` + request.page && (url += `page=${request.page}&`) + request.limit && (url += `limit=${request.limit}&`) + request.sort && (url += `sort=${request.sort}&`) + request.order && (url += `order=${request.order}&`) + request.search && (url += `search=${request.search}&`) + return this.apiClient.get(url, headers) } } diff --git a/packages/api-client/src/types/secret.types.d.ts b/packages/api-client/src/types/secret.types.d.ts index ca15d257..13e0dd43 100644 --- a/packages/api-client/src/types/secret.types.d.ts +++ b/packages/api-client/src/types/secret.types.d.ts @@ -1,13 +1,16 @@ export interface CreateSecretRequest { projectId: string name: string - entries: Entries[] + note?: string + rotateAfter?: string + entries?: [ + { + value: string + environmentId: string + } + ] } -export interface Entries { - value: string - environmentId: string -} export interface CreateSecretResponse { id: string name: string @@ -17,36 +20,107 @@ export interface CreateSecretResponse { note: string | null lastUpdatedById: string projectId: string - project: Project - versions: Entries[] + project: { + workspaceId: string + } + versions: [ + { + value: string + environmentId: string + } + ] } -export interface Project { - workspaceId: string -} export interface UpdateSecretRequest { secretId: string + name?: string + note?: string + rotateAfter?: string + entries?: [ + { + value: string + environmentId: string + } + ] } + export interface UpdateSecretResponse { secret: any updatedVersions: any } + export interface DeleteSecretRequest { secretId: string } + export interface DeleteSecretResponse {} + export interface RollBackSecretRequest { environmentId: string version: number secretId: string } -export interface RollBackSecretResponse {} -export interface getAllSecretsOfProjectRequest { +export interface RollBackSecretResponse { + count: string +} + +export interface GetAllSecretsOfProjectRequest { projectId: string + page?: number + limit?: number + sort?: string + order?: string + search?: string } -export interface getAllSecretsOfProjectResponse {} -export interface getAllSecretsOfEnvironmentRequest { +export interface GetAllSecretsOfProjectResponse { + items: { + id: string + name: string + createdAt: string + updatedAt: string + rotateAt: string | null + note: string | null + lastUpdatedById: string + projectId: string + project: { + workspaceId: string + } + versions: [ + { + value: string + environmentId: string + } + ] + }[] +} + +export interface GetAllSecretsOfEnvironmentRequest { projectId: string environmentId: string + page?: number + limit?: number + sort?: string + order?: string + search?: string +} +export interface GetAllSecretsOfEnvironmentResponse { + items: { + id: string + name: string + createdAt: string + updatedAt: string + rotateAt: string | null + note: string | null + lastUpdatedById: string + projectId: string + project: { + workspaceId: string + } + versions: [ + { + value: string + environmentId: string + } + ] + }[] } -export interface getAllSecretsOfEnvironmentResponse {} diff --git a/packages/api-client/tests/secret.spec.ts b/packages/api-client/tests/secret.spec.ts new file mode 100644 index 00000000..dd7af751 --- /dev/null +++ b/packages/api-client/tests/secret.spec.ts @@ -0,0 +1,164 @@ +import client from '@package/client' +import SecretController from '@package/controllers/secret/secret' + +describe('Get Variable tests', () => { + const email = 'johndoe@example.com' + let projectId: string | null + let workspaceId: string | null + let environment: any + let secretId: string + + beforeAll(async () => { + //Create the user's workspace + const workspaceResponse = (await client.post( + '/api/workspace', + { + name: 'My Workspace' + }, + { + 'x-e2e-user-email': email + } + )) as any + + workspaceId = workspaceResponse.id + + // Create a project + const projectResponse = (await client.post( + `/api/project/${workspaceId}`, + { + name: 'Project', + storePrivateKey: true + }, + { + 'x-e2e-user-email': email + } + )) as any + + projectId = projectResponse.id + + // Create an environment + const createEnvironmentResponse = await client.post( + `/api/environment/${projectId}`, + { + name: 'Dev' + }, + { + 'x-e2e-user-email': email + } + ) + + environment = createEnvironmentResponse + }) + + afterAll(async () => { + // Delete the environment + await client.delete(`/api/environment/${environment.id}`, { + 'x-e2e-user-email': email + }) + + // Delete the workspace + await client.delete(`/api/workspace/${workspaceId}`, { + 'x-e2e-user-email': email + }) + }) + + // Create a Secret + it('should create a secret', async () => { + const secret = await SecretController.createSecret( + { + name: 'Secret 1', + note: 'Secret 1 note', + entries: [ + { + environmentId: environment.id, + value: 'Secret 1 value' + } + ], + projectId + }, + { 'x-e2e-user-email': email } + ) + expect(secret.projectId).toBe(projectId) + expect(secret.project.workspaceId).toBe(workspaceId) + expect(secret.name).toBe('Secret 1') + expect(secret.versions.length).toBe(1) + secretId = secret.id + }) + + // Update Name of a Secret + it('should update name of a secret', async () => { + const updatedSecret = await SecretController.updateSecret( + { + name: 'Updated Secret 1', + secretId + }, + { 'x-e2e-user-email': email } + ) + expect(updatedSecret.secret.name).toBe('Updated Secret 1') + }) + + // Add Version to a Secret + it('should add version of a secret', async () => { + const updatedSecret = await SecretController.updateSecret( + { + entries: [ + { + value: 'Updated Secret 1 value', + environmentId: environment.id + } + ], + secretId + }, + { 'x-e2e-user-email': email } + ) + console.log(updatedSecret) + expect(updatedSecret.updatedVersions.length).toBe(1) + }) + + // RollBack a Particular Version of a Secret + it('should roll back a version of a secret', async () => { + try { + const rollbackSecret = await SecretController.rollbackSecret( + { secretId, environmentId: environment.id, version: 1 }, + { 'x-e2e-user-email': email } + ) + expect(rollbackSecret.count).toBe(1) + } catch (error) { + console.log(error) + } + }) + + // Get all secrets of a Project + it('should get all secrets of a project', async () => { + const secrets: any = await SecretController.getAllSecretsOfProject( + { projectId }, + { 'x-e2e-user-email': email } + ) + expect(secrets.length).toBe(1) + }) + + // Get all secrets of an Environment + it('should get all secrets of an environment', async () => { + const secrets: any = await SecretController.getAllSecretsOfEnvironment( + { + environmentId: environment.id, + projectId: projectId + }, + { 'x-e2e-user-email': email } + ) + expect(secrets.length).toBe(1) + }) + + // Delete a Secert from a Project + it('should delete a secret', async () => { + await SecretController.deleteSecret( + { secretId }, + { 'x-e2e-user-email': email } + ) + const secrets: any = await SecretController.getAllSecretsOfProject( + { projectId }, + { 'x-e2e-user-email': email } + ) + expect(secrets.length).toBe(0) + }) +}) From 603736bbaa63e9923ad85d54824f03188863c358 Mon Sep 17 00:00:00 2001 From: vr-varad Date: Sun, 28 Jul 2024 18:49:34 +0530 Subject: [PATCH 4/5] Resolved Issues --- .../src/controllers/secret/secret.ts | 11 ++-- .../api-client/src/types/secret.types.d.ts | 65 +++++++++---------- packages/api-client/tests/secret.spec.ts | 24 ++++--- 3 files changed, 53 insertions(+), 47 deletions(-) diff --git a/packages/api-client/src/controllers/secret/secret.ts b/packages/api-client/src/controllers/secret/secret.ts index d08a4978..c8ecd5bf 100644 --- a/packages/api-client/src/controllers/secret/secret.ts +++ b/packages/api-client/src/controllers/secret/secret.ts @@ -59,26 +59,29 @@ export default class SecretController { static async getAllSecretsOfProject( request: GetAllSecretsOfProjectRequest, headers?: Record - ): Promise { + ): Promise { let url = `/api/secret/${request.projectId}?decryptValue=true` request.page && (url += `page=${request.page}&`) request.limit && (url += `limit=${request.limit}&`) request.sort && (url += `sort=${request.sort}&`) request.order && (url += `order=${request.order}&`) request.search && (url += `search=${request.search}&`) - return this.apiClient.get(url, headers) + return this.apiClient.get(url, headers) } static async getAllSecretsOfEnvironment( request: GetAllSecretsOfEnvironmentRequest, headers?: Record - ): Promise { + ): Promise { let url = `/api/secret/${request.projectId}/${request.environmentId}` request.page && (url += `page=${request.page}&`) request.limit && (url += `limit=${request.limit}&`) request.sort && (url += `sort=${request.sort}&`) request.order && (url += `order=${request.order}&`) request.search && (url += `search=${request.search}&`) - return this.apiClient.get(url, headers) + return this.apiClient.get( + url, + headers + ) } } diff --git a/packages/api-client/src/types/secret.types.d.ts b/packages/api-client/src/types/secret.types.d.ts index 13e0dd43..033666e6 100644 --- a/packages/api-client/src/types/secret.types.d.ts +++ b/packages/api-client/src/types/secret.types.d.ts @@ -2,7 +2,7 @@ export interface CreateSecretRequest { projectId: string name: string note?: string - rotateAfter?: string + rotateAfter?: '24' | '168' | '720' | '8760' | 'never' entries?: [ { value: string @@ -35,7 +35,7 @@ export interface UpdateSecretRequest { secretId: string name?: string note?: string - rotateAfter?: string + rotateAfter?: '24' | '168' | '720' | '8760' | 'never' entries?: [ { value: string @@ -45,8 +45,18 @@ export interface UpdateSecretRequest { } export interface UpdateSecretResponse { - secret: any - updatedVersions: any + secret: { + id: string + name: string + note: string + } + updatedVersions: [ + { + id?: string + environmentId: string + value: string + } + ] } export interface DeleteSecretRequest { @@ -73,25 +83,28 @@ export interface GetAllSecretsOfProjectRequest { search?: string } export interface GetAllSecretsOfProjectResponse { - items: { + secret: { id: string name: string createdAt: string updatedAt: string - rotateAt: string | null + rotateAt: string note: string | null lastUpdatedById: string projectId: string - project: { - workspaceId: string + lastUpdatedBy: { + id: string + name: string + } + } + values: { + environment: { + id: string + name: string } - versions: [ - { - value: string - environmentId: string - } - ] - }[] + value: string + version: number + } } export interface GetAllSecretsOfEnvironmentRequest { @@ -104,23 +117,7 @@ export interface GetAllSecretsOfEnvironmentRequest { search?: string } export interface GetAllSecretsOfEnvironmentResponse { - items: { - id: string - name: string - createdAt: string - updatedAt: string - rotateAt: string | null - note: string | null - lastUpdatedById: string - projectId: string - project: { - workspaceId: string - } - versions: [ - { - value: string - environmentId: string - } - ] - }[] + name: string + value: string + isPlaintext: boolean } diff --git a/packages/api-client/tests/secret.spec.ts b/packages/api-client/tests/secret.spec.ts index dd7af751..35ad1825 100644 --- a/packages/api-client/tests/secret.spec.ts +++ b/packages/api-client/tests/secret.spec.ts @@ -117,15 +117,11 @@ describe('Get Variable tests', () => { // RollBack a Particular Version of a Secret it('should roll back a version of a secret', async () => { - try { - const rollbackSecret = await SecretController.rollbackSecret( - { secretId, environmentId: environment.id, version: 1 }, - { 'x-e2e-user-email': email } - ) - expect(rollbackSecret.count).toBe(1) - } catch (error) { - console.log(error) - } + const rollbackSecret = await SecretController.rollbackSecret( + { secretId, environmentId: environment.id, version: 1 }, + { 'x-e2e-user-email': email } + ) + expect(rollbackSecret.count).toBe(1) }) // Get all secrets of a Project @@ -147,6 +143,16 @@ describe('Get Variable tests', () => { { 'x-e2e-user-email': email } ) expect(secrets.length).toBe(1) + secrets.forEach((secret) => { + expect(secret).toHaveProperty('name') + expect(typeof secret.name).toBe('string') + + expect(secret).toHaveProperty('value') + expect(typeof secret.value).toBe('string') + + expect(secret).toHaveProperty('isPlaintext') + expect(typeof secret.isPlaintext).toBe('boolean') + }) }) // Delete a Secert from a Project From d0555d49ce350132b4b59fd61894afc46d667bbc Mon Sep 17 00:00:00 2001 From: vr-varad Date: Fri, 2 Aug 2024 23:42:55 +0530 Subject: [PATCH 5/5] According to new convention --- .../src/controllers/secret/secret.ts | 63 +++++---- .../api-client/src/types/secret.types.d.ts | 54 ++++---- packages/api-client/tests/secret.spec.ts | 127 ++++++++++-------- 3 files changed, 136 insertions(+), 108 deletions(-) diff --git a/packages/api-client/src/controllers/secret/secret.ts b/packages/api-client/src/controllers/secret/secret.ts index c8ecd5bf..69b76931 100644 --- a/packages/api-client/src/controllers/secret/secret.ts +++ b/packages/api-client/src/controllers/secret/secret.ts @@ -1,4 +1,6 @@ -import client from '@package/client' +import { APIClient } from '../../core/client' +import { ClientResponse } from 'src/types/index.types' +import { parseResponse } from '../../core/response-parser' import { CreateSecretRequest, CreateSecretResponse, @@ -12,76 +14,89 @@ import { RollBackSecretResponse, UpdateSecretRequest, UpdateSecretResponse -} from '@package/types/secret.types' +} from '../../types/secret.types' export default class SecretController { - private static apiClient = client + private apiClient: APIClient - static async createSecret( + constructor(private readonly backendUrl: string) { + this.apiClient = new APIClient(this.backendUrl) + } + + async createSecret( request: CreateSecretRequest, headers?: Record - ): Promise { - return this.apiClient.post( + ): Promise> { + const response = await this.apiClient.post( `/api/secret/${request.projectId}`, request, headers ) + return await parseResponse(response) } - static async updateSecret( + async updateSecret( request: UpdateSecretRequest, headers?: Record - ): Promise { - return this.apiClient.put( + ): Promise> { + const response = await this.apiClient.put( `/api/secret/${request.secretId}`, request, headers ) + + return await parseResponse(response) } - static async rollbackSecret( + async rollbackSecret( request: RollBackSecretRequest, headers?: Record - ): Promise { - return this.apiClient.put( + ): Promise> { + const response = await this.apiClient.put( `/api/secret/${request.secretId}/rollback/${request.version}?environmentId=${request.environmentId}`, request, headers ) + return await parseResponse(response) } - static async deleteSecret( + async deleteSecret( request: DeleteSecretRequest, headers?: Record - ): Promise { - return this.apiClient.delete(`/api/secret/${request.secretId}`, headers) + ): Promise> { + const response = await this.apiClient.delete( + `/api/secret/${request.secretId}`, + headers + ) + return await parseResponse(response) } - static async getAllSecretsOfProject( + async getAllSecretsOfProject( request: GetAllSecretsOfProjectRequest, headers?: Record - ): Promise { + ): Promise> { let url = `/api/secret/${request.projectId}?decryptValue=true` request.page && (url += `page=${request.page}&`) request.limit && (url += `limit=${request.limit}&`) request.sort && (url += `sort=${request.sort}&`) request.order && (url += `order=${request.order}&`) request.search && (url += `search=${request.search}&`) - return this.apiClient.get(url, headers) + const response = await this.apiClient.get(url, headers) + + return await parseResponse(response) } - static async getAllSecretsOfEnvironment( + async getAllSecretsOfEnvironment( request: GetAllSecretsOfEnvironmentRequest, headers?: Record - ): Promise { + ): Promise> { let url = `/api/secret/${request.projectId}/${request.environmentId}` request.page && (url += `page=${request.page}&`) request.limit && (url += `limit=${request.limit}&`) request.sort && (url += `sort=${request.sort}&`) request.order && (url += `order=${request.order}&`) request.search && (url += `search=${request.search}&`) - return this.apiClient.get( - url, - headers - ) + const response = await this.apiClient.get(url, headers) + + return await parseResponse(response) } } diff --git a/packages/api-client/src/types/secret.types.d.ts b/packages/api-client/src/types/secret.types.d.ts index 033666e6..20f00fad 100644 --- a/packages/api-client/src/types/secret.types.d.ts +++ b/packages/api-client/src/types/secret.types.d.ts @@ -1,3 +1,5 @@ +import { Page } from '../../../../apps/cli/src/types/index.types' + export interface CreateSecretRequest { projectId: string name: string @@ -82,30 +84,31 @@ export interface GetAllSecretsOfProjectRequest { order?: string search?: string } -export interface GetAllSecretsOfProjectResponse { - secret: { - id: string - name: string - createdAt: string - updatedAt: string - rotateAt: string - note: string | null - lastUpdatedById: string - projectId: string - lastUpdatedBy: { +export interface GetAllSecretsOfProjectResponse + extends Page<{ + secret: { id: string name: string + createdAt: string + updatedAt: string + rotateAt: 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 GetAllSecretsOfEnvironmentRequest { projectId: string @@ -116,8 +119,9 @@ export interface GetAllSecretsOfEnvironmentRequest { order?: string search?: string } -export interface GetAllSecretsOfEnvironmentResponse { - name: string - value: string - isPlaintext: boolean -} +export interface GetAllSecretsOfEnvironmentResponse + extends Page<{ + name: string + value: string + isPlaintext: boolean + }> {} diff --git a/packages/api-client/tests/secret.spec.ts b/packages/api-client/tests/secret.spec.ts index 35ad1825..294ad912 100644 --- a/packages/api-client/tests/secret.spec.ts +++ b/packages/api-client/tests/secret.spec.ts @@ -1,7 +1,12 @@ -import client from '@package/client' -import SecretController from '@package/controllers/secret/secret' +import { APIClient } from '../src/core/client' +import SecretController from '../src/controllers/secret/secret' describe('Get Variable tests', () => { + const backendUrl = process.env.BACKEND_URL + + const client = new APIClient(backendUrl) + const secretController = new SecretController(backendUrl) + const email = 'johndoe@example.com' let projectId: string | null let workspaceId: string | null @@ -10,42 +15,45 @@ describe('Get Variable tests', () => { beforeAll(async () => { //Create the user's workspace - 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 // Create a project - const projectResponse = (await client.post( - `/api/project/${workspaceId}`, - { - name: 'Project', - storePrivateKey: true - }, - { - 'x-e2e-user-email': email - } - )) as any - + const projectResponse = (await ( + await client.post( + `/api/project/${workspaceId}`, + { + name: 'Project', + storePrivateKey: true + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any projectId = projectResponse.id - // Create an environment - 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 }) @@ -64,7 +72,7 @@ describe('Get Variable tests', () => { // Create a Secret it('should create a secret', async () => { - const secret = await SecretController.createSecret( + const secret = await secretController.createSecret( { name: 'Secret 1', note: 'Secret 1 note', @@ -78,28 +86,30 @@ describe('Get Variable tests', () => { }, { 'x-e2e-user-email': email } ) - expect(secret.projectId).toBe(projectId) - expect(secret.project.workspaceId).toBe(workspaceId) - expect(secret.name).toBe('Secret 1') - expect(secret.versions.length).toBe(1) - secretId = secret.id + + expect(secret.data.projectId).toBe(projectId) + expect(secret.data.project.workspaceId).toBe(workspaceId) + expect(secret.data.name).toBe('Secret 1') + expect(secret.data.versions.length).toBe(1) + expect(secret.error).toBe(null) + secretId = secret.data.id }) // Update Name of a Secret it('should update name of a secret', async () => { - const updatedSecret = await SecretController.updateSecret( + const updatedSecret = await secretController.updateSecret( { name: 'Updated Secret 1', secretId }, { 'x-e2e-user-email': email } ) - expect(updatedSecret.secret.name).toBe('Updated Secret 1') + expect(updatedSecret.data.secret.name).toBe('Updated Secret 1') }) - // Add Version to a Secret + // // Add Version to a Secret it('should add version of a secret', async () => { - const updatedSecret = await SecretController.updateSecret( + const updatedSecret = await secretController.updateSecret( { entries: [ { @@ -111,39 +121,38 @@ describe('Get Variable tests', () => { }, { 'x-e2e-user-email': email } ) - console.log(updatedSecret) - expect(updatedSecret.updatedVersions.length).toBe(1) + expect(updatedSecret.data.updatedVersions.length).toBe(1) }) - // RollBack a Particular Version of a Secret + // // RollBack a Particular Version of a Secret it('should roll back a version of a secret', async () => { - const rollbackSecret = await SecretController.rollbackSecret( + const rollbackSecret = await secretController.rollbackSecret( { secretId, environmentId: environment.id, version: 1 }, { 'x-e2e-user-email': email } ) - expect(rollbackSecret.count).toBe(1) + expect(rollbackSecret.data.count).toBe(1) }) - // Get all secrets of a Project + // // Get all secrets of a Project it('should get all secrets of a project', async () => { - const secrets: any = await SecretController.getAllSecretsOfProject( + const secrets: any = await secretController.getAllSecretsOfProject( { projectId }, { 'x-e2e-user-email': email } ) - expect(secrets.length).toBe(1) + expect(secrets.data.items.length).toBe(1) }) - // Get all secrets of an Environment + // // Get all secrets of an Environment it('should get all secrets of an environment', async () => { - const secrets: any = await SecretController.getAllSecretsOfEnvironment( + const secrets: any = await secretController.getAllSecretsOfEnvironment( { environmentId: environment.id, projectId: projectId }, { 'x-e2e-user-email': email } ) - expect(secrets.length).toBe(1) - secrets.forEach((secret) => { + expect(secrets.data.length).toBe(1) + secrets.data.forEach((secret) => { expect(secret).toHaveProperty('name') expect(typeof secret.name).toBe('string') @@ -155,16 +164,16 @@ describe('Get Variable tests', () => { }) }) - // Delete a Secert from a Project + // // Delete a Secert from a Project it('should delete a secret', async () => { - await SecretController.deleteSecret( + await secretController.deleteSecret( { secretId }, { 'x-e2e-user-email': email } ) - const secrets: any = await SecretController.getAllSecretsOfProject( + const secrets: any = await secretController.getAllSecretsOfProject( { projectId }, { 'x-e2e-user-email': email } ) - expect(secrets.length).toBe(0) + expect(secrets.data.items.length).toBe(0) }) })