2018-08-10 03:27:06 +00:00
|
|
|
import { getOwner } from '@ember/application';
|
|
|
|
import Service from '@ember/service';
|
2019-03-13 00:04:16 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupTest } from 'ember-qunit';
|
2018-08-10 03:27:06 +00:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
hooks.beforeEach(function() {
|
2018-08-10 03:27:06 +00:00
|
|
|
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');
|
2018-08-10 03:27:06 +00:00
|
|
|
|
|
|
|
this.server = new Pretender(function() {
|
|
|
|
this.get('/path', () => [200, {}, null]);
|
|
|
|
});
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
hooks.afterEach(function() {
|
2018-08-10 03:27:06 +00:00
|
|
|
this.server.shutdown();
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
2018-08-10 03:27:06 +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();
|
2018-08-10 03:27:06 +00:00
|
|
|
|
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'
|
|
|
|
);
|
2018-08-10 03:27:06 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
this.system.set('shouldIncludeRegion', false);
|
2018-08-10 03:27:06 +00:00
|
|
|
|
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'
|
|
|
|
);
|
|
|
|
});
|
2018-08-10 03:27:06 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
test('authorizedRawRequest bypasses adding the region param', function(assert) {
|
|
|
|
const token = this.subject();
|
2018-08-10 03:27:06 +00:00
|
|
|
|
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'
|
|
|
|
);
|
|
|
|
});
|
2018-08-10 03:27:06 +00:00
|
|
|
});
|