2023-04-10 15:36:59 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2021-07-13 00:42:08 +00:00
|
|
|
/* eslint-disable ember-a11y-testing/a11y-audit-called */ // Tests for non-UI behaviour.
|
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupApplicationTest } from 'ember-qunit';
|
|
|
|
import { setupMirage } from 'ember-cli-mirage/test-support';
|
|
|
|
import Jobs from 'nomad-ui/tests/pages/jobs/list';
|
|
|
|
|
|
|
|
let managementToken;
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
module('Acceptance | reverse proxy', function (hooks) {
|
2021-07-13 00:42:08 +00:00
|
|
|
setupApplicationTest(hooks);
|
|
|
|
setupMirage(hooks);
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
hooks.beforeEach(function () {
|
2021-07-13 00:42:08 +00:00
|
|
|
window.localStorage.clear();
|
|
|
|
window.sessionStorage.clear();
|
|
|
|
|
|
|
|
server.create('agent');
|
|
|
|
managementToken = server.create('token');
|
|
|
|
|
2022-05-10 17:42:12 +00:00
|
|
|
// Prepare a setRequestHeader that accumulate headers already set. This is to avoid double setting X-Nomad-Token
|
|
|
|
this._originalXMLHttpRequestSetRequestHeader =
|
|
|
|
XMLHttpRequest.prototype.setRequestHeader;
|
|
|
|
(function (setRequestHeader) {
|
|
|
|
XMLHttpRequest.prototype.setRequestHeader = function (header, value) {
|
|
|
|
if (!this.headers) {
|
|
|
|
this.headers = {};
|
|
|
|
}
|
|
|
|
if (!this.headers[header]) {
|
|
|
|
this.headers[header] = [];
|
|
|
|
}
|
|
|
|
// Add the value to the header
|
|
|
|
this.headers[header].push(value);
|
|
|
|
setRequestHeader.call(this, header, value);
|
|
|
|
};
|
|
|
|
})(this._originalXMLHttpRequestSetRequestHeader);
|
|
|
|
|
2021-07-13 00:42:08 +00:00
|
|
|
// Simulate a reverse proxy injecting X-Nomad-Token header for all requests
|
|
|
|
this._originalXMLHttpRequestSend = XMLHttpRequest.prototype.send;
|
2021-12-28 14:45:20 +00:00
|
|
|
(function (send) {
|
|
|
|
XMLHttpRequest.prototype.send = function (data) {
|
2022-05-10 17:42:12 +00:00
|
|
|
if (!this.headers || !('X-Nomad-Token' in this.headers)) {
|
|
|
|
this.setRequestHeader('X-Nomad-Token', managementToken.secretId);
|
|
|
|
}
|
2021-07-13 00:42:08 +00:00
|
|
|
send.call(this, data);
|
|
|
|
};
|
|
|
|
})(this._originalXMLHttpRequestSend);
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
hooks.afterEach(function () {
|
2022-05-10 17:42:12 +00:00
|
|
|
XMLHttpRequest.prototype.setRequestHeader =
|
|
|
|
this._originalXMLHttpRequestSetRequestHeader;
|
2021-07-13 00:42:08 +00:00
|
|
|
XMLHttpRequest.prototype.send = this._originalXMLHttpRequestSend;
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('when token is inserted by a reverse proxy, the UI is adjusted', async function (assert) {
|
2021-07-13 00:42:08 +00:00
|
|
|
// when token is inserted by reserve proxy, the token is reverse proxy
|
|
|
|
const { secretId } = managementToken;
|
|
|
|
|
|
|
|
await Jobs.visit();
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.equal(
|
|
|
|
window.localStorage.nomadTokenSecret,
|
2022-05-10 17:42:12 +00:00
|
|
|
secretId,
|
|
|
|
'Token secret was set'
|
2021-12-28 16:08:12 +00:00
|
|
|
);
|
2021-07-13 00:42:08 +00:00
|
|
|
|
|
|
|
// Make sure that server received the header
|
|
|
|
assert.ok(
|
|
|
|
server.pretender.handledRequests
|
|
|
|
.mapBy('requestHeaders')
|
2021-12-28 14:45:20 +00:00
|
|
|
.every((headers) => headers['X-Nomad-Token'] === secretId),
|
2021-07-13 00:42:08 +00:00
|
|
|
'The token header is always present'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.notOk(Jobs.runJobButton.isDisabled, 'Run job button is enabled');
|
|
|
|
});
|
|
|
|
});
|