From ec213d14f5a447d9de3512dc96e0b3c49f2a985a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Obernd=C3=B6rfer?= Date: Sat, 7 Dec 2024 15:44:04 +0100 Subject: [PATCH] Added tests for Mail Receiver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Florian Oberndörfer --- .../app/utils/mail/mail.controller.spec.ts | 98 +++++++++++++++++++ .../src/app/utils/mail/mail.service.spec.ts | 69 ++++++++++++- .../src/app/utils/mail/mail.service.ts | 4 +- 3 files changed, 165 insertions(+), 6 deletions(-) create mode 100644 apps/backend/src/app/utils/mail/mail.controller.spec.ts diff --git a/apps/backend/src/app/utils/mail/mail.controller.spec.ts b/apps/backend/src/app/utils/mail/mail.controller.spec.ts new file mode 100644 index 0000000..193f071 --- /dev/null +++ b/apps/backend/src/app/utils/mail/mail.controller.spec.ts @@ -0,0 +1,98 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { INestApplication } from '@nestjs/common'; +import request from 'supertest'; +import { MailController } from './mail.controller'; +import { MailService } from './mail.service'; +import { getRepositoryToken } from '@nestjs/typeorm'; +import { MailReceiverEntity } from './entity/MailReceiver.entity'; +import { DeleteResult, Repository } from 'typeorm'; +import { MailerService } from '@nestjs-modules/mailer'; +import { ConfigService } from '@nestjs/config'; + +describe('MailController (e2e)', () => { + let app: INestApplication; + let mailReceiverRepository: Repository; + + beforeEach(async () => { + const moduleFixture: TestingModule = await Test.createTestingModule({ + controllers: [MailController], + providers: [ + MailService, + { + provide: MailerService, + useValue: { + sendMail: jest.fn(), + }, + }, + { + provide: ConfigService, + useValue: { + get: jest.fn().mockReturnValue('true'), + }, + }, + { + provide: getRepositoryToken(MailReceiverEntity), + useValue: { + find: jest.fn(), + save: jest.fn(), + findOneBy: jest.fn(), + delete: jest.fn(), + }, + }, + ], + }).compile(); + + app = moduleFixture.createNestApplication(); + await app.init(); + + mailReceiverRepository = moduleFixture.get>( + getRepositoryToken(MailReceiverEntity) + ); + }); + + it('/GET mail receivers', async () => { + const receivers = [{ id: '1', mail: 'test@example.com' }]; + jest.spyOn(mailReceiverRepository, 'find').mockResolvedValue(receivers); + + const response = await request(app.getHttpServer()).get('/mail'); + expect(response.status).toBe(200); + expect(response.body).toEqual(receivers); + }); + + it('/POST mail receiver', async () => { + const createMailReceiverDto = { mail: 'new@example.com' }; + const savedReceiver = { id: '2', mail: 'new@example.com' }; + jest.spyOn(mailReceiverRepository, 'save').mockResolvedValue(savedReceiver); + + const response = await request(app.getHttpServer()) + .post('/mail') + .send(createMailReceiverDto); + expect(response.status).toBe(201); + expect(response.body).toEqual(savedReceiver); + }); + + it('/DELETE mail receiver', async () => { + const id = 'ea1a2f52-5cf4-44a6-b266-175ee396a18c'; + jest + .spyOn(mailReceiverRepository, 'findOneBy') + .mockResolvedValue({ id, mail: 'test@example.com' }); + jest + .spyOn(mailReceiverRepository, 'delete') + .mockResolvedValue(new DeleteResult()); + + const response = await request(app.getHttpServer()).delete(`/mail/${id}`); + expect(response.status).toBe(200); + }); + + it('should throw NotFoundException if mail receiver not found', async () => { + const id = 'ea1a2f52-5cf4-44a6-b266-175ee396a18e'; + jest.spyOn(mailReceiverRepository, 'findOneBy').mockResolvedValue(null); + + const response = await request(app.getHttpServer()).delete(`/mail/${id}`); + expect(response.status).toBe(404); + }); + + afterAll(async () => { + await app.close(); + }); +}); diff --git a/apps/backend/src/app/utils/mail/mail.service.spec.ts b/apps/backend/src/app/utils/mail/mail.service.spec.ts index cea783f..7821598 100644 --- a/apps/backend/src/app/utils/mail/mail.service.spec.ts +++ b/apps/backend/src/app/utils/mail/mail.service.spec.ts @@ -6,15 +6,39 @@ import { BackupType } from '../../backupData/dto/backupType'; import { SizeAlertEntity } from '../../alerting/entity/alerts/sizeAlert.entity'; import { SeverityType } from '../../alerting/dto/severityType'; import { SIZE_ALERT } from '../constants'; +import { getRepositoryToken } from '@nestjs/typeorm'; +import { MailReceiverEntity } from './entity/MailReceiver.entity'; +import { NotFoundException } from '@nestjs/common'; jest.mock('path', () => ({ resolve: jest.fn().mockReturnValue('mocked/path/to/logo.png'), + dirname: jest.fn(), })); +const mockMailReceiverRepository = { + find: jest.fn().mockResolvedValue([ + { + id: '1', + mail: 'test@example.com', + }, + ]), + save: jest.fn().mockImplementation((receiver) => Promise.resolve(receiver)), + findOneBy: jest.fn().mockImplementation(({ id }) => { + if (id === '1') { + return Promise.resolve({ + id: '1', + mail: 'test@example.com', + }); + } else { + return Promise.resolve(null); + } + }), + delete: jest.fn().mockResolvedValue({}), +}; + describe('MailService', () => { let service: MailService; let mailerService: MailerService; - let configService: ConfigService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -29,16 +53,18 @@ describe('MailService', () => { { provide: ConfigService, useValue: { - getOrThrow: jest.fn().mockReturnValue('test@example.com'), get: jest.fn().mockReturnValue('true'), }, }, + { + provide: getRepositoryToken(MailReceiverEntity), + useValue: mockMailReceiverRepository, + }, ], }).compile(); service = module.get(MailService); mailerService = module.get(MailerService); - configService = module.get(ConfigService); }); it('should be defined', () => { @@ -112,4 +138,41 @@ describe('MailService', () => { attachments, }); }); + + it('should get all mail receivers', async () => { + const receivers = [{ id: '1', mail: 'test@example.com' }]; + + expect(await service.getAllMailReceiver()).toStrictEqual(receivers); + }); + + it('should add a mail receiver', async () => { + const createMailReceiverDto = { mail: 'new@example.com' }; + const savedReceiver = { mail: 'new@example.com' }; + + await service.addMailReceiver(createMailReceiverDto); + + expect(mockMailReceiverRepository.save).toBeCalledWith({ + mail: createMailReceiverDto.mail, + }); + + expect(await service.addMailReceiver(createMailReceiverDto)).toStrictEqual( + savedReceiver + ); + }); + + it('should remove a mail receiver', async () => { + const id = '1'; + await service.removeMailReceiver(id); + + expect(mockMailReceiverRepository.findOneBy).toHaveBeenCalledWith({ id }); + expect(mockMailReceiverRepository.delete).toHaveBeenCalledWith({ id }); + }); + + it('should throw NotFoundException if mail receiver not found', async () => { + const id = 'non-existent-id'; + + await expect(service.removeMailReceiver(id)).rejects.toThrow( + NotFoundException + ); + }); }); diff --git a/apps/backend/src/app/utils/mail/mail.service.ts b/apps/backend/src/app/utils/mail/mail.service.ts index ba1c641..2129c2e 100644 --- a/apps/backend/src/app/utils/mail/mail.service.ts +++ b/apps/backend/src/app/utils/mail/mail.service.ts @@ -38,9 +38,7 @@ export class MailService { .map((receiver) => receiver.mail) .join(',') .split(','); - - console.log('receivers', receivers); - + let reason = ''; let description = ''; let valueColumnName = '';