1
1
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq' ;
2
2
import { Test , TestingModule } from '@nestjs/testing' ;
3
+ import { createMock } from '@golevelup/ts-jest' ;
4
+ import { ConfigService } from '@nestjs/config' ;
3
5
import { Mail } from './mail.interface' ;
4
6
import { MailService } from './mail.service' ;
7
+ import { IMailConfig } from './interfaces/mail-config' ;
5
8
6
9
describe ( 'MailService' , ( ) => {
7
10
let module : TestingModule ;
@@ -19,6 +22,10 @@ describe('MailService', () => {
19
22
MailService ,
20
23
{ provide : AmqpConnection , useValue : { publish : ( ) => { } } } ,
21
24
{ provide : 'MAIL_SERVICE_OPTIONS' , useValue : mailServiceOptions } ,
25
+ {
26
+ provide : ConfigService ,
27
+ useValue : createMock < ConfigService < IMailConfig , true > > ( { get : ( ) => [ 'schul-cloud.org' , 'example.com' ] } ) ,
28
+ } ,
22
29
] ,
23
30
} ) . compile ( ) ;
24
31
@@ -34,13 +41,43 @@ describe('MailService', () => {
34
41
expect ( service ) . toBeDefined ( ) ;
35
42
} ) ;
36
43
37
- it ( 'should send given data to queue' , async ( ) => {
38
- const data :
Mail = { mail :
{ plainTextContent :
'content' , subject :
'Test' } , recipients :
[ '[email protected] ' ] } ;
39
- const amqpConnectionSpy = jest . spyOn ( amqpConnection , 'publish' ) ;
44
+ describe ( 'send' , ( ) => {
45
+ describe ( 'when recipients array is empty' , ( ) => {
46
+ it ( 'should not send email' , async ( ) => {
47
+ const data : Mail = {
48
+ mail : { plainTextContent : 'content' , subject : 'Test' } ,
49
+ recipients :
[ '[email protected] ' ] ,
50
+ } ;
40
51
41
- await service . send ( data ) ;
52
+ const amqpConnectionSpy = jest . spyOn ( amqpConnection , 'publish' ) ;
42
53
43
- const expectedParams = [ mailServiceOptions . exchange , mailServiceOptions . routingKey , data , { persistent : true } ] ;
44
- expect ( amqpConnectionSpy ) . toHaveBeenCalledWith ( ...expectedParams ) ;
54
+ await service . send ( data ) ;
55
+
56
+ expect ( amqpConnectionSpy ) . toHaveBeenCalledTimes ( 0 ) ;
57
+ } ) ;
58
+ } ) ;
59
+ describe ( 'when sending email' , ( ) => {
60
+ it ( 'should remove email address that have blacklisted domain and send given data to queue' , async ( ) => {
61
+ const data : Mail = {
62
+ mail : { plainTextContent : 'content' , subject : 'Test' } ,
63
+
64
+
65
+
66
+
67
+ } ;
68
+
69
+ const amqpConnectionSpy = jest . spyOn ( amqpConnection , 'publish' ) ;
70
+
71
+ await service . send ( data ) ;
72
+
73
+ expect ( data . recipients ) . toEqual ( [ '[email protected] ' ] ) ;
74
+ expect ( data . cc ) . toEqual ( [ ] ) ;
75
+ expect ( data . bcc ) . toEqual ( [ '[email protected] ' ] ) ;
76
+ expect ( data . replyTo ) . toEqual ( [ '[email protected] ' ] ) ;
77
+
78
+ const expectedParams = [ mailServiceOptions . exchange , mailServiceOptions . routingKey , data , { persistent : true } ] ;
79
+ expect ( amqpConnectionSpy ) . toHaveBeenCalledWith ( ...expectedParams ) ;
80
+ } ) ;
81
+ } ) ;
45
82
} ) ;
46
83
} ) ;
0 commit comments