Skip to content

Commit

Permalink
tests(api,schema): Enhance secret and variable tests to validate stri…
Browse files Browse the repository at this point in the history
…ctly; update schemas for them
  • Loading branch information
muntaxir4 committed Dec 16, 2024
1 parent 1794c20 commit 9b6191f
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 49 deletions.
111 changes: 101 additions & 10 deletions apps/api/src/secret/secret.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,14 +619,28 @@ describe('Secret Controller Tests', () => {
expect(response.json().items.length).toBe(1)

const { secret, values } = response.json().items[0]
expect(secret.id).toBeDefined()
expect(secret.name).toBeDefined()
expect(secret.note).toBeDefined()
expect(secret.projectId).toBeDefined()
expect(secret).toStrictEqual({
id: secret1.id,
name: secret1.name,
slug: secret1.slug,
note: secret1.note,
projectId: project1.id,
lastUpdatedById: secret1.lastUpdatedById,
lastUpdatedBy: {
id: user1.id,
name: user1.name
},
createdAt: secret1.createdAt.toISOString(),
updatedAt: secret1.updatedAt.toISOString(),
rotateAt: secret1.rotateAt.toISOString()
})
expect(values.length).toBe(1)

const value = values[0]
expect(value.environment).toBeDefined()
expect(value.version).toBe(1)
expect(value.environment.id).toBe(environment1.id)
expect(value.environment.slug).toBe(environment1.slug)
expect(value.environment.name).toBe(environment1.name)
expect(value.value).not.toEqual('Secret 1 value')

//check metadata
Expand All @@ -645,6 +659,69 @@ describe('Secret Controller Tests', () => {
)
})

it('should be able to fetch only new versions of secrets', async () => {
// Update secret1
await secretService.updateSecret(user1, secret1.slug, {
entries: [
{
value: 'Secret new 1 value',
environmentSlug: environment1.slug
}
]
})

const response = await app.inject({
method: 'GET',
url: `/secret/${project1.slug}?page=0&limit=10`,
headers: {
'x-e2e-user-email': user1.email
}
})

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

const { secret, values } = response.json().items[0]
expect(secret).toStrictEqual({
id: secret1.id,
name: secret1.name,
slug: secret1.slug,
note: secret1.note,
projectId: project1.id,
lastUpdatedById: secret1.lastUpdatedById,
lastUpdatedBy: {
id: user1.id,
name: user1.name
},
createdAt: secret1.createdAt.toISOString(),
updatedAt: expect.any(String),
rotateAt: secret1.rotateAt.toISOString()
})
expect(values.length).toBe(1)

const value = values[0]
expect(value.version).toBe(2)
expect(value.environment.id).toBe(environment1.id)
expect(value.environment.slug).toBe(environment1.slug)
expect(value.environment.name).toBe(environment1.name)
expect(value.value).not.toEqual('Secret 1 new value')

//check metadata
const metadata = response.json().metadata
expect(metadata.totalCount).toEqual(1)
expect(metadata.links.self).toEqual(
`/secret/${project1.slug}?decryptValue=false&page=0&limit=10&sort=name&order=asc&search=`
)
expect(metadata.links.first).toEqual(
`/secret/${project1.slug}?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.slug}?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',
Expand All @@ -658,14 +735,28 @@ describe('Secret Controller Tests', () => {
expect(response.json().items.length).toBe(1)

const { secret, values } = response.json().items[0]
expect(secret.id).toBeDefined()
expect(secret.name).toBeDefined()
expect(secret.note).toBeDefined()
expect(secret.projectId).toBeDefined()
expect(secret).toStrictEqual({
id: secret1.id,
name: secret1.name,
slug: secret1.slug,
note: secret1.note,
projectId: project1.id,
lastUpdatedById: secret1.lastUpdatedById,
lastUpdatedBy: {
id: user1.id,
name: user1.name
},
createdAt: secret1.createdAt.toISOString(),
updatedAt: secret1.updatedAt.toISOString(),
rotateAt: secret1.rotateAt.toISOString()
})
expect(values.length).toBe(1)

const value = values[0]
expect(value.environment).toBeDefined()
expect(value.version).toBe(1)
expect(value.environment.id).toBe(environment1.id)
expect(value.environment.slug).toBe(environment1.slug)
expect(value.environment.name).toBe(environment1.name)
expect(value.value).toEqual('Secret 1 value')

//check metadata
Expand Down
83 changes: 81 additions & 2 deletions apps/api/src/variable/variable.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,13 +597,92 @@ describe('Variable Controller Tests', () => {

const { variable, values } = response.json().items[0]
expect(variable).toBeDefined()
expect(variable.versions).toBeUndefined()
expect(values).toBeDefined()
expect(values.length).toBe(1)
expect(values[0].value).toBe('Variable 1 value')
expect(values[0].version).toBe(1)
expect(values[0].environment.id).toBe(environment1.id)
expect(values[0].environment.slug).toBe(environment1.slug)
expect(variable.id).toBe(variable1.id)
expect(variable.name).toBe('Variable 1')
expect(values[0].environment.name).toBe(environment1.name)
expect(variable).toStrictEqual({
id: variable1.id,
name: variable1.name,
slug: variable1.slug,
note: variable1.note,
projectId: project1.id,
lastUpdatedById: variable1.lastUpdatedById,
lastUpdatedBy: {
id: user1.id,
name: user1.name
},
createdAt: variable1.createdAt.toISOString(),
updatedAt: variable1.updatedAt.toISOString()
})

//check metadata
const metadata = response.json().metadata
expect(metadata.totalCount).toEqual(1)
expect(metadata.links.self).toEqual(
`/variable/${project1.slug}?page=0&limit=10&sort=name&order=asc&search=`
)
expect(metadata.links.first).toEqual(
`/variable/${project1.slug}?page=0&limit=10&sort=name&order=asc&search=`
)
expect(metadata.links.previous).toBeNull()
expect(metadata.links.next).toBeNull()
expect(metadata.links.last).toEqual(
`/variable/${project1.slug}?page=0&limit=10&sort=name&order=asc&search=`
)
})

it('should be able to fetch only new versions of variable', async () => {
// update variable1
await variableService.updateVariable(user1, variable1.slug, {
entries: [
{
environmentSlug: environment1.slug,
value: 'Variable 1 new value'
}
]
})

const response = await app.inject({
method: 'GET',
url: `/variable/${project1.slug}?page=0&limit=10`,
headers: {
'x-e2e-user-email': user1.email
}
})

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

const { variable, values } = response.json().items[0]
expect(variable).toBeDefined()
expect(variable.versions).toBeUndefined()
expect(values).toBeDefined()
expect(values.length).toBe(1)
expect(values[0].value).toBe('Variable 1 new value')
expect(values[0].version).toBe(2)
expect(values[0].environment.id).toBe(environment1.id)
expect(values[0].environment.slug).toBe(environment1.slug)
expect(values[0].environment.name).toBe(environment1.name)

expect(variable).toStrictEqual({
id: variable1.id,
name: variable1.name,
slug: variable1.slug,
note: variable1.note,
projectId: project1.id,
lastUpdatedById: variable1.lastUpdatedById,
lastUpdatedBy: {
id: user1.id,
name: user1.name
},
createdAt: variable1.createdAt.toISOString(),
updatedAt: expect.any(String)
})

//check metadata
const metadata = response.json().metadata
Expand Down
11 changes: 1 addition & 10 deletions packages/schema/src/secret/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,7 @@ export const GetAllSecretsOfProjectResponseSchema = PageResponseSchema(
lastUpdatedBy: z.object({
id: z.string(),
name: z.string()
}),
versions: z.array(
z.object({
environment: z.object({
id: EnvironmentSchema.shape.id,
slug: EnvironmentSchema.shape.slug,
name: EnvironmentSchema.shape.name
})
})
)
})
}),
values: z.object({
environment: z.object({
Expand Down
8 changes: 0 additions & 8 deletions packages/schema/src/variable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,6 @@ export const GetAllVariablesOfProjectRequestSchema = PageRequestSchema.extend({
export const GetAllVariablesOfProjectResponseSchema = PageResponseSchema(
z.object({
variable: VariableSchema.omit({ project: true, versions: true }).extend({
versions: z.array(
z.object({
environment: z.object({
id: EnvironmentSchema.shape.id,
slug: EnvironmentSchema.shape.slug
})
})
),
lastUpdatedBy: z.object({
id: z.string(),
name: z.string()
Expand Down
11 changes: 1 addition & 10 deletions packages/schema/tests/secret.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,7 @@ describe('Secret Schema Tests', () => {
lastUpdatedBy: {
id: 'user123',
name: 'John Doe'
},
versions: [
{
environment: {
id: 'env123',
slug: 'development',
name: 'Development'
}
}
]
}
},
values: {
environment: {
Expand Down
10 changes: 1 addition & 9 deletions packages/schema/tests/variable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,7 @@ describe('Variable Schema Tests', () => {
lastUpdatedBy: {
id: 'user123',
name: 'John Doe'
},
versions: [
{
environment: {
id: 'env123',
slug: 'development'
}
}
]
}
},
values: [
{
Expand Down

0 comments on commit 9b6191f

Please sign in to comment.