-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
test-support-test.js
421 lines (354 loc) · 13.5 KB
/
test-support-test.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
const expect = require('chai').expect;
const TestProject = require('ember-addon-tests').default;
const fs = require('fs-extra');
const denodeify = require('denodeify');
const request = denodeify(require('request'));
const {
CSP_META_TAG_REG_EXP,
removeConfig,
setConfig,
setResolutionForDependency,
readPackageJson,
removeResolutionsForDependencies,
} = require('../utils');
const path = require('path');
const semverGtr = require('semver/ranges/gtr');
// Depending on Ember CLI version used some manual adjustments are needed even
// for newly created projects to not violate the default CSP.
async function adjustForCompatibility(testProject) {
// Ember Auto Import is a default dependency of new Ember projects. It uses an `eval` function
// internally unless configured to not do so. This violates the default CSP and causes an
// `EvalError` to be thrown. This uncatched error will cause the tests to fail - regardless
// of our custom test support.
// To avoid this issue we uninstall Ember Auto Import for these tests. This can be removed
// as soon as Ember CLI Content Security Policy works out of the box with Ember Auto Import.
try {
await testProject.runCommand('yarn', 'remove', 'ember-auto-import');
} catch (error) {
// Trying to remove ember-auto-import dependency may fail cause that dependency is not
// present for older Ember CLI versions.
}
// Older Ember CLI versions install a QUnit version, which violates the
// default CSP. We ask consumer to upgrade QUnit to a more recent QUnit
// version in a warning that is logged if QUnit version is less than
// 2.9.2. The issue was fixed in Ember CLI 3.10 by upgrading ember-qunit
// to ^4.4.1.
// We need to upgrade QUnit in tests as well if an Ember CLI version less
// than 3.10 is used.
const packageJson = await readPackageJson(testProject);
const emberCliVersionUsed = packageJson.devDependencies['ember-cli'];
if (semverGtr('3.10.0', emberCliVersionUsed)) {
await setResolutionForDependency(testProject, { qunit: '>= 2.9.2' });
await testProject.runCommand('yarn', 'install');
}
}
describe('e2e: provides test support', function () {
this.timeout(300000);
let testProject;
before(async function () {
testProject = new TestProject({
projectRoot: path.join(__dirname, '../..'),
});
await testProject.createEmberApp();
await testProject.addOwnPackageAsDevDependency(
'ember-cli-content-security-policy'
);
await adjustForCompatibility(testProject);
});
after(async function () {
await removeResolutionsForDependencies(testProject);
});
describe('does not cause test failures on new project', async function () {
it('tests are passing for untouched application', async function () {
await testProject.runEmberCommand('test');
// No need to assert anything. Test scenario is fulfilled as long as the
// command does not throw.
});
it('tests are passing for untouched addon', async function () {
// Global test project is an application. To assert against an addon
// we need to create another test project.
const testProject = new TestProject({
projectRoot: path.join(__dirname, '../..'),
});
await testProject.createEmberAddon();
await testProject.addOwnPackageAsDevDependency(
'ember-cli-content-security-policy'
);
await adjustForCompatibility(testProject);
await testProject.runEmberCommand('test');
// No need to assert anything. Test scenario is fulfilled as long as the
// command does not throw.
});
});
describe('causes tests to fail on CSP violations', async function () {
const folderForIntegrationTests = 'tests/integration/components';
const fileViolatingCSP = `${folderForIntegrationTests}/my-component-test.js`;
beforeEach(async function () {
// create a simple rendering tests that violates default CSP by using
// inline JavaScript
await fs.ensureDir(
path.join(testProject.path, folderForIntegrationTests)
);
await testProject.writeFile(
fileViolatingCSP,
`
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
module('Integration | Component | my-component', function(hooks) {
setupRenderingTest(hooks);
test('it renders', async function(assert) {
await render(hbs\`<div style='display: none;'></div>\`);
assert.ok(true);
});
});
`
);
});
afterEach(async function () {
await removeConfig(testProject);
});
it('causes tests to fail on CSP violations', async function () {
// runEmberCommand throws result object if command exists with non zero
// exit code
try {
await testProject.runEmberCommand('test');
// expect runEmberCommand to throw
expect(false).to.be.true;
} catch ({ exitCode }) {
expect(exitCode).to.equal(1);
}
});
it('does not cause tests failures if addon is disabled', async function () {
await setConfig(testProject, {
enabled: false,
});
let { exitCode } = await testProject.runEmberCommand('test');
expect(exitCode).to.equal(0);
});
it('does not cause tests failures if `failTests` config option is `false`', async function () {
await setConfig(testProject, {
failTests: false,
});
let { exitCode } = await testProject.runEmberCommand('test');
expect(exitCode).to.equal(0);
});
// One common scenario is when running the server in production mode locally, i.e.
// you are doing the production build on your local machine, connecting to your actual production server,
// for example via `ember serve -prod`. In these cases we don't want this addon to break.
it('does not break development server for builds not including tests', async function () {
// TODO: not supported yet
await testProject.startEmberServer({
environment: 'prodocution',
port: '49741',
});
let response = await request({
url: 'http://localhost:49741/',
headers: {
Accept: 'text/html',
},
});
let responseForTests = await request({
url: 'http://localhost:49741/tests',
headers: {
Accept: 'text/html',
},
});
expect(response.statusCode).to.equal(200);
expect(responseForTests.statusCode).to.equal(200);
await testProject.stopEmberServer();
});
});
describe('ensures consistent results regardless how tests are executed', async function () {
afterEach(async function () {
await removeConfig(testProject);
});
it('ensures CSP is applied in tests regradless if executed with development server or not', async function () {
await setConfig(testProject, {
delivery: ['header'],
});
await testProject.runEmberCommand('build');
let testsIndexHtml = await testProject.readFile(
'dist/tests/index.html',
'utf8'
);
let indexHtml = await testProject.readFile('dist/index.html', 'utf8');
expect(testsIndexHtml).to.match(CSP_META_TAG_REG_EXP);
expect(indexHtml).to.not.match(CSP_META_TAG_REG_EXP);
});
});
describe('adds nonce to script-src if needed', function () {
afterEach(async function () {
await removeConfig(testProject);
await testProject.stopEmberServer();
});
it('adds nonce to script-src when required by tests', async function () {
await setConfig(testProject, {
delivery: ['meta'],
});
await testProject.startEmberServer({
port: '49741',
});
let response = await request({
url: 'http://localhost:49741/tests/',
headers: {
Accept: 'text/html',
},
});
let cspInHeader = response.headers['content-security-policy-report-only'];
let cspInMetaElement = response.body.match(CSP_META_TAG_REG_EXP)[1];
[cspInHeader, cspInMetaElement].forEach((csp) => {
expect(csp).to.match(/script-src [^;]* 'nonce-/);
});
});
it("does not add nonce to script-src if directive contains 'unsafe-inline'", async function () {
await setConfig(testProject, {
delivery: ['meta'],
policy: {
'script-src': ["'self'", "'unsafe-inline'"],
},
});
await testProject.startEmberServer({
port: '49741',
});
let response = await request({
url: 'http://localhost:49741/tests/',
headers: {
Accept: 'text/html',
},
});
let cspInHeader = response.headers['content-security-policy-report-only'];
let cspInMetaElement = response.body.match(CSP_META_TAG_REG_EXP)[1];
[cspInHeader, cspInMetaElement].forEach((csp) => {
expect(csp).to.not.include('nonce-');
});
});
});
describe('it uses CSP configuration for test environment if running tests', function () {
before(async function () {
// setConfig utility does not support configuration depending on environment
// need to write the file manually
let configuration = `
module.exports = function(environment) {
return {
delivery: ['header', 'meta'],
policy: {
'default-src': environment === 'test' ? ["'none'"] : ["'self'"]
},
reportOnly: false
};
};
`;
await testProject.writeFile(
'config/content-security-policy.js',
configuration
);
await testProject.startEmberServer({
port: '49741',
});
});
after(async function () {
await testProject.stopEmberServer();
await removeConfig(testProject);
});
it('uses CSP configuration for test environment for meta tag in tests/index.html', async function () {
let testsIndexHtml = await testProject.readFile(
'dist/tests/index.html',
'utf8'
);
let indexHtml = await testProject.readFile('dist/index.html', 'utf8');
let [, cspInTestsIndexHtml] = testsIndexHtml.match(CSP_META_TAG_REG_EXP);
let [, cspInIndexHtml] = indexHtml.match(CSP_META_TAG_REG_EXP);
expect(cspInTestsIndexHtml).to.include("default-src 'none';");
expect(cspInIndexHtml).to.include("default-src 'self';");
});
it('uses CSP configuration for test environment for CSP header serving tests/', async function () {
let responseForTests = await request({
url: 'http://localhost:49741/tests',
headers: {
Accept: 'text/html',
},
});
let responseForApp = await request({
url: 'http://localhost:49741',
headers: {
Accept: 'text/html',
},
});
let cspForTests = responseForTests.headers['content-security-policy'];
let cspForApp = responseForApp.headers['content-security-policy'];
expect(cspForTests).to.include("default-src 'none';");
expect(cspForApp).to.include("default-src 'self';");
});
});
describe('includes frame-src required by testem', function () {
before(async function () {
await setConfig(testProject, {
delivery: ['header', 'meta'],
reportOnly: false,
});
await testProject.startEmberServer({
port: '49741',
});
});
after(async function () {
await testProject.stopEmberServer();
await removeConfig(testProject);
});
it('includes frame-src required by testem in CSP delivered by meta tag', async function () {
let testsIndexHtml = await testProject.readFile(
'dist/tests/index.html',
'utf8'
);
let [, cspInTestsIndexHtml] = testsIndexHtml.match(CSP_META_TAG_REG_EXP);
expect(cspInTestsIndexHtml).to.include("frame-src 'self';");
});
it('includes frame-src required by testem in CSP delivered by HTTP header', async function () {
let responseForTests = await request({
url: 'http://localhost:49741/tests',
headers: {
Accept: 'text/html',
},
});
let cspForTests = responseForTests.headers['content-security-policy'];
expect(cspForTests).to.include("frame-src 'self';");
});
});
describe('it appends to existing frame-src', function () {
before(async function () {
await setConfig(testProject, {
delivery: ['header', 'meta'],
reportOnly: false,
policy: {
'frame-src': ['data:'],
},
});
await testProject.startEmberServer({
port: '49741',
});
});
after(async function () {
await testProject.stopEmberServer();
await removeConfig(testProject);
});
it('it appends to existing frame-src in CSP delivered by meta tag', async function () {
let testsIndexHtml = await testProject.readFile(
'dist/tests/index.html',
'utf8'
);
let [, cspInTestsIndexHtml] = testsIndexHtml.match(CSP_META_TAG_REG_EXP);
expect(cspInTestsIndexHtml).to.include("frame-src data: 'self';");
});
it('it appends to existing frame-src in CSP delivered by HTTP header', async function () {
let responseForTests = await request({
url: 'http://localhost:49741/tests',
headers: {
Accept: 'text/html',
},
});
let cspForTests = responseForTests.headers['content-security-policy'];
expect(cspForTests).to.include("frame-src data: 'self';");
});
});
});