Skip to content

Commit

Permalink
refactor(api): Update secret and variable fetching endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Jul 11, 2024
1 parent 871b6cd commit 7d9acd0
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 61 deletions.
4 changes: 2 additions & 2 deletions apps/api/src/secret/controller/secret.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class SecretController {
return await this.secretService.deleteSecret(user, secretId)
}

@Get('/all/:projectId')
@Get('/:projectId')
@RequiredApiKeyAuthorities(Authority.READ_SECRET)
async getAllSecretsOfProject(
@CurrentUser() user: User,
Expand All @@ -88,7 +88,7 @@ export class SecretController {
)
}

@Get('/all/:projectId/:environmentId')
@Get('/:projectId/:environmentId')
@RequiredApiKeyAuthorities(Authority.READ_SECRET)
async getAllSecretsOfEnvironment(
@CurrentUser() user: User,
Expand Down
30 changes: 16 additions & 14 deletions apps/api/src/secret/secret.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,9 @@ describe('Secret Controller Tests', () => {
})

expect(response.statusCode).toBe(200)
expect(response.json().name).toEqual('Updated Secret 1')
expect(response.json().note).toEqual('Updated Secret 1 note')
expect(response.json().secret.name).toEqual('Updated Secret 1')
expect(response.json().secret.note).toEqual('Updated Secret 1 note')
expect(response.json().updatedVersions.length).toBe(0)

const secretVersion = await prisma.secretVersion.findMany({
where: {
Expand Down Expand Up @@ -361,6 +362,7 @@ describe('Secret Controller Tests', () => {
})

expect(response.statusCode).toBe(200)
expect(response.json().updatedVersions.length).toBe(1)

const secretVersion = await prisma.secretVersion.findMany({
where: {
Expand Down Expand Up @@ -579,7 +581,7 @@ describe('Secret Controller Tests', () => {

const response = await app.inject({
method: 'GET',
url: `/secret/all/${project2.id}?decryptValue=true`,
url: `/secret/${project2.id}?decryptValue=true`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -594,7 +596,7 @@ describe('Secret Controller Tests', () => {
it('should be able to fetch all secrets', async () => {
const response = await app.inject({
method: 'GET',
url: `/secret/all/${project1.id}`,
url: `/secret/${project1.id}`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -618,7 +620,7 @@ describe('Secret Controller Tests', () => {
it('should be able to fetch all secrets decrypted', async () => {
const response = await app.inject({
method: 'GET',
url: `/secret/all/${project1.id}?decryptValue=true`,
url: `/secret/${project1.id}?decryptValue=true`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -642,7 +644,7 @@ describe('Secret Controller Tests', () => {
it('should not be able to fetch all secrets decrypted if the project does not store the private key', async () => {
const response = await app.inject({
method: 'GET',
url: `/secret/all/${project2.id}?decryptValue=true`,
url: `/secret/${project2.id}?decryptValue=true`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -667,7 +669,7 @@ describe('Secret Controller Tests', () => {

const response = await app.inject({
method: 'GET',
url: `/secret/all/${project1.id}?decryptValue=true`,
url: `/secret/${project1.id}?decryptValue=true`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -691,7 +693,7 @@ describe('Secret Controller Tests', () => {
it('should not be able to fetch all secrets if the user has no access to the project', async () => {
const response = await app.inject({
method: 'GET',
url: `/secret/all/${project1.id}`,
url: `/secret/${project1.id}`,
headers: {
'x-e2e-user-email': user2.email
}
Expand All @@ -706,7 +708,7 @@ describe('Secret Controller Tests', () => {
it('should not be able to fetch all secrets if the project does not exist', async () => {
const response = await app.inject({
method: 'GET',
url: `/secret/all/non-existing-project-id`,
url: `/secret/non-existing-project-id`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -721,7 +723,7 @@ describe('Secret Controller Tests', () => {
it('should be able to fetch all secrets by project and environment', async () => {
const response = await app.inject({
method: 'GET',
url: `/secret/all/${project1.id}/${environment1.id}`,
url: `/secret/${project1.id}/${environment1.id}`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -739,7 +741,7 @@ describe('Secret Controller Tests', () => {
it('should not be able to fetch all secrets by project and environment if project does not exists', async () => {
const response = await app.inject({
method: 'GET',
url: `/secret/all/non-existing-project-id/${environment1.id}`,
url: `/secret/non-existing-project-id/${environment1.id}`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -754,7 +756,7 @@ describe('Secret Controller Tests', () => {
it('should not be able to fetch all secrets by project and environment if environment does not exists', async () => {
const response = await app.inject({
method: 'GET',
url: `/secret/all/${project1.id}/non-existing-environment-id`,
url: `/secret/${project1.id}/non-existing-environment-id`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -769,7 +771,7 @@ describe('Secret Controller Tests', () => {
it('should not be able to fetch all secrets by project and environment if the user has no access to the project', async () => {
const response = await app.inject({
method: 'GET',
url: `/secret/all/${project1.id}/${environment1.id}`,
url: `/secret/${project1.id}/${environment1.id}`,
headers: {
'x-e2e-user-email': user2.email
}
Expand Down Expand Up @@ -808,7 +810,7 @@ describe('Secret Controller Tests', () => {

const response = await app.inject({
method: 'GET',
url: `/secret/all/${project2.id}/${environment.id}`,
url: `/secret/${project2.id}/${environment.id}`,
headers: {
'x-e2e-user-email': user1.email
}
Expand Down
33 changes: 17 additions & 16 deletions apps/api/src/secret/service/secret.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,21 +207,10 @@ export class SecretService {
: undefined,
lastUpdatedById: user.id
},
include: {
project: {
select: {
workspaceId: true
}
},
versions: {
select: {
environmentId: true,
value: true
},
orderBy: {
version: 'desc'
}
}
select: {
id: true,
name: true,
note: true
}
})
)
Expand Down Expand Up @@ -254,6 +243,12 @@ export class SecretService {
createdById: user.id,
environmentId: entry.environmentId,
secretId: secret.id
},
select: {
id: true,
environmentId: true,
value: true,
version: true
}
})
)
Expand All @@ -263,6 +258,12 @@ export class SecretService {
// Make the transaction
const tx = await this.prisma.$transaction(op)
const updatedSecret = tx[0]
const updatedVersions = tx.slice(1)

const result = {
secret: updatedSecret,
updatedVersions: updatedVersions
}

// Notify the new secret version through Redis
if (dto.entries && dto.entries.length > 0) {
Expand Down Expand Up @@ -303,7 +304,7 @@ export class SecretService {

this.logger.log(`User ${user.id} updated secret ${secret.id}`)

return updatedSecret
return result
}

async rollbackSecret(
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/variable/controller/variable.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class VariableController {
return await this.variableService.deleteVariable(user, variableId)
}

@Get('/all/:projectId')
@Get('/:projectId')
@RequiredApiKeyAuthorities(Authority.READ_VARIABLE)
async getAllVariablesOfProject(
@CurrentUser() user: User,
Expand All @@ -86,7 +86,7 @@ export class VariableController {
)
}

@Get('/all/:projectId/:environmentId')
@Get('/:projectId/:environmentId')
@RequiredApiKeyAuthorities(Authority.READ_VARIABLE)
async getAllSecretsOfEnvironment(
@CurrentUser() user: User,
Expand Down
32 changes: 16 additions & 16 deletions apps/api/src/variable/service/variable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,21 +206,10 @@ export class VariableService {
note: dto.note,
lastUpdatedById: user.id
},
include: {
project: {
select: {
workspaceId: true
}
},
versions: {
select: {
environmentId: true,
value: true
},
orderBy: {
version: 'desc'
}
}
select: {
id: true,
name: true,
note: true
}
})
)
Expand Down Expand Up @@ -253,6 +242,12 @@ export class VariableService {
createdById: user.id,
environmentId: entry.environmentId,
variableId: variable.id
},
select: {
id: true,
environmentId: true,
value: true,
version: true
}
})
)
Expand All @@ -262,6 +257,11 @@ export class VariableService {
// Make the transaction
const tx = await this.prisma.$transaction(op)
const updatedVariable = tx[0]
const updatedVersions = tx.slice(1)
const result = {
variable: updatedVariable,
updatedVersions: updatedVersions
}

// Notify the new variable version through Redis
if (dto.entries && dto.entries.length > 0) {
Expand Down Expand Up @@ -304,7 +304,7 @@ export class VariableService {

this.logger.log(`User ${user.id} updated variable ${variable.id}`)

return updatedVariable
return result
}

async rollbackVariable(
Expand Down
20 changes: 11 additions & 9 deletions apps/api/src/variable/variable.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,9 @@ describe('Variable Controller Tests', () => {
})

expect(response.statusCode).toBe(200)
expect(response.json().name).toEqual('Updated Variable 1')
expect(response.json().note).toEqual('Updated Variable 1 note')
expect(response.json().variable.name).toEqual('Updated Variable 1')
expect(response.json().variable.note).toEqual('Updated Variable 1 note')
expect(response.json().updatedVersions.length).toEqual(0)

const variableVersion = await prisma.variableVersion.findMany({
where: {
Expand Down Expand Up @@ -381,6 +382,7 @@ describe('Variable Controller Tests', () => {
})

expect(response.statusCode).toBe(200)
expect(response.json().updatedVersions.length).toEqual(1)

const variableVersion = await prisma.variableVersion.findMany({
where: {
Expand Down Expand Up @@ -573,7 +575,7 @@ describe('Variable Controller Tests', () => {
it('should be able to fetch all variables', async () => {
const response = await app.inject({
method: 'GET',
url: `/variable/all/${project1.id}`,
url: `/variable/${project1.id}`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -595,7 +597,7 @@ describe('Variable Controller Tests', () => {
it('should not be able to fetch all variables if the user has no access to the project', async () => {
const response = await app.inject({
method: 'GET',
url: `/variable/all/${project1.id}`,
url: `/variable/${project1.id}`,
headers: {
'x-e2e-user-email': user2.email
}
Expand All @@ -610,7 +612,7 @@ describe('Variable Controller Tests', () => {
it('should not be able to fetch all variables if the project does not exist', async () => {
const response = await app.inject({
method: 'GET',
url: `/variable/all/non-existing-project-id`,
url: `/variable/non-existing-project-id`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -625,7 +627,7 @@ describe('Variable Controller Tests', () => {
it('should be able to fetch all variables by project and environment', async () => {
const response = await app.inject({
method: 'GET',
url: `/variable/all/${project1.id}/${environment1.id}`,
url: `/variable/${project1.id}/${environment1.id}`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -643,7 +645,7 @@ describe('Variable Controller Tests', () => {
it('should not be able to fetch all variables by project and environment if the user has no access to the project', async () => {
const response = await app.inject({
method: 'GET',
url: `/variable/all/${project1.id}/${environment1.id}`,
url: `/variable/${project1.id}/${environment1.id}`,
headers: {
'x-e2e-user-email': user2.email
}
Expand All @@ -658,7 +660,7 @@ describe('Variable Controller Tests', () => {
it('should not be able to fetch all variables by project and environment if the project does not exist', async () => {
const response = await app.inject({
method: 'GET',
url: `/variable/all/non-existing-project-id/${environment1.id}`,
url: `/variable/non-existing-project-id/${environment1.id}`,
headers: {
'x-e2e-user-email': user1.email
}
Expand All @@ -673,7 +675,7 @@ describe('Variable Controller Tests', () => {
it('should not be able to fetch all variables by project and environment if the environment does not exist', async () => {
const response = await app.inject({
method: 'GET',
url: `/variable/all/${project1.id}/non-existing-environment-id`,
url: `/variable/${project1.id}/non-existing-environment-id`,
headers: {
'x-e2e-user-email': user1.email
}
Expand Down
2 changes: 1 addition & 1 deletion apps/cli/src/http/secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const SecretController = {
environmentId: string
): Promise<Configuration[]> {
const response = await fetch(
`${baseUrl}/api/secret/all/${projectId}/${environmentId}`,
`${baseUrl}/api/secret/${projectId}/${environmentId}`,
{
method: 'GET',
headers: {
Expand Down
2 changes: 1 addition & 1 deletion apps/cli/src/http/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const VariableController = {
environmentId: string
): Promise<Configuration[]> {
const response = await fetch(
`${baseUrl}/api/variable/all/${projectId}/${environmentId}`,
`${baseUrl}/api/variable/${projectId}/${environmentId}`,
{
method: 'GET',
headers: {
Expand Down

0 comments on commit 7d9acd0

Please sign in to comment.