From e7c1748f3fea0a79fdee716cfcbea206617a336c Mon Sep 17 00:00:00 2001 From: Greg Akins Date: Thu, 29 Feb 2024 14:05:44 -0500 Subject: [PATCH] MAT-6726 Removed some redundant code and completed test coverage --- src/app.controller.spec.ts | 4 +--- src/app.module.ts | 4 ++-- src/auth/auth.controller.spec.ts | 22 ---------------------- src/auth/auth.controller.ts | 31 ------------------------------- src/auth/auth.guard.spec.ts | 3 +-- src/auth/auth.module.ts | 8 +------- src/auth/auth.service.spec.ts | 20 -------------------- src/auth/auth.service.ts | 25 ------------------------- src/users/users.module.ts | 9 --------- src/users/users.service.spec.ts | 18 ------------------ src/users/users.service.ts | 25 ------------------------- 11 files changed, 5 insertions(+), 164 deletions(-) delete mode 100644 src/auth/auth.controller.spec.ts delete mode 100644 src/auth/auth.controller.ts delete mode 100644 src/auth/auth.service.spec.ts delete mode 100644 src/auth/auth.service.ts delete mode 100644 src/users/users.module.ts delete mode 100644 src/users/users.service.spec.ts delete mode 100644 src/users/users.service.ts diff --git a/src/app.controller.spec.ts b/src/app.controller.spec.ts index 2d513e5..3264eb5 100644 --- a/src/app.controller.spec.ts +++ b/src/app.controller.spec.ts @@ -3,9 +3,7 @@ import { AppController } from './app.controller'; import {Request as ExpressRequest, Response} from 'express'; import { AppService } from './app.service'; -import { UsersService } from './users/users.service'; import { JwtService } from '@nestjs/jwt'; -import { AuthService } from './auth/auth.service'; describe('AppController', () => { @@ -15,7 +13,7 @@ describe('AppController', () => { const app: TestingModule = await Test.createTestingModule({ controllers: [AppController], - providers: [AppService, AuthService, UsersService, JwtService], + providers: [AppService, JwtService], }).compile(); appController = app.get(AppController); diff --git a/src/app.module.ts b/src/app.module.ts index d41f6c6..ed4c337 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -2,10 +2,10 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { AuthModule } from './auth/auth.module'; -import { UsersModule } from './users/users.module'; + @Module({ - imports: [AuthModule, UsersModule], + imports: [AuthModule], controllers: [AppController], providers: [AppService], }) diff --git a/src/auth/auth.controller.spec.ts b/src/auth/auth.controller.spec.ts deleted file mode 100644 index b87c7ac..0000000 --- a/src/auth/auth.controller.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AuthController } from './auth.controller'; -import { AuthService } from './auth.service'; -import { UsersService } from '../users/users.service'; -import { JwtService } from '@nestjs/jwt'; - -describe('AuthController', () => { - let controller: AuthController; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [AuthService, UsersService, JwtService], - controllers: [AuthController], - }).compile(); - - controller = module.get(AuthController); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); -}); diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts deleted file mode 100644 index 226955c..0000000 --- a/src/auth/auth.controller.ts +++ /dev/null @@ -1,31 +0,0 @@ - -import { - Body, - Controller, - Get, - HttpCode, - HttpStatus, - Post, - Request, - UseGuards - } from '@nestjs/common'; - import { AuthGuard } from './auth.guard'; - import { AuthService } from './auth.service'; - - @Controller('auth') - export class AuthController { - constructor(private authService: AuthService) {} - - @HttpCode(HttpStatus.OK) - @Post('login') - signIn(@Body() signInDto: Record) { - return this.authService.signIn(signInDto.username, signInDto.password); - } - - @UseGuards(AuthGuard) - @Get('profile') - getProfile(@Request() req) { - return req.user; - } - } - \ No newline at end of file diff --git a/src/auth/auth.guard.spec.ts b/src/auth/auth.guard.spec.ts index 9dcdbb1..48f85d0 100644 --- a/src/auth/auth.guard.spec.ts +++ b/src/auth/auth.guard.spec.ts @@ -1,6 +1,5 @@ import { Test, TestingModule } from '@nestjs/testing'; import { AuthGuard } from './auth.guard'; -import { UsersService } from '../users/users.service'; import { JwtService } from '@nestjs/jwt'; import { createMock } from '@golevelup/ts-jest'; import { ExecutionContext, UnauthorizedException } from '@nestjs/common'; @@ -10,7 +9,7 @@ describe('AuthGuard', () => { beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [UsersService, JwtService], + providers: [JwtService], }).compile(); guard = new AuthGuard(new JwtService()); diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts index a354bdb..484cd16 100644 --- a/src/auth/auth.module.ts +++ b/src/auth/auth.module.ts @@ -1,22 +1,16 @@ import { Module } from '@nestjs/common'; -import { AuthService } from './auth.service'; -import { UsersModule } from '../users/users.module'; import { JwtModule } from '@nestjs/jwt'; -import { AuthController } from './auth.controller'; import { jwtConstants } from './constants'; @Module({ imports: [ - UsersModule, JwtModule.register({ global: true, secret: jwtConstants.secret, signOptions: { expiresIn: '60s' }, }), ], - providers: [AuthService], - controllers: [AuthController], - exports: [AuthService], + }) export class AuthModule {} diff --git a/src/auth/auth.service.spec.ts b/src/auth/auth.service.spec.ts deleted file mode 100644 index 1d16f93..0000000 --- a/src/auth/auth.service.spec.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AuthService } from './auth.service'; -import { UsersService } from '../users/users.service'; -import { JwtService } from '@nestjs/jwt'; - -describe('AuthService', () => { - let service: AuthService; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [AuthService, UsersService, JwtService], - }).compile(); - - service = module.get(AuthService); - }); - - it('should be defined', () => { - expect(service).toBeDefined(); - }); -}); diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts deleted file mode 100644 index f9ec2e0..0000000 --- a/src/auth/auth.service.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Injectable, UnauthorizedException } from '@nestjs/common'; -import { UsersService } from '../users/users.service'; -import { JwtService } from '@nestjs/jwt'; - -@Injectable() -export class AuthService { - constructor( - private usersService: UsersService, - private jwtService: JwtService - ) {} - - async signIn( - username: string, - pass: string, - ): Promise<{ access_token: string }> { - const user = await this.usersService.findOne(username); - if (user?.password !== pass) { - throw new UnauthorizedException(); - } - const payload = { sub: user.userId, username: user.username }; - return { - access_token: await this.jwtService.signAsync(payload), - }; - } -} diff --git a/src/users/users.module.ts b/src/users/users.module.ts deleted file mode 100644 index dac33ad..0000000 --- a/src/users/users.module.ts +++ /dev/null @@ -1,9 +0,0 @@ - -import { Module } from '@nestjs/common'; -import { UsersService } from './users.service'; - -@Module({ - providers: [UsersService], - exports: [UsersService], -}) -export class UsersModule {} diff --git a/src/users/users.service.spec.ts b/src/users/users.service.spec.ts deleted file mode 100644 index 62815ba..0000000 --- a/src/users/users.service.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { UsersService } from './users.service'; - -describe('UsersService', () => { - let service: UsersService; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [UsersService], - }).compile(); - - service = module.get(UsersService); - }); - - it('should be defined', () => { - expect(service).toBeDefined(); - }); -}); diff --git a/src/users/users.service.ts b/src/users/users.service.ts deleted file mode 100644 index 5662125..0000000 --- a/src/users/users.service.ts +++ /dev/null @@ -1,25 +0,0 @@ - -import { Injectable } from '@nestjs/common'; - -// This should be a real class/interface representing a user entity -export type User = any; - -@Injectable() -export class UsersService { - private readonly users = [ - { - userId: 1, - username: 'john', - password: 'changeme', - }, - { - userId: 2, - username: 'maria', - password: 'guess', - }, - ]; - - async findOne(username: string): Promise { - return this.users.find(user => user.username === username); - } -}