|
| 1 | +import { Test, type TestingModule } from '@nestjs/testing'; |
| 2 | +import { createMock, type DeepMocked } from '@golevelup/ts-jest'; |
| 3 | +import { ConflictException } from '@nestjs/common'; |
| 4 | +import { ConfigService } from '@nestjs/config'; |
| 5 | +import { MailService } from './mail.service'; |
| 6 | +import { HttpClient } from '../http/http.service'; |
| 7 | + |
| 8 | +describe('MailService', () => { |
| 9 | + let mailService: MailService; |
| 10 | + let httpClient: DeepMocked<HttpClient>; |
| 11 | + let configService: DeepMocked<ConfigService>; |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + const moduleRef: TestingModule = await Test.createTestingModule({ |
| 15 | + providers: [MailService], |
| 16 | + }) |
| 17 | + .useMocker(() => createMock()) |
| 18 | + .compile(); |
| 19 | + |
| 20 | + mailService = moduleRef.get(MailService); |
| 21 | + httpClient = moduleRef.get(HttpClient); |
| 22 | + configService = moduleRef.get(ConfigService); |
| 23 | + |
| 24 | + configService.get.mockImplementation((key: string) => { |
| 25 | + const config = { |
| 26 | + 'apis.mail.url': 'http://mail:3100', |
| 27 | + 'secrets.gateway': |
| 28 | + 'LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlCT3dJQkFBSkJBTDlDTVRlZGEramdIcGJuTmtlSm51TlpnYzg5TGFvMGNQNkl6dlJrYTJ0MUVKbnh5ZTA1CndSWGZLMXFpbTFOMGU3cGhkd0RkRWYvNGJ1eFc5V2g1UWxzQ0F3RUFBUUpCQUpnRXljLzF2VDdGWFNyK3JpTWcKWFAxQ09LNTdaeCtCUFVyamZQTytHYSszWk1MRHhqaG44dGZmV1E4VUpKemJ5VkQ0Q0JqTmNra2xRN3phQ29BNwo1WWtDSVFEd0h2MXhVRkFVUkI2b3QwL0JMMWNxek5SNU80dFBMT0NjL2gyK0o4Y09WUUloQU12b0FrMm5IQWhSClpRNmhNZGFTdWtPVTE3MTYvRGxnNWNiSXNWYXh0bDN2QWlBTUdTT2YzL0lJODEyd0ZueFlPWEJrNGFrYTZwc2MKUkNDVkNHQ3JRZ25QZVFJZ2NTU2E2cFc0YzFFZTN5Qkl0RVNVZ0YxOTNKRDZsYWdUdDlxeXRHVkZ5UmNDSVFDYgp6dE85ampXcERmYTlnWTV2dVB4MFgyUkcxbjJQb0ZYVjVXT29RanNqbnc9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=', |
| 29 | + isDevelopment: true, |
| 30 | + }; |
| 31 | + return config[key]; |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should be defined', () => { |
| 36 | + expect(mailService).toBeDefined(); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('createAccount', () => { |
| 40 | + const payload = { |
| 41 | + userId: 'user-uuid', |
| 42 | + address: 'john@inxt.eu', |
| 43 | + domain: 'inxt.eu', |
| 44 | + displayName: 'John Doe', |
| 45 | + }; |
| 46 | + |
| 47 | + it('When mail gateway responds successfully, then it should return account data', async () => { |
| 48 | + httpClient.post.mockResolvedValueOnce({ |
| 49 | + data: { address: 'john@inxt.eu', domain: 'inxt.eu' }, |
| 50 | + } as any); |
| 51 | + |
| 52 | + const result = await mailService.createAccount(payload); |
| 53 | + |
| 54 | + expect(result).toEqual({ address: 'john@inxt.eu', domain: 'inxt.eu' }); |
| 55 | + expect(httpClient.post).toHaveBeenCalledWith( |
| 56 | + 'http://mail:3100/gateway/accounts', |
| 57 | + payload, |
| 58 | + expect.objectContaining({ |
| 59 | + headers: expect.objectContaining({ |
| 60 | + 'Content-Type': 'application/json', |
| 61 | + Authorization: expect.stringMatching(/^Bearer /), |
| 62 | + }), |
| 63 | + }), |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it('When mail gateway returns 409, then it should throw ConflictException', async () => { |
| 68 | + const axiosError = { |
| 69 | + response: { |
| 70 | + status: 409, |
| 71 | + data: { message: 'Address already in use' }, |
| 72 | + }, |
| 73 | + }; |
| 74 | + httpClient.post.mockRejectedValueOnce(axiosError); |
| 75 | + |
| 76 | + await expect(mailService.createAccount(payload)).rejects.toThrow( |
| 77 | + ConflictException, |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it('When mail gateway returns 409 without message, then it should use default message', async () => { |
| 82 | + const axiosError = { |
| 83 | + response: { status: 409, data: {} }, |
| 84 | + }; |
| 85 | + httpClient.post.mockRejectedValueOnce(axiosError); |
| 86 | + |
| 87 | + await expect(mailService.createAccount(payload)).rejects.toThrow( |
| 88 | + 'Mail account already exists', |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('When mail gateway returns non-409 error, then it should rethrow', async () => { |
| 93 | + const axiosError = { |
| 94 | + response: { status: 500 }, |
| 95 | + message: 'Internal Server Error', |
| 96 | + }; |
| 97 | + httpClient.post.mockRejectedValueOnce(axiosError); |
| 98 | + |
| 99 | + await expect(mailService.createAccount(payload)).rejects.toBe(axiosError); |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments