From 9bd3400264de40a90b45d0cd997c2c752548c0bb Mon Sep 17 00:00:00 2001 From: Sergey Krylov Date: Mon, 24 Aug 2026 00:50:48 +0300 Subject: [PATCH] fix: align auth success status contract --- src/auth/auth.controller.spec.ts | 21 +++++++++++++++++++++ src/auth/auth.controller.ts | 3 +++ test/app.e2e-spec.ts | 19 +++++++++++++------ 3 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 src/auth/auth.controller.spec.ts diff --git a/src/auth/auth.controller.spec.ts b/src/auth/auth.controller.spec.ts new file mode 100644 index 0000000..cad6202 --- /dev/null +++ b/src/auth/auth.controller.spec.ts @@ -0,0 +1,21 @@ +import { HTTP_CODE_METADATA } from '@nestjs/common/constants'; + +jest.mock('./auth.service', () => ({ + AuthService: class AuthService {}, +})); + +import { AuthController } from './auth.controller'; + +describe('AuthController response statuses', () => { + it.each(['register', 'login', 'refresh'] as const)( + '%s explicitly returns HTTP 200', + (handler) => { + // Nest stores @HttpCode metadata on the unbound handler function. + const handlerTarget = Object.getOwnPropertyDescriptor( + AuthController.prototype, + handler, + )?.value as object; + expect(Reflect.getMetadata(HTTP_CODE_METADATA, handlerTarget)).toBe(200); + }, + ); +}); diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 7fb7b51..3f52899 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -49,6 +49,7 @@ export class AuthController { constructor(private readonly auth: AuthService) {} @Post('register') + @HttpCode(200) @ApiOkResponse({ type: AuthenticationDto }) async register( @Body() credentials: CredentialsDto, @@ -61,6 +62,7 @@ export class AuthController { } @Post('login') + @HttpCode(200) @ApiOkResponse({ type: AuthenticationDto }) async login( @Body() credentials: CredentialsDto, @@ -73,6 +75,7 @@ export class AuthController { } @Post('refresh') + @HttpCode(200) @ApiOkResponse({ type: AuthenticationDto }) async refresh( @Req() request: Request, diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts index 6a0e708..0b0d3f2 100644 --- a/test/app.e2e-spec.ts +++ b/test/app.e2e-spec.ts @@ -34,7 +34,7 @@ describe('Authentication (e2e)', () => { const registration = await agent .post('/api/v1/auth/register') .send({ email: ' User@Example.com ', password: 'a secure password' }) - .expect(201); + .expect(200); const authentication = registration.body as Authentication; expect(authentication.user.email).toBe('user@example.com'); @@ -49,7 +49,7 @@ describe('Authentication (e2e)', () => { ); const firstCookie = registration.headers['set-cookie'][0]; - await agent.post('/api/v1/auth/refresh').expect(201); + await agent.post('/api/v1/auth/refresh').expect(200); await request(app.getHttpServer()) .post('/api/v1/auth/refresh') .set('Cookie', firstCookie) @@ -63,7 +63,14 @@ describe('Authentication (e2e)', () => { await request(app.getHttpServer()) .post('/api/v1/auth/register') .send({ email: 'user@example.com', password: 'a secure password' }) - .expect(201); + .expect(200); + await request(app.getHttpServer()) + .post('/api/v1/auth/login') + .send({ email: 'user@example.com', password: 'a secure password' }) + .expect(200) + .expect(({ body }: { body: unknown }) => + expect((body as Authentication).user.email).toBe('user@example.com'), + ); await request(app.getHttpServer()) .post('/api/v1/auth/register') .send({ email: 'USER@example.com', password: 'a secure password' }) @@ -84,7 +91,7 @@ describe('Authentication (e2e)', () => { const registration = await request(app.getHttpServer()) .post('/api/v1/auth/register') .send({ email: 'connection@example.com', password: 'a secure password' }) - .expect(201); + .expect(200); const accessToken = (registration.body as Authentication).accessToken; await request(app.getHttpServer()) @@ -103,7 +110,7 @@ describe('Authentication (e2e)', () => { const registration = await request(app.getHttpServer()) .post('/api/v1/auth/register') .send({ email: 'accounts@example.com', password: 'a secure password' }) - .expect(201); + .expect(200); const accessToken = (registration.body as Authentication).accessToken; await request(app.getHttpServer()) @@ -122,7 +129,7 @@ describe('Authentication (e2e)', () => { const registration = await request(app.getHttpServer()) .post('/api/v1/auth/register') .send({ email: 'portfolio@example.com', password: 'a secure password' }) - .expect(201); + .expect(200); const accessToken = (registration.body as Authentication).accessToken; await request(app.getHttpServer())