github.com/hernad/nomad@v1.6.112/ui/tests/unit/services/token-test.js (about)

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