-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathattachments.test.js
More file actions
152 lines (134 loc) · 4.16 KB
/
attachments.test.js
File metadata and controls
152 lines (134 loc) · 4.16 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
'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 Attachments = require('../../../srv/lib/attachments');
const fs = require('fs');
const path = require('path');
// Authentication for tests; role PoetrySlamManager
axios.defaults.auth = { username: 'peter', password: 'welcome' };
describe('Util Attachments', () => {
before(async () => {
await test.data.reset();
await POST(`/odata/v4/poetryslamservice/createTestData`);
});
describe('handleAttachmentTestData', () => {
let poetrySlamIds,
req,
db,
stubBuffer,
stubSELECTOne,
stubFs,
stubPathJoin,
stubConsoleError,
stubI18n,
stubCdsEnv,
stubRun,
attachments;
beforeEach(function () {
// Create stubs
stubBuffer = sinon.stub(Buffer, 'from').returns({
toString: () => 'Mocked Base64 encoded PDF'
});
stubFs = sinon.stub(fs, 'readFileSync').returns('Mocked file bytes');
stubPathJoin = sinon.stub(path, 'join').returns('/mocked/path.pdf');
stubConsoleError = sinon.stub(console, 'error');
stubCdsEnv = sinon.stub(cds, 'env').value({
requires: {
sdm: {
credentials: { key: 'value' }
}
}
});
stubI18n = sinon.stub(cds, 'i18n').value({
labels: { at: sinon.stub().returns('Mocked i18n text') }
});
req = {
error: sinon.stub().throws(new Error('Mocked request error')),
info: sinon.stub(),
user: {
authInfo: {
token: {
xsSystemAttributes: {
'xs.rolecollections': [
'PoetrySlamDocumentManagementRoleCollection'
]
}
}
}
}
};
const poetrySlamId = '79ceab87-300d-4b66-8cc3-f82c679b77a2';
poetrySlamIds = [poetrySlamId];
});
afterEach(function () {
// Restore stubs
stubBuffer.restore();
stubSELECTOne.restore();
stubFs.restore();
stubConsoleError.restore();
stubCdsEnv.restore();
stubPathJoin.restore();
stubI18n.restore();
sinon.restore();
});
it('should handle attachment test data', async function () {
db = {
run: sinon.stub()
};
sinon.stub(cds, 'context').value({
locale: 'en'
});
stubSELECTOne = sinon.stub(SELECT.one, 'from').returns({
where: () => {
return {
ID: 'Mocked Poetry Slam'
};
}
});
attachments = new Attachments(req, db, poetrySlamIds);
await attachments.handleAttachmentTestData();
sinon.assert.calledOnce(stubBuffer);
sinon.assert.calledOnce(stubSELECTOne);
sinon.assert.calledOnce(stubFs);
sinon.assert.calledOnce(stubPathJoin);
});
it('should throw error caused by database when handling attachment test data', async function () {
stubRun = sinon.stub().throws(new Error('Mocked DB error'));
db = {
run: stubRun
};
stubSELECTOne = sinon.stub(SELECT.one, 'from').returns({
where: () => {
return {
ID: 'Mocked Poetry Slam'
};
}
});
attachments = new Attachments(req, db, poetrySlamIds);
await expect(attachments.handleAttachmentTestData()).to.rejectedWith(
'Mocked request error'
);
sinon.assert.calledOnce(stubConsoleError);
sinon.assert.calledOnce(req.error);
sinon.assert.calledOnce(stubRun);
});
it('should throw error caused by missing Poetry Slam when handling attachment test data', async function () {
db = {
run: sinon.stub()
};
stubSELECTOne = sinon.stub(SELECT.one, 'from').returns({
where: () => null
});
attachments = new Attachments(req, db, poetrySlamIds);
await expect(attachments.handleAttachmentTestData()).to.rejectedWith(
'Mocked request error'
);
sinon.assert.calledTwice(stubConsoleError);
sinon.assert.calledTwice(req.error);
});
});
});