debian-mirror-gitlab/spec/frontend/api_spec.js

517 lines
15 KiB
JavaScript
Raw Normal View History

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';
2019-07-31 22:56:46 +05:30
const dummyUrlRoot = '/gitlab';
2017-09-10 17:25:29 +05:30
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);
});
2019-07-31 22:56:46 +05:30
[null, '', '/'].forEach(root => {
it(`works when relative_url_root is ${root}`, () => {
window.gon.relative_url_root = root;
const input = '/api/:version/foo/bar';
const expectedOutput = `/api/${dummyApiVersion}/foo/bar`;
const builtUrl = Api.buildUrl(input);
expect(builtUrl).toEqual(expectedOutput);
});
});
2017-09-10 17:25:29 +05:30
});
describe('group', () => {
2018-05-09 12:01:36 +05:30
it('fetches a group', done => {
2017-09-10 17:25:29 +05:30
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
});
2018-05-09 12:01:36 +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();
});
});
});
2019-03-02 22:35:43 +05:30
describe('groupMembers', () => {
it('fetches group members', done => {
const groupId = '54321';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/members`;
const expectedData = [{ id: 7 }];
mock.onGet(expectedUrl).reply(200, expectedData);
Api.groupMembers(groupId)
.then(({ data }) => {
expect(data).toEqual(expectedData);
})
.then(done)
.catch(done.fail);
});
});
2017-09-10 17:25:29 +05:30
describe('groups', () => {
2018-05-09 12:01:36 +05:30
it('fetches groups', done => {
2017-09-10 17:25:29 +05:30
const query = 'dummy query';
const options = { unused: 'option' };
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups.json`;
2018-05-09 12:01:36 +05:30
mock.onGet(expectedUrl).reply(200, [
{
name: 'test',
},
]);
2017-09-10 17:25:29 +05:30
2018-05-09 12:01:36 +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', () => {
2018-05-09 12:01:36 +05:30
it('fetches namespaces', done => {
2017-09-10 17:25:29 +05:30
const query = 'dummy query';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/namespaces.json`;
2018-05-09 12:01:36 +05:30
mock.onGet(expectedUrl).reply(200, [
{
name: 'test',
},
]);
2017-09-10 17:25:29 +05:30
2018-05-09 12:01:36 +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-05-09 12:01:36 +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;
2018-05-09 12:01:36 +05:30
mock.onGet(expectedUrl).reply(200, [
{
name: 'test',
},
]);
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +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');
done();
2017-09-10 17:25:29 +05:30
});
2018-03-17 18:26:18 +05:30
});
2018-05-09 12:01:36 +05:30
it('fetches projects without membership when not logged in', done => {
2018-03-17 18:26:18 +05:30
const query = 'dummy query';
const options = { unused: 'option' };
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects.json`;
2018-05-09 12:01:36 +05:30
mock.onGet(expectedUrl).reply(200, [
{
name: 'test',
},
]);
2017-09-10 17:25:29 +05:30
2018-05-09 12:01:36 +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();
});
});
});
2019-07-07 11:18:12 +05:30
describe('projectMergeRequests', () => {
const projectPath = 'abc';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests`;
it('fetches all merge requests for a project', done => {
const mockData = [{ source_branch: 'foo' }, { source_branch: 'bar' }];
mock.onGet(expectedUrl).reply(200, mockData);
Api.projectMergeRequests(projectPath)
.then(({ data }) => {
expect(data.length).toEqual(2);
expect(data[0].source_branch).toBe('foo');
expect(data[1].source_branch).toBe('bar');
})
.then(done)
.catch(done.fail);
});
it('fetches merge requests filtered with passed params', done => {
const params = {
source_branch: 'bar',
};
const mockData = [{ source_branch: 'bar' }];
mock.onGet(expectedUrl, { params }).reply(200, mockData);
Api.projectMergeRequests(projectPath, params)
.then(({ data }) => {
expect(data.length).toEqual(1);
expect(data[0].source_branch).toBe('bar');
})
.then(done)
.catch(done.fail);
});
});
2019-02-15 15:39:39 +05:30
describe('projectMergeRequest', () => {
2018-05-09 12:01:36 +05:30
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',
});
2019-02-15 15:39:39 +05:30
Api.projectMergeRequest(projectPath, mergeRequestId)
2018-05-09 12:01:36 +05:30
.then(({ data }) => {
expect(data.title).toBe('test');
})
.then(done)
.catch(done.fail);
});
});
2019-02-15 15:39:39 +05:30
describe('projectMergeRequestChanges', () => {
2018-05-09 12:01:36 +05:30
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',
});
2019-02-15 15:39:39 +05:30
Api.projectMergeRequestChanges(projectPath, mergeRequestId)
2018-05-09 12:01:36 +05:30
.then(({ data }) => {
expect(data.title).toBe('test');
})
.then(done)
.catch(done.fail);
});
});
2019-02-15 15:39:39 +05:30
describe('projectMergeRequestVersions', () => {
2018-05-09 12:01:36 +05:30
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,
},
]);
2019-02-15 15:39:39 +05:30
Api.projectMergeRequestVersions(projectPath, mergeRequestId)
2018-05-09 12:01:36 +05:30
.then(({ data }) => {
expect(data.length).toBe(1);
expect(data[0].id).toBe(123);
})
.then(done)
.catch(done.fail);
});
});
2019-02-15 15:39:39 +05:30
describe('projectRunners', () => {
it('fetches the runners of a project', done => {
const projectPath = 7;
const params = { scope: 'active' };
const mockData = [{ id: 4 }];
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/runners`;
mock.onGet(expectedUrl, { params }).reply(200, mockData);
Api.projectRunners(projectPath, { params })
.then(({ data }) => {
expect(data).toEqual(mockData);
})
.then(done)
.catch(done.fail);
});
});
2017-09-10 17:25:29 +05:30
describe('newLabel', () => {
2018-05-09 12:01:36 +05:30
it('creates a new label', done => {
2017-09-10 17:25:29 +05:30
const namespace = 'some namespace';
const project = 'some project';
const labelData = { some: 'data' };
2019-09-04 21:01:54 +05:30
const expectedUrl = `${dummyUrlRoot}/${namespace}/${project}/-/labels`;
2017-09-10 17:25:29 +05:30
const expectedData = {
label: labelData,
};
2018-05-09 12:01:36 +05:30
mock.onPost(expectedUrl).reply(config => {
2018-03-17 18:26:18 +05:30
expect(config.data).toBe(JSON.stringify(expectedData));
2018-05-09 12:01:36 +05:30
return [
200,
{
name: 'test',
},
];
2017-09-10 17:25:29 +05:30
});
2018-05-09 12:01:36 +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();
});
});
2018-03-27 19:54:05 +05:30
2018-05-09 12:01:36 +05:30
it('creates a group label', done => {
2018-03-27 19:54:05 +05:30
const namespace = 'group/subgroup';
const labelData = { some: 'data' };
2019-09-04 21:01:54 +05:30
const expectedUrl = Api.buildUrl(Api.groupLabelsPath).replace(':namespace_path', namespace);
2018-03-27 19:54:05 +05:30
const expectedData = {
label: labelData,
};
2018-05-09 12:01:36 +05:30
mock.onPost(expectedUrl).reply(config => {
2018-03-27 19:54:05 +05:30
expect(config.data).toBe(JSON.stringify(expectedData));
2018-05-09 12:01:36 +05:30
return [
200,
{
name: 'test',
},
];
2018-03-27 19:54:05 +05:30
});
2018-05-09 12:01:36 +05:30
Api.newLabel(namespace, undefined, labelData, response => {
2018-03-27 19:54:05 +05:30
expect(response.name).toBe('test');
done();
});
});
2017-09-10 17:25:29 +05:30
});
describe('groupProjects', () => {
2018-05-09 12:01:36 +05:30
it('fetches group projects', done => {
2017-09-10 17:25:29 +05:30
const groupId = '123456';
const query = 'dummy query';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/projects.json`;
2018-05-09 12:01:36 +05:30
mock.onGet(expectedUrl).reply(200, [
{
name: 'test',
},
]);
2017-09-10 17:25:29 +05:30
2018-11-08 19:23:39 +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();
});
});
});
2018-12-05 23:21:45 +05:30
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}/${encodeURIComponent(
templateKey,
)}`;
2018-03-17 18:26:18 +05:30
mock.onGet(expectedUrl).reply(200, 'test');
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +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();
});
});
});
2018-12-05 23:21:45 +05:30
describe('projectTemplates', () => {
it('fetches a list of templates', done => {
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/gitlab-org%2Fgitlab-ce/templates/licenses`;
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
mock.onGet(expectedUrl).reply(200, 'test');
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +05:30
Api.projectTemplates('gitlab-org/gitlab-ce', 'licenses', {}, response => {
2018-03-17 18:26:18 +05:30
expect(response).toBe('test');
2017-09-10 17:25:29 +05:30
done();
});
});
});
2018-12-05 23:21:45 +05:30
describe('projectTemplate', () => {
it('fetches a single template', done => {
const data = { unused: 'option' };
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/gitlab-org%2Fgitlab-ce/templates/licenses/test%20license`;
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
mock.onGet(expectedUrl).reply(200, 'test');
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +05:30
Api.projectTemplate('gitlab-org/gitlab-ce', 'licenses', 'test license', data, response => {
2018-03-17 18:26:18 +05:30
expect(response).toBe('test');
2017-09-10 17:25:29 +05:30
done();
});
});
});
describe('users', () => {
2018-05-09 12:01:36 +05:30
it('fetches users', done => {
2017-09-10 17:25:29 +05:30
const query = 'dummy query';
const options = { unused: 'option' };
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users.json`;
2018-05-09 12:01:36 +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);
});
});
2018-11-08 19:23:39 +05:30
2019-02-15 15:39:39 +05:30
describe('user', () => {
it('fetches single user', done => {
const userId = '123456';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users/${userId}`;
mock.onGet(expectedUrl).reply(200, {
name: 'testuser',
});
Api.user(userId)
.then(({ data }) => {
expect(data.name).toBe('testuser');
})
.then(done)
.catch(done.fail);
});
});
2019-09-30 21:07:59 +05:30
describe('user counts', () => {
it('fetches single user counts', done => {
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/user_counts`;
mock.onGet(expectedUrl).reply(200, {
merge_requests: 4,
});
Api.userCounts()
.then(({ data }) => {
expect(data.merge_requests).toBe(4);
})
.then(done)
.catch(done.fail);
});
});
2019-02-15 15:39:39 +05:30
describe('user status', () => {
it('fetches single user status', done => {
const userId = '123456';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users/${userId}/status`;
mock.onGet(expectedUrl).reply(200, {
message: 'testmessage',
});
Api.userStatus(userId)
.then(({ data }) => {
expect(data.message).toBe('testmessage');
})
.then(done)
.catch(done.fail);
});
});
2018-11-08 19:23:39 +05:30
describe('commitPipelines', () => {
it('fetches pipelines for a given commit', done => {
const projectId = 'example/foobar';
const commitSha = 'abc123def';
const expectedUrl = `${dummyUrlRoot}/${projectId}/commit/${commitSha}/pipelines`;
mock.onGet(expectedUrl).reply(200, [
{
name: 'test',
},
]);
Api.commitPipelines(projectId, commitSha)
.then(({ data }) => {
expect(data.length).toBe(1);
expect(data[0].name).toBe('test');
})
.then(done)
.catch(done.fail);
});
});
describe('createBranch', () => {
it('creates new branch', done => {
const ref = 'master';
const branch = 'new-branch-name';
const dummyProjectPath = 'gitlab-org/gitlab-ce';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${encodeURIComponent(
dummyProjectPath,
)}/repository/branches`;
2019-09-04 21:01:54 +05:30
jest.spyOn(axios, 'post');
2018-11-08 19:23:39 +05:30
mock.onPost(expectedUrl).replyOnce(200, {
name: branch,
});
Api.createBranch(dummyProjectPath, { ref, branch })
.then(({ data }) => {
expect(data.name).toBe(branch);
expect(axios.post).toHaveBeenCalledWith(expectedUrl, { ref, branch });
})
.then(done)
.catch(done.fail);
});
});
2019-09-30 21:07:59 +05:30
describe('projectForks', () => {
it('gets forked projects', done => {
const dummyProjectPath = 'gitlab-org/gitlab-ce';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${encodeURIComponent(
dummyProjectPath,
)}/forks`;
jest.spyOn(axios, 'get');
mock.onGet(expectedUrl).replyOnce(200, ['fork']);
Api.projectForks(dummyProjectPath, { visibility: 'private' })
.then(({ data }) => {
expect(data).toEqual(['fork']);
expect(axios.get).toHaveBeenCalledWith(expectedUrl, {
params: { visibility: 'private' },
});
})
.then(done)
.catch(done.fail);
});
});
2017-09-10 17:25:29 +05:30
});