-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathforms.test.js
More file actions
260 lines (226 loc) · 8.07 KB
/
forms.test.js
File metadata and controls
260 lines (226 loc) · 8.07 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
'strict';
// Adds cds module
const cds = require('@sap/cds');
// Defines required CDS functions for testing
const { expect, POST, axios, test } = cds.test(__dirname + '/../../..');
// Add modules to allow mocking
const sinon = require('sinon');
const { XMLBuilder } = require('fast-xml-parser');
const { Readable } = require('stream');
const {
StoreFormTemplatesApi,
ADSRenderRequestApi
} = require('../../../srv/external/FORMSAPI');
const Forms = require('../../../srv/lib/forms');
const Logo = require('../../../srv/lib/logo');
const serviceCredentials = require('../../../srv/lib/serviceCredentials');
const { mimeTypes } = require('../../../srv/lib/codes');
// Authentication for tests; role PoetrySlamManager
axios.defaults.auth = { username: 'peter', password: 'welcome' };
// Test data
const mockBase64Image = Buffer.from('Mock base64 image').toString('base64');
const poetrySlamId = '79ceab87-300d-4b66-8cc3-f82c679b77a2';
const expectedFormsData = {
ps_bookings_list_form: {
logo: mockBase64Image,
title: 'Being Human Poetry Slam',
description:
'Poetry Slams about human existence, revealing the beauty and complexity within',
visitors_table: {
visitor_row: [
{
name: 'Thomas Schmidt'
}
]
},
artists_table: {
artist_row: [
{
name: 'Miguel Rodriguez'
}
]
}
}
};
describe('Util Forms', () => {
before(async () => {
await test.data.reset();
await POST(`/odata/v4/poetryslamservice/createTestData`);
});
describe('Data XML', () => {
// Data XML Stubs
let bufferStub, buildStub, logoStub;
beforeEach(function () {
// Create Data XML Stubs
// Create a stub for the XMLBuilder instance
const date = new Date();
date.setDate(date.getDate() + 60);
date.setHours(15, 0, 0);
date.setMilliseconds(0);
expectedFormsData.ps_bookings_list_form.date =
date.toISOString().split('.')[0] + 'Z';
sinon.createStubInstance(XMLBuilder);
buildStub = sinon
.stub(XMLBuilder.prototype, 'build')
.returns(expectedFormsData);
bufferStub = sinon.stub(Buffer, 'from').returns({
toString: () => 'Mock data XML base64'
});
logoStub = sinon.stub(Logo, 'encodeFileBase64').returns(mockBase64Image);
});
afterEach(function () {
// Restore create Data XML Stubs
buildStub.restore();
bufferStub.restore();
logoStub.restore();
});
it('should create xml data', async function () {
const forms = new Forms(undefined, poetrySlamId);
const xmlDataBase64 = await forms.createDataXML();
expect(xmlDataBase64).to.equal('Mock data XML base64');
sinon.assert.calledOnce(buildStub);
sinon.assert.calledWith(buildStub, expectedFormsData);
sinon.assert.calledOnce(bufferStub);
});
});
describe('getFileName', () => {
it('should return a fallback file name', async function () {
const forms = new Forms(undefined, poetrySlamId);
const fileName = await forms.getFileName(null);
expect(fileName).to.equal('PoetrySlam_2');
});
it('should return the correct file name', async function () {
const forms = new Forms(undefined, poetrySlamId);
const fileName = await forms.getFileName();
expect(fileName).to.equal('GuestList_2');
});
});
describe('getReadable', () => {
it('should return a Readable for a given file input', async function () {
const forms = new Forms(undefined, poetrySlamId);
const testContent = Buffer.from('test file content').toString('base64');
const readable = await forms.getReadable(testContent);
expect(readable.value instanceof Readable).eql(true);
expect(readable.$mediaContentType).eql(mimeTypes.application_pdf);
expect(readable.$mediaContentDispositionFilename).eql('GuestList_2.pdf');
});
});
describe('ADS Render Request', () => {
let mockXdpTemplate;
let mockFileContent;
let mockBufferedDataBase64;
// ADS Render Requests Stubs
let getCredentialsStub;
let getTokenStub;
let StoreFormTemplatesApiStub;
let ADSRenderRequestApiStub;
let bufferStub;
let readableStub;
beforeEach(async () => {
mockXdpTemplate = 'Mock XdpTemplate';
mockFileContent = 'Mock File Content';
mockBufferedDataBase64 = 'Mock buffered data XML base64';
// Various stubs for external services
getCredentialsStub = sinon
.stub(serviceCredentials, 'getServiceCredentials')
.returns({ uri: 'Mock srvUrl' });
getTokenStub = sinon
.stub(serviceCredentials, 'getServiceToken')
.resolves('Mock token');
bufferStub = sinon.stub(Buffer, 'from').returns(mockBufferedDataBase64);
sinon.createStubInstance(Readable);
readableStub = sinon.stub(Readable.prototype, 'push');
StoreFormTemplatesApiStub = sinon
.stub(StoreFormTemplatesApi, 'templateGet')
.returns({
addCustomHeaders: () => ({
execute: () => Promise.resolve({ xdpTemplate: mockXdpTemplate })
})
});
ADSRenderRequestApiStub = sinon
.stub(ADSRenderRequestApi, 'renderingPdfPost')
.returns({
addCustomHeaders: () => ({
addCustomHeaders: () => ({
execute: () => Promise.resolve({ fileContent: mockFileContent })
})
})
});
});
afterEach(function () {
// Restore stubs
StoreFormTemplatesApiStub.restore();
getCredentialsStub.restore();
getTokenStub.restore();
ADSRenderRequestApiStub.restore();
bufferStub.restore();
readableStub.restore();
});
it('should send render request to SAP Forms Service by Adobe API', async function () {
const forms = new Forms(undefined, poetrySlamId);
const result = await forms.getRenderedPDF();
expect(result).eql(mockFileContent);
sinon.assert.calledOnce(getCredentialsStub);
sinon.assert.calledOnce(getTokenStub);
sinon.assert.calledOnce(StoreFormTemplatesApiStub);
sinon.assert.calledOnce(ADSRenderRequestApiStub);
});
});
describe('ADS Render Request Error', () => {
// ADS Render Requests Stubs
let stubLog;
let getCredentialsStub;
let getTokenStub;
let StoreFormTemplatesApiStub;
beforeEach(async () => {
// Use Stubs for external services
stubLog = sinon.stub(console, 'error');
getCredentialsStub = sinon
.stub(serviceCredentials, 'getServiceCredentials')
.returns(Promise.resolve({ uri: 'Mock srvUrl' }));
getTokenStub = sinon
.stub(serviceCredentials, 'getServiceToken')
.returns(Promise.resolve('Mock token'));
StoreFormTemplatesApiStub = sinon
.stub(StoreFormTemplatesApi, 'templateGet')
.throws('Error');
});
afterEach(function () {
// Restore Stubs
getCredentialsStub.restore();
getTokenStub.restore();
StoreFormTemplatesApiStub.restore();
stubLog.restore();
});
it('should handle error during render request to SAP Forms Service by Adobe API', async function () {
const forms = new Forms(undefined, poetrySlamId);
await forms.getRenderedPDF();
sinon.assert.calledWith(
stubLog,
'An error occurred while getting the rendered PDF: Sinon-provided Error'
);
});
});
describe('ADS getCredentials', () => {
let bufferStub, stubLog;
beforeEach(async () => {
stubLog = sinon.stub(console, 'error');
bufferStub = sinon.stub(Buffer, 'from').returns({
toString: () => 'Mock base64 encoded client credentials'
});
});
afterEach(function () {
// Restore Get Credential Stub
stubLog.restore();
bufferStub.restore();
});
it('should handle error received from trying to get a JWT token without service binding', async function () {
const forms = new Forms(undefined, poetrySlamId);
await forms.getRenderedPDF();
sinon.assert.calledWith(
stubLog,
`An error occurred while getting the rendered PDF: Missing binding credentials for service "adsrestapi"`
);
});
});
});