Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests the verify correct dataType in jQuery.ajax request when loading Console app state. #13092

Merged
merged 1 commit into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/core_plugins/console/public/src/__tests__/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import sinon from 'sinon';
import $ from 'jquery';

import history from '../history';
import mappings from '../mappings';
import init from '../app';

describe('app initialization', () => {
const sandbox = sinon.sandbox.create();

let inputMock, outputMock;
let ajaxDoneStub;
beforeEach(() => {
ajaxDoneStub = sinon.stub();
sandbox.stub($, 'ajax').returns({ done: ajaxDoneStub });
sandbox.stub(history, 'getSavedEditorState');
sandbox.stub(mappings, 'startRetrievingAutoCompleteInfo');

inputMock = {
update: sinon.stub(),
moveToNextRequestEdge: sinon.stub(),
highlightCurrentRequestsAndUpdateActionBar: sinon.stub(),
updateActionsBar: sinon.stub(),
getSession: sinon.stub().returns({ on() {} })
};

outputMock = {
update: sinon.stub()
};
});

afterEach(() => {
sandbox.restore();
});

it('correctly loads state from any external HTTPS links.', () => {
const mockContent = {};
ajaxDoneStub.yields(mockContent);

init(inputMock, outputMock, 'https://state.link.com/content');

sinon.assert.calledOnce($.ajax);
sinon.assert.calledWithExactly($.ajax, {
url: 'https://state.link.com/content',
dataType: 'text',
kbnXsrfToken: false
});

sinon.assert.calledTwice(inputMock.moveToNextRequestEdge);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: it's a bit weird that it's called twice (not sure if it's a bug or not), but here is how it works :)

sinon.assert.calledWithExactly(inputMock.moveToNextRequestEdge, true);
sinon.assert.calledOnce(inputMock.highlightCurrentRequestsAndUpdateActionBar);
sinon.assert.calledOnce(inputMock.updateActionsBar);
sinon.assert.calledOnce(inputMock.update);
sinon.assert.calledWithExactly(inputMock.update, sinon.match.same(mockContent));

sinon.assert.calledOnce(outputMock.update);
sinon.assert.calledWithExactly(outputMock.update, '');
});

it('correctly loads state from GitHub API HTTPS links.', () => {
const mockContent = {};
ajaxDoneStub.yields(mockContent);

init(inputMock, outputMock, 'https://api.github.com/content');

sinon.assert.calledOnce($.ajax);
sinon.assert.calledWithExactly($.ajax, {
url: 'https://api.github.com/content',
dataType: 'text',
kbnXsrfToken: false,
headers: { Accept: "application/vnd.github.v3.raw" }
});

sinon.assert.calledTwice(inputMock.moveToNextRequestEdge);
sinon.assert.calledWithExactly(inputMock.moveToNextRequestEdge, true);
sinon.assert.calledOnce(inputMock.highlightCurrentRequestsAndUpdateActionBar);
sinon.assert.calledOnce(inputMock.updateActionsBar);
sinon.assert.calledOnce(inputMock.update);
sinon.assert.calledWithExactly(inputMock.update, sinon.match.same(mockContent));

sinon.assert.calledOnce(outputMock.update);
sinon.assert.calledWithExactly(outputMock.update, '');
});
});
14 changes: 11 additions & 3 deletions src/core_plugins/console/public/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ export default function init(input, output, sourceLocation = 'stored') {
}
}
else if (/^https?:\/\//.test(sourceLocation)) {
var loadFrom = { url: sourceLocation, dataType: "text", kbnXsrfToken: false };
const loadFrom = {
url: sourceLocation,
// Having dataType here is required as it doesn't allow jQuery to `eval` content
// coming from the external source thereby preventing XSS attack.
dataType: 'text',
kbnXsrfToken: false
};

if (/https?:\/\/api.github.com/.test(sourceLocation)) {
loadFrom.headers = { Accept: "application/vnd.github.v3.raw" };
loadFrom.headers = { Accept: 'application/vnd.github.v3.raw' };
}
$.ajax(loadFrom).done(function (data) {

$.ajax(loadFrom).done((data) => {
resetToValues(data);
input.moveToNextRequestEdge(true);
input.highlightCurrentRequestsAndUpdateActionBar();
Expand Down