From d4fad3f0ce1a1afd832426ff9211ca169ec7bc32 Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Wed, 2 Oct 2024 16:36:51 -0700 Subject: [PATCH] [TM-1312] Change controller creation scheme. --- apps/user-service/src/app.module.ts | 4 ++-- ...controller.spec.ts => login.controller.spec.ts} | 14 +++++++------- .../{auth.controller.ts => login.controller.ts} | 8 ++++---- apps/user-service/src/users/users.controller.ts | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) rename apps/user-service/src/auth/{auth.controller.spec.ts => login.controller.spec.ts} (69%) rename apps/user-service/src/auth/{auth.controller.ts => login.controller.ts} (95%) diff --git a/apps/user-service/src/app.module.ts b/apps/user-service/src/app.module.ts index 715ebd1..3fa54e4 100644 --- a/apps/user-service/src/app.module.ts +++ b/apps/user-service/src/app.module.ts @@ -1,5 +1,5 @@ import { Module } from '@nestjs/common'; -import { AuthController } from './auth/auth.controller'; +import { LoginController } from './auth/login.controller'; import { AuthService } from './auth/auth.service'; import { DatabaseModule } from '@terramatch-microservices/database'; import { UsersController } from './users/users.controller'; @@ -7,7 +7,7 @@ import { CommonModule } from '@terramatch-microservices/common'; @Module({ imports: [DatabaseModule, CommonModule], - controllers: [AuthController, UsersController], + controllers: [LoginController, UsersController], providers: [AuthService], }) export class AppModule {} diff --git a/apps/user-service/src/auth/auth.controller.spec.ts b/apps/user-service/src/auth/login.controller.spec.ts similarity index 69% rename from apps/user-service/src/auth/auth.controller.spec.ts rename to apps/user-service/src/auth/login.controller.spec.ts index 722f469..435ae88 100644 --- a/apps/user-service/src/auth/auth.controller.spec.ts +++ b/apps/user-service/src/auth/login.controller.spec.ts @@ -1,22 +1,22 @@ import { Test, TestingModule } from '@nestjs/testing'; -import { AuthController } from './auth.controller'; +import { LoginController } from './login.controller'; import { AuthService } from './auth.service'; import { createMock, DeepMocked } from '@golevelup/ts-jest'; import { UnauthorizedException } from '@nestjs/common'; -describe('AuthController', () => { - let controller: AuthController; +describe('LoginController', () => { + let controller: LoginController; let authService: DeepMocked; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - controllers: [AuthController], + controllers: [LoginController], providers: [ { provide: AuthService, useValue: authService = createMock() }, ], }).compile(); - controller = module.get(AuthController); + controller = module.get(LoginController); }); afterEach(() => { @@ -26,7 +26,7 @@ describe('AuthController', () => { it('should throw if creds are invalid', async () => { authService.login.mockResolvedValue(null); - await expect(() => controller.login({ emailAddress: 'foo@bar.com', password: 'asdfasdfasdf' })) + await expect(() => controller.create({ emailAddress: 'foo@bar.com', password: 'asdfasdfasdf' })) .rejects .toThrow(UnauthorizedException) }) @@ -36,7 +36,7 @@ describe('AuthController', () => { const userId = 123; authService.login.mockResolvedValue({ token, userId }) - const result = await controller.login({ emailAddress: 'foo@bar.com', password: 'asdfasdfasdf' }); + const result = await controller.create({ emailAddress: 'foo@bar.com', password: 'asdfasdfasdf' }); expect(result).toEqual({ type: 'logins', token, id: `${userId}` }) }) }); diff --git a/apps/user-service/src/auth/auth.controller.ts b/apps/user-service/src/auth/login.controller.ts similarity index 95% rename from apps/user-service/src/auth/auth.controller.ts rename to apps/user-service/src/auth/login.controller.ts index 53278eb..c7190fe 100644 --- a/apps/user-service/src/auth/auth.controller.ts +++ b/apps/user-service/src/auth/login.controller.ts @@ -14,11 +14,11 @@ import { NoBearerAuth } from '@terramatch-microservices/common/guards'; import { JsonApiResponse } from '@terramatch-microservices/common/decorators'; import { buildJsonApi, JsonApiDocument } from '@terramatch-microservices/common/util'; -@Controller('auth/v3') -export class AuthController { +@Controller('auth/v3/logins') +export class LoginController { constructor(private readonly authService: AuthService) {} - @Post('logins') + @Post() @NoBearerAuth() @ApiOperation({ operationId: 'authLogin', @@ -26,7 +26,7 @@ export class AuthController { }) @JsonApiResponse({ status: HttpStatus.CREATED, data: { type: LoginDto } }) @ApiException(() => UnauthorizedException, { description: 'Authentication failed.' }) - async login( + async create( @Body() { emailAddress, password }: LoginRequest ): Promise { const { token, userId } = diff --git a/apps/user-service/src/users/users.controller.ts b/apps/user-service/src/users/users.controller.ts index 5d650a9..8d5ffb5 100644 --- a/apps/user-service/src/users/users.controller.ts +++ b/apps/user-service/src/users/users.controller.ts @@ -17,11 +17,11 @@ import { JsonApiDocument, } from '@terramatch-microservices/common/util'; -@Controller('users/v3') +@Controller('users/v3/users') export class UsersController { constructor(private readonly policyService: PolicyService) {} - @Get('users/:id') + @Get(':id') @ApiOperation({ operationId: 'usersFind', description: "Fetch a user by ID, or with the 'me' identifier" }) @ApiParam({ name: 'id', example: 'me', description: 'A valid user id or "me"' }) @JsonApiResponse({