open-nomad/ui/tests/unit/services/token-test.js

64 lines
1.8 KiB
JavaScript
Raw Normal View History

import Service from '@ember/service';
2019-03-13 00:04:16 +00:00
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import Pretender from 'pretender';
2019-03-13 00:04:16 +00:00
module('Unit | Service | Token', function(hooks) {
setupTest(hooks);
hooks.beforeEach(function() {
this.subject = function() {
return this.owner.factoryFor('service:token').create();
2019-03-13 00:04:16 +00:00
};
});
hooks.beforeEach(function() {
const mockSystem = Service.extend({
activeRegion: 'region-1',
shouldIncludeRegion: true,
});
2019-03-13 00:04:16 +00:00
this.owner.register('service:system', mockSystem);
this.system = this.owner.lookup('service:system');
this.server = new Pretender(function() {
this.get('/path', () => [200, {}, null]);
});
2019-03-13 00:04:16 +00:00
});
hooks.afterEach(function() {
this.server.shutdown();
2019-03-13 00:04:16 +00:00
});
2019-03-13 00:04:16 +00:00
test('authorizedRequest includes the region param when the system service says to', function(assert) {
const token = this.subject();
2019-03-13 00:04:16 +00:00
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'
);
2019-03-13 00:04:16 +00:00
this.system.set('shouldIncludeRegion', false);
2019-03-13 00:04:16 +00:00
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'
);
});
2019-03-13 00:04:16 +00:00
test('authorizedRawRequest bypasses adding the region param', function(assert) {
const token = this.subject();
2019-03-13 00:04:16 +00:00
token.authorizedRawRequest('/path');
assert.equal(
this.server.handledRequests.pop().url,
'/path',
'The region param is ommitted when making a raw request'
);
});
});