github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/unit/services/token-test.js (about)

     1  import Service from '@ember/service';
     2  import { module, test } from 'qunit';
     3  import { setupTest } from 'ember-qunit';
     4  import Pretender from 'pretender';
     5  
     6  module('Unit | Service | Token', function(hooks) {
     7    setupTest(hooks);
     8  
     9    hooks.beforeEach(function() {
    10      this.subject = function() {
    11        return this.owner.factoryFor('service:token').create();
    12      };
    13    });
    14  
    15    hooks.beforeEach(function() {
    16      const mockSystem = Service.extend({
    17        activeRegion: 'region-1',
    18        shouldIncludeRegion: true,
    19      });
    20  
    21      this.owner.register('service:system', mockSystem);
    22      this.system = this.owner.lookup('service:system');
    23  
    24      this.server = new Pretender(function() {
    25        this.get('/path', () => [200, {}, null]);
    26      });
    27    });
    28  
    29    hooks.afterEach(function() {
    30      this.server.shutdown();
    31    });
    32  
    33    test('authorizedRequest includes the region param when the system service says to', function(assert) {
    34      const token = this.subject();
    35  
    36      token.authorizedRequest('/path');
    37      assert.equal(
    38        this.server.handledRequests.pop().url,
    39        `/path?region=${this.system.get('activeRegion')}`,
    40        'The region param is included when the system service shouldIncludeRegion property is true'
    41      );
    42  
    43      this.system.set('shouldIncludeRegion', false);
    44  
    45      token.authorizedRequest('/path');
    46      assert.equal(
    47        this.server.handledRequests.pop().url,
    48        '/path',
    49        'The region param is not included when the system service shouldIncludeRegion property is false'
    50      );
    51    });
    52  
    53    test('authorizedRequest does not include the region param when the region param is already in the URL', function(assert) {
    54      const token = this.subject();
    55  
    56      token.authorizedRequest('/path?query=param&region=already-here');
    57      assert.equal(
    58        this.server.handledRequests.pop().url,
    59        '/path?query=param&region=already-here',
    60        'The region param that is already in the URL takes precedence over the region in the service'
    61      );
    62    });
    63  
    64    test('authorizedRawRequest bypasses adding the region param', function(assert) {
    65      const token = this.subject();
    66  
    67      token.authorizedRawRequest('/path');
    68      assert.equal(
    69        this.server.handledRequests.pop().url,
    70        '/path',
    71        'The region param is ommitted when making a raw request'
    72      );
    73    });
    74  });