Skip to content

Commit

Permalink
More tests bad requests to PUT /projects
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn committed Oct 29, 2024
1 parent 50a5271 commit b95e24c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/server/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export default async function routes(
{
schema: {
body: Type.Object({
projectName: Type.String(),
projectName: Type.String({ minLength: 1 }),
projectKey: HEX_STRING_32_BYTES,
encryptionKeys: Type.Object({
auth: HEX_STRING_32_BYTES,
Expand Down
34 changes: 33 additions & 1 deletion src/server/test/add-project-endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import assert from 'node:assert/strict'
import test from 'node:test'
import { omit } from '../../lib/omit.js'
import { projectKeyToPublicId } from '../../utils.js'
import { createTestServer, randomAddProjectBody } from './test-helpers.js'
import {
createTestServer,
randomAddProjectBody,
randomHex,
} from './test-helpers.js'

test('request missing project name', async (t) => {
const server = createTestServer(t)
Expand Down Expand Up @@ -40,6 +44,18 @@ test('request missing project key', async (t) => {
assert.equal(response.statusCode, 400)
})

test("request with a project key that's too short", async (t) => {
const server = createTestServer(t)

const response = await server.inject({
method: 'PUT',
url: '/projects',
body: { ...randomAddProjectBody(), projectKey: randomHex(31) },
})

assert.equal(response.statusCode, 400)
})

test('request missing any encryption keys', async (t) => {
const server = createTestServer(t)

Expand Down Expand Up @@ -68,6 +84,22 @@ test('request missing an encryption key', async (t) => {
assert.equal(response.statusCode, 400)
})

test("request with an encryption key that's too short", async (t) => {
const server = createTestServer(t)
const body = randomAddProjectBody()

const response = await server.inject({
method: 'PUT',
url: '/projects',
body: {
...body,
encryptionKeys: { ...body.encryptionKeys, config: randomHex(31) },
},
})

assert.equal(response.statusCode, 400)
})

test('adding a project', async (t) => {
const server = createTestServer(t)

Expand Down
2 changes: 1 addition & 1 deletion src/server/test/test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function createTestServer(t, serverOptions) {
return server
}

const randomHex = (length = 32) =>
export const randomHex = (length = 32) =>
Buffer.from(randomBytes(length)).toString('hex')

export const randomAddProjectBody = () => ({
Expand Down

0 comments on commit b95e24c

Please sign in to comment.