-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from B-ki/auth-test
Auth and User tests
- Loading branch information
Showing
11 changed files
with
190 additions
and
17 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
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ on: | |
- front/** | ||
|
||
jobs: | ||
lint: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
defaults: | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '.'; |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { Test } from '@nestjs/testing'; | ||
|
||
import { AuthService } from '../../src/modules/auth/auth.service'; | ||
import { UserService } from '../../src/modules/user'; | ||
|
||
describe('AuthService', () => { | ||
let authService: AuthService; | ||
|
||
const mockJwtService = {}; | ||
|
||
const mockUserService = { | ||
getAll: jest.fn(), | ||
getUnique: jest.fn(), | ||
createUser: jest.fn(), | ||
}; | ||
|
||
beforeEach(async () => { | ||
// initialize a NestJS module with authService | ||
const module = await Test.createTestingModule({ | ||
providers: [ | ||
AuthService, | ||
{ | ||
provide: JwtService, | ||
useValue: mockJwtService, | ||
}, | ||
{ | ||
provide: UserService, | ||
useValue: mockUserService, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
authService = module.get(AuthService); | ||
}); | ||
|
||
// it = "test case" | ||
it('should be defined', () => { | ||
expect(authService).toBeDefined(); | ||
}); | ||
|
||
it('should have functions : login, fetchProfileInformation and generateJWT', () => { | ||
expect(authService.login).toBeDefined(); | ||
expect(authService.fetchProfileInformations).toBeDefined(); | ||
expect(authService.generateJWT).toBeDefined(); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { PrismaClient } from '@prisma/client'; | ||
import { DeepMockProxy, mockDeep } from 'jest-mock-extended'; | ||
|
||
import { FortyTwoProfile } from '@/modules/auth'; | ||
|
||
import { UserService } from '../../src/modules/user'; | ||
import { PrismaService } from '../../src/prisma'; | ||
|
||
describe('UserService', () => { | ||
let userService: UserService; | ||
let prismaService: DeepMockProxy<PrismaClient>; | ||
|
||
beforeEach(async () => { | ||
const module = await Test.createTestingModule({ | ||
providers: [UserService, PrismaService], | ||
}) | ||
.overrideProvider(PrismaService) | ||
.useValue(mockDeep<PrismaClient>()) | ||
.compile(); | ||
|
||
userService = module.get(UserService); | ||
prismaService = module.get(PrismaService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(userService).toBeDefined(); | ||
}); | ||
|
||
it('should have functions', () => { | ||
expect(userService.getAll).toBeDefined(); | ||
expect(userService.getUnique).toBeDefined(); | ||
expect(userService.createUser).toBeDefined(); | ||
}); | ||
|
||
const user = { | ||
id: '1', | ||
login: 'testLogin', | ||
email: 'testMail', | ||
imageUrl: 'testUrl', | ||
displayName: 'testLogin', | ||
firstName: 'testFirstName', | ||
lastName: 'testLastName', | ||
createdAt: new Date(), | ||
}; | ||
|
||
const profile: FortyTwoProfile = { | ||
login: 'testLogin', | ||
email: 'testMail', | ||
imageUrl: 'testUrl', | ||
displayName: 'testLogin', | ||
firstName: 'testFirstName', | ||
lastName: 'testLastName', | ||
}; | ||
it('should create users correctly', async () => { | ||
prismaService.user.create.mockResolvedValue(user); | ||
const result = await userService.createUser(profile); | ||
expect(result.id).toEqual('1'); | ||
expect(result.login).toEqual(profile.login); | ||
expect(result.email).toEqual(profile.email); | ||
expect(result.imageUrl).toEqual(profile.imageUrl); | ||
expect(result.displayName).toEqual(profile.displayName); | ||
expect(result.firstName).toEqual(profile.firstName); | ||
expect(result.lastName).toEqual(profile.lastName); | ||
}); | ||
|
||
it('should get a user correctly', async () => { | ||
prismaService.user.findUnique.mockResolvedValue(user); | ||
const result = await userService.getUnique('testLogin'); | ||
expect(result.id).toEqual('1'); | ||
expect(result.login).toEqual(profile.login); | ||
expect(result.email).toEqual(profile.email); | ||
expect(result.imageUrl).toEqual(profile.imageUrl); | ||
expect(result.displayName).toEqual(profile.displayName); | ||
expect(result.firstName).toEqual(profile.firstName); | ||
expect(result.lastName).toEqual(profile.lastName); | ||
}); | ||
|
||
it('should throw an error for non-existing user', async () => { | ||
prismaService.user.findUnique.mockResolvedValue(null); | ||
|
||
try { | ||
await userService.getUnique('nonExistentUser'); | ||
} catch (error) { | ||
expect(error.message).toBe('User nonExistentUser not found'); | ||
} | ||
}); | ||
|
||
it('should get all users correctly', () => { | ||
const users = [user]; | ||
|
||
prismaService.user.findMany.mockResolvedValue(users); | ||
|
||
expect(userService.getAll()).resolves.toEqual(users); | ||
}); | ||
}); |