github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/ui/tests/unit/adapters/job-test.js (about)

     1  import { test, moduleFor } from 'ember-qunit';
     2  import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage';
     3  
     4  moduleFor('adapter:job', 'Unit | Adapter | Job', {
     5    unit: true,
     6    needs: ['service:token', 'service:system', 'model:namespace', 'adapter:application'],
     7    beforeEach() {
     8      window.sessionStorage.clear();
     9  
    10      this.server = startMirage();
    11      this.server.create('node');
    12      this.server.create('job', { id: 'job-1' });
    13      this.server.create('job', { id: 'job-2', namespaceId: 'some-namespace' });
    14    },
    15    afterEach() {
    16      this.server.shutdown();
    17    },
    18  });
    19  
    20  test('The job summary is stitched into the job request', function(assert) {
    21    const { pretender } = this.server;
    22    const jobName = 'job-1';
    23    const jobNamespace = 'default';
    24    const jobId = JSON.stringify([jobName, jobNamespace]);
    25  
    26    this.subject().findRecord(null, { modelName: 'job' }, jobId);
    27  
    28    assert.deepEqual(
    29      pretender.handledRequests.mapBy('url'),
    30      ['/v1/namespaces', `/v1/job/${jobName}`, `/v1/job/${jobName}/summary`],
    31      'The three requests made are /namespaces, /job/:id, and /job/:id/summary'
    32    );
    33  });
    34  
    35  test('When the job has a namespace other than default, it is in the URL', function(assert) {
    36    const { pretender } = this.server;
    37    const jobName = 'job-2';
    38    const jobNamespace = 'some-namespace';
    39    const jobId = JSON.stringify([jobName, jobNamespace]);
    40  
    41    this.subject().findRecord(null, { modelName: 'job' }, jobId);
    42  
    43    assert.deepEqual(
    44      pretender.handledRequests.mapBy('url'),
    45      [
    46        '/v1/namespaces',
    47        `/v1/job/${jobName}?namespace=${jobNamespace}`,
    48        `/v1/job/${jobName}/summary?namespace=${jobNamespace}`,
    49      ],
    50      'The three requests made are /namespaces, /job/:id?namespace=:namespace, and /job/:id/summary?namespace=:namespace'
    51    );
    52  });
    53  
    54  test('When there is no token set in the token service, no x-nomad-token header is set', function(
    55    assert
    56  ) {
    57    const { pretender } = this.server;
    58    const jobId = JSON.stringify(['job-1', 'default']);
    59  
    60    this.subject().findRecord(null, { modelName: 'job' }, jobId);
    61  
    62    assert.notOk(
    63      pretender.handledRequests.mapBy('requestHeaders').some(headers => headers['X-Nomad-Token']),
    64      'No token header present on either job request'
    65    );
    66  });
    67  
    68  test('When a token is set in the token service, then x-nomad-token header is set', function(
    69    assert
    70  ) {
    71    const { pretender } = this.server;
    72    const jobId = JSON.stringify(['job-1', 'default']);
    73    const secret = 'here is the secret';
    74  
    75    this.subject().set('token.secret', secret);
    76    this.subject().findRecord(null, { modelName: 'job' }, jobId);
    77  
    78    assert.ok(
    79      pretender.handledRequests
    80        .mapBy('requestHeaders')
    81        .every(headers => headers['X-Nomad-Token'] === secret),
    82      'The token header is present on both job requests'
    83    );
    84  });