This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
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.
- Loading branch information
1 parent
bbb80af
commit 1ee4d14
Showing
8 changed files
with
81 additions
and
9 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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthService } from './auth.service'; | ||
import { UserRepository } from 'src/db/repositories/user.repository'; | ||
import { DeepMockProxy, mockDeep } from 'jest-mock-extended'; | ||
import { hashSync } from 'bcrypt'; | ||
import { PrismaClient } from '@prisma/client'; | ||
import { UserRepository } from '../db/repositories/user.repository'; | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
|
@@ -24,7 +24,7 @@ describe('AuthService', () => { | |
}) | ||
.overrideProvider(UserRepository) | ||
.useValue(mockDeep<UserRepository>()) | ||
.compile(); | ||
.compile(); | ||
|
||
service = module.get<AuthService>(AuthService); | ||
userRepository = await module.resolve(UserRepository); | ||
|
@@ -37,7 +37,24 @@ describe('AuthService', () => { | |
it('should return a user when a valid email and password is supplied', async () => { | ||
userRepository.findByEmail.mockResolvedValue(exampleUser); | ||
|
||
expect(await service.validateUserPassword("[email protected]", "1234")).toBeDefined(); | ||
}) | ||
expect( | ||
await service.validateUserPassword('[email protected]', '1234'), | ||
).toBeDefined(); | ||
}); | ||
|
||
it('should return null if the user is invalid', async () => { | ||
userRepository.findByEmail.mockResolvedValue(null); | ||
|
||
expect( | ||
await service.validateUserPassword('[email protected]', '1234'), | ||
).toBeNull(); | ||
}); | ||
|
||
it('should return null if the user is found, but the password is incorrect', async () => { | ||
userRepository.findByEmail.mockResolvedValue(exampleUser); | ||
|
||
expect( | ||
await service.validateUserPassword('[email protected]', 'incorrect'), | ||
).toBeNull(); | ||
}); | ||
}); |
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,40 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthService } from '../auth.service'; | ||
import { DeepMockProxy, mockDeep } from 'jest-mock-extended'; | ||
import { TestConstants } from '../../../test/lib/constants'; | ||
import { HTTPStrategy } from './http.strategy'; | ||
import { UnauthorizedException } from '@nestjs/common'; | ||
|
||
describe('HTTPStrategy tests', () => { | ||
let authService: DeepMockProxy<AuthService>; | ||
let cut: HTTPStrategy; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AuthService], | ||
}) | ||
.overrideProvider(AuthService) | ||
.useValue(mockDeep(AuthService)) | ||
.compile(); | ||
|
||
authService = module.get(AuthService); | ||
|
||
cut = new HTTPStrategy(authService); | ||
}); | ||
|
||
it('should return the user if the credentials are correct', async () => { | ||
authService.validateUserPassword.mockResolvedValue( | ||
TestConstants.database.users.exampleUser, | ||
); | ||
|
||
expect(await cut.validate('[email protected]', '1234')).toEqual( | ||
TestConstants.database.users.exampleUser, | ||
); | ||
}); | ||
|
||
it('should fail with an exception if the credentials are invalid', async () => { | ||
authService.validateUserPassword.mockResolvedValue(null); | ||
|
||
expect(async () => {await cut.validate('[email protected]', '1234')}).rejects.toThrow(); | ||
}); | ||
}); |
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,5 @@ | ||
import { Users } from './database.constants'; | ||
|
||
export const TestConstants = { | ||
database: { users: Users }, | ||
}; |
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,10 @@ | ||
export const Users = { | ||
exampleUser: { | ||
id: '0', | ||
email: '[email protected]', | ||
displayName: 'Max Mustermann', | ||
password: '$2a$04$7r2EqdYAla6UUw9rxMlfc.pACgmONvT/0jFLC2xTLdrIvoxOGmzAC', // 1234 | ||
enabled: true, | ||
verified: false, | ||
}, | ||
}; |