2018-03-17 18:26:18 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2017-09-10 17:25:29 +05:30
|
|
|
import Api from '~/api';
|
|
|
|
|
|
|
|
describe('Api', () => {
|
|
|
|
const dummyApiVersion = 'v3000';
|
|
|
|
const dummyUrlRoot = 'http://host.invalid';
|
|
|
|
const dummyGon = {
|
|
|
|
api_version: dummyApiVersion,
|
|
|
|
relative_url_root: dummyUrlRoot,
|
|
|
|
};
|
|
|
|
let originalGon;
|
2018-03-17 18:26:18 +05:30
|
|
|
let mock;
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-03-17 18:26:18 +05:30
|
|
|
mock = new MockAdapter(axios);
|
2017-09-10 17:25:29 +05:30
|
|
|
originalGon = window.gon;
|
2018-03-17 18:26:18 +05:30
|
|
|
window.gon = Object.assign({}, dummyGon);
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2018-03-17 18:26:18 +05:30
|
|
|
mock.restore();
|
2017-09-10 17:25:29 +05:30
|
|
|
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';
|
2018-03-17 18:26:18 +05:30
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}`;
|
|
|
|
mock.onGet(expectedUrl).reply(200, {
|
|
|
|
name: 'test',
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
Api.group(groupId, (response) => {
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response.name).toBe('test');
|
2017-09-10 17:25:29 +05:30
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('groups', () => {
|
|
|
|
it('fetches groups', (done) => {
|
|
|
|
const query = 'dummy query';
|
|
|
|
const options = { unused: 'option' };
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups.json`;
|
2018-03-17 18:26:18 +05:30
|
|
|
mock.onGet(expectedUrl).reply(200, [{
|
|
|
|
name: 'test',
|
|
|
|
}]);
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
Api.groups(query, options, (response) => {
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response.length).toBe(1);
|
|
|
|
expect(response[0].name).toBe('test');
|
2017-09-10 17:25:29 +05:30
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('namespaces', () => {
|
|
|
|
it('fetches namespaces', (done) => {
|
|
|
|
const query = 'dummy query';
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/namespaces.json`;
|
2018-03-17 18:26:18 +05:30
|
|
|
mock.onGet(expectedUrl).reply(200, [{
|
|
|
|
name: 'test',
|
|
|
|
}]);
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
Api.namespaces(query, (response) => {
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response.length).toBe(1);
|
|
|
|
expect(response[0].name).toBe('test');
|
2017-09-10 17:25:29 +05:30
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('projects', () => {
|
2018-03-17 18:26:18 +05:30
|
|
|
it('fetches projects with membership when logged in', (done) => {
|
2017-09-10 17:25:29 +05:30
|
|
|
const query = 'dummy query';
|
|
|
|
const options = { unused: 'option' };
|
2018-03-17 18:26:18 +05:30
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects.json`;
|
|
|
|
window.gon.current_user_id = 1;
|
|
|
|
mock.onGet(expectedUrl).reply(200, [{
|
|
|
|
name: 'test',
|
|
|
|
}]);
|
|
|
|
|
|
|
|
Api.projects(query, options, (response) => {
|
|
|
|
expect(response.length).toBe(1);
|
|
|
|
expect(response[0].name).toBe('test');
|
|
|
|
done();
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('fetches projects without membership when not logged in', (done) => {
|
|
|
|
const query = 'dummy query';
|
|
|
|
const options = { unused: 'option' };
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects.json`;
|
|
|
|
mock.onGet(expectedUrl).reply(200, [{
|
|
|
|
name: 'test',
|
|
|
|
}]);
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
Api.projects(query, options, (response) => {
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response.length).toBe(1);
|
|
|
|
expect(response[0].name).toBe('test');
|
2017-09-10 17:25:29 +05:30
|
|
|
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,
|
|
|
|
};
|
2018-03-17 18:26:18 +05:30
|
|
|
mock.onPost(expectedUrl).reply((config) => {
|
|
|
|
expect(config.data).toBe(JSON.stringify(expectedData));
|
|
|
|
|
|
|
|
return [200, {
|
|
|
|
name: 'test',
|
|
|
|
}];
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
Api.newLabel(namespace, project, labelData, (response) => {
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response.name).toBe('test');
|
2017-09-10 17:25:29 +05:30
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('groupProjects', () => {
|
|
|
|
it('fetches group projects', (done) => {
|
|
|
|
const groupId = '123456';
|
|
|
|
const query = 'dummy query';
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/projects.json`;
|
2018-03-17 18:26:18 +05:30
|
|
|
mock.onGet(expectedUrl).reply(200, [{
|
|
|
|
name: 'test',
|
|
|
|
}]);
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
Api.groupProjects(groupId, query, (response) => {
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response.length).toBe(1);
|
|
|
|
expect(response[0].name).toBe('test');
|
2017-09-10 17:25:29 +05:30
|
|
|
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}`;
|
2018-03-17 18:26:18 +05:30
|
|
|
mock.onGet(expectedUrl).reply(200, 'test');
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
Api.licenseText(licenseKey, data, (response) => {
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).toBe('test');
|
2017-09-10 17:25:29 +05:30
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('gitignoreText', () => {
|
|
|
|
it('fetches a gitignore text', (done) => {
|
|
|
|
const gitignoreKey = 'ignore git';
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/templates/gitignores/${gitignoreKey}`;
|
2018-03-17 18:26:18 +05:30
|
|
|
mock.onGet(expectedUrl).reply(200, 'test');
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
Api.gitignoreText(gitignoreKey, (response) => {
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).toBe('test');
|
2017-09-10 17:25:29 +05:30
|
|
|
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}`;
|
2018-03-17 18:26:18 +05:30
|
|
|
mock.onGet(expectedUrl).reply(200, 'test');
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
Api.gitlabCiYml(gitlabCiYmlKey, (response) => {
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).toBe('test');
|
2017-09-10 17:25:29 +05:30
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('dockerfileYml', () => {
|
|
|
|
it('fetches a Dockerfile', (done) => {
|
|
|
|
const dockerfileYmlKey = 'a giant whale';
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/templates/dockerfiles/${dockerfileYmlKey}`;
|
2018-03-17 18:26:18 +05:30
|
|
|
mock.onGet(expectedUrl).reply(200, 'test');
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
Api.dockerfileYml(dockerfileYmlKey, (response) => {
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).toBe('test');
|
2017-09-10 17:25:29 +05:30
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('issueTemplate', () => {
|
|
|
|
it('fetches an issue template', (done) => {
|
|
|
|
const namespace = 'some namespace';
|
|
|
|
const project = 'some project';
|
2018-03-17 18:26:18 +05:30
|
|
|
const templateKey = ' template #%?.key ';
|
2017-09-10 17:25:29 +05:30
|
|
|
const templateType = 'template type';
|
2018-03-17 18:26:18 +05:30
|
|
|
const expectedUrl = `${dummyUrlRoot}/${namespace}/${project}/templates/${templateType}/${encodeURIComponent(templateKey)}`;
|
|
|
|
mock.onGet(expectedUrl).reply(200, 'test');
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
Api.issueTemplate(namespace, project, templateKey, templateType, (error, response) => {
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).toBe('test');
|
2017-09-10 17:25:29 +05:30
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('users', () => {
|
|
|
|
it('fetches users', (done) => {
|
|
|
|
const query = 'dummy query';
|
|
|
|
const options = { unused: 'option' };
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users.json`;
|
2018-03-17 18:26:18 +05:30
|
|
|
mock.onGet(expectedUrl).reply(200, [{
|
|
|
|
name: 'test',
|
|
|
|
}]);
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
Api.users(query, options)
|
2018-03-17 18:26:18 +05:30
|
|
|
.then(({ data }) => {
|
|
|
|
expect(data.length).toBe(1);
|
|
|
|
expect(data[0].name).toBe('test');
|
2017-09-10 17:25:29 +05:30
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|