diff --git a/apps/api/src/secret/secret.e2e.spec.ts b/apps/api/src/secret/secret.e2e.spec.ts index dec86537..05b0db26 100644 --- a/apps/api/src/secret/secret.e2e.spec.ts +++ b/apps/api/src/secret/secret.e2e.spec.ts @@ -596,16 +596,16 @@ describe('Secret Controller Tests', () => { it('should be able to fetch all secrets', async () => { const response = await app.inject({ method: 'GET', - url: `/secret/${project1.id}`, + url: `/secret/${project1.id}?page=0&limit=10`, headers: { 'x-e2e-user-email': user1.email } }) expect(response.statusCode).toBe(200) - expect(response.json().length).toBe(1) + expect(response.json().items.length).toBe(1) - const { secret, values } = response.json()[0] + const { secret, values } = response.json().items[0] expect(secret.id).toBeDefined() expect(secret.name).toBeDefined() expect(secret.note).toBeDefined() @@ -615,21 +615,36 @@ describe('Secret Controller Tests', () => { const value = values[0] expect(value.environment).toBeDefined() expect(value.value).not.toEqual('Secret 1 value') + + //check metadata + const metadata = response.json().metadata + expect(metadata.totalCount).toEqual(1) + expect(metadata.links.self).toEqual( + `/secret/${project1.id}?decryptValue=false&page=0&limit=10&sort=name&order=asc&search=` + ) + expect(metadata.links.first).toEqual( + `/secret/${project1.id}?decryptValue=false&page=0&limit=10&sort=name&order=asc&search=` + ) + expect(metadata.links.previous).toBeNull() + expect(metadata.links.next).toBeNull() + expect(metadata.links.last).toEqual( + `/secret/${project1.id}?decryptValue=false&page=0&limit=10&sort=name&order=asc&search=` + ) }) it('should be able to fetch all secrets decrypted', async () => { const response = await app.inject({ method: 'GET', - url: `/secret/${project1.id}?decryptValue=true`, + url: `/secret/${project1.id}?decryptValue=true&page=0&limit=10`, headers: { 'x-e2e-user-email': user1.email } }) expect(response.statusCode).toBe(200) - expect(response.json().length).toBe(1) + expect(response.json().items.length).toBe(1) - const { secret, values } = response.json()[0] + const { secret, values } = response.json().items[0] expect(secret.id).toBeDefined() expect(secret.name).toBeDefined() expect(secret.note).toBeDefined() @@ -639,6 +654,21 @@ describe('Secret Controller Tests', () => { const value = values[0] expect(value.environment).toBeDefined() expect(value.value).toEqual('Secret 1 value') + + //check metadata + const metadata = response.json().metadata + expect(metadata.totalCount).toEqual(1) + expect(metadata.links.self).toEqual( + `/secret/${project1.id}?decryptValue=true&page=0&limit=10&sort=name&order=asc&search=` + ) + expect(metadata.links.first).toEqual( + `/secret/${project1.id}?decryptValue=true&page=0&limit=10&sort=name&order=asc&search=` + ) + expect(metadata.links.previous).toBeNull() + expect(metadata.links.next).toBeNull() + expect(metadata.links.last).toEqual( + `/secret/${project1.id}?decryptValue=true&page=0&limit=10&sort=name&order=asc&search=` + ) }) it('should not be able to fetch all secrets decrypted if the project does not store the private key', async () => { diff --git a/apps/api/src/secret/service/secret.service.ts b/apps/api/src/secret/service/secret.service.ts index 2b38d3bf..df1909db 100644 --- a/apps/api/src/secret/service/secret.service.ts +++ b/apps/api/src/secret/service/secret.service.ts @@ -31,6 +31,7 @@ import { ChangeNotification, ChangeNotificationEvent } from 'src/socket/socket.types' +import { paginate } from '../../common/paginate' @Injectable() export class SecretService { @@ -530,7 +531,7 @@ export class SecretService { } }, skip: page * limit, - take: limit, + take: Number(limit), orderBy: { [sort]: order } @@ -615,7 +616,32 @@ export class SecretService { } } - return Array.from(secretsWithEnvironmentalValues.values()) + const items = Array.from(secretsWithEnvironmentalValues.values()) + + //Calculate pagination metadata + const totalCount = await this.prisma.secret.count({ + where: { + projectId, + name: { + contains: search + } + } + }) + + const metadata = paginate( + totalCount, + `/secret/${projectId}`, + { + page: Number(page), + limit: Number(limit), + sort, + order, + search + }, + { decryptValue } + ) + + return { items, metadata } } private async secretExists(