-
Notifications
You must be signed in to change notification settings - Fork 27.4k
chore(ngCsp): add e2e tests #9136
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,125 @@ | |
... | ||
</html> | ||
``` | ||
*/ | ||
* @example | ||
// Note: the suffix `.csp` in the example name triggers | ||
// csp mode in our http server! | ||
<example name="example.csp" module="cspExample" ng-csp="true"> | ||
<file name="index.html"> | ||
<div ng-controller="MainController as ctrl"> | ||
<div> | ||
<button ng-click="ctrl.inc()" id="inc">Increment</button> | ||
<span id="counter"> | ||
{{ctrl.counter}} | ||
</span> | ||
</div> | ||
|
||
<div> | ||
<button ng-click="ctrl.evil()" id="evil">Evil</button> | ||
<span id="evilError"> | ||
{{ctrl.evilError}} | ||
</span> | ||
</div> | ||
</div> | ||
</file> | ||
<file name="script.js"> | ||
angular.module('cspExample', []) | ||
.controller('MainController', function() { | ||
this.counter = 0; | ||
this.inc = function() { | ||
this.counter++; | ||
}; | ||
this.evil = function() { | ||
// jshint evil:true | ||
try { | ||
eval('1+2'); | ||
} catch (e) { | ||
this.evilError = e.message; | ||
} | ||
}; | ||
}); | ||
</file> | ||
<file name="protractor.js" type="protractor"> | ||
var util, webdriver; | ||
|
||
var incBtn = element(by.id('inc')); | ||
var counter = element(by.id('counter')); | ||
var evilBtn = element(by.id('evil')); | ||
var evilError = element(by.id('evilError')); | ||
|
||
function getAndClearSevereErrors() { | ||
return browser.manage().logs().get('browser').then(function(browserLog) { | ||
return browserLog.filter(function(logEntry) { | ||
return logEntry.level.value > webdriver.logging.Level.WARNING.value; | ||
}); | ||
}); | ||
} | ||
|
||
function clearErrors() { | ||
getAndClearSevereErrors(); | ||
} | ||
|
||
function expectNoErrors() { | ||
getAndClearSevereErrors().then(function(filteredLog) { | ||
expect(filteredLog.length).toEqual(0); | ||
if (filteredLog.length) { | ||
console.log('browser console errors: ' + util.inspect(filteredLog)); | ||
} | ||
}); | ||
} | ||
|
||
function expectError(regex) { | ||
getAndClearSevereErrors().then(function(filteredLog) { | ||
var found = false; | ||
filteredLog.forEach(function(log) { | ||
if (log.message.match(regex)) { | ||
found = true; | ||
} | ||
}); | ||
if (!found) { | ||
throw new Error('expected an error that matches ' + regex); | ||
} | ||
}); | ||
} | ||
|
||
beforeEach(function() { | ||
util = require('util'); | ||
webdriver = require('protractor/node_modules/selenium-webdriver'); | ||
}); | ||
|
||
// For now, we only test on Chrome, | ||
// as Safari does not load the page with Protractor's injected scripts, | ||
// and Firefox webdriver always disables content security policy (#6358) | ||
if (browser.params.browser !== 'chrome') { | ||
return; | ||
} | ||
|
||
it('should not report errors when the page is loaded', function() { | ||
// clear errors so we are not dependent on previous tests | ||
clearErrors(); | ||
// Need to reload the page as the page is already loaded when | ||
// we come here | ||
browser.driver.getCurrentUrl().then(function(url) { | ||
browser.get(url); | ||
}); | ||
expectNoErrors(); | ||
}); | ||
|
||
it('should evaluate expressions', function() { | ||
expect(counter.getText()).toEqual('0'); | ||
incBtn.click(); | ||
expect(counter.getText()).toEqual('1'); | ||
expectNoErrors(); | ||
}); | ||
|
||
it('should throw and report an error when using "eval"', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do all the browsers we support respect CSP headers? I'm just not sure about safari 6 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Safari 6 does with a prefix: http://caniuse.com/#feat=contentsecuritypolicy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like IE9 does not, which is a problem for travis. IE10/11 also use prefixed headers :( Just skip the test if ie is detected? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we need to do feature detection for IE9, but should keep testing IE10/11. |
||
evilBtn.click(); | ||
expect(evilError.getText()).toMatch(/Content Security Policy/); | ||
expectError(/Content Security Policy/); | ||
}); | ||
</file> | ||
</example> | ||
*/ | ||
|
||
// ngCsp is not implemented as a proper directive any more, because we need it be processed while we | ||
// bootstrap the system (before $parse is instantiated), for this reason we just have | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how I feel about this; if I understand correctly, this clears all errors, but returns only the severe ones?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Ignores warnings about css problems, ...
Right now protractor just ignores the errors...