-
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
Showing
3 changed files
with
81 additions
and
20 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ import config from '../../common/config'; | |
import * as bcrypt from 'bcrypt'; | ||
import { Prisma } from '@prisma/client'; | ||
import { ConflictException, UnauthorizedException } from '@nestjs/common'; | ||
import mockUser from '../../../test/mockUser'; | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
|
@@ -17,16 +18,6 @@ describe('AuthService', () => { | |
let spyJwtService: JwtService; | ||
let spyPrismaService: DeepMockProxy<PrismaService>; | ||
|
||
const date = new Date(); | ||
const user = { | ||
id: 'fcd2fa2d-f5f4-4ed0-9d75-f3ca6ddd4c21', | ||
name: 'John', | ||
email: '[email protected]', | ||
passwordHash: 'hashedPassword', | ||
createdAt: date, | ||
updatedAt: date, | ||
}; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [ | ||
|
@@ -70,16 +61,16 @@ describe('AuthService', () => { | |
}; | ||
|
||
it('should create a new user', async () => { | ||
jest.spyOn(bcrypt, 'hash').mockResolvedValue(user.passwordHash); | ||
spyPrismaService.user.create.mockResolvedValue(user); | ||
jest.spyOn(bcrypt, 'hash').mockResolvedValue(mockUser.passwordHash); | ||
spyPrismaService.user.create.mockResolvedValue(mockUser); | ||
|
||
await service.signup(signUpData); | ||
expect(spyPrismaService.user.create).toHaveBeenCalledTimes(1); | ||
expect(spyPrismaService.user.create).toHaveBeenCalledWith({ | ||
data: { | ||
name: signUpData.name, | ||
email: signUpData.email, | ||
passwordHash: user.passwordHash, | ||
passwordHash: mockUser.passwordHash, | ||
}, | ||
select: null, | ||
}); | ||
|
@@ -110,14 +101,14 @@ describe('AuthService', () => { | |
it('should allow an user to login', async () => { | ||
jest.spyOn(bcrypt, 'compareSync').mockResolvedValue(true); | ||
jest.spyOn(spyJwtService, 'signAsync').mockResolvedValue(testJWT); | ||
spyPrismaService.user.findFirst.mockResolvedValue(user); | ||
spyPrismaService.user.findFirst.mockResolvedValue(mockUser); | ||
|
||
expect(await service.login(loginInfo)).toStrictEqual(testJWT); | ||
expect(spyPrismaService.user.findFirst).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should retrieve an unauthorized error', async () => { | ||
spyPrismaService.user.findFirst.mockResolvedValue(user); | ||
spyPrismaService.user.findFirst.mockResolvedValue(mockUser); | ||
jest.spyOn(bcrypt, 'compareSync').mockResolvedValue(false); | ||
|
||
await expect(service.login(loginInfo)).rejects.toThrow( | ||
|
@@ -128,13 +119,13 @@ describe('AuthService', () => { | |
|
||
describe('validateUser', () => { | ||
const payload = { | ||
id: user.id, | ||
name: user.name, | ||
email: user.email, | ||
id: mockUser.id, | ||
name: mockUser.name, | ||
email: mockUser.email, | ||
}; | ||
it('should return a valid user', async () => { | ||
spyPrismaService.user.findUnique.mockResolvedValue(user); | ||
expect(await service.validateUser(payload)).toStrictEqual(user); | ||
spyPrismaService.user.findUnique.mockResolvedValue(mockUser); | ||
expect(await service.validateUser(payload)).toStrictEqual(mockUser); | ||
}); | ||
|
||
it('should throw an unauthorized error', async () => { | ||
|
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { DeepMockProxy, mockDeep } from 'jest-mock-extended'; | ||
import { UserService } from '../user.service'; | ||
import { PrismaService } from '../../common/services/prisma.service'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { NotFoundException } from '@nestjs/common'; | ||
import mockUser from '../../../test/mockUser'; | ||
|
||
describe('UserService', () => { | ||
let service: UserService; | ||
let spyPrismaService: DeepMockProxy<PrismaService>; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
UserService, | ||
{ | ||
provide: PrismaService, | ||
useFactory: () => mockDeep<PrismaService>(), | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<UserService>(UserService); | ||
spyPrismaService = module.get( | ||
PrismaService, | ||
) as DeepMockProxy<PrismaService>; | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('getUserById', () => { | ||
it('should return the user with the requested id', async () => { | ||
spyPrismaService.user.findUnique.mockResolvedValue(mockUser); | ||
|
||
expect(await service.getUserById(mockUser.id)).toStrictEqual(mockUser); | ||
expect(spyPrismaService.user.findUnique).toHaveBeenCalledTimes(1); | ||
expect(spyPrismaService.user.findUnique).toHaveBeenCalledWith({ | ||
where: { id: mockUser.id }, | ||
}); | ||
}); | ||
|
||
it('should return the user from the database', async () => { | ||
spyPrismaService.user.findUnique.mockResolvedValue(mockUser); | ||
|
||
expect(await service.getUserById(mockUser.id)).toStrictEqual(mockUser); | ||
}); | ||
|
||
it('should throw a not found exception', async () => { | ||
spyPrismaService.user.findUnique.mockResolvedValue(null); | ||
|
||
await expect(service.getUserById(mockUser.id)).rejects.toThrow( | ||
NotFoundException, | ||
); | ||
expect(spyPrismaService.user.findUnique).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const date = new Date(); | ||
const mockUser = { | ||
id: 'fcd2fa2d-f5f4-4ed0-9d75-f3ca6ddd4c21', | ||
name: 'John', | ||
email: '[email protected]', | ||
passwordHash: 'hashedPassword', | ||
createdAt: date, | ||
updatedAt: date, | ||
}; | ||
|
||
export default mockUser; |