2018-01-25 11:40:31 -05:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2017-05-19 17:22:46 -04:00
|
|
|
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-01-25 11:40:31 -05:00
|
|
|
let mock;
|
2017-05-19 17:22:46 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-01-25 11:40:31 -05:00
|
|
|
mock = new MockAdapter(axios);
|
2017-05-19 17:22:46 -04:00
|
|
|
originalGon = window.gon;
|
2017-08-22 11:10:49 -04:00
|
|
|
window.gon = Object.assign({}, dummyGon);
|
2017-05-19 17:22:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2018-01-25 11:40:31 -05:00
|
|
|
mock.restore();
|
2017-05-19 17:22:46 -04:00
|
|
|
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', () => {
|
2018-03-26 08:18:24 -04:00
|
|
|
it('fetches a group', done => {
|
2017-05-19 17:22:46 -04:00
|
|
|
const groupId = '123456';
|
2018-01-30 05:52:38 -05:00
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}`;
|
2018-01-25 11:40:31 -05:00
|
|
|
mock.onGet(expectedUrl).reply(200, {
|
|
|
|
name: 'test',
|
2017-05-19 17:22:46 -04:00
|
|
|
});
|
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
Api.group(groupId, response => {
|
2018-01-25 11:40:31 -05:00
|
|
|
expect(response.name).toBe('test');
|
2017-05-19 17:22:46 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('groups', () => {
|
2018-03-26 08:18:24 -04:00
|
|
|
it('fetches groups', done => {
|
2017-05-19 17:22:46 -04:00
|
|
|
const query = 'dummy query';
|
|
|
|
const options = { unused: 'option' };
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups.json`;
|
2018-03-26 08:18:24 -04:00
|
|
|
mock.onGet(expectedUrl).reply(200, [
|
|
|
|
{
|
|
|
|
name: 'test',
|
|
|
|
},
|
|
|
|
]);
|
2017-05-19 17:22:46 -04:00
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
Api.groups(query, options, response => {
|
2018-01-25 11:40:31 -05:00
|
|
|
expect(response.length).toBe(1);
|
|
|
|
expect(response[0].name).toBe('test');
|
2017-05-19 17:22:46 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('namespaces', () => {
|
2018-03-26 08:18:24 -04:00
|
|
|
it('fetches namespaces', done => {
|
2017-05-19 17:22:46 -04:00
|
|
|
const query = 'dummy query';
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/namespaces.json`;
|
2018-03-26 08:18:24 -04:00
|
|
|
mock.onGet(expectedUrl).reply(200, [
|
|
|
|
{
|
|
|
|
name: 'test',
|
|
|
|
},
|
|
|
|
]);
|
2017-05-19 17:22:46 -04:00
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
Api.namespaces(query, response => {
|
2018-01-25 11:40:31 -05:00
|
|
|
expect(response.length).toBe(1);
|
|
|
|
expect(response[0].name).toBe('test');
|
2017-05-19 17:22:46 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('projects', () => {
|
2018-03-26 08:18:24 -04:00
|
|
|
it('fetches projects with membership when logged in', done => {
|
2017-05-19 17:22:46 -04:00
|
|
|
const query = 'dummy query';
|
|
|
|
const options = { unused: 'option' };
|
2017-09-04 11:41:55 -04:00
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects.json`;
|
2017-08-22 11:10:49 -04:00
|
|
|
window.gon.current_user_id = 1;
|
2018-03-26 08:18:24 -04:00
|
|
|
mock.onGet(expectedUrl).reply(200, [
|
|
|
|
{
|
|
|
|
name: 'test',
|
|
|
|
},
|
|
|
|
]);
|
2017-05-19 17:22:46 -04:00
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
Api.projects(query, options, response => {
|
2018-01-25 11:40:31 -05:00
|
|
|
expect(response.length).toBe(1);
|
|
|
|
expect(response[0].name).toBe('test');
|
2017-05-19 17:22:46 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-08-22 11:10:49 -04:00
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
it('fetches projects without membership when not logged in', done => {
|
2017-08-22 11:10:49 -04:00
|
|
|
const query = 'dummy query';
|
|
|
|
const options = { unused: 'option' };
|
2017-09-04 11:41:55 -04:00
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects.json`;
|
2018-03-26 08:18:24 -04:00
|
|
|
mock.onGet(expectedUrl).reply(200, [
|
|
|
|
{
|
|
|
|
name: 'test',
|
|
|
|
},
|
|
|
|
]);
|
2017-08-22 11:10:49 -04:00
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
Api.projects(query, options, response => {
|
2018-01-25 11:40:31 -05:00
|
|
|
expect(response.length).toBe(1);
|
|
|
|
expect(response[0].name).toBe('test');
|
2017-08-22 11:10:49 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-05-19 17:22:46 -04:00
|
|
|
});
|
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
describe('mergerequest', () => {
|
|
|
|
it('fetches a merge request', done => {
|
|
|
|
const projectPath = 'abc';
|
|
|
|
const mergeRequestId = '123456';
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}`;
|
|
|
|
mock.onGet(expectedUrl).reply(200, {
|
|
|
|
title: 'test',
|
|
|
|
});
|
|
|
|
|
|
|
|
Api.mergeRequest(projectPath, mergeRequestId)
|
|
|
|
.then(({ data }) => {
|
|
|
|
expect(data.title).toBe('test');
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('mergerequest changes', () => {
|
|
|
|
it('fetches the changes of a merge request', done => {
|
|
|
|
const projectPath = 'abc';
|
|
|
|
const mergeRequestId = '123456';
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}/changes`;
|
|
|
|
mock.onGet(expectedUrl).reply(200, {
|
|
|
|
title: 'test',
|
|
|
|
});
|
|
|
|
|
|
|
|
Api.mergeRequestChanges(projectPath, mergeRequestId)
|
|
|
|
.then(({ data }) => {
|
|
|
|
expect(data.title).toBe('test');
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('mergerequest versions', () => {
|
|
|
|
it('fetches the versions of a merge request', done => {
|
|
|
|
const projectPath = 'abc';
|
|
|
|
const mergeRequestId = '123456';
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}/versions`;
|
|
|
|
mock.onGet(expectedUrl).reply(200, [
|
|
|
|
{
|
|
|
|
id: 123,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
Api.mergeRequestVersions(projectPath, mergeRequestId)
|
|
|
|
.then(({ data }) => {
|
|
|
|
expect(data.length).toBe(1);
|
|
|
|
expect(data[0].id).toBe(123);
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-05-19 17:22:46 -04:00
|
|
|
describe('newLabel', () => {
|
2018-03-26 08:18:24 -04:00
|
|
|
it('creates a new label', done => {
|
2017-05-19 17:22:46 -04:00
|
|
|
const namespace = 'some namespace';
|
|
|
|
const project = 'some project';
|
|
|
|
const labelData = { some: 'data' };
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/${namespace}/${project}/labels`;
|
|
|
|
const expectedData = {
|
|
|
|
label: labelData,
|
|
|
|
};
|
2018-03-26 08:18:24 -04:00
|
|
|
mock.onPost(expectedUrl).reply(config => {
|
2018-01-25 11:40:31 -05:00
|
|
|
expect(config.data).toBe(JSON.stringify(expectedData));
|
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
return [
|
|
|
|
200,
|
|
|
|
{
|
|
|
|
name: 'test',
|
|
|
|
},
|
|
|
|
];
|
2017-05-19 17:22:46 -04:00
|
|
|
});
|
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
Api.newLabel(namespace, project, labelData, response => {
|
2018-01-25 11:40:31 -05:00
|
|
|
expect(response.name).toBe('test');
|
2017-05-19 17:22:46 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-02-19 14:06:16 -05:00
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
it('creates a group label', done => {
|
2018-02-19 14:06:16 -05:00
|
|
|
const namespace = 'group/subgroup';
|
|
|
|
const labelData = { some: 'data' };
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/groups/${namespace}/-/labels`;
|
|
|
|
const expectedData = {
|
|
|
|
label: labelData,
|
|
|
|
};
|
2018-03-26 08:18:24 -04:00
|
|
|
mock.onPost(expectedUrl).reply(config => {
|
2018-02-19 14:06:16 -05:00
|
|
|
expect(config.data).toBe(JSON.stringify(expectedData));
|
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
return [
|
|
|
|
200,
|
|
|
|
{
|
|
|
|
name: 'test',
|
|
|
|
},
|
|
|
|
];
|
2018-02-19 14:06:16 -05:00
|
|
|
});
|
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
Api.newLabel(namespace, undefined, labelData, response => {
|
2018-02-19 14:06:16 -05:00
|
|
|
expect(response.name).toBe('test');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-05-19 17:22:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('groupProjects', () => {
|
2018-03-26 08:18:24 -04:00
|
|
|
it('fetches group projects', done => {
|
2017-05-19 17:22:46 -04:00
|
|
|
const groupId = '123456';
|
|
|
|
const query = 'dummy query';
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/projects.json`;
|
2018-03-26 08:18:24 -04:00
|
|
|
mock.onGet(expectedUrl).reply(200, [
|
|
|
|
{
|
|
|
|
name: 'test',
|
|
|
|
},
|
|
|
|
]);
|
2017-05-19 17:22:46 -04:00
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
Api.groupProjects(groupId, query, response => {
|
2018-01-25 11:40:31 -05:00
|
|
|
expect(response.length).toBe(1);
|
|
|
|
expect(response[0].name).toBe('test');
|
2017-05-19 17:22:46 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('licenseText', () => {
|
2018-03-26 08:18:24 -04:00
|
|
|
it('fetches a license text', done => {
|
2017-05-19 17:22:46 -04:00
|
|
|
const licenseKey = "driver's license";
|
|
|
|
const data = { unused: 'option' };
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/templates/licenses/${licenseKey}`;
|
2018-01-25 11:40:31 -05:00
|
|
|
mock.onGet(expectedUrl).reply(200, 'test');
|
2017-05-19 17:22:46 -04:00
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
Api.licenseText(licenseKey, data, response => {
|
2018-01-25 11:40:31 -05:00
|
|
|
expect(response).toBe('test');
|
2017-05-19 17:22:46 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('gitignoreText', () => {
|
2018-03-26 08:18:24 -04:00
|
|
|
it('fetches a gitignore text', done => {
|
2017-05-19 17:22:46 -04:00
|
|
|
const gitignoreKey = 'ignore git';
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/templates/gitignores/${gitignoreKey}`;
|
2018-01-25 11:40:31 -05:00
|
|
|
mock.onGet(expectedUrl).reply(200, 'test');
|
2017-05-19 17:22:46 -04:00
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
Api.gitignoreText(gitignoreKey, response => {
|
2018-01-25 11:40:31 -05:00
|
|
|
expect(response).toBe('test');
|
2017-05-19 17:22:46 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('gitlabCiYml', () => {
|
2018-03-26 08:18:24 -04:00
|
|
|
it('fetches a .gitlab-ci.yml', done => {
|
2017-05-19 17:22:46 -04:00
|
|
|
const gitlabCiYmlKey = 'Y CI ML';
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/templates/gitlab_ci_ymls/${gitlabCiYmlKey}`;
|
2018-01-25 11:40:31 -05:00
|
|
|
mock.onGet(expectedUrl).reply(200, 'test');
|
2017-05-19 17:22:46 -04:00
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
Api.gitlabCiYml(gitlabCiYmlKey, response => {
|
2018-01-25 11:40:31 -05:00
|
|
|
expect(response).toBe('test');
|
2017-05-19 17:22:46 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('dockerfileYml', () => {
|
2018-03-26 08:18:24 -04:00
|
|
|
it('fetches a Dockerfile', done => {
|
2017-05-19 17:22:46 -04:00
|
|
|
const dockerfileYmlKey = 'a giant whale';
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/templates/dockerfiles/${dockerfileYmlKey}`;
|
2018-01-25 11:40:31 -05:00
|
|
|
mock.onGet(expectedUrl).reply(200, 'test');
|
2017-05-19 17:22:46 -04:00
|
|
|
|
2018-03-26 08:18:24 -04:00
|
|
|
Api.dockerfileYml(dockerfileYmlKey, response => {
|
2018-01-25 11:40:31 -05:00
|
|
|
expect(response).toBe('test');
|
2017-05-19 17:22:46 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('issueTemplate', () => {
|
2018-03-26 08:18:24 -04:00
|
|
|
it('fetches an issue template', done => {
|
2017-05-19 17:22:46 -04:00
|
|
|
const namespace = 'some namespace';
|
|
|
|
const project = 'some project';
|
2017-11-10 10:02:42 -05:00
|
|
|
const templateKey = ' template #%?.key ';
|
2017-05-19 17:22:46 -04:00
|
|
|
const templateType = 'template type';
|
2018-03-26 08:18:24 -04:00
|
|
|
const expectedUrl = `${dummyUrlRoot}/${namespace}/${project}/templates/${templateType}/${encodeURIComponent(
|
|
|
|
templateKey,
|
|
|
|
)}`;
|
2018-01-25 11:40:31 -05:00
|
|
|
mock.onGet(expectedUrl).reply(200, 'test');
|
2017-05-19 17:22:46 -04:00
|
|
|
|
|
|
|
Api.issueTemplate(namespace, project, templateKey, templateType, (error, response) => {
|
2018-01-25 11:40:31 -05:00
|
|
|
expect(response).toBe('test');
|
2017-05-19 17:22:46 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('users', () => {
|
2018-03-26 08:18:24 -04:00
|
|
|
it('fetches users', done => {
|
2017-05-19 17:22:46 -04:00
|
|
|
const query = 'dummy query';
|
|
|
|
const options = { unused: 'option' };
|
|
|
|
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users.json`;
|
2018-03-26 08:18:24 -04:00
|
|
|
mock.onGet(expectedUrl).reply(200, [
|
|
|
|
{
|
|
|
|
name: 'test',
|
|
|
|
},
|
|
|
|
]);
|
2017-05-19 17:22:46 -04:00
|
|
|
|
|
|
|
Api.users(query, options)
|
2018-01-25 11:40:31 -05:00
|
|
|
.then(({ data }) => {
|
|
|
|
expect(data.length).toBe(1);
|
|
|
|
expect(data[0].name).toBe('test');
|
2017-05-19 17:22:46 -04:00
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|