-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mrkeksz
committed
Oct 25, 2023
1 parent
7d03693
commit 0d92ef6
Showing
34 changed files
with
477 additions
and
278 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,46 +2,31 @@ import {Test, TestingModule} from '@nestjs/testing' | |
import {AuthController} from './auth.controller' | ||
import {AuthModule} from './auth.module' | ||
import {AuthService} from './auth.service' | ||
import {AuthUser} from './entities/auth-user.entity' | ||
import {configModule} from '../config' | ||
|
||
describe('AuthController', () => { | ||
let authController: AuthController | ||
let authService: AuthService | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [AuthModule], | ||
imports: [AuthModule, configModule], | ||
}).compile() | ||
|
||
authController = module.get(AuthController) | ||
authService = module.get(AuthService) | ||
}) | ||
|
||
describe('signIn', () => { | ||
describe('login', () => { | ||
it('should return a token', async () => { | ||
jest.spyOn(authService, 'signIn').mockImplementation(async () => { | ||
return { | ||
access_token: 'test', | ||
} | ||
}) | ||
|
||
const result = { | ||
const expected = { | ||
access_token: 'test', | ||
} | ||
expect(await authController.signIn({email: '[email protected]', password: '123'})).toStrictEqual( | ||
result, | ||
) | ||
}) | ||
}) | ||
|
||
describe('getProfile', () => { | ||
it('should return a user', async () => { | ||
const request = { | ||
user: {sub: 1, email: '[email protected]', iat: 123, exp: 123}, | ||
} | ||
expect(authController.getProfile(request as Request & {user: AuthUser})).toStrictEqual( | ||
request.user, | ||
) | ||
jest.spyOn(authService, 'login').mockImplementation(() => expected) | ||
|
||
const req = {user: {id: 1}} as Request & {user: {id: number}} | ||
expect(authController.login(req)).toStrictEqual(expected) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,18 @@ | ||
import {Body, Controller, Get, HttpCode, HttpStatus, Post, Request} from '@nestjs/common' | ||
import {Controller, HttpCode, HttpStatus, Post, Request, UseGuards} from '@nestjs/common' | ||
import {User} from '../users/entities/user.entity' | ||
import {LocalAuthGuard} from './guards/local-auth.guard' | ||
import {AuthService} from './auth.service' | ||
import {SignInDto} from './dto/sign-in.dto' | ||
import {AuthUser} from './entities/auth-user.entity' | ||
import {SkipAuth} from './decorators/skip-auth.decorator' | ||
import {SkipJwtAuth} from './decorators/skip-jwt-auth.decorator' | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private authService: AuthService) {} | ||
|
||
@SkipAuth() | ||
@SkipJwtAuth() | ||
@UseGuards(LocalAuthGuard) | ||
@HttpCode(HttpStatus.OK) | ||
@Post('login') | ||
signIn(@Body() signInDto: SignInDto) { | ||
return this.authService.signIn(signInDto.email, signInDto.password) | ||
} | ||
|
||
@Get('profile') | ||
getProfile(@Request() request: Request & {user: AuthUser}) { | ||
return request.user | ||
login(@Request() req: Request & {user: {id: User['id']}}) { | ||
return this.authService.login(req.user.id) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.