Skip to content

Commit

Permalink
[TM-1312] Change controller creation scheme.
Browse files Browse the repository at this point in the history
  • Loading branch information
roguenet committed Oct 2, 2024
1 parent 53bcf1c commit d4fad3f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions apps/user-service/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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';
import { CommonModule } from '@terramatch-microservices/common';

@Module({
imports: [DatabaseModule, CommonModule],
controllers: [AuthController, UsersController],
controllers: [LoginController, UsersController],
providers: [AuthService],
})
export class AppModule {}
Original file line number Diff line number Diff line change
@@ -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<AuthService>;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
controllers: [LoginController],
providers: [
{ provide: AuthService, useValue: authService = createMock<AuthService>() },
],
}).compile();

controller = module.get<AuthController>(AuthController);
controller = module.get<LoginController>(LoginController);
});

afterEach(() => {
Expand All @@ -26,7 +26,7 @@ describe('AuthController', () => {
it('should throw if creds are invalid', async () => {
authService.login.mockResolvedValue(null);

await expect(() => controller.login({ emailAddress: '[email protected]', password: 'asdfasdfasdf' }))
await expect(() => controller.create({ emailAddress: '[email protected]', password: 'asdfasdfasdf' }))
.rejects
.toThrow(UnauthorizedException)
})
Expand All @@ -36,7 +36,7 @@ describe('AuthController', () => {
const userId = 123;
authService.login.mockResolvedValue({ token, userId })

const result = await controller.login({ emailAddress: '[email protected]', password: 'asdfasdfasdf' });
const result = await controller.create({ emailAddress: '[email protected]', password: 'asdfasdfasdf' });
expect(result).toEqual({ type: 'logins', token, id: `${userId}` })
})
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ 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',
description: 'Receive a JWT Token in exchange for login credentials',
})
@JsonApiResponse({ status: HttpStatus.CREATED, data: { type: LoginDto } })
@ApiException(() => UnauthorizedException, { description: 'Authentication failed.' })
async login(
async create(
@Body() { emailAddress, password }: LoginRequest
): Promise<JsonApiDocument> {
const { token, userId } =
Expand Down
4 changes: 2 additions & 2 deletions apps/user-service/src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit d4fad3f

Please sign in to comment.