Skip to content

Commit

Permalink
Unit test coverage for adding the region param to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
DingoEatingFuzz committed Aug 10, 2018
1 parent dbc18c9 commit ae464a0
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
62 changes: 62 additions & 0 deletions ui/tests/unit/adapters/job-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
{
Expand Down
59 changes: 59 additions & 0 deletions ui/tests/unit/services/token-test.js
Original file line number Diff line number Diff line change
@@ -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'
);
});

0 comments on commit ae464a0

Please sign in to comment.