From ae464a01dcf047d4a05a8e01d1125ed5abb6ecd8 Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Thu, 9 Aug 2018 20:27:06 -0700 Subject: [PATCH] Unit test coverage for adding the region param to requests --- ui/tests/unit/adapters/job-test.js | 62 ++++++++++++++++++++++++++++ ui/tests/unit/services/token-test.js | 59 ++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 ui/tests/unit/services/token-test.js diff --git a/ui/tests/unit/adapters/job-test.js b/ui/tests/unit/adapters/job-test.js index 9b091c33e1a..0a1fb9f657d 100644 --- a/ui/tests/unit/adapters/job-test.js +++ b/ui/tests/unit/adapters/job-test.js @@ -40,6 +40,9 @@ moduleForAdapter('job', 'Unit | Adapter | Job', { this.server.create('job', { id: 'job-1', namespaceId: 'default' }); this.server.create('job', { id: 'job-2', namespaceId: 'some-namespace' }); + this.server.create('region', { id: 'region-1' }); + this.server.create('region', { id: 'region-2' }); + this.system = getOwner(this).lookup('service:system'); // Namespace, default region, and all regions are requests that all @@ -391,6 +394,65 @@ test('canceling a find record request will never cancel a request with the same }); }); +test('when there is no region set, requests are made without the region query param', function(assert) { + const { pretender } = this.server; + const jobName = 'job-1'; + const jobNamespace = 'default'; + const jobId = JSON.stringify([jobName, jobNamespace]); + + return wait().then(() => { + this.subject().findRecord(null, { modelName: 'job' }, jobId); + this.subject().findAll(null, { modelName: 'job' }, null); + + assert.deepEqual( + pretender.handledRequests.mapBy('url'), + [`/v1/job/${jobName}`, '/v1/jobs'], + 'No requests include the region query param' + ); + }); +}); + +test('when there is a region set, requests are made with the region query param', function(assert) { + const region = 'region-2'; + window.localStorage.nomadActiveRegion = region; + + const { pretender } = this.server; + const jobName = 'job-1'; + const jobNamespace = 'default'; + const jobId = JSON.stringify([jobName, jobNamespace]); + + return wait().then(() => { + this.subject().findRecord(null, { modelName: 'job' }, jobId); + this.subject().findAll(null, { modelName: 'job' }, null); + + assert.deepEqual( + pretender.handledRequests.mapBy('url'), + [`/v1/job/${jobName}?region=${region}`, `/v1/jobs?region=${region}`], + 'Requests include the region query param' + ); + }); +}); + +test('when the region is set to the default region, requests are made without the region query param', function(assert) { + window.localStorage.nomadActiveRegion = 'region-1'; + + const { pretender } = this.server; + const jobName = 'job-1'; + const jobNamespace = 'default'; + const jobId = JSON.stringify([jobName, jobNamespace]); + + return wait().then(() => { + this.subject().findRecord(null, { modelName: 'job' }, jobId); + this.subject().findAll(null, { modelName: 'job' }, null); + + assert.deepEqual( + pretender.handledRequests.mapBy('url'), + [`/v1/job/${jobName}`, '/v1/jobs'], + 'No requests include the region query param' + ); + }); +}); + function makeMockModel(id, options) { return assign( { diff --git a/ui/tests/unit/services/token-test.js b/ui/tests/unit/services/token-test.js new file mode 100644 index 00000000000..ed9d6f2e97c --- /dev/null +++ b/ui/tests/unit/services/token-test.js @@ -0,0 +1,59 @@ +import { getOwner } from '@ember/application'; +import Service from '@ember/service'; +import { moduleFor, test } from 'ember-qunit'; +import Pretender from 'pretender'; + +moduleFor('service:token', 'Unit | Service | Token', { + beforeEach() { + const mockSystem = Service.extend({ + activeRegion: 'region-1', + shouldIncludeRegion: true, + }); + + this.register('service:system', mockSystem); + this.system = getOwner(this).lookup('service:system'); + + this.server = new Pretender(function() { + this.get('/path', () => [200, {}, null]); + }); + }, + afterEach() { + this.server.shutdown(); + }, + subject() { + return getOwner(this) + .factoryFor('service:token') + .create(); + }, +}); + +test('authorizedRequest includes the region param when the system service says to', function(assert) { + const token = this.subject(); + + token.authorizedRequest('/path'); + assert.equal( + this.server.handledRequests.pop().url, + `/path?region=${this.system.get('activeRegion')}`, + 'The region param is included when the system service shouldIncludeRegion property is true' + ); + + this.system.set('shouldIncludeRegion', false); + + token.authorizedRequest('/path'); + assert.equal( + this.server.handledRequests.pop().url, + '/path', + 'The region param is not included when the system service shouldIncludeRegion property is false' + ); +}); + +test('authorizedRawRequest bypasses adding the region param', function(assert) { + const token = this.subject(); + + token.authorizedRawRequest('/path'); + assert.equal( + this.server.handledRequests.pop().url, + '/path', + 'The region param is ommitted when making a raw request' + ); +});