-
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.
hash passwords and tokens, create new user with login (#9)
- Loading branch information
Showing
20 changed files
with
496 additions
and
76 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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 |
---|---|---|
|
@@ -35,6 +35,7 @@ describe('AuthService', () => { | |
email: '[email protected]', | ||
password: '123', | ||
})) | ||
jest.spyOn(authService, 'hashText').mockImplementation(async () => 'test-hash') | ||
const [tokens, access_token, refresh_token] = await Promise.all([ | ||
authService.login(1), | ||
jwtService.signAsync({sub: 1}), | ||
|
@@ -47,7 +48,7 @@ describe('AuthService', () => { | |
), | ||
]) | ||
expect(tokens).toStrictEqual({access_token, refresh_token}) | ||
expect(usersService.update).toHaveBeenCalledWith(1, {refreshToken: refresh_token}) | ||
expect(usersService.update).toHaveBeenCalledWith(1, {refreshToken: 'test-hash'}) | ||
}) | ||
}) | ||
|
||
|
@@ -58,12 +59,13 @@ describe('AuthService', () => { | |
password: '123', | ||
} | ||
|
||
beforeEach(() => { | ||
beforeEach(async () => { | ||
returnedUser.password = await authService.hashText(returnedUser.password) | ||
jest.spyOn(usersService, 'findOneByEmail').mockImplementation(async () => returnedUser) | ||
}) | ||
|
||
it('should return user id', async () => { | ||
const user = await authService.validateUser(returnedUser.email, returnedUser.password) | ||
const user = await authService.validateUser(returnedUser.email, '123') | ||
expect(user).toEqual({id: returnedUser.id}) | ||
}) | ||
|
||
|
@@ -72,10 +74,13 @@ describe('AuthService', () => { | |
expect(user).toBeNull() | ||
}) | ||
|
||
it('should return null if the email is wrong', async () => { | ||
it('should return new user id if the email is new', async () => { | ||
jest.restoreAllMocks() | ||
jest | ||
.spyOn(usersService, 'create') | ||
.mockImplementation(async () => ({id: 2, password: '123', email: 'tt.tt.tt'})) | ||
const user = await authService.validateUser('tt.tt.tt', returnedUser.password) | ||
expect(user).toBeNull() | ||
expect(user).toStrictEqual({id: 2}) | ||
}) | ||
}) | ||
|
||
|
@@ -98,6 +103,7 @@ describe('AuthService', () => { | |
email: '[email protected]', | ||
password: '123', | ||
})) | ||
jest.spyOn(authService, 'hashText').mockImplementation(async () => 'test-hash') | ||
|
||
const [tokens, access_token, refresh_token] = await Promise.all([ | ||
authService.refreshTokens(1), | ||
|
@@ -112,7 +118,29 @@ describe('AuthService', () => { | |
]) | ||
|
||
expect(tokens).toStrictEqual({access_token, refresh_token}) | ||
expect(usersService.update).toHaveBeenCalledWith(1, {refreshToken: refresh_token}) | ||
expect(usersService.update).toHaveBeenCalledWith(1, {refreshToken: 'test-hash'}) | ||
}) | ||
}) | ||
|
||
describe('hashText', () => { | ||
it('should return a hashed text', async () => { | ||
jest.spyOn(authService, 'hashText').mockImplementation(async () => 'test-hash') | ||
const hashedText = await authService.hashText('test') | ||
expect(hashedText).toBe('test-hash') | ||
}) | ||
}) | ||
|
||
describe('compareHash', () => { | ||
it('should return true if the text and hash are the same', async () => { | ||
jest.spyOn(authService, 'compareHash').mockImplementation(async () => true) | ||
const isMatch = await authService.compareHash('test', 'test-hash') | ||
expect(isMatch).toBe(true) | ||
}) | ||
|
||
it('should return false if the text and hash are not the same', async () => { | ||
jest.spyOn(authService, 'compareHash').mockImplementation(async () => false) | ||
const isMatch = await authService.compareHash('test', 'test-hash') | ||
expect(isMatch).toBe(false) | ||
}) | ||
}) | ||
}) |
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,8 @@ | ||
import {HASH_ROUNDS} from './auth.config.constants' | ||
import {NODE_ENV} from '../environment/environment.config.constants' | ||
|
||
const isTest = process.env[NODE_ENV.name] === NODE_ENV.options.TEST | ||
|
||
export default () => ({ | ||
[HASH_ROUNDS.name]: isTest ? 1 : HASH_ROUNDS.defaultValue, | ||
}) |
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
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,8 @@ | ||
import {NODE_ENV} from '../environment/environment.config.constants' | ||
import {REQUEST_TIMEOUT_MS} from './server.config.constants' | ||
|
||
const isTest = process.env[NODE_ENV.name] === NODE_ENV.options.TEST | ||
|
||
export default () => ({ | ||
[REQUEST_TIMEOUT_MS.name]: isTest ? 20 : REQUEST_TIMEOUT_MS.defaultValue, | ||
}) |
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
Oops, something went wrong.