diff options
Diffstat (limited to 'spec/frontend/api_spec.js')
-rw-r--r-- | spec/frontend/api_spec.js | 441 |
1 files changed, 364 insertions, 77 deletions
diff --git a/spec/frontend/api_spec.js b/spec/frontend/api_spec.js index c94637e04af..4f4de62c229 100644 --- a/spec/frontend/api_spec.js +++ b/spec/frontend/api_spec.js @@ -1,6 +1,7 @@ import MockAdapter from 'axios-mock-adapter'; import axios from '~/lib/utils/axios_utils'; import Api from '~/api'; +import httpStatus from '~/lib/utils/http_status'; describe('Api', () => { const dummyApiVersion = 'v3000'; @@ -57,7 +58,7 @@ describe('Api', () => { it('fetch all group packages', () => { const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/packages`; jest.spyOn(axios, 'get'); - mock.onGet(expectedUrl).replyOnce(200, apiResponse); + mock.onGet(expectedUrl).replyOnce(httpStatus.OK, apiResponse); return Api.groupPackages(groupId).then(({ data }) => { expect(data).toEqual(apiResponse); @@ -70,7 +71,7 @@ describe('Api', () => { it('fetch all project packages', () => { const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/packages`; jest.spyOn(axios, 'get'); - mock.onGet(expectedUrl).replyOnce(200, apiResponse); + mock.onGet(expectedUrl).replyOnce(httpStatus.OK, apiResponse); return Api.projectPackages(projectId).then(({ data }) => { expect(data).toEqual(apiResponse); @@ -92,7 +93,7 @@ describe('Api', () => { const expectedUrl = `foo`; jest.spyOn(Api, 'buildProjectPackageUrl').mockReturnValue(expectedUrl); jest.spyOn(axios, 'get'); - mock.onGet(expectedUrl).replyOnce(200, apiResponse); + mock.onGet(expectedUrl).replyOnce(httpStatus.OK, apiResponse); return Api.projectPackage(projectId, packageId).then(({ data }) => { expect(data).toEqual(apiResponse); @@ -107,7 +108,7 @@ describe('Api', () => { jest.spyOn(Api, 'buildProjectPackageUrl').mockReturnValue(expectedUrl); jest.spyOn(axios, 'delete'); - mock.onDelete(expectedUrl).replyOnce(200, true); + mock.onDelete(expectedUrl).replyOnce(httpStatus.OK, true); return Api.deleteProjectPackage(projectId, packageId).then(({ data }) => { expect(data).toEqual(true); @@ -121,7 +122,7 @@ describe('Api', () => { it('fetches a group', done => { const groupId = '123456'; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}`; - mock.onGet(expectedUrl).reply(200, { + mock.onGet(expectedUrl).reply(httpStatus.OK, { name: 'test', }); @@ -137,7 +138,7 @@ describe('Api', () => { const groupId = '54321'; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/members`; const expectedData = [{ id: 7 }]; - mock.onGet(expectedUrl).reply(200, expectedData); + mock.onGet(expectedUrl).reply(httpStatus.OK, expectedData); Api.groupMembers(groupId) .then(({ data }) => { @@ -148,12 +149,42 @@ describe('Api', () => { }); }); + describe('groupMilestones', () => { + it('fetches group milestones', done => { + const groupId = '16'; + const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/milestones`; + const expectedData = [ + { + id: 12, + iid: 3, + group_id: 16, + title: '10.0', + description: 'Version', + due_date: '2013-11-29', + start_date: '2013-11-10', + state: 'active', + updated_at: '2013-10-02T09:24:18Z', + created_at: '2013-10-02T09:24:18Z', + web_url: 'https://gitlab.com/groups/gitlab-org/-/milestones/42', + }, + ]; + mock.onGet(expectedUrl).reply(httpStatus.OK, expectedData); + + Api.groupMilestones(groupId) + .then(({ data }) => { + expect(data).toEqual(expectedData); + }) + .then(done) + .catch(done.fail); + }); + }); + describe('groups', () => { it('fetches groups', done => { const query = 'dummy query'; const options = { unused: 'option' }; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups.json`; - mock.onGet(expectedUrl).reply(200, [ + mock.onGet(expectedUrl).reply(httpStatus.OK, [ { name: 'test', }, @@ -171,7 +202,7 @@ describe('Api', () => { it('fetches namespaces', done => { const query = 'dummy query'; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/namespaces.json`; - mock.onGet(expectedUrl).reply(200, [ + mock.onGet(expectedUrl).reply(httpStatus.OK, [ { name: 'test', }, @@ -191,7 +222,7 @@ describe('Api', () => { const options = { unused: 'option' }; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects.json`; window.gon.current_user_id = 1; - mock.onGet(expectedUrl).reply(200, [ + mock.onGet(expectedUrl).reply(httpStatus.OK, [ { name: 'test', }, @@ -208,7 +239,7 @@ describe('Api', () => { const query = 'dummy query'; const options = { unused: 'option' }; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects.json`; - mock.onGet(expectedUrl).reply(200, [ + mock.onGet(expectedUrl).reply(httpStatus.OK, [ { name: 'test', }, @@ -226,7 +257,7 @@ describe('Api', () => { it('update a project with the given payload', done => { const projectPath = 'foo'; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}`; - mock.onPut(expectedUrl).reply(200, { foo: 'bar' }); + mock.onPut(expectedUrl).reply(httpStatus.OK, { foo: 'bar' }); Api.updateProject(projectPath, { foo: 'bar' }) .then(({ data }) => { @@ -243,7 +274,7 @@ describe('Api', () => { const options = { unused: 'option' }; const projectPath = 'gitlab-org%2Fgitlab-ce'; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/users`; - mock.onGet(expectedUrl).reply(200, [ + mock.onGet(expectedUrl).reply(httpStatus.OK, [ { name: 'test', }, @@ -265,7 +296,7 @@ describe('Api', () => { it('fetches all merge requests for a project', done => { const mockData = [{ source_branch: 'foo' }, { source_branch: 'bar' }]; - mock.onGet(expectedUrl).reply(200, mockData); + mock.onGet(expectedUrl).reply(httpStatus.OK, mockData); Api.projectMergeRequests(projectPath) .then(({ data }) => { expect(data.length).toEqual(2); @@ -281,7 +312,7 @@ describe('Api', () => { source_branch: 'bar', }; const mockData = [{ source_branch: 'bar' }]; - mock.onGet(expectedUrl, { params }).reply(200, mockData); + mock.onGet(expectedUrl, { params }).reply(httpStatus.OK, mockData); Api.projectMergeRequests(projectPath, params) .then(({ data }) => { @@ -298,7 +329,7 @@ describe('Api', () => { const projectPath = 'abc'; const mergeRequestId = '123456'; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}`; - mock.onGet(expectedUrl).reply(200, { + mock.onGet(expectedUrl).reply(httpStatus.OK, { title: 'test', }); @@ -316,7 +347,7 @@ describe('Api', () => { const projectPath = 'abc'; const mergeRequestId = '123456'; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}/changes`; - mock.onGet(expectedUrl).reply(200, { + mock.onGet(expectedUrl).reply(httpStatus.OK, { title: 'test', }); @@ -334,7 +365,7 @@ describe('Api', () => { const projectPath = 'abc'; const mergeRequestId = '123456'; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}/versions`; - mock.onGet(expectedUrl).reply(200, [ + mock.onGet(expectedUrl).reply(httpStatus.OK, [ { id: 123, }, @@ -356,7 +387,7 @@ describe('Api', () => { const params = { scope: 'active' }; const mockData = [{ id: 4 }]; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/runners`; - mock.onGet(expectedUrl, { params }).reply(200, mockData); + mock.onGet(expectedUrl, { params }).reply(httpStatus.OK, mockData); Api.projectRunners(projectPath, { params }) .then(({ data }) => { @@ -380,7 +411,7 @@ describe('Api', () => { expect(config.data).toBe(JSON.stringify(expectedData)); return [ - 200, + httpStatus.OK, { name: 'test', }, @@ -404,7 +435,7 @@ describe('Api', () => { expect(config.data).toBe(JSON.stringify(expectedData)); return [ - 200, + httpStatus.OK, { name: 'test', }, @@ -423,7 +454,7 @@ describe('Api', () => { const groupId = '123456'; const query = 'dummy query'; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/projects.json`; - mock.onGet(expectedUrl).reply(200, [ + mock.onGet(expectedUrl).reply(httpStatus.OK, [ { name: 'test', }, @@ -445,7 +476,7 @@ describe('Api', () => { )}/repository/commits/${sha}`; it('fetches a single commit', () => { - mock.onGet(expectedUrl).reply(200, { id: sha }); + mock.onGet(expectedUrl).reply(httpStatus.OK, { id: sha }); return Api.commit(projectId, sha).then(({ data: commit }) => { expect(commit.id).toBe(sha); @@ -453,7 +484,7 @@ describe('Api', () => { }); it('fetches a single commit without stats', () => { - mock.onGet(expectedUrl, { params: { stats: false } }).reply(200, { id: sha }); + mock.onGet(expectedUrl, { params: { stats: false } }).reply(httpStatus.OK, { id: sha }); return Api.commit(projectId, sha, { stats: false }).then(({ data: commit }) => { expect(commit.id).toBe(sha); @@ -470,7 +501,7 @@ describe('Api', () => { const expectedUrl = `${dummyUrlRoot}/${namespace}/${project}/templates/${templateType}/${encodeURIComponent( templateKey, )}`; - mock.onGet(expectedUrl).reply(200, 'test'); + mock.onGet(expectedUrl).reply(httpStatus.OK, 'test'); Api.issueTemplate(namespace, project, templateKey, templateType, (error, response) => { expect(response).toBe('test'); @@ -483,7 +514,7 @@ describe('Api', () => { it('fetches a list of templates', done => { const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/gitlab-org%2Fgitlab-ce/templates/licenses`; - mock.onGet(expectedUrl).reply(200, 'test'); + mock.onGet(expectedUrl).reply(httpStatus.OK, 'test'); Api.projectTemplates('gitlab-org/gitlab-ce', 'licenses', {}, response => { expect(response).toBe('test'); @@ -497,7 +528,7 @@ describe('Api', () => { const data = { unused: 'option' }; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/gitlab-org%2Fgitlab-ce/templates/licenses/test%20license`; - mock.onGet(expectedUrl).reply(200, 'test'); + mock.onGet(expectedUrl).reply(httpStatus.OK, 'test'); Api.projectTemplate('gitlab-org/gitlab-ce', 'licenses', 'test license', data, response => { expect(response).toBe('test'); @@ -511,7 +542,7 @@ describe('Api', () => { const query = 'dummy query'; const options = { unused: 'option' }; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users.json`; - mock.onGet(expectedUrl).reply(200, [ + mock.onGet(expectedUrl).reply(httpStatus.OK, [ { name: 'test', }, @@ -531,7 +562,7 @@ describe('Api', () => { it('fetches single user', done => { const userId = '123456'; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users/${userId}`; - mock.onGet(expectedUrl).reply(200, { + mock.onGet(expectedUrl).reply(httpStatus.OK, { name: 'testuser', }); @@ -547,7 +578,7 @@ describe('Api', () => { describe('user counts', () => { it('fetches single user counts', done => { const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/user_counts`; - mock.onGet(expectedUrl).reply(200, { + mock.onGet(expectedUrl).reply(httpStatus.OK, { merge_requests: 4, }); @@ -564,7 +595,7 @@ describe('Api', () => { it('fetches single user status', done => { const userId = '123456'; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users/${userId}/status`; - mock.onGet(expectedUrl).reply(200, { + mock.onGet(expectedUrl).reply(httpStatus.OK, { message: 'testmessage', }); @@ -583,7 +614,7 @@ describe('Api', () => { const options = { unused: 'option' }; const userId = '123456'; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users/${userId}/projects`; - mock.onGet(expectedUrl).reply(200, [ + mock.onGet(expectedUrl).reply(httpStatus.OK, [ { name: 'test', }, @@ -602,7 +633,7 @@ describe('Api', () => { const projectId = 'example/foobar'; const commitSha = 'abc123def'; const expectedUrl = `${dummyUrlRoot}/${projectId}/commit/${commitSha}/pipelines`; - mock.onGet(expectedUrl).reply(200, [ + mock.onGet(expectedUrl).reply(httpStatus.OK, [ { name: 'test', }, @@ -629,7 +660,7 @@ describe('Api', () => { jest.spyOn(axios, 'post'); - mock.onPost(expectedUrl).replyOnce(200, { + mock.onPost(expectedUrl).replyOnce(httpStatus.OK, { name: branch, }); @@ -652,7 +683,7 @@ describe('Api', () => { jest.spyOn(axios, 'get'); - mock.onGet(expectedUrl).replyOnce(200, ['fork']); + mock.onGet(expectedUrl).replyOnce(httpStatus.OK, ['fork']); Api.projectForks(dummyProjectPath, { visibility: 'private' }) .then(({ data }) => { @@ -666,62 +697,239 @@ describe('Api', () => { }); }); - describe('createReleaseLink', () => { + describe('createContextCommits', () => { + it('creates a new context commit', done => { + const projectPath = 'abc'; + const mergeRequestId = '123456'; + const commitsData = ['abcdefg']; + const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}/context_commits`; + const expectedData = { + commits: commitsData, + }; + + jest.spyOn(axios, 'post'); + + mock.onPost(expectedUrl).replyOnce(200, [ + { + id: 'abcdefghijklmnop', + short_id: 'abcdefg', + title: 'Dummy commit', + }, + ]); + + Api.createContextCommits(projectPath, mergeRequestId, expectedData) + .then(({ data }) => { + expect(data[0].title).toBe('Dummy commit'); + }) + .then(done) + .catch(done.fail); + }); + }); + + describe('allContextCommits', () => { + it('gets all context commits', done => { + const projectPath = 'abc'; + const mergeRequestId = '123456'; + const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}/context_commits`; + + jest.spyOn(axios, 'get'); + + mock + .onGet(expectedUrl) + .replyOnce(200, [{ id: 'abcdef', short_id: 'abcdefghi', title: 'Dummy commit title' }]); + + Api.allContextCommits(projectPath, mergeRequestId) + .then(({ data }) => { + expect(data[0].title).toBe('Dummy commit title'); + }) + .then(done) + .catch(done.fail); + }); + }); + + describe('removeContextCommits', () => { + it('removes context commits', done => { + const projectPath = 'abc'; + const mergeRequestId = '123456'; + const commitsData = ['abcdefg']; + const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}/context_commits`; + const expectedData = { + commits: commitsData, + }; + + jest.spyOn(axios, 'delete'); + + mock.onDelete(expectedUrl).replyOnce(204); + + Api.removeContextCommits(projectPath, mergeRequestId, expectedData) + .then(() => { + expect(axios.delete).toHaveBeenCalledWith(expectedUrl, { data: expectedData }); + }) + .then(done) + .catch(done.fail); + }); + }); + + describe('release-related methods', () => { const dummyProjectPath = 'gitlab-org/gitlab'; - const dummyReleaseTag = 'v1.3'; - const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${encodeURIComponent( + const dummyTagName = 'v1.3'; + const baseReleaseUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${encodeURIComponent( dummyProjectPath, - )}/releases/${dummyReleaseTag}/assets/links`; - const expectedLink = { - url: 'https://example.com', - name: 'An example link', - }; + )}/releases`; - describe('when the Release is successfully created', () => { - it('resolves the Promise', () => { - mock.onPost(expectedUrl, expectedLink).replyOnce(201); + describe('releases', () => { + const expectedUrl = baseReleaseUrl; - return Api.createReleaseLink(dummyProjectPath, dummyReleaseTag, expectedLink).then(() => { - expect(mock.history.post).toHaveLength(1); + describe('when releases are successfully returned', () => { + it('resolves the Promise', () => { + mock.onGet(expectedUrl).replyOnce(httpStatus.OK); + + return Api.releases(dummyProjectPath).then(() => { + expect(mock.history.get).toHaveLength(1); + }); + }); + }); + + describe('when an error occurs while fetching releases', () => { + it('rejects the Promise', () => { + mock.onGet(expectedUrl).replyOnce(httpStatus.INTERNAL_SERVER_ERROR); + + return Api.releases(dummyProjectPath).catch(() => { + expect(mock.history.get).toHaveLength(1); + }); }); }); }); - describe('when an error occurs while creating the Release', () => { - it('rejects the Promise', () => { - mock.onPost(expectedUrl, expectedLink).replyOnce(500); + describe('release', () => { + const expectedUrl = `${baseReleaseUrl}/${encodeURIComponent(dummyTagName)}`; - return Api.createReleaseLink(dummyProjectPath, dummyReleaseTag, expectedLink).catch(() => { - expect(mock.history.post).toHaveLength(1); + describe('when the release is successfully returned', () => { + it('resolves the Promise', () => { + mock.onGet(expectedUrl).replyOnce(httpStatus.OK); + + return Api.release(dummyProjectPath, dummyTagName).then(() => { + expect(mock.history.get).toHaveLength(1); + }); + }); + }); + + describe('when an error occurs while fetching the release', () => { + it('rejects the Promise', () => { + mock.onGet(expectedUrl).replyOnce(httpStatus.INTERNAL_SERVER_ERROR); + + return Api.release(dummyProjectPath, dummyTagName).catch(() => { + expect(mock.history.get).toHaveLength(1); + }); }); }); }); - }); - describe('deleteReleaseLink', () => { - const dummyProjectPath = 'gitlab-org/gitlab'; - const dummyReleaseTag = 'v1.3'; - const dummyLinkId = '4'; - const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${encodeURIComponent( - dummyProjectPath, - )}/releases/${dummyReleaseTag}/assets/links/${dummyLinkId}`; + describe('createRelease', () => { + const expectedUrl = baseReleaseUrl; - describe('when the Release is successfully deleted', () => { - it('resolves the Promise', () => { - mock.onDelete(expectedUrl).replyOnce(200); + const release = { + name: 'Version 1.0', + }; + + describe('when the release is successfully created', () => { + it('resolves the Promise', () => { + mock.onPost(expectedUrl, release).replyOnce(httpStatus.CREATED); + + return Api.createRelease(dummyProjectPath, release).then(() => { + expect(mock.history.post).toHaveLength(1); + }); + }); + }); - return Api.deleteReleaseLink(dummyProjectPath, dummyReleaseTag, dummyLinkId).then(() => { - expect(mock.history.delete).toHaveLength(1); + describe('when an error occurs while creating the release', () => { + it('rejects the Promise', () => { + mock.onPost(expectedUrl, release).replyOnce(httpStatus.INTERNAL_SERVER_ERROR); + + return Api.createRelease(dummyProjectPath, release).catch(() => { + expect(mock.history.post).toHaveLength(1); + }); }); }); }); - describe('when an error occurs while deleting the Release', () => { - it('rejects the Promise', () => { - mock.onDelete(expectedUrl).replyOnce(500); + describe('updateRelease', () => { + const expectedUrl = `${baseReleaseUrl}/${encodeURIComponent(dummyTagName)}`; + + const release = { + name: 'Version 1.0', + }; + + describe('when the release is successfully updated', () => { + it('resolves the Promise', () => { + mock.onPut(expectedUrl, release).replyOnce(httpStatus.OK); + + return Api.updateRelease(dummyProjectPath, dummyTagName, release).then(() => { + expect(mock.history.put).toHaveLength(1); + }); + }); + }); - return Api.deleteReleaseLink(dummyProjectPath, dummyReleaseTag, dummyLinkId).catch(() => { - expect(mock.history.delete).toHaveLength(1); + describe('when an error occurs while updating the release', () => { + it('rejects the Promise', () => { + mock.onPut(expectedUrl, release).replyOnce(httpStatus.INTERNAL_SERVER_ERROR); + + return Api.updateRelease(dummyProjectPath, dummyTagName, release).catch(() => { + expect(mock.history.put).toHaveLength(1); + }); + }); + }); + }); + + describe('createReleaseLink', () => { + const expectedUrl = `${baseReleaseUrl}/${dummyTagName}/assets/links`; + const expectedLink = { + url: 'https://example.com', + name: 'An example link', + }; + + describe('when the Release is successfully created', () => { + it('resolves the Promise', () => { + mock.onPost(expectedUrl, expectedLink).replyOnce(httpStatus.CREATED); + + return Api.createReleaseLink(dummyProjectPath, dummyTagName, expectedLink).then(() => { + expect(mock.history.post).toHaveLength(1); + }); + }); + }); + + describe('when an error occurs while creating the Release', () => { + it('rejects the Promise', () => { + mock.onPost(expectedUrl, expectedLink).replyOnce(httpStatus.INTERNAL_SERVER_ERROR); + + return Api.createReleaseLink(dummyProjectPath, dummyTagName, expectedLink).catch(() => { + expect(mock.history.post).toHaveLength(1); + }); + }); + }); + }); + + describe('deleteReleaseLink', () => { + const dummyLinkId = '4'; + const expectedUrl = `${baseReleaseUrl}/${dummyTagName}/assets/links/${dummyLinkId}`; + + describe('when the Release is successfully deleted', () => { + it('resolves the Promise', () => { + mock.onDelete(expectedUrl).replyOnce(httpStatus.OK); + + return Api.deleteReleaseLink(dummyProjectPath, dummyTagName, dummyLinkId).then(() => { + expect(mock.history.delete).toHaveLength(1); + }); + }); + }); + + describe('when an error occurs while deleting the Release', () => { + it('rejects the Promise', () => { + mock.onDelete(expectedUrl).replyOnce(httpStatus.INTERNAL_SERVER_ERROR); + + return Api.deleteReleaseLink(dummyProjectPath, dummyTagName, dummyLinkId).catch(() => { + expect(mock.history.delete).toHaveLength(1); + }); }); }); }); @@ -736,7 +944,7 @@ describe('Api', () => { describe('when the raw file is successfully fetched', () => { it('resolves the Promise', () => { - mock.onGet(expectedUrl).replyOnce(200); + mock.onGet(expectedUrl).replyOnce(httpStatus.OK); return Api.getRawFile(dummyProjectPath, dummyFilePath).then(() => { expect(mock.history.get).toHaveLength(1); @@ -746,7 +954,7 @@ describe('Api', () => { describe('when an error occurs while getting a raw file', () => { it('rejects the Promise', () => { - mock.onPost(expectedUrl).replyOnce(500); + mock.onPost(expectedUrl).replyOnce(httpStatus.INTERNAL_SERVER_ERROR); return Api.getRawFile(dummyProjectPath, dummyFilePath).catch(() => { expect(mock.history.get).toHaveLength(1); @@ -768,7 +976,7 @@ describe('Api', () => { describe('when the merge request is successfully created', () => { it('resolves the Promise', () => { - mock.onPost(expectedUrl, options).replyOnce(201); + mock.onPost(expectedUrl, options).replyOnce(httpStatus.CREATED); return Api.createProjectMergeRequest(dummyProjectPath, options).then(() => { expect(mock.history.post).toHaveLength(1); @@ -778,7 +986,7 @@ describe('Api', () => { describe('when an error occurs while getting a raw file', () => { it('rejects the Promise', () => { - mock.onPost(expectedUrl).replyOnce(500); + mock.onPost(expectedUrl).replyOnce(httpStatus.INTERNAL_SERVER_ERROR); return Api.createProjectMergeRequest(dummyProjectPath).catch(() => { expect(mock.history.post).toHaveLength(1); @@ -793,7 +1001,7 @@ describe('Api', () => { const issue = 1; const expectedArray = [1, 2, 3]; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/issues/${issue}`; - mock.onPut(expectedUrl).reply(200, { assigneeIds: expectedArray }); + mock.onPut(expectedUrl).reply(httpStatus.OK, { assigneeIds: expectedArray }); Api.updateIssue(projectId, issue, { assigneeIds: expectedArray }) .then(({ data }) => { @@ -810,7 +1018,7 @@ describe('Api', () => { const mergeRequest = 1; const expectedArray = [1, 2, 3]; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/merge_requests/${mergeRequest}`; - mock.onPut(expectedUrl).reply(200, { assigneeIds: expectedArray }); + mock.onPut(expectedUrl).reply(httpStatus.OK, { assigneeIds: expectedArray }); Api.updateMergeRequest(projectId, mergeRequest, { assigneeIds: expectedArray }) .then(({ data }) => { @@ -827,7 +1035,7 @@ describe('Api', () => { const options = { unused: 'option' }; const projectId = 8; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/repository/tags`; - mock.onGet(expectedUrl).reply(200, [ + mock.onGet(expectedUrl).reply(httpStatus.OK, [ { name: 'test', }, @@ -842,4 +1050,83 @@ describe('Api', () => { .catch(done.fail); }); }); + + describe('freezePeriods', () => { + it('fetches freezePeriods', () => { + const projectId = 8; + const freezePeriod = { + id: 3, + freeze_start: '5 4 * * *', + freeze_end: '5 9 * 8 *', + cron_timezone: 'America/New_York', + created_at: '2020-07-10T05:10:35.122Z', + updated_at: '2020-07-10T05:10:35.122Z', + }; + const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/freeze_periods`; + mock.onGet(expectedUrl).reply(httpStatus.OK, [freezePeriod]); + + return Api.freezePeriods(projectId).then(({ data }) => { + expect(data[0]).toStrictEqual(freezePeriod); + }); + }); + }); + + describe('createFreezePeriod', () => { + const projectId = 8; + const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/freeze_periods`; + const options = { + freeze_start: '* * * * *', + freeze_end: '* * * * *', + cron_timezone: 'America/Juneau', + }; + + const expectedResult = { + id: 10, + freeze_start: '* * * * *', + freeze_end: '* * * * *', + cron_timezone: 'America/Juneau', + created_at: '2020-07-11T07:04:50.153Z', + updated_at: '2020-07-11T07:04:50.153Z', + }; + + describe('when the freeze period is successfully created', () => { + it('resolves the Promise', () => { + mock.onPost(expectedUrl, options).replyOnce(httpStatus.CREATED, expectedResult); + + return Api.createFreezePeriod(projectId, options).then(({ data }) => { + expect(data).toStrictEqual(expectedResult); + }); + }); + }); + }); + + describe('createPipeline', () => { + it('creates new pipeline', () => { + const redirectUrl = 'ci-project/-/pipelines/95'; + const projectId = 8; + const postData = { + ref: 'tag-1', + variables: [ + { key: 'test_file', value: 'test_file_val', variable_type: 'file' }, + { key: 'test_var', value: 'test_var_val', variable_type: 'env_var' }, + ], + }; + const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/pipeline`; + + jest.spyOn(axios, 'post'); + + mock.onPost(expectedUrl).replyOnce(httpStatus.OK, { + web_url: redirectUrl, + }); + + return Api.createPipeline(projectId, postData).then(({ data }) => { + expect(data.web_url).toBe(redirectUrl); + expect(axios.post).toHaveBeenCalledWith(expectedUrl, postData, { + headers: { + 'Content-Type': 'application/json', + }, + }); + }); + }); + }); }); |