-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
236 lines (196 loc) · 7.61 KB
/
index.js
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
'use strict';
const hippie = require('hippie');
const expect = require('chai').expect;
const {URL} = require('url');
const sanitizeHtml = require('sanitize-html');
const tokenService = require('./token-service');
describe('Specification conformance', function () {
// Process incoming arguments
const baseUrl = process.env.npm_config_endpoint ||
process.env.WDAY_CC_TEST_ENDPOINT ||
'http://localhost:8080/api/contents?filter=' +
encodeURIComponent(JSON.stringify({"limit": 1000, "skip": 0}));
let token = process.env.npm_config_token ||
process.env.WDAY_CC_TEST_TOKEN ||
'';
if (!token || token.length < 1) {
// If no token, let's generate a default token
tokenService(function(newToken) {
token = newToken;
});
}
let client;
beforeEach(function (done) {
hippie.assert.showDiff = true;
client = hippie()
.time(true)
.header("Content-Type", "application/vnd.workday.contentcloud.v1+json")
.json()
.get(baseUrl);
done();
});
describe('Basic connectivity and security test cases', function () {
it('MUST not timeout after 30secs', function (done) {
client.timeout(30000)
.end(done);
});
it('SHOULD reject missing token', function (done) {
client.expectStatus(401)
.end(done)
});
});
describe('Authentication test cases', function () {
it('SHOULD accept a JWT via Authorization Header', function (done) {
client.expectStatus(200)
.header('Authorization', 'Bearer ' + token)
.end(done);
});
it('MUST not accept a bad JWT via Authorization Header', function (done) {
client.header('Authorization', 'Bearer 123.123.123')
.expectStatus(401)
.end(done);
});
it('MUST not accept a malformed Authorization Header', function (done) {
client.header('Authorization', 'Bearer ')
.expectStatus(401)
.end(done);
});
});
describe('Content type validation test cases', function () {
it('MUST return a specific Content-Type response headers', function (done) {
client.header('Authorization', 'Bearer ' + token)
.expectStatus(200)
.expectHeader('Content-Type', 'application/vnd.workday.contentcloud.v1+json; charset=utf-8')
.end(done);
});
});
describe('Response body data validation test cases', function () {
let contents = [];
it('MUST return the entire contents', function (done) {
this.timeout(60000); // 60 seconds timeout
function next(url) {
client.get(url)
.header('Authorization', 'Bearer ' + token)
.expectStatus(200)
.end(function (err, res, body) {
if (err) throw err;
expect(body).to.be.an('array');
// We'll append all pages together
for (let i = 0; i < body.length; i++) {
contents.push(body[i]);
}
// Check for link header (used for pagination)
if (res.headers.link) {
next(res.headers.link);
} else {
done();
}
});
}
next(baseUrl);
});
it('MUST return all required JSON elements and not be empty', function (done) {
for (let i = 0; i < contents.length; i++) {
const content = contents[i];
expect(content).to.have.property('id');
if (content.id.length) {
expect(content.id.length).to.be.at.least(1);
}
expect(content).to.have.property('title');
expect(content.title.length).to.be.at.least(1);
expect(content).to.have.property('description');
expect(content.description.length).to.be.at.least(1);
expect(content).to.have.property('contentType');
expect(content.contentType.length).to.be.at.least(1);
expect(content).to.have.property('playbackType');
expect(content.playbackType.length).to.be.at.least(1);
expect(content).to.have.property('webPlaybackUrl');
expect(content.webPlaybackUrl.length).to.be.at.least(1);
}
done();
});
it('MUST have not too long strings', function (done) {
for (let i = 0; i < contents.length; i++) {
const content = contents[i];
expect(content).to.have.property('id');
if (content.id.length) {
expect(content.id.length).to.be.at.most(128);
}
expect(content).to.have.property('title');
expect(content.title.length).to.be.at.most(200);
expect(content).to.have.property('description');
expect(content.description.length).to.be.at.most(5000);
expect(content).to.have.property('contentType');
expect(content.contentType.length).to.be.at.most(6);
expect(content).to.have.property('playbackType');
expect(content.playbackType.length).to.be.at.most(8);
expect(content).to.have.property('webPlaybackUrl');
expect(content.webPlaybackUrl.length).to.be.at.most(2048);
if (content.mobilePlaybackUrl.length) {
expect(content.mobilePlaybackUrl.length).to.be.at.most(2048);
}
if (content.channelTitle) {
expect(content.channelTitle.length).to.be.at.most(200);
}
}
done();
});
it('MUST have correct enumerations', function (done) {
for (let i = 0; i < contents.content; i++) {
const content = contents[i];
expect(content.contentType).to.be.oneOf(['video', 'audio', 'course']);
expect(content.playbackType).to.be.oneOf(['workday', 'iframe', 'external']);
}
done();
});
it('MUST have properly formats URLs', function (done) {
for (let i = 0; i < contents.length; i++) {
const content = contents[i];
expect(content.webPlaybackUrl).to.satisfy(function (parseUrl) {
// Try parsing, if it fails it will throw an error failing this test case
const urlObject = new URL(parseUrl);
return urlObject.protocol === "https:";
}, content.webPlaybackUrl);
if (content.mobilePlaybackUrl && content.mobilePlaybackUrl.length > 0) {
expect(content.mobilePlaybackUrl).to.satisfy(function (parseUrl) {
// Try parsing, if it fails it will throw an error failing this test case
new URL(parseUrl);
return true;
});
}
if (content.thumbnails) {
content.thumbnails.every(thumbnail => {
if (thumbnail.url && thumbnail.url.length > 0) {
expect(thumbnail.url).to.satisfy(function (parseUrl) {
// Try parsing, if it fails it will throw an error failing this test case
const urlObject = new URL(parseUrl);
return urlObject.protocol === "https:";
});
}
});
}
}
done();
});
it('SHOULD have NOT have disallowed HTML tags', function (done) {
// setup rules for HTML filtering
const santizeOptions = {
allowedTags: [ 'h1', 'h2', 'h3', 'p', 'strong', 'i', 'u', 'span', 'ul', 'li' ],
};
for (let i = 0; i < contents.length; i++) {
const content = contents[i];
// test the description
const cleanDescription = sanitizeHtml(content.description, santizeOptions);
// if the string are different lengths, it means we've done some cleaning.
try {
expect(cleanDescription).to.equal(content.description);
} catch (e) {
console.warn('Warning: we detected disallowed HTML in the `description` for content item id: ', content.id);
console.warn('Original: ', content.description);
console.warn('Sanitized: ', cleanDescription);
}
}
done();
});
});
});