From b73a67915da2940ea0aaa6839dd12347cb8b5419 Mon Sep 17 00:00:00 2001 From: eoussama Date: Sun, 6 Jul 2025 21:18:04 +0100 Subject: [PATCH] Added optional website ID for creation --- cypress/e2e/api-website.cy.ts | 33 +++++++++++++++++++++++++++++++++ src/app/api/websites/route.ts | 5 +++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/cypress/e2e/api-website.cy.ts b/cypress/e2e/api-website.cy.ts index 3fba48d3..7f7d17c3 100644 --- a/cypress/e2e/api-website.cy.ts +++ b/cypress/e2e/api-website.cy.ts @@ -1,3 +1,5 @@ +import { uuid } from '../../src/lib/crypto'; + describe('Website API tests', () => { Cypress.session.clearAllSavedSessions(); @@ -65,6 +67,37 @@ describe('Website API tests', () => { }); }); + it('Creates a website with a fixed ID.', () => { + cy.fixture('websites').then(data => { + const websiteCreate = data.websiteCreate; + const fixedId = uuid(); + cy.request({ + method: 'POST', + url: '/api/websites', + headers: { + 'Content-Type': 'application/json', + Authorization: Cypress.env('authorization'), + }, + body: { ...websiteCreate, id: fixedId }, + }).then(response => { + expect(response.status).to.eq(200); + expect(response.body).to.have.property('id', fixedId); + expect(response.body).to.have.property('name', 'Cypress Website'); + expect(response.body).to.have.property('domain', 'cypress.com'); + + // cleanup + cy.request({ + method: 'DELETE', + url: `/api/websites/${fixedId}`, + headers: { + 'Content-Type': 'application/json', + Authorization: Cypress.env('authorization'), + }, + }); + }); + }); + }); + it('Returns all tracked websites.', () => { cy.request({ method: 'GET', diff --git a/src/app/api/websites/route.ts b/src/app/api/websites/route.ts index b8fb2a0b..7a2146f8 100644 --- a/src/app/api/websites/route.ts +++ b/src/app/api/websites/route.ts @@ -26,6 +26,7 @@ export async function POST(request: Request) { domain: z.string().max(500), shareId: z.string().max(50).nullable().optional(), teamId: z.string().nullable().optional(), + id: z.string().uuid().nullable().optional(), }); const { auth, body, error } = await parseRequest(request, schema); @@ -34,14 +35,14 @@ export async function POST(request: Request) { return error(); } - const { name, domain, shareId, teamId } = body; + const { id, name, domain, shareId, teamId } = body; if ((teamId && !(await canCreateTeamWebsite(auth, teamId))) || !(await canCreateWebsite(auth))) { return unauthorized(); } const data: any = { - id: uuid(), + id: id ?? uuid(), createdBy: auth.user.id, name, domain,