-
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 11, 2023
1 parent
3a92f60
commit 1174419
Showing
2 changed files
with
128 additions
and
43 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 |
---|---|---|
@@ -1 +1,97 @@ | ||
// TODO: tests | ||
import {Test, TestingModule} from '@nestjs/testing' | ||
import {getRepositoryToken} from '@nestjs/typeorm' | ||
import {Repository} from 'typeorm' | ||
import {EmailVerificationService} from './email-verification.service' | ||
import {UsersService} from '../users/users.service' | ||
import {AuthService} from '../auth/auth.service' | ||
import {EmailVerificationConfigService} from '../config/email-verification/email-verification.config.service' | ||
import {EmailVerificationCode} from './entities/email-verification-code.entity' | ||
import {User} from '../users/entities/user.entity' | ||
import {UnauthorizedException} from '@nestjs/common' | ||
|
||
describe('EmailVerificationService', () => { | ||
let service: EmailVerificationService | ||
let usersService: UsersService | ||
let authService: AuthService | ||
let verificationCodeRepository: Repository<EmailVerificationCode> | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
EmailVerificationService, | ||
{provide: getRepositoryToken(EmailVerificationCode), useClass: Repository}, | ||
{ | ||
provide: UsersService, | ||
useValue: { | ||
getOrCreateUserByEmail: jest.fn(), | ||
getUserByEmail: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: AuthService, | ||
useValue: { | ||
login: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: EmailVerificationConfigService, | ||
useValue: {verificationCodeLifetimeMilliseconds: 60000}, | ||
}, | ||
], | ||
}).compile() | ||
|
||
service = module.get<EmailVerificationService>(EmailVerificationService) | ||
usersService = module.get<UsersService>(UsersService) | ||
authService = module.get<AuthService>(AuthService) | ||
verificationCodeRepository = module.get<Repository<EmailVerificationCode>>( | ||
getRepositoryToken(EmailVerificationCode), | ||
) | ||
}) | ||
|
||
it('should send verification code to email', async () => { | ||
const user = new User() | ||
const verificationCode = new EmailVerificationCode() | ||
verificationCode.code = 123456 | ||
|
||
jest.spyOn(usersService, 'getOrCreateUserByEmail').mockResolvedValue(user) | ||
jest.spyOn(verificationCodeRepository, 'findOne').mockResolvedValue(verificationCode) | ||
|
||
await service.sendVerificationCodeToEmail('[email protected]') | ||
}) | ||
|
||
it('should throw UnauthorizedException when verifying email with invalid user', async () => { | ||
jest.spyOn(usersService, 'getUserByEmail').mockResolvedValue(null) | ||
|
||
await expect(service.verifyEmail('[email protected]', 123456)).rejects.toThrow( | ||
UnauthorizedException, | ||
) | ||
}) | ||
|
||
it('should throw UnauthorizedException when verifying email with invalid code', async () => { | ||
const user = new User() | ||
const verificationCode = new EmailVerificationCode() | ||
verificationCode.code = 123456 | ||
|
||
jest.spyOn(usersService, 'getUserByEmail').mockResolvedValue(user) | ||
jest.spyOn(verificationCodeRepository, 'findOne').mockResolvedValue(verificationCode) | ||
|
||
await expect(service.verifyEmail('[email protected]', 654321)).rejects.toThrow( | ||
UnauthorizedException, | ||
) | ||
}) | ||
|
||
it('should verify email with valid user and code', async () => { | ||
const user = new User() | ||
const verificationCode = new EmailVerificationCode() | ||
verificationCode.code = 123456 | ||
|
||
jest.spyOn(usersService, 'getUserByEmail').mockResolvedValue(user) | ||
jest.spyOn(verificationCodeRepository, 'findOne').mockResolvedValue(verificationCode) | ||
jest.spyOn(authService, 'login').mockResolvedValue({} as any) | ||
jest.spyOn(verificationCodeRepository, 'save').mockResolvedValue(undefined as any) | ||
|
||
await expect(service.verifyEmail('[email protected]', 123456)).resolves.toBeDefined() | ||
|
||
expect(verificationCode.status).toBe('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