BigW Consortium Gitlab

api_spec.js 8.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
import Api from '~/api';

describe('Api', () => {
  const dummyApiVersion = 'v3000';
  const dummyUrlRoot = 'http://host.invalid';
  const dummyGon = {
    api_version: dummyApiVersion,
    relative_url_root: dummyUrlRoot,
  };
  const dummyResponse = 'hello from outer space!';
  const sendDummyResponse = () => {
    const deferred = $.Deferred();
    deferred.resolve(dummyResponse);
    return deferred.promise();
  };
  let originalGon;

  beforeEach(() => {
    originalGon = window.gon;
    window.gon = dummyGon;
  });

  afterEach(() => {
    window.gon = originalGon;
  });

  describe('buildUrl', () => {
    it('adds URL root and fills in API version', () => {
      const input = '/api/:version/foo/bar';
      const expectedOutput = `${dummyUrlRoot}/api/${dummyApiVersion}/foo/bar`;

      const builtUrl = Api.buildUrl(input);

      expect(builtUrl).toEqual(expectedOutput);
    });
  });

  describe('group', () => {
    it('fetches a group', (done) => {
      const groupId = '123456';
      const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}.json`;
      spyOn(jQuery, 'ajax').and.callFake((request) => {
        expect(request.url).toEqual(expectedUrl);
        expect(request.dataType).toEqual('json');
        return sendDummyResponse();
      });

      Api.group(groupId, (response) => {
        expect(response).toBe(dummyResponse);
        done();
      });
    });
  });

  describe('groups', () => {
    it('fetches groups', (done) => {
      const query = 'dummy query';
      const options = { unused: 'option' };
      const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups.json`;
      const expectedData = Object.assign({
        search: query,
        per_page: 20,
      }, options);
      spyOn(jQuery, 'ajax').and.callFake((request) => {
        expect(request.url).toEqual(expectedUrl);
        expect(request.dataType).toEqual('json');
        expect(request.data).toEqual(expectedData);
        return sendDummyResponse();
      });

      Api.groups(query, options, (response) => {
        expect(response).toBe(dummyResponse);
        done();
      });
    });
  });

  describe('namespaces', () => {
    it('fetches namespaces', (done) => {
      const query = 'dummy query';
      const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/namespaces.json`;
      const expectedData = {
        search: query,
        per_page: 20,
      };
      spyOn(jQuery, 'ajax').and.callFake((request) => {
        expect(request.url).toEqual(expectedUrl);
        expect(request.dataType).toEqual('json');
        expect(request.data).toEqual(expectedData);
        return sendDummyResponse();
      });

      Api.namespaces(query, (response) => {
        expect(response).toBe(dummyResponse);
        done();
      });
    });
  });

  describe('projects', () => {
    it('fetches projects', (done) => {
      const query = 'dummy query';
      const options = { unused: 'option' };
      const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects.json?simple=true`;
      const expectedData = Object.assign({
        search: query,
        per_page: 20,
        membership: true,
      }, options);
      spyOn(jQuery, 'ajax').and.callFake((request) => {
        expect(request.url).toEqual(expectedUrl);
        expect(request.dataType).toEqual('json');
        expect(request.data).toEqual(expectedData);
        return sendDummyResponse();
      });

      Api.projects(query, options, (response) => {
        expect(response).toBe(dummyResponse);
        done();
      });
    });
  });

  describe('newLabel', () => {
    it('creates a new label', (done) => {
      const namespace = 'some namespace';
      const project = 'some project';
      const labelData = { some: 'data' };
      const expectedUrl = `${dummyUrlRoot}/${namespace}/${project}/labels`;
      const expectedData = {
        label: labelData,
      };
      spyOn(jQuery, 'ajax').and.callFake((request) => {
        expect(request.url).toEqual(expectedUrl);
        expect(request.dataType).toEqual('json');
        expect(request.type).toEqual('POST');
        expect(request.data).toEqual(expectedData);
        return sendDummyResponse();
      });

      Api.newLabel(namespace, project, labelData, (response) => {
        expect(response).toBe(dummyResponse);
        done();
      });
    });
  });

  describe('groupProjects', () => {
    it('fetches group projects', (done) => {
      const groupId = '123456';
      const query = 'dummy query';
      const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/projects.json`;
      const expectedData = {
        search: query,
        per_page: 20,
      };
      spyOn(jQuery, 'ajax').and.callFake((request) => {
        expect(request.url).toEqual(expectedUrl);
        expect(request.dataType).toEqual('json');
        expect(request.data).toEqual(expectedData);
        return sendDummyResponse();
      });

      Api.groupProjects(groupId, query, (response) => {
        expect(response).toBe(dummyResponse);
        done();
      });
    });
  });

  describe('licenseText', () => {
    it('fetches a license text', (done) => {
      const licenseKey = "driver's license";
      const data = { unused: 'option' };
      const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/templates/licenses/${licenseKey}`;
      spyOn(jQuery, 'ajax').and.callFake((request) => {
        expect(request.url).toEqual(expectedUrl);
        expect(request.data).toEqual(data);
        return sendDummyResponse();
      });

      Api.licenseText(licenseKey, data, (response) => {
        expect(response).toBe(dummyResponse);
        done();
      });
    });
  });

  describe('gitignoreText', () => {
    it('fetches a gitignore text', (done) => {
      const gitignoreKey = 'ignore git';
      const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/templates/gitignores/${gitignoreKey}`;
      spyOn(jQuery, 'get').and.callFake((url, callback) => {
        expect(url).toEqual(expectedUrl);
        callback(dummyResponse);
      });

      Api.gitignoreText(gitignoreKey, (response) => {
        expect(response).toBe(dummyResponse);
        done();
      });
    });
  });

  describe('gitlabCiYml', () => {
    it('fetches a .gitlab-ci.yml', (done) => {
      const gitlabCiYmlKey = 'Y CI ML';
      const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/templates/gitlab_ci_ymls/${gitlabCiYmlKey}`;
      spyOn(jQuery, 'get').and.callFake((url, callback) => {
        expect(url).toEqual(expectedUrl);
        callback(dummyResponse);
      });

      Api.gitlabCiYml(gitlabCiYmlKey, (response) => {
        expect(response).toBe(dummyResponse);
        done();
      });
    });
  });

  describe('dockerfileYml', () => {
    it('fetches a Dockerfile', (done) => {
      const dockerfileYmlKey = 'a giant whale';
      const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/templates/dockerfiles/${dockerfileYmlKey}`;
      spyOn(jQuery, 'get').and.callFake((url, callback) => {
        expect(url).toEqual(expectedUrl);
        callback(dummyResponse);
      });

      Api.dockerfileYml(dockerfileYmlKey, (response) => {
        expect(response).toBe(dummyResponse);
        done();
      });
    });
  });

  describe('issueTemplate', () => {
    it('fetches an issue template', (done) => {
      const namespace = 'some namespace';
      const project = 'some project';
      const templateKey = 'template key';
      const templateType = 'template type';
      const expectedUrl = `${dummyUrlRoot}/${namespace}/${project}/templates/${templateType}/${templateKey}`;
      spyOn(jQuery, 'ajax').and.callFake((request) => {
        expect(request.url).toEqual(expectedUrl);
        return sendDummyResponse();
      });

      Api.issueTemplate(namespace, project, templateKey, templateType, (error, response) => {
        expect(error).toBe(null);
        expect(response).toBe(dummyResponse);
        done();
      });
    });
  });

  describe('users', () => {
    it('fetches users', (done) => {
      const query = 'dummy query';
      const options = { unused: 'option' };
      const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users.json`;
      const expectedData = Object.assign({
        search: query,
        per_page: 20,
      }, options);
      spyOn(jQuery, 'ajax').and.callFake((request) => {
        expect(request.url).toEqual(expectedUrl);
        expect(request.dataType).toEqual('json');
        expect(request.data).toEqual(expectedData);
        return sendDummyResponse();
      });

      Api.users(query, options)
        .then((response) => {
          expect(response).toBe(dummyResponse);
        })
        .then(done)
        .catch(done.fail);
    });
  });
});