-
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 17, 2023
1 parent
ed378b0
commit 62227be
Showing
9 changed files
with
75 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,23 +99,5 @@ describe('EmailVerificationService', () => { | |
).resolves.toBeDefined() | ||
|
||
expect(verificationCode.status).toBe('used') | ||
expect(usersService.activateUserById).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should activate user when verifying email', async () => { | ||
const user = new User() | ||
user.status = 'unconfirmed' | ||
const verificationCode = new EmailVerificationCode() | ||
verificationCode.code = 123456 | ||
|
||
jest.spyOn(usersService, 'getUserByEmail').mockResolvedValue(user) | ||
jest.spyOn(verificationCodeRepository, 'findOne').mockResolvedValue(verificationCode) | ||
jest.spyOn(usersService, 'activateUserById').mockResolvedValue(undefined as any) | ||
jest.spyOn(tokenService, 'generateAndSaveTokens').mockResolvedValue(new Tokens()) | ||
jest.spyOn(verificationCodeRepository, 'save').mockResolvedValue(undefined as any) | ||
|
||
await service.verifyEmail('[email protected]', 123456, 'deviceInfo') | ||
|
||
expect(usersService.activateUserById).toHaveBeenCalledWith(user.id) | ||
}) | ||
}) |
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 {Module} from '@nestjs/common' | ||
import {TypeOrmModule} from '@nestjs/typeorm' | ||
import {Email} from './entities/email.entity' | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Email])], | ||
}) | ||
export class EmailsModule {} |
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,32 @@ | ||
import { | ||
Column, | ||
CreateDateColumn, | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
UpdateDateColumn, | ||
OneToOne, | ||
JoinColumn, | ||
} from 'typeorm' | ||
import {User} from '../../users/entities/user.entity' | ||
|
||
@Entity() | ||
export class Email { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string | ||
|
||
@Column({unique: true}) | ||
email: string | ||
|
||
// Email does not have a status field because it is not necessary to deactivate/activate it | ||
// and email notifications will be realized by another service and another entity | ||
|
||
@OneToOne(() => User, {onDelete: 'CASCADE', nullable: false}) | ||
@JoinColumn() | ||
user: User | ||
|
||
@CreateDateColumn({type: 'timestamp with time zone'}) | ||
createdAt: Date | ||
|
||
@UpdateDateColumn({type: 'timestamp with time zone'}) | ||
updatedAt: 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,8 +1,9 @@ | ||
import {User} from './entities/user.entity' | ||
import {UsersService} from './users.service' | ||
import {Repository} from 'typeorm' | ||
import {User} from './entities/user.entity' | ||
import {Test, TestingModule} from '@nestjs/testing' | ||
import {getRepositoryToken} from '@nestjs/typeorm' | ||
import {Email} from '../emails/entities/email.entity' | ||
|
||
describe('UsersService', () => { | ||
let service: UsersService | ||
|
@@ -25,7 +26,9 @@ describe('UsersService', () => { | |
|
||
it('should create a user by email', async () => { | ||
const testUser = new User() | ||
testUser.email = '[email protected]' | ||
const email = new Email() | ||
email.email = '[email protected]' | ||
testUser.email = email | ||
|
||
jest.spyOn(repo, 'create').mockReturnValueOnce(testUser) | ||
jest.spyOn(repo, 'save').mockResolvedValueOnce(testUser) | ||
|
@@ -36,11 +39,13 @@ describe('UsersService', () => { | |
|
||
it('should not create a user by email if it already exists', async () => { | ||
const testUser = new User() | ||
testUser.email = '[email protected]' | ||
const email = new Email() | ||
email.email = '[email protected]' | ||
testUser.email = email | ||
jest.spyOn(repo, 'findOne').mockResolvedValueOnce(testUser) | ||
jest.spyOn(repo, 'create') | ||
jest.spyOn(repo, 'save') | ||
expect(await service.getOrCreateUserByEmail(testUser.email)).toEqual(testUser) | ||
expect(await service.getOrCreateUserByEmail(testUser.email.email)).toEqual(testUser) | ||
expect(repo.create).not.toHaveBeenCalled() | ||
expect(repo.save).not.toHaveBeenCalled() | ||
}) | ||
|
@@ -56,22 +61,12 @@ describe('UsersService', () => { | |
|
||
it('should get a user by email', async () => { | ||
const testUser = new User() | ||
testUser.email = '[email protected]' | ||
const email = new Email() | ||
email.email = '[email protected]' | ||
testUser.email = email | ||
|
||
jest.spyOn(repo, 'findOne').mockResolvedValueOnce(testUser) | ||
|
||
expect(await service.getUserByEmail('[email protected]')).toEqual(testUser) | ||
}) | ||
|
||
it('should activate a user by id', async () => { | ||
const testUser = new User() | ||
testUser.id = '1' | ||
testUser.status = 'unconfirmed' | ||
|
||
jest.spyOn(repo, 'update').mockResolvedValueOnce(undefined as any) | ||
|
||
await service.activateUserById('1') | ||
|
||
expect(repo.update).toHaveBeenCalledWith({id: '1'}, {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