-
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
mrkeksz
committed
Dec 3, 2023
1 parent
baff9a6
commit ab472fb
Showing
16 changed files
with
150 additions
and
166 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 |
---|---|---|
|
@@ -60,7 +60,7 @@ describe(LocalStrategy.name, () => { | |
usersService = module.get(UsersService) | ||
loginAttemptsService = module.get(LoginAttemptsService) | ||
|
||
jest.spyOn(verificationCodesService, 'deleteByID').mockResolvedValueOnce(undefined) | ||
jest.spyOn(verificationCodesService, 'setStatusByID').mockResolvedValueOnce(undefined) | ||
}) | ||
|
||
afterEach(() => { | ||
|
@@ -73,24 +73,29 @@ describe(LocalStrategy.name, () => { | |
id: '1', | ||
email: '[email protected]', | ||
refreshToken: null, | ||
isActivated: false, | ||
status: 'unconfirmed', | ||
createdAt: currentDate, | ||
updatedAt: currentDate, | ||
} | ||
const verificationCode = { | ||
const verificationCode: VerificationCode = { | ||
id: '1', | ||
user, | ||
code: 123456, | ||
sendingAttempts: 1, | ||
status: 'active', | ||
expirationDate: new Date(currentDate.getTime() + 10 * 60 * 1000), | ||
createdAt: currentDate, | ||
updatedAt: currentDate, | ||
} | ||
const req = {socket: {remoteAddress: '123.123.123.123'}} as Request & { | ||
socket: {remoteAddress: string} | ||
} | ||
|
||
it('should return user if code matches', async () => { | ||
jest.spyOn(verificationCodesService, 'findOne').mockResolvedValueOnce(verificationCode) | ||
jest.spyOn(verificationCodesService, 'deleteByID').mockResolvedValueOnce(undefined) | ||
jest | ||
.spyOn(verificationCodesService, 'findOneByUserAndCode') | ||
.mockResolvedValueOnce(verificationCode) | ||
jest.spyOn(verificationCodesService, 'setStatusByID').mockResolvedValueOnce(undefined) | ||
jest.spyOn(usersService, 'activate').mockResolvedValueOnce(undefined) | ||
jest.spyOn(usersService, 'findOneByEmail').mockResolvedValueOnce(user) | ||
jest.spyOn(loginAttemptsService, 'findByIpWhereCreatedAtMoreThen').mockResolvedValueOnce([]) | ||
|
@@ -101,7 +106,7 @@ describe(LocalStrategy.name, () => { | |
}) | ||
|
||
it('should throw UnauthorizedException if code does not match', async () => { | ||
jest.spyOn(verificationCodesService, 'findOne').mockResolvedValueOnce(null) | ||
jest.spyOn(verificationCodesService, 'findOneByUserAndCode').mockResolvedValueOnce(null) | ||
jest.spyOn(usersService, 'findOneByEmail').mockResolvedValueOnce(user) | ||
jest.spyOn(loginAttemptsService, 'findByIpWhereCreatedAtMoreThen').mockResolvedValueOnce([]) | ||
|
||
|
@@ -111,7 +116,7 @@ describe(LocalStrategy.name, () => { | |
}) | ||
|
||
it('should throw UnauthorizedException if email does not match', async () => { | ||
jest.spyOn(verificationCodesService, 'findOne').mockResolvedValueOnce(null) | ||
jest.spyOn(verificationCodesService, 'findOneByUserAndCode').mockResolvedValueOnce(null) | ||
jest.spyOn(usersService, 'findOneByEmail').mockResolvedValueOnce(null) | ||
jest.spyOn(loginAttemptsService, 'findByIpWhereCreatedAtMoreThen').mockResolvedValueOnce([]) | ||
|
||
|
@@ -121,7 +126,7 @@ describe(LocalStrategy.name, () => { | |
}) | ||
|
||
it('should throw UnauthorizedException if code is expired', async () => { | ||
jest.spyOn(verificationCodesService, 'findOne').mockResolvedValueOnce({ | ||
jest.spyOn(verificationCodesService, 'findOneByUserAndCode').mockResolvedValueOnce({ | ||
...verificationCode, | ||
createdAt: new Date(currentDate.getTime() - 16 * 60 * 1000), | ||
}) | ||
|
@@ -134,7 +139,9 @@ describe(LocalStrategy.name, () => { | |
}) | ||
|
||
it('should throw UnauthorizedException if too many login attempts', async () => { | ||
jest.spyOn(verificationCodesService, 'findOne').mockResolvedValueOnce(verificationCode) | ||
jest | ||
.spyOn(verificationCodesService, 'findOneByUserAndCode') | ||
.mockResolvedValueOnce(verificationCode) | ||
jest.spyOn(usersService, 'findOneByEmail').mockResolvedValueOnce(user) | ||
jest.spyOn(loginAttemptsService, 'findByIpWhereCreatedAtMoreThen').mockResolvedValueOnce([ | ||
{ | ||
|
@@ -180,7 +187,9 @@ describe(LocalStrategy.name, () => { | |
}) | ||
|
||
it('should activate user if user is not activated', async () => { | ||
jest.spyOn(verificationCodesService, 'findOne').mockResolvedValueOnce(verificationCode) | ||
jest | ||
.spyOn(verificationCodesService, 'findOneByUserAndCode') | ||
.mockResolvedValueOnce(verificationCode) | ||
jest.spyOn(usersService, 'findOneByEmail').mockResolvedValueOnce(user) | ||
jest.spyOn(loginAttemptsService, 'findByIpWhereCreatedAtMoreThen').mockResolvedValueOnce([]) | ||
jest.spyOn(usersService, 'activate').mockResolvedValueOnce(undefined) | ||
|
@@ -191,7 +200,9 @@ describe(LocalStrategy.name, () => { | |
}) | ||
|
||
it('should create login attempt', async () => { | ||
jest.spyOn(verificationCodesService, 'findOne').mockResolvedValueOnce(verificationCode) | ||
jest | ||
.spyOn(verificationCodesService, 'findOneByUserAndCode') | ||
.mockResolvedValueOnce(verificationCode) | ||
jest.spyOn(usersService, 'findOneByEmail').mockResolvedValueOnce(user) | ||
jest.spyOn(loginAttemptsService, 'findByIpWhereCreatedAtMoreThen').mockResolvedValueOnce([]) | ||
jest.spyOn(loginAttemptsService, 'create').mockResolvedValueOnce({ | ||
|
@@ -211,14 +222,16 @@ describe(LocalStrategy.name, () => { | |
}) | ||
}) | ||
|
||
it('should delete verification code', async () => { | ||
jest.spyOn(verificationCodesService, 'findOne').mockResolvedValueOnce(verificationCode) | ||
it('should set "used" status for the verification code', async () => { | ||
jest | ||
.spyOn(verificationCodesService, 'findOneByUserAndCode') | ||
.mockResolvedValueOnce(verificationCode) | ||
jest.spyOn(usersService, 'findOneByEmail').mockResolvedValueOnce(user) | ||
jest.spyOn(loginAttemptsService, 'findByIpWhereCreatedAtMoreThen').mockResolvedValueOnce([]) | ||
|
||
await localStrategy.validate(req, user.email, 123456) | ||
|
||
expect(verificationCodesService.deleteByID).toBeCalledWith(verificationCode.id) | ||
expect(verificationCodesService.setStatusByID).toBeCalledWith(verificationCode.id, 'used') | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
|
@@ -83,7 +83,7 @@ describe(RefreshTokenStrategy.name, () => { | |
id: '1', | ||
email: '[email protected]', | ||
refreshToken: 'refreshToken', | ||
isActivated: true, | ||
status: 'active', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
} | ||
|
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,10 +1,13 @@ | ||
import {HASH_ROUNDS, MAX_SENDING_VERIFICATION_CODE_ATTEMPTS} from './auth.config.constants' | ||
import {NODE_ENV} from '../environment/environment.config.constants' | ||
|
||
const isTest = process.env[NODE_ENV.name] === NODE_ENV.options.TEST | ||
import { | ||
HASH_ROUNDS, | ||
MAX_SENDING_VERIFICATION_CODE_ATTEMPTS, | ||
VERIFICATION_CODE_LIFETIME_SECONDS, | ||
} from './auth.config.constants' | ||
import {EnvironmentConfigService} from '../environment/environment.config.service' | ||
|
||
export default (): Record<string, unknown> => ({ | ||
[HASH_ROUNDS.name]: isTest ? 1 : HASH_ROUNDS.defaultValue, | ||
[HASH_ROUNDS.name]: EnvironmentConfigService.isTest ? 1 : HASH_ROUNDS.defaultValue, | ||
[MAX_SENDING_VERIFICATION_CODE_ATTEMPTS.name]: | ||
MAX_SENDING_VERIFICATION_CODE_ATTEMPTS.defaultValue, | ||
[VERIFICATION_CODE_LIFETIME_SECONDS.name]: VERIFICATION_CODE_LIFETIME_SECONDS.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
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 |
---|---|---|
|
@@ -34,12 +34,12 @@ describe(UsersService.name, () => { | |
it('should return a created user', async () => { | ||
const currentDate = new Date() | ||
const userInput = {email: '[email protected]'} | ||
const expected = { | ||
const expected: User = { | ||
id: '1', | ||
refreshToken: null, | ||
createdAt: currentDate, | ||
updatedAt: currentDate, | ||
isActivated: false, | ||
status: 'active', | ||
...userInput, | ||
} | ||
|
||
|
@@ -54,11 +54,11 @@ describe(UsersService.name, () => { | |
describe(UsersService.prototype.findOneByID.name, () => { | ||
it('should return a user', async () => { | ||
const currentDate = new Date() | ||
const expected = { | ||
const expected: User = { | ||
id: '1', | ||
email: '[email protected]', | ||
refreshToken: null, | ||
isActivated: true, | ||
status: 'active', | ||
createdAt: currentDate, | ||
updatedAt: currentDate, | ||
} | ||
|
@@ -85,7 +85,7 @@ describe(UsersService.name, () => { | |
id: '1', | ||
email: '[email protected]', | ||
refreshToken: null, | ||
isActivated: true, | ||
status: 'active', | ||
createdAt: currentDate, | ||
updatedAt: currentDate, | ||
} | ||
|
@@ -96,6 +96,10 @@ describe(UsersService.name, () => { | |
expect(user).toStrictEqual({ | ||
id: expected.id, | ||
email: expected.email, | ||
refreshToken: expected.refreshToken, | ||
status: expected.status, | ||
createdAt: expected.createdAt, | ||
updatedAt: expected.updatedAt, | ||
}) | ||
}) | ||
|
||
|
@@ -133,7 +137,7 @@ describe(UsersService.name, () => { | |
|
||
const userID = '1' | ||
await usersService.activate(userID) | ||
expect(usersService.usersRepository.update).toHaveBeenCalledWith(userID, {isActivated: true}) | ||
expect(usersService.usersRepository.update).toHaveBeenCalledWith(userID, {status: 'active'}) | ||
}) | ||
}) | ||
}) |
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.