-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathnotification.test.js
More file actions
296 lines (268 loc) · 9.77 KB
/
notification.test.js
File metadata and controls
296 lines (268 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// ----------------------------------------------------------------------------
// Initialization of test
// CAP Unit Testing: https://cap.cloud.sap/docs/node.js/cds-test?q=cds.test#run
// ----------------------------------------------------------------------------
'strict';
// Adds cds module
const cds = require('@sap/cds');
// Defines required CDS functions for testing
const { expect } = cds.test(__dirname + '/../../..');
// Import notification Service
const Notification = require('../../../srv/lib/notification');
const { httpCodes, visitStatusCode } = require('../../../srv/lib/codes');
describe('Util Notification - Reuse', () => {
let cdsTestLog = cds.test.log();
it('should be initialized successfully', function () {
const notification = new Notification(
'receiver@praondemand.com',
'subject',
'text',
'notificationSubtitle',
{
title: 'title',
description: 'description',
dateTime: new Date(),
visitorName: 'visitorName',
visitorStatusCode: visitStatusCode.booked
}
);
expect(notification.mailConfig.to).to.equal('receiver@praondemand.com');
expect(notification.mailConfig.subject).to.equal('subject');
expect(notification.mailConfig.html).to.equal('text');
expect(notification.mailConfig.notificationSubtitle).to.equal(
'notificationSubtitle'
);
});
it('should return the correct notification type ', async function () {
cds.context = {
locale: 'en'
};
const notification = new Notification(
'receiver@pra.ondemand.com',
'subject',
'text',
'notificationSubtitle',
{
title: 'title',
description: 'description',
dateTime: new Date(),
visitorName: 'visitorName',
visitorStatusCode: visitStatusCode.booked
}
);
const type = notification.getNotificationType();
expect(type.NotificationTypeKey).to.equal('PoetrySlamManagerEventReminder');
expect(type.NotificationTypeVersion).to.equal('1.0');
expect(type.Templates[0].Language).to.equal('en');
expect(type.Templates[0].TemplatePublic).to.equal('subject');
expect(type.Templates[0].TemplateSensitive).to.equal('subject');
expect(type.Templates[0].TemplateGrouped).to.equal('Poetry Slam Manager');
expect(type.Templates[0].Subtitle).to.equal('notificationSubtitle');
expect(type.Templates[0].EmailSubject).to.equal('subject');
expect(type.Templates[0].EmailHtml).to.equal('text');
expect(type.DeliveryChannels[0]).to.eql({
Type: 'MAIL',
Enabled: true,
DefaultPreference: true
});
});
it('should return the correct notification ', async function () {
cds.context = {
locale: 'en'
};
const notification = new Notification(
'receiver@pra.ondemand.com',
'subject',
'text',
'notificationSubtitle',
{
title: 'title',
description: 'description',
dateTime: new Date(),
visitorName: 'visitorName'
}
);
const notificationToSend = notification.getNotification({
NotificationTypeKey: 'PoetrySlamManagerEventReminder',
NotificationTypeVersion: '1.0'
});
expect(notificationToSend.NotificationTypeKey).to.equal(
'PoetrySlamManagerEventReminder'
);
expect(notificationToSend.NotificationTypeVersion).to.equal('1.0');
expect(notificationToSend.NavigationTargetAction).to.equal('display');
expect(notificationToSend.NavigationTargetObject).to.equal('poetryslams');
expect(notificationToSend.Priority).to.equal('Neutral');
expect(notificationToSend.Recipients[0]).to.eql({
RecipientId: 'receiver@pra.ondemand.com'
});
});
it('should not send notifications for pra.ondemand.com mail addresses ', async function () {
let errorObject;
const req = {
error: function (statusCode, i18n, params) {
errorObject = {
statusCode: statusCode,
i18n: i18n,
params: params
};
}
};
const notification = new Notification(
'receiver@pra.ondemand.com',
'subject',
'text',
'notificationSubtitle',
{
title: 'title',
description: 'description',
dateTime: new Date(),
visitorName: 'visitorName',
visitorStatusCode: visitStatusCode.booked
}
);
await notification.send(req);
expect(errorObject.statusCode).to.equal(httpCodes.bad_request);
expect(errorObject.i18n).to.equal('ACTION_NOTIFICATION_SEND_FAIL');
expect(errorObject.params[0]).to.equal('receiver@pra.ondemand.com');
expect(cdsTestLog.output).to.eql(
'ACTION send notification: Sending notification to pra.ondemand.com not allowed\n'
);
});
it('should throw an error message when error occurs during sending the notification', async function () {
let errorObject;
const req = {
error: function (statusCode, i18n, params) {
errorObject = {
statusCode: statusCode,
i18n: i18n,
params: params
};
}
};
const notification = new Notification(
'receiver@test.com',
'subject',
'text',
'notificationSubtitle',
{
title: 'title',
description: 'description',
dateTime: new Date(),
visitorName: 'visitorName',
visitStatusCode: visitStatusCode.booked
}
);
await notification.send(req);
expect(errorObject.statusCode).to.equal(httpCodes.bad_request);
expect(errorObject.i18n).to.equal('ACTION_NOTIFICATION_SEND_FAIL');
expect(errorObject.params[0]).to.equal('receiver@test.com');
expect(cdsTestLog.output).to.deep.include(
"ACTION send notification: Destination not found: Cannot read properties of null (reading 'authTokens')"
);
});
it('should throw an info message when visit status is canceled', async function () {
let infoObject;
const req = {
info: function (statusCode, i18n, params) {
infoObject = {
statusCode: statusCode,
i18n: i18n,
params: params
};
}
};
const notification = new Notification(
'receiver@test.com',
'subject',
'text',
'notificationSubtitle',
{
title: 'title',
description: 'description',
dateTime: new Date(),
visitorName: 'visitorName',
visitStatusCode: visitStatusCode.canceled
}
);
await notification.send(req);
expect(infoObject.statusCode).to.equal(httpCodes.ok);
expect(infoObject.i18n).to.equal('ACTION_NOTIFICATION_SEND_CANCELLED');
expect(infoObject.params[0]).to.equal('receiver@test.com');
expect(cdsTestLog.output).to.eql(
`ACTION send notification: Can't send to canceled visitor\n`
);
});
it('should generate email content for a poetry slam including translation German', function () {
cds.context = {
locale: 'de'
};
const content = Notification.generateMailContentForPoetrySlam();
expect(content).to.include('Hallo {{visitor_name}}');
expect(content).to.include(
'<title>{{poetry_slam_title}}</title>'
);
expect(content).to.include('{{poetry_slam_description}');
expect(content).to.include('{{poetry_slam_time}}');
expect(content).to.include('{{poetry_slam_date}}');
});
it('should generate email content for a poetry slam including translation English', function () {
cds.context = {
locale: 'EN'
};
const content = Notification.generateMailContentForPoetrySlam();
expect(content).to.include('Dear {{visitor_name}}');
expect(content).to.include(
'<title>{{poetry_slam_title}}</title>'
);
expect(content).to.include('{{poetry_slam_description}}');
expect(content).to.include('{{poetry_slam_time}}');
expect(content).to.include('{{poetry_slam_date}}');
});
it('should escape the generated email content for a poetry slam', function () {
cds.context = {
locale: 'EN'
};
const content = Notification.generateMailContentForPoetrySlam();
expect(content).to.include('Dear {{visitor_name}}!');
expect(content).to.include('>{{poetry_slam_title}}<');
expect(content).to.include('Description: {{poetry_slam_description}}');
expect(content).to.include(
'<p><img src="https://github.com/SAP-samples/partner-reference-application/blob/main-multi-tenant-features/srv/poetryslam/sample_data/poetrySlamLogo.jpg?raw=true"></p>'
);
expect(content).to.include('{{poetry_slam_time}}');
expect(content).to.include('{{poetry_slam_date}}');
});
it('should generate email title for a poetry slam in German', function () {
cds.context = {
locale: 'de'
};
const content = Notification.getMailTitleForPoetrySlam();
expect(content).to.equal('Deine Reime sind reserviert! 🖋️✨');
});
it('should generate mail title for a poetry slam in English', function () {
cds.context = {
locale: 'EN'
};
const content = Notification.getMailTitleForPoetrySlam();
expect(content).to.equal('Your Rhymes Are Reserved! 🖋️✨');
});
it('should generate notification subtitle for a poetry slam in German', function () {
cds.context = {
locale: 'de'
};
const content = Notification.getNotificationSubtitleForPoetrySlam();
expect(content).to.equal(
'Herzlichen Glückwunsch! Sie haben sich gerade Ihren Platz für die diVERSeste Veranstaltung des Jahres gesichert — {{poetry_slam_title}}! 🎤🔥'
);
});
it('should generate notification subtitle for a poetry slam in English', function () {
cds.context = {
locale: 'EN'
};
const content = Notification.getNotificationSubtitleForPoetrySlam();
expect(content).to.equal(
'Congratulations! You’ve just booked your spot at the most *verse-tile* event of the year — {{poetry_slam_title}}! 🎤🔥'
);
});
});