{"size": 2528, "ext": "js", "max_stars_repo_path": "tests/unit/cache/service-test.js", "max_stars_repo_name": "mchengat/ui", "max_stars_repo_stars_event_min_datetime": "2021-03-25T16:38:25.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-25T16:38:25.000Z", "max_issues_repo_path": "tests/unit/cache/service-test.js", "max_issues_repo_name": "mchengat/ui", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "tests/unit/cache/service-test.js", "max_forks_repo_name": "mchengat/ui", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.5436893204, "max_line_length": 86, "alphanum_fraction": 0.6143196203}
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import Pretender from 'pretender';
import Service from '@ember/service';

let server;

const sessionStub = Service.extend({
  data: {
    authenticated: {
      token: 'faketoken'
    }
  }
});

module('Unit | Service | cache', function(hooks) {
  setupTest(hooks);

  // Specify the other units that are required for this test.
  // needs: ['service:session'],

  hooks.beforeEach(function() {
    server = new Pretender();
    this.owner.register('service:session', sessionStub);
  });

  hooks.afterEach(function() {
    server.shutdown();
  });

  test('it exists', function(assert) {
    const service = this.owner.lookup('service:cache');

    assert.ok(service);
  });

  test('it makes a call to delete pipeline cache successfully', function(assert) {
    let url = 'http://localhost:8080/v4/pipelines/1/caches';

    server.delete(url, () => [204]);

    let service = this.owner.lookup('service:cache');

    assert.ok(service);

    const p = service.clearCache({ scope: 'pipelines', cacheId: '1', pipelineId: 1 });

    p.then(() => {
      const [request] = server.handledRequests;

      assert.equal(request.status, '204');
      assert.equal(request.method, 'DELETE');
      assert.equal(request.url, `${url}?scope=pipelines&cacheId=1`);
    });
  });

  test('it makes a call to delete job cache successfully', function(assert) {
    let url = 'http://localhost:8080/v4/pipelines/1/caches';

    server.delete(url, () => [204]);

    let service = this.owner.lookup('service:cache');

    assert.ok(service);

    const p = service.clearCache({ scope: 'jobs', cacheId: '1', pipelineId: 1 });

    p.then(() => {
      const [request] = server.handledRequests;

      assert.equal(request.status, '204');
      assert.equal(request.method, 'DELETE');
      assert.equal(request.url, `${url}?scope=jobs&cacheId=1`);
    });
  });

  test('it returns 401 on unauthorized deletion', function(assert) {
    assert.expect(2);
    let url = 'http://localhost:8080/v4/pipelines/1/caches';

    server.delete(url, () => [
      401,
      {
        'Content-Type': 'application/json'
      },
      'Unauthorized'
    ]);

    let service = this.owner.lookup('service:cache');

    assert.ok(service);

    const p = service.clearCache({ scope: 'pipelines', cacheId: '1', pipelineId: 1 });

    p.then(
      () => {},
      err => {
        assert.equal(err, 'You do not have the permissions to clear the cache.');
      }
    );
  });
});
