From b95e24c1880966317eaf03fa7d4bf81c7946187f Mon Sep 17 00:00:00 2001 From: Evan Hahn Date: Tue, 29 Oct 2024 00:06:56 +0000 Subject: [PATCH] More tests bad requests to `PUT /projects` Closes [#17]. [#17]: https://github.com/digidem/comapeo-cloud/issues/17 --- src/server/routes.js | 2 +- src/server/test/add-project-endpoint.js | 34 ++++++++++++++++++++++++- src/server/test/test-helpers.js | 2 +- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/server/routes.js b/src/server/routes.js index 66285217..8ba1ccf9 100644 --- a/src/server/routes.js +++ b/src/server/routes.js @@ -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, diff --git a/src/server/test/add-project-endpoint.js b/src/server/test/add-project-endpoint.js index 2d06793b..7d26ca60 100644 --- a/src/server/test/add-project-endpoint.js +++ b/src/server/test/add-project-endpoint.js @@ -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) @@ -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) @@ -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) diff --git a/src/server/test/test-helpers.js b/src/server/test/test-helpers.js index f3875e18..5622176f 100644 --- a/src/server/test/test-helpers.js +++ b/src/server/test/test-helpers.js @@ -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 = () => ({