-
-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add remainder auth tests #2233
Merged
imolorhe
merged 3 commits into
altair-graphql:master
from
horvathmarton:horvathmarton/auth-tests
Jul 22, 2023
Merged
Add remainder auth tests #2233
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,9 @@ | ||
import { Request, Response } from 'express'; | ||
|
||
export function mockRequest(props?: object): Request { | ||
return { ...props } as Request; | ||
} | ||
|
||
export function mockResponse(props?: object): Response { | ||
return { ...props } as Response; | ||
} |
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
69 changes: 69 additions & 0 deletions
69
packages/altair-api/src/auth/password/password.service.spec.ts
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,19 +1,88 @@ | ||
import { ConfigService } from '@nestjs/config'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { PasswordService } from './password.service'; | ||
import * as bcrypt from 'bcrypt'; | ||
|
||
describe('PasswordService', () => { | ||
let service: PasswordService; | ||
let configService: ConfigService; | ||
|
||
const passwordMock = '123456'; | ||
const hashMock = | ||
'8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92'; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [PasswordService, ConfigService], | ||
}).compile(); | ||
|
||
service = module.get<PasswordService>(PasswordService); | ||
configService = module.get<ConfigService>(ConfigService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('bcryptSaltRounds', () => { | ||
it(`should return the salt rounds`, () => { | ||
// GIVEN | ||
jest | ||
.spyOn(configService, 'get') | ||
.mockReturnValueOnce({ bcryptSaltOrRound: 16 }); | ||
|
||
// WHEN | ||
const rounds = service.bcryptSaltRounds; | ||
|
||
// THEN | ||
expect(rounds).toEqual(16); | ||
}); | ||
}); | ||
|
||
describe('validatePassword', () => { | ||
it(`should return true if the password matches the provided hash`, async () => { | ||
// GIVEN | ||
jest | ||
.spyOn(bcrypt, 'compare') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of mocking bcrypt, let's generate a valid hash and hardcode it in the test. You can create a map of password-to-hash that you can use in the tests, if you need multiple. |
||
.mockImplementationOnce(() => Promise.resolve(true)); | ||
|
||
// WHEN | ||
const validationResult = await service.validatePassword( | ||
passwordMock, | ||
hashMock | ||
); | ||
|
||
// THEN | ||
expect(validationResult).toBe(true); | ||
}); | ||
|
||
it(`should return false if the password doesn't match the provided hash`, async () => { | ||
// GIVEN | ||
jest | ||
.spyOn(bcrypt, 'compare') | ||
.mockImplementationOnce(() => Promise.resolve(false)); | ||
|
||
// WHEN | ||
const validationResult = await service.validatePassword( | ||
passwordMock, | ||
`${hashMock}-test` | ||
); | ||
|
||
// THEN | ||
expect(validationResult).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('hashPassword', () => { | ||
it(`should returned the hash of the password`, async () => { | ||
// GIVEN | ||
jest.spyOn(bcrypt, 'hash').mockImplementationOnce(() => hashMock); | ||
|
||
// WHEN | ||
const hash = await service.hashPassword(passwordMock); | ||
|
||
// THEN | ||
expect(hash).toEqual(hashMock); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the props should be valid fields in request right? Same treatment for response below.