Commit 04c56cb7 authored by Nicolò Maria Mezzopera's avatar Nicolò Maria Mezzopera Committed by Fatih Acet

Add project package api call to api.js

- paths and function in api.js
- unit tests
parent fd926738
...@@ -15,6 +15,8 @@ export default { ...@@ -15,6 +15,8 @@ export default {
'/:project_full_path/environments/:environment_id/pods/:pod_name/containers/logs.json', '/:project_full_path/environments/:environment_id/pods/:pod_name/containers/logs.json',
podLogsPathWithPodContainer: podLogsPathWithPodContainer:
'/:project_full_path/environments/:environment_id/pods/:pod_name/containers/:container_name/logs.json', '/:project_full_path/environments/:environment_id/pods/:pod_name/containers/:container_name/logs.json',
projectPackagesPath: '/api/:version/projects/:id/packages',
projectPackagePath: '/api/:version/projects/:id/packages/:package_id',
userSubscription(namespaceId) { userSubscription(namespaceId) {
const url = Api.buildUrl(this.subscriptionPath).replace(':id', encodeURIComponent(namespaceId)); const url = Api.buildUrl(this.subscriptionPath).replace(':id', encodeURIComponent(namespaceId));
...@@ -96,4 +98,25 @@ export default { ...@@ -96,4 +98,25 @@ export default {
} }
return axios.get(url); return axios.get(url);
}, },
projectPackages(id) {
const url = Api.buildUrl(this.projectPackagesPath).replace(':id', id);
return axios.get(url);
},
buildProjectPackageUrl(projectId, packageId) {
return Api.buildUrl(this.projectPackagePath)
.replace(':id', projectId)
.replace(':package_id', packageId);
},
projectPackage(projectId, packageId) {
const url = this.buildProjectPackageUrl(projectId, packageId);
return axios.get(url);
},
deleteProjectPackage(projectId, packageId) {
const url = this.buildProjectPackageUrl(projectId, packageId);
return axios.delete(url);
},
}; };
...@@ -212,4 +212,63 @@ describe('Api', () => { ...@@ -212,4 +212,63 @@ describe('Api', () => {
.catch(done.fail); .catch(done.fail);
}); });
}); });
describe('packages', () => {
const projectId = 'project_a';
const packageId = 'package_b';
describe('projectPackages', () => {
it('fetch all project packages', () => {
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/packages`;
const apiResponse = [{ id: 1, name: 'foo' }];
jest.spyOn(axios, 'get');
mock.onGet(expectedUrl).replyOnce(200, apiResponse);
return Api.projectPackages(projectId).then(({ data }) => {
expect(data).toEqual(apiResponse);
expect(axios.get).toHaveBeenCalledWith(expectedUrl);
});
});
});
describe('buildProjectPackageUrl', () => {
it('returns the right url', () => {
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/packages/${packageId}`;
const url = Api.buildProjectPackageUrl(projectId, packageId);
expect(url).toEqual(expectedUrl);
});
});
describe('projectPackage', () => {
it('fetch package details', () => {
const expectedUrl = `foo`;
const apiResponse = { id: 1, name: 'foo' };
jest.spyOn(Api, 'buildProjectPackageUrl').mockReturnValue(expectedUrl);
jest.spyOn(axios, 'get');
mock.onGet(expectedUrl).replyOnce(200, apiResponse);
return Api.projectPackage(projectId, packageId).then(({ data }) => {
expect(data).toEqual(apiResponse);
expect(axios.get).toHaveBeenCalledWith(expectedUrl);
});
});
});
describe('deleteProjectPackage', () => {
it('delete a package', () => {
const expectedUrl = `foo`;
const apiResponse = true;
jest.spyOn(Api, 'buildProjectPackageUrl').mockReturnValue(expectedUrl);
jest.spyOn(axios, 'delete');
mock.onDelete(expectedUrl).replyOnce(200, apiResponse);
return Api.deleteProjectPackage(projectId, packageId).then(({ data }) => {
expect(data).toEqual(apiResponse);
expect(axios.delete).toHaveBeenCalledWith(expectedUrl);
});
});
});
});
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment