From 6784b5aeaab75eaacf38a5019d2352a60da883d9 Mon Sep 17 00:00:00 2001 From: tsudhakar87 Date: Thu, 30 Oct 2025 15:50:27 -0400 Subject: [PATCH 1/5] added endpoint logic --- apps/backend/lambdas/users/handler.ts | 36 ++++++++++++++++++++++++- apps/backend/lambdas/users/openapi.yaml | 18 +++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/apps/backend/lambdas/users/handler.ts b/apps/backend/lambdas/users/handler.ts index 54fd7d7..65b8e71 100644 --- a/apps/backend/lambdas/users/handler.ts +++ b/apps/backend/lambdas/users/handler.ts @@ -65,7 +65,41 @@ export const handler = async (event: any): Promise => { return json(200, { ok: true, route: 'PATCH /users/{userId}', pathParams: { userId }, body: { email: updatedUser!.email, name: updatedUser!.name, isAdmin: updatedUser!.is_admin } }); } - // <<< ROUTES-END + + // POST /{userId} // (dev server strips /users prefix) + if ( + normalizedPath.startsWith('/') && + normalizedPath.split('/').length === 2 && + method === 'POST' + ) { + const userId = normalizedPath.split('/')[1]; + if (!userId) return json(400, { message: 'userId is required' }); + const body = event.body + ? (JSON.parse(event.body) as Record) + : {}; + + // extract fields to create user + let email = body.email as string; + let name = body.name as string; + let isAdmin = body.isAdmin as boolean; + if (!email || !name || typeof isAdmin !== 'boolean') { + return json(400, { message: 'email, name, and isAdmin are required' }); + } + + // insert new user + await db + .insertInto('branch.users') + .values({ user_id: userId, email, name, is_admin: isAdmin }) + .execute(); + + return json(201, { + ok: true, + route: 'POST /users', + pathParams: { userId }, + body, + }); + } + // <<< ROUTES-END return json(404, { message: 'Not Found', path: normalizedPath, method }); } catch (err) { diff --git a/apps/backend/lambdas/users/openapi.yaml b/apps/backend/lambdas/users/openapi.yaml index 0f18d08..55253be 100644 --- a/apps/backend/lambdas/users/openapi.yaml +++ b/apps/backend/lambdas/users/openapi.yaml @@ -46,3 +46,21 @@ paths: description: OK '404': description: User not found + + /users: + post: + summary: POST /users + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + name: + type: string + isAdmin: + type: boolean + responses: + '201': + description: Success From 76a5caef8ab4d04950dcf57adb885ccb2d23747f Mon Sep 17 00:00:00 2001 From: tsudhakar87 Date: Thu, 30 Oct 2025 16:10:28 -0400 Subject: [PATCH 2/5] tests for 201 and 400 status cases --- .../lambdas/users/test/example.test.ts | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/apps/backend/lambdas/users/test/example.test.ts b/apps/backend/lambdas/users/test/example.test.ts index 8afaccb..8b0f327 100644 --- a/apps/backend/lambdas/users/test/example.test.ts +++ b/apps/backend/lambdas/users/test/example.test.ts @@ -31,4 +31,49 @@ test("patch user 404 test 🌞", async () => { }) }) expect(res.status).toBe(404); +}); + +test("POST user success case", async () => { + let res = await fetch("http://localhost:3000/users/5", { + method: "POST", + body: JSON.stringify({ + name: "Jane Branch", + email: "jane@branch.com", + isAdmin: true + }) + }); + + console.log(res) + expect(res.status).toBe(201); + + let body = await res.json(); + + expect(body.ok).toBe(true); + expect(body.body.name).toBe("Jane Branch"); + expect(body.body.email).toBe("jane@branch.com"); + expect(body.body.isAdmin).toBe(true); +}); + +test("POST user 400 case when invalid userId is sent", async () => { + let res = await fetch("http://localhost:3000/users/invalidUserId", { + method: "POST", + body: JSON.stringify({ + name: "Invalid User", + email: "", + isAdmin: false + }) + }); + + expect(res.status).toBe(400); +}); + +test("POST user 400 case when request sent with missing fields", async () => { + let res = await fetch("http://localhost:3000/users/6", { + method: "POST", + body: JSON.stringify({ + name: "Invalid User", + }) // missing email and admin fields + }); + + expect(res.status).toBe(400); }); \ No newline at end of file From a212e454ce01484c2fd3fc1c2ab389976cef2f02 Mon Sep 17 00:00:00 2001 From: tsudhakar87 Date: Thu, 30 Oct 2025 17:36:44 -0400 Subject: [PATCH 3/5] 409 handling --- apps/backend/lambdas/users/handler.ts | 6 ++++++ apps/backend/lambdas/users/openapi.yaml | 17 ++++++++++++++--- .../backend/lambdas/users/test/example.test.ts | 18 +++++++++++++++--- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/apps/backend/lambdas/users/handler.ts b/apps/backend/lambdas/users/handler.ts index 65b8e71..4667256 100644 --- a/apps/backend/lambdas/users/handler.ts +++ b/apps/backend/lambdas/users/handler.ts @@ -86,6 +86,12 @@ export const handler = async (event: any): Promise => { return json(400, { message: 'email, name, and isAdmin are required' }); } + // check if user already exists + const existingUser = await db.selectFrom("branch.users").where("user_id", "=", Number(userId)).selectAll().executeTakeFirst(); + if (existingUser) { + return json(409, { message: 'User already exists' }); + } + // insert new user await db .insertInto('branch.users') diff --git a/apps/backend/lambdas/users/openapi.yaml b/apps/backend/lambdas/users/openapi.yaml index 55253be..7a18dfc 100644 --- a/apps/backend/lambdas/users/openapi.yaml +++ b/apps/backend/lambdas/users/openapi.yaml @@ -19,7 +19,7 @@ paths: ok: type: boolean - /users/{userId}: + /{userId}: patch: summary: PATCH /users/{userId} parameters: @@ -47,9 +47,14 @@ paths: '404': description: User not found - /users: post: - summary: POST /users + summary: POST /users/{userId} + parameters: + - in: path + name: userId + required: true + schema: + type: string requestBody: required: true content: @@ -59,8 +64,14 @@ paths: properties: name: type: string + email: + type: string isAdmin: type: boolean responses: '201': description: Success + '400': + description: Bad Request + '409': + description: Conflict diff --git a/apps/backend/lambdas/users/test/example.test.ts b/apps/backend/lambdas/users/test/example.test.ts index 8b0f327..c668216 100644 --- a/apps/backend/lambdas/users/test/example.test.ts +++ b/apps/backend/lambdas/users/test/example.test.ts @@ -38,12 +38,11 @@ test("POST user success case", async () => { method: "POST", body: JSON.stringify({ name: "Jane Branch", - email: "jane@branch.com", + email: "jane1@branch.com", isAdmin: true }) }); - console.log(res) expect(res.status).toBe(201); let body = await res.json(); @@ -68,7 +67,7 @@ test("POST user 400 case when invalid userId is sent", async () => { }); test("POST user 400 case when request sent with missing fields", async () => { - let res = await fetch("http://localhost:3000/users/6", { + let res = await fetch("http://localhost:3000/users/4", { method: "POST", body: JSON.stringify({ name: "Invalid User", @@ -76,4 +75,17 @@ test("POST user 400 case when request sent with missing fields", async () => { }); expect(res.status).toBe(400); +}); + +test("POST user 409 case when user already exists", async () => { + let res = await fetch("http://localhost:3000/users/1", { + method: "POST", + body: JSON.stringify({ + name: "Existing User", + email: "some@email.com", + isAdmin: false + }) + }); + + expect(res.status).toBe(409); }); \ No newline at end of file From aa9263149a9d7595907d1be3ecf0e15f942fe81b Mon Sep 17 00:00:00 2001 From: tsudhakar87 Date: Thu, 13 Nov 2025 14:39:47 -0500 Subject: [PATCH 4/5] tests --- .../lambdas/users/test/user.unit.test.ts | 232 ++++++++++++++++++ apps/backend/lambdas/users/test/users.test.ts | 2 +- 2 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 apps/backend/lambdas/users/test/user.unit.test.ts diff --git a/apps/backend/lambdas/users/test/user.unit.test.ts b/apps/backend/lambdas/users/test/user.unit.test.ts new file mode 100644 index 0000000..f6ec1c4 --- /dev/null +++ b/apps/backend/lambdas/users/test/user.unit.test.ts @@ -0,0 +1,232 @@ +import { describe, test, expect, beforeEach, jest } from '@jest/globals'; + +// Mock the database module BEFORE importing handler +jest.mock('../db'); + +import { handler } from '../handler'; +import db from '../db'; + +const mockDb = db as any; + +// Helper function to create a POST event +function postEvent(userId: string, body: Record) { + return { + rawPath: `/${userId}`, + requestContext: { + http: { + method: 'POST', + }, + }, + body: JSON.stringify(body), + }; +} + +describe('POST /users/{userId} unit tests', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + describe('Input Validation', () => { + test('400: missing email field', async () => { + const res = await handler( + postEvent('test-user-1', { + name: 'John Doe', + isAdmin: false, + }) + ); + + expect(res.statusCode).toBe(400); + const json = JSON.parse(res.body); + expect(json.message).toBeDefined(); + expect(json.message).toContain('required'); + }); + + test('400: missing name field', async () => { + const res = await handler( + postEvent('test-user-2', { + email: 'john@example.com', + isAdmin: false, + }) + ); + + expect(res.statusCode).toBe(400); + const json = JSON.parse(res.body); + expect(json.message).toBeDefined(); + expect(json.message).toContain('required'); + }); + + test('400: missing isAdmin field', async () => { + const res = await handler( + postEvent('test-user-3', { + name: 'John Doe', + email: 'john@example.com', + }) + ); + + expect(res.statusCode).toBe(400); + const json = JSON.parse(res.body); + expect(json.message).toBeDefined(); + expect(json.message).toContain('required'); + }); + + test('400: isAdmin is not a boolean', async () => { + const res = await handler( + postEvent('test-user-4', { + name: 'John Doe', + email: 'john@example.com', + isAdmin: 'yes', + }) + ); + + expect(res.statusCode).toBe(400); + const json = JSON.parse(res.body); + expect(json.message).toBeDefined(); + expect(json.message).toContain('required'); + }); + + test('400: empty email field', async () => { + const res = await handler( + postEvent('test-user-5', { + name: 'John Doe', + email: '', + isAdmin: false, + }) + ); + + expect(res.statusCode).toBe(400); + const json = JSON.parse(res.body); + expect(json.message).toContain('required'); + }); + + test('400: empty name field', async () => { + const res = await handler( + postEvent('test-user-6', { + name: '', + email: 'john@example.com', + isAdmin: false, + }) + ); + + expect(res.statusCode).toBe(400); + const json = JSON.parse(res.body); + expect(json.message).toContain('required'); + }); + }); + + describe('Response Format', () => { + test('404: POST to root path returns not found', async () => { + const res = await handler({ + rawPath: '/', + requestContext: { + http: { + method: 'POST', + }, + }, + body: JSON.stringify({ + name: 'John Doe', + email: 'john@example.com', + isAdmin: false, + }), + }); + + expect(res.statusCode).toBe(404); + const json = JSON.parse(res.body); + expect(json.message).toBe('Not Found'); + expect(json.path).toBeDefined(); + expect(json.method).toBeDefined(); + }); + + test('response has correct HTTP headers', async () => { + // Setup mocks for successful user creation + mockDb.selectFrom.mockReturnValue({ + where: jest.fn().mockReturnValue({ + selectAll: jest.fn().mockReturnValue({ + executeTakeFirst: jest.fn().mockReturnValue(null), + }), + }), + }); + + mockDb.insertInto.mockReturnValue({ + values: jest.fn().mockReturnValue({ + execute: jest.fn().mockReturnValue(undefined), + }), + }); + + const res = await handler( + postEvent('format-test', { + name: 'Format Test', + email: 'format@example.com', + isAdmin: false, + }) + ); + + expect(res.headers?.['Content-Type']).toBe('application/json'); + expect(res.headers?.['Access-Control-Allow-Origin']).toBe('*'); + expect(res.headers?.['Access-Control-Allow-Headers']).toBe('Content-Type,Authorization'); + expect(res.headers?.['Access-Control-Allow-Methods']).toContain('POST'); + }); + }); + + describe('Success Cases', () => { + test('201: successful POST returns 201 status and correct response shape', async () => { + // Setup mocks for successful user creation + mockDb.selectFrom.mockReturnValue({ + where: jest.fn().mockReturnValue({ + selectAll: jest.fn().mockReturnValue({ + executeTakeFirst: jest.fn().mockReturnValue(null), + }), + }), + }); + + mockDb.insertInto.mockReturnValue({ + values: jest.fn().mockReturnValue({ + execute: jest.fn().mockReturnValue(undefined), + }), + }); + + const res = await handler( + postEvent('test-user', { + name: 'John Doe', + email: 'john@example.com', + isAdmin: false, + }) + ); + + expect(res.statusCode).toBe(201); + const json = JSON.parse(res.body); + expect(json).toHaveProperty('ok'); + expect(json).toHaveProperty('route'); + expect(json).toHaveProperty('pathParams'); + expect(json).toHaveProperty('body'); + }); + + test('409: returns 409 when user already exists', async () => { + // Mock: user already exists + mockDb.selectFrom.mockReturnValue({ + where: jest.fn().mockReturnValue({ + selectAll: jest.fn().mockReturnValue({ + executeTakeFirst: jest.fn().mockReturnValue({ + user_id: 'existing-user', + name: 'Existing User', + email: 'existing@example.com', + is_admin: false, + created_at: new Date(), + }), + }), + }), + }); + + const res = await handler( + postEvent('existing-user', { + name: 'New User', + email: 'new@example.com', + isAdmin: true, + }) + ); + + expect(res.statusCode).toBe(409); + const json = JSON.parse(res.body); + expect(json).toHaveProperty('message'); + }); + }); +}); diff --git a/apps/backend/lambdas/users/test/users.test.ts b/apps/backend/lambdas/users/test/users.test.ts index a0e5dca..3bdb812 100644 --- a/apps/backend/lambdas/users/test/users.test.ts +++ b/apps/backend/lambdas/users/test/users.test.ts @@ -173,7 +173,7 @@ test("POST user success case", async () => { method: "POST", body: JSON.stringify({ name: "Jane Branch", - email: "jane1@branch.com", + email: "jane@branch.com", isAdmin: true }) }); From 6c5398433a91c67a05a3ab668147fddb23a096f9 Mon Sep 17 00:00:00 2001 From: tsudhakar87 Date: Thu, 13 Nov 2025 16:23:45 -0500 Subject: [PATCH 5/5] post should not take in an id oops --- apps/backend/lambdas/users/handler.ts | 44 ++++--- apps/backend/lambdas/users/openapi.yaml | 39 +++--- .../lambdas/users/test/user.unit.test.ts | 111 +++++------------- apps/backend/lambdas/users/test/users.test.ts | 21 +--- 4 files changed, 78 insertions(+), 137 deletions(-) diff --git a/apps/backend/lambdas/users/handler.ts b/apps/backend/lambdas/users/handler.ts index 9d10846..4bcedff 100644 --- a/apps/backend/lambdas/users/handler.ts +++ b/apps/backend/lambdas/users/handler.ts @@ -115,14 +115,8 @@ export const handler = async (event: any): Promise => { return json(200, { ok: true, route: 'PATCH /users/{userId}', pathParams: { userId }, body: { email: updatedUser!.email, name: updatedUser!.name, isAdmin: updatedUser!.is_admin } }); } - // POST /{userId} // (dev server strips /users prefix) - if ( - normalizedPath.startsWith('/') && - normalizedPath.split('/').length === 2 && - method === 'POST' - ) { - const userId = normalizedPath.split('/')[1]; - if (!userId) return json(400, { message: 'userId is required' }); + // POST /users + if ((normalizedPath === '/users' || normalizedPath === '') && method === 'POST') { const body = event.body ? (JSON.parse(event.body) as Record) : {}; @@ -135,23 +129,37 @@ export const handler = async (event: any): Promise => { return json(400, { message: 'email, name, and isAdmin are required' }); } - // check if user already exists - const existingUser = await db.selectFrom("branch.users").where("user_id", "=", Number(userId)).selectAll().executeTakeFirst(); + // Check if user with this email already exists + const existingUser = await db + .selectFrom('branch.users') + .where('email', '=', email) + .selectAll() + .executeTakeFirst(); + if (existingUser) { - return json(409, { message: 'User already exists' }); + return json(409, { message: 'User with this email already exists' }); } - // insert new user - await db - .insertInto('branch.users') - .values({ user_id: userId, email, name, is_admin: isAdmin }) - .execute(); + // insert new user (user_id auto-increments) + try { + await db + .insertInto('branch.users') + .values({ email, name, is_admin: isAdmin }) + .execute(); + } catch (err) { + console.error('Database insert error:', err); + return json(500, { message: 'Failed to create user' }); + } return json(201, { ok: true, route: 'POST /users', - pathParams: { userId }, - body, + pathParams: {}, + body: { + email, + name, + isAdmin, + }, }); } // <<< ROUTES-END diff --git a/apps/backend/lambdas/users/openapi.yaml b/apps/backend/lambdas/users/openapi.yaml index bf95684..c639172 100644 --- a/apps/backend/lambdas/users/openapi.yaml +++ b/apps/backend/lambdas/users/openapi.yaml @@ -34,15 +34,8 @@ paths: responses: '200': description: OK - /{userId}: - patch: - summary: PATCH /{userId} - parameters: - - in: path - name: userId - required: true - schema: - type: string + post: + summary: POST /users requestBody: required: true content: @@ -57,19 +50,21 @@ paths: isAdmin: type: boolean responses: - '200': - description: OK - '404': - description: User not found - - post: - summary: POST /users/{userId} + '201': + description: Success + '400': + description: Bad Request + '500': + description: Internal Server Error + /{userId}: + patch: + summary: PATCH /{userId} parameters: - in: path name: userId required: true schema: - type: string + type: string requestBody: required: true content: @@ -84,9 +79,7 @@ paths: isAdmin: type: boolean responses: - '201': - description: Success - '400': - description: Bad Request - '409': - description: Conflict + '200': + description: OK + '404': + description: User not found diff --git a/apps/backend/lambdas/users/test/user.unit.test.ts b/apps/backend/lambdas/users/test/user.unit.test.ts index f6ec1c4..dad172d 100644 --- a/apps/backend/lambdas/users/test/user.unit.test.ts +++ b/apps/backend/lambdas/users/test/user.unit.test.ts @@ -9,9 +9,9 @@ import db from '../db'; const mockDb = db as any; // Helper function to create a POST event -function postEvent(userId: string, body: Record) { +function postEvent(body: Record) { return { - rawPath: `/${userId}`, + rawPath: '/users', requestContext: { http: { method: 'POST', @@ -21,7 +21,7 @@ function postEvent(userId: string, body: Record) { }; } -describe('POST /users/{userId} unit tests', () => { +describe('POST /users unit tests', () => { beforeEach(() => { jest.clearAllMocks(); }); @@ -29,7 +29,7 @@ describe('POST /users/{userId} unit tests', () => { describe('Input Validation', () => { test('400: missing email field', async () => { const res = await handler( - postEvent('test-user-1', { + postEvent({ name: 'John Doe', isAdmin: false, }) @@ -43,7 +43,7 @@ describe('POST /users/{userId} unit tests', () => { test('400: missing name field', async () => { const res = await handler( - postEvent('test-user-2', { + postEvent({ email: 'john@example.com', isAdmin: false, }) @@ -57,7 +57,7 @@ describe('POST /users/{userId} unit tests', () => { test('400: missing isAdmin field', async () => { const res = await handler( - postEvent('test-user-3', { + postEvent({ name: 'John Doe', email: 'john@example.com', }) @@ -71,7 +71,7 @@ describe('POST /users/{userId} unit tests', () => { test('400: isAdmin is not a boolean', async () => { const res = await handler( - postEvent('test-user-4', { + postEvent({ name: 'John Doe', email: 'john@example.com', isAdmin: 'yes', @@ -86,7 +86,7 @@ describe('POST /users/{userId} unit tests', () => { test('400: empty email field', async () => { const res = await handler( - postEvent('test-user-5', { + postEvent({ name: 'John Doe', email: '', isAdmin: false, @@ -100,7 +100,7 @@ describe('POST /users/{userId} unit tests', () => { test('400: empty name field', async () => { const res = await handler( - postEvent('test-user-6', { + postEvent({ name: '', email: 'john@example.com', isAdmin: false, @@ -114,78 +114,30 @@ describe('POST /users/{userId} unit tests', () => { }); describe('Response Format', () => { - test('404: POST to root path returns not found', async () => { - const res = await handler({ - rawPath: '/', - requestContext: { - http: { - method: 'POST', - }, - }, - body: JSON.stringify({ - name: 'John Doe', - email: 'john@example.com', - isAdmin: false, - }), - }); - - expect(res.statusCode).toBe(404); - const json = JSON.parse(res.body); - expect(json.message).toBe('Not Found'); - expect(json.path).toBeDefined(); - expect(json.method).toBeDefined(); - }); - - test('response has correct HTTP headers', async () => { - // Setup mocks for successful user creation - mockDb.selectFrom.mockReturnValue({ - where: jest.fn().mockReturnValue({ - selectAll: jest.fn().mockReturnValue({ - executeTakeFirst: jest.fn().mockReturnValue(null), - }), - }), - }); - - mockDb.insertInto.mockReturnValue({ - values: jest.fn().mockReturnValue({ - execute: jest.fn().mockReturnValue(undefined), - }), - }); - - const res = await handler( - postEvent('format-test', { - name: 'Format Test', - email: 'format@example.com', - isAdmin: false, - }) - ); - - expect(res.headers?.['Content-Type']).toBe('application/json'); - expect(res.headers?.['Access-Control-Allow-Origin']).toBe('*'); - expect(res.headers?.['Access-Control-Allow-Headers']).toBe('Content-Type,Authorization'); - expect(res.headers?.['Access-Control-Allow-Methods']).toContain('POST'); - }); }); describe('Success Cases', () => { test('201: successful POST returns 201 status and correct response shape', async () => { // Setup mocks for successful user creation - mockDb.selectFrom.mockReturnValue({ - where: jest.fn().mockReturnValue({ - selectAll: jest.fn().mockReturnValue({ - executeTakeFirst: jest.fn().mockReturnValue(null), - }), + // Mock the email check to return null (user doesn't exist) + const whereChain = { + selectAll: jest.fn().mockReturnValue({ + executeTakeFirst: (jest.fn() as any).mockResolvedValue(null), }), + }; + mockDb.selectFrom.mockReturnValue({ + where: jest.fn().mockReturnValue(whereChain), }); + // Mock the insert mockDb.insertInto.mockReturnValue({ values: jest.fn().mockReturnValue({ - execute: jest.fn().mockReturnValue(undefined), + execute: (jest.fn() as any).mockResolvedValue(undefined), }), }); const res = await handler( - postEvent('test-user', { + postEvent({ name: 'John Doe', email: 'john@example.com', isAdmin: false, @@ -202,24 +154,25 @@ describe('POST /users/{userId} unit tests', () => { test('409: returns 409 when user already exists', async () => { // Mock: user already exists - mockDb.selectFrom.mockReturnValue({ - where: jest.fn().mockReturnValue({ - selectAll: jest.fn().mockReturnValue({ - executeTakeFirst: jest.fn().mockReturnValue({ - user_id: 'existing-user', - name: 'Existing User', - email: 'existing@example.com', - is_admin: false, - created_at: new Date(), - }), + const whereChain = { + selectAll: jest.fn().mockReturnValue({ + executeTakeFirst: (jest.fn() as any).mockResolvedValue({ + user_id: 1, + name: 'Existing User', + email: 'existing@example.com', + is_admin: false, + created_at: new Date(), }), }), + }; + mockDb.selectFrom.mockReturnValue({ + where: jest.fn().mockReturnValue(whereChain), }); const res = await handler( - postEvent('existing-user', { + postEvent({ name: 'New User', - email: 'new@example.com', + email: 'existing@example.com', isAdmin: true, }) ); diff --git a/apps/backend/lambdas/users/test/users.test.ts b/apps/backend/lambdas/users/test/users.test.ts index 3bdb812..2d9b897 100644 --- a/apps/backend/lambdas/users/test/users.test.ts +++ b/apps/backend/lambdas/users/test/users.test.ts @@ -169,7 +169,7 @@ test("get users error", async () => { }); test("POST user success case", async () => { - let res = await fetch("http://localhost:3000/users/5", { + let res = await fetch("http://localhost:3000/users", { method: "POST", body: JSON.stringify({ name: "Jane Branch", @@ -188,8 +188,8 @@ test("POST user success case", async () => { expect(body.body.isAdmin).toBe(true); }); -test("POST user 400 case when invalid userId is sent", async () => { - let res = await fetch("http://localhost:3000/users/invalidUserId", { +test("POST user 400 case when invalid email is sent", async () => { + let res = await fetch("http://localhost:3000/users", { method: "POST", body: JSON.stringify({ name: "Invalid User", @@ -202,7 +202,7 @@ test("POST user 400 case when invalid userId is sent", async () => { }); test("POST user 400 case when request sent with missing fields", async () => { - let res = await fetch("http://localhost:3000/users/4", { + let res = await fetch("http://localhost:3000/users", { method: "POST", body: JSON.stringify({ name: "Invalid User", @@ -210,17 +210,4 @@ test("POST user 400 case when request sent with missing fields", async () => { }); expect(res.status).toBe(400); -}); - -test("POST user 409 case when user already exists", async () => { - let res = await fetch("http://localhost:3000/users/1", { - method: "POST", - body: JSON.stringify({ - name: "Existing User", - email: "some@email.com", - isAdmin: false - }) - }); - - expect(res.status).toBe(409); }); \ No newline at end of file