From 16a80311ef88bbdb83da2ebcf64c84dae722929c Mon Sep 17 00:00:00 2001 From: Bjarki Date: Thu, 20 May 2021 18:37:31 +0000 Subject: [PATCH] test: add e2e tests for Trusted Types Ensure that a simple Angular application is compatible with Trusted Types, even when lazy-loading is used. This test currently fails due to a Trusted Types violation originating in Webpack. --- .../e2e/tests/misc/trusted-types.ts | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/legacy-cli/e2e/tests/misc/trusted-types.ts diff --git a/tests/legacy-cli/e2e/tests/misc/trusted-types.ts b/tests/legacy-cli/e2e/tests/misc/trusted-types.ts new file mode 100644 index 0000000000..4efb8b9f70 --- /dev/null +++ b/tests/legacy-cli/e2e/tests/misc/trusted-types.ts @@ -0,0 +1,78 @@ +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import { appendToFile, prependToFile, replaceInFile, writeFile } from '../../utils/fs'; +import { ng } from '../../utils/process'; +import { updateJsonFile } from '../../utils/project'; + +export default async function () { + // Add app routing. + // This is done automatically on a new app with --routing. + await prependToFile('src/app/app.module.ts', `import { RouterModule } from '@angular/router';`); + await replaceInFile( + 'src/app/app.module.ts', + `imports: [`, + `imports: [ RouterModule.forRoot([]),`, + ); + await appendToFile('src/app/app.component.html', ''); + + // Add lazy route. + await ng('generate', 'module', 'lazy', '--route', 'lazy', '--module', 'app.module'); + + // Add lazy route e2e + await writeFile( + 'e2e/src/app.e2e-spec.ts', + ` + import { browser, logging, element, by } from 'protractor'; + + describe('workspace-project App', () => { + it('should display lazy route', async () => { + await browser.get(browser.baseUrl + '/lazy'); + expect(await element(by.css('app-lazy p')).getText()).toEqual('lazy works!'); + }); + + afterEach(async () => { + // Assert that there are no errors emitted from the browser + const logs = await browser.manage().logs().get(logging.Type.BROWSER); + expect(logs).not.toContain(jasmine.objectContaining({ + level: logging.Level.SEVERE, + })); + }); + }); + `, + ); + + const testCases = [ + { + aot: false, + csp: `trusted-types angular angular#unsafe-bypass angular#unsafe-jit angular#bundler; require-trusted-types-for 'script';`, + }, + { + aot: true, + csp: `trusted-types angular angular#unsafe-bypass angular#bundler; require-trusted-types-for 'script';`, + }, + ]; + + for (const { aot, csp } of testCases) { + await updateJsonFile('angular.json', (json) => { + const architect = json['projects']['test-project']['architect']; + architect['build']['options']['aot'] = aot; + if (!architect['serve']['options']) architect['serve']['options'] = {}; + architect['serve']['options']['headers'] = { + 'Content-Security-Policy': csp, + }; + }); + + try { + await ng('e2e'); + } catch (error) { + console.error(`Test case AOT ${aot} with CSP header ${csp} failed.`); + throw error; + } + } +}