github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/acceptance/proxy-test.js (about) 1 /* eslint-disable ember-a11y-testing/a11y-audit-called */ // Tests for non-UI behaviour. 2 import { module, test } from 'qunit'; 3 import { setupApplicationTest } from 'ember-qunit'; 4 import { setupMirage } from 'ember-cli-mirage/test-support'; 5 import Jobs from 'nomad-ui/tests/pages/jobs/list'; 6 7 let managementToken; 8 9 module('Acceptance | reverse proxy', function (hooks) { 10 setupApplicationTest(hooks); 11 setupMirage(hooks); 12 13 hooks.beforeEach(function () { 14 window.localStorage.clear(); 15 window.sessionStorage.clear(); 16 17 server.create('agent'); 18 managementToken = server.create('token'); 19 20 // Prepare a setRequestHeader that accumulate headers already set. This is to avoid double setting X-Nomad-Token 21 this._originalXMLHttpRequestSetRequestHeader = 22 XMLHttpRequest.prototype.setRequestHeader; 23 (function (setRequestHeader) { 24 XMLHttpRequest.prototype.setRequestHeader = function (header, value) { 25 if (!this.headers) { 26 this.headers = {}; 27 } 28 if (!this.headers[header]) { 29 this.headers[header] = []; 30 } 31 // Add the value to the header 32 this.headers[header].push(value); 33 setRequestHeader.call(this, header, value); 34 }; 35 })(this._originalXMLHttpRequestSetRequestHeader); 36 37 // Simulate a reverse proxy injecting X-Nomad-Token header for all requests 38 this._originalXMLHttpRequestSend = XMLHttpRequest.prototype.send; 39 (function (send) { 40 XMLHttpRequest.prototype.send = function (data) { 41 if (!this.headers || !('X-Nomad-Token' in this.headers)) { 42 this.setRequestHeader('X-Nomad-Token', managementToken.secretId); 43 } 44 send.call(this, data); 45 }; 46 })(this._originalXMLHttpRequestSend); 47 }); 48 49 hooks.afterEach(function () { 50 XMLHttpRequest.prototype.setRequestHeader = 51 this._originalXMLHttpRequestSetRequestHeader; 52 XMLHttpRequest.prototype.send = this._originalXMLHttpRequestSend; 53 }); 54 55 test('when token is inserted by a reverse proxy, the UI is adjusted', async function (assert) { 56 // when token is inserted by reserve proxy, the token is reverse proxy 57 const { secretId } = managementToken; 58 59 await Jobs.visit(); 60 assert.equal( 61 window.localStorage.nomadTokenSecret, 62 secretId, 63 'Token secret was set' 64 ); 65 66 // Make sure that server received the header 67 assert.ok( 68 server.pretender.handledRequests 69 .mapBy('requestHeaders') 70 .every((headers) => headers['X-Nomad-Token'] === secretId), 71 'The token header is always present' 72 ); 73 74 assert.notOk(Jobs.runJobButton.isDisabled, 'Run job button is enabled'); 75 }); 76 });