Skip to content

Commit

Permalink
Merge pull request #234 from Quickchive/test-#232/post-send-password-…
Browse files Browse the repository at this point in the history
…reset-email

Test #232/post send password reset email
  • Loading branch information
stae1102 authored Jan 23, 2024
2 parents b246596 + 96be472 commit aa90f43
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
124 changes: 124 additions & 0 deletions test/auth/post-send-password-reset-email.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { CACHE_MANAGER, HttpStatus, INestApplication } from '@nestjs/common';
import { StartedPostgreSqlContainer } from '@testcontainers/postgresql';
import { DataSource, Repository } from 'typeorm';
import { Cache } from 'cache-manager';
import { User } from '../../src/users/entities/user.entity';
import { Seeder } from '../seeder/seeder.interface';
import { UserSeeder } from '../seeder/user.seeder';
import { getBuilder } from '../common/application-builder';
import { cacheManagerMock } from '../mock/cache-manager.mock';
import { customJwtService } from '../../src/auth/jwt/jwt.service';
import { customJwtServiceMock } from '../mock/custom-jwt-service.mock';
import { MailService } from '../../src/mail/mail.service';
import { mailServiceMock } from '../mock/mail-service.mock';
import * as request from 'supertest';

jest.setTimeout(30_000);
describe('[POST] /api/auth/logout', () => {
// Application
let app: INestApplication;
let container: StartedPostgreSqlContainer; // TODO 결합도 낮추기
let dataSource: DataSource;
let cacheManager: Cache;
let jwtService: customJwtService;

// Repository
let userRepository: Repository<User>;

// Seeder
const userSeeder: Seeder<User> = new UserSeeder();

// Stub
let userStub: User = userSeeder.generateOne({ id: 1 }); // auto increment이므로 1로 적용.

beforeAll(async () => {
const {
builder,
container: _container,
dataSource: _dataSource,
} = await getBuilder();
container = _container;
dataSource = _dataSource;

const module = await builder
.overrideProvider(DataSource)
.useValue(dataSource)
.overrideProvider(CACHE_MANAGER)
.useValue(cacheManagerMock)
.overrideProvider(customJwtService)
.useValue(customJwtServiceMock)
.overrideProvider(MailService)
.useValue(mailServiceMock)
.compile();

app = module.createNestApplication();
await app.init();

userRepository = dataSource.getRepository(User);
cacheManager = app.get(CACHE_MANAGER);
jwtService = app.get(customJwtService);
});

beforeEach(async () => {
await dataSource.synchronize(true);
});

afterAll(async () => {
await app.close();
await container.stop();
});

/**
* 1. 이메일 전송에 성공한다.
* 2. 유저가 존재하지 않아 실패한다.
* 3. 유저가 인증되지 않아 실패한다.
*/

describe('이메일 전송에 성공한다.', () => {
beforeEach(async () => {
userStub = userSeeder.generateOne({ verified: true });
await userRepository.save(userStub);
});

it('빈 응답과 함께 201을 반환한다.', async () => {
const emailParam = userStub.email;

const { status, body } = await request(app.getHttpServer()).post(
`/auth/send-password-reset-email/${emailParam}`,
);

expect(status).toBe(HttpStatus.CREATED);
});
});

describe('유저가 존재하지 않아 실패한다.', () => {
it('404 예외를 던진다.', async () => {
const emailParam = 'test@email.com';

const { status, body } = await request(app.getHttpServer()).post(
`/auth/send-password-reset-email/${emailParam}`,
);

expect(status).toBe(HttpStatus.NOT_FOUND);
expect(body.message).toBe('User not found');
});
});

describe('유저가 인증되지 않아 실패한다.', () => {
beforeEach(async () => {
userStub = userSeeder.generateOne({ verified: false });
await userRepository.save(userStub);
});

it('빈 응답과 함께 201을 반환한다.', async () => {
const emailParam = userStub.email;

const { status, body } = await request(app.getHttpServer()).post(
`/auth/send-password-reset-email/${emailParam}`,
);

expect(status).toBe(HttpStatus.UNAUTHORIZED);
expect(body.message).toBe('User not verified');
});
});
});
3 changes: 3 additions & 0 deletions test/mock/mail-service.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const mailServiceMock = {
sendResetPasswordEmail: jest.fn(),
};

0 comments on commit aa90f43

Please sign in to comment.