Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
[chrome/csrf] polish up some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Nov 10, 2015
1 parent 5cdeae5 commit b4517cb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
45 changes: 29 additions & 16 deletions src/ui/public/chrome/api/__tests__/xsrf.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ngMock from 'ngMock';

import xsrfChromeApi from '../xsrf';

const xsrfHeader = 'kbn-xsrf-header';
const xsrfHeader = 'kbn-xsrf-token';
const xsrfToken = 'xsrfToken';

describe('chrome xsrf apis', function () {
Expand Down Expand Up @@ -39,13 +39,8 @@ describe('chrome xsrf apis', function () {

it('can be canceled by setting the kbnXsrfToken option', function () {
const setHeader = stub();
prefilter({}, {}, { setRequestHeader: setHeader });

expect(setHeader.callCount).to.be(1);
expect(setHeader.args[0]).to.eql([
xsrfHeader,
xsrfToken
]);
prefilter({ kbnXsrfToken: false }, {}, { setRequestHeader: setHeader });
expect(setHeader.callCount).to.be(0);
});
});

Expand All @@ -58,7 +53,7 @@ describe('chrome xsrf apis', function () {
stub($, 'ajaxPrefilter');
const chrome = {};
xsrfChromeApi(chrome, { xsrfToken });
ngMock.module(chrome.$setupCsrfRequestInterceptor);
ngMock.module(chrome.$setupXsrfRequestInterceptor);
});

beforeEach(ngMock.inject(function ($injector) {
Expand All @@ -84,24 +79,42 @@ describe('chrome xsrf apis', function () {
$httpBackend.flush();
});

it('skips requests with the kbnCsrfToken set falsey', function () {
it('skips requests with the kbnXsrfToken set falsey', function () {
$httpBackend.expectPOST('/api/test', undefined, function (headers) {
return !(xsrfHeader in headers);
}).respond(200, '');

$http.post({
$http({
method: 'POST',
url: '/api/test',
kbnXsrfToken: 0
});

$http({
method: 'POST',
url: '/api/test',
xsrfHeader: 0
kbnXsrfToken: ''
});

$http.post({
$http({
method: 'POST',
url: '/api/test',
xsrfHeader: ''
kbnXsrfToken: false
});

$http.post({
$httpBackend.flush();
});

it('accepts alternate tokens to use', function () {
const customToken = `custom:${xsrfToken}`;
$httpBackend.expectPOST('/api/test', undefined, function (headers) {
return headers[xsrfHeader] === customToken;
}).respond(200, '');

$http({
method: 'POST',
url: '/api/test',
xsrfHeader: false
kbnXsrfToken: customToken
});

$httpBackend.flush();
Expand Down
2 changes: 1 addition & 1 deletion src/ui/public/chrome/api/angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = function (chrome, internals) {
a.href = '/elasticsearch';
return a.href;
}()))
.config(chrome.$setupCsrfRequestInterceptor)
.config(chrome.$setupXsrfRequestInterceptor)
.directive('kbnChrome', function ($rootScope) {
return {
template: function ($el) {
Expand Down
15 changes: 8 additions & 7 deletions src/ui/public/chrome/api/xsrf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ export default function (chrome, internals) {
return internals.xsrfToken;
};

$.ajaxPrefilter(function ({ kbnCsrfToken = internals.xsrfToken }, originalOptions, jqXHR) {
if (kbnCsrfToken) {
jqXHR.setRequestHeader('kbn-xsrf-token', kbnCsrfToken);
$.ajaxPrefilter(function ({ kbnXsrfToken = internals.xsrfToken }, originalOptions, jqXHR) {
if (kbnXsrfToken) {
jqXHR.setRequestHeader('kbn-xsrf-token', kbnXsrfToken);
}
});

chrome.$setupCsrfRequestInterceptor = function ($httpProvider) {
chrome.$setupXsrfRequestInterceptor = function ($httpProvider) {
$httpProvider.interceptors.push(function () {
return {
request: function (opts) {
const { kbnCsrfToken = internals.xsrfToken } = opts;
if (kbnCsrfToken) {
return set(opts, ['headers', 'kbn-xsrf-token'], kbnCsrfToken);
const { kbnXsrfToken = internals.xsrfToken } = opts;
if (kbnXsrfToken) {
set(opts, ['headers', 'kbn-xsrf-token'], kbnXsrfToken);
}
return opts;
}
};
});
Expand Down

0 comments on commit b4517cb

Please sign in to comment.