diff --git a/cypress/integration/origin-save.spec.js b/cypress/integration/origin-save.spec.js new file mode 100644 --- /dev/null +++ b/cypress/integration/origin-save.spec.js @@ -0,0 +1,100 @@ +/** + * Copyright (C) 2019 The Software Heritage developers + * See the AUTHORS file at the top-level directory of this distribution + * License: GNU Affero General Public License version 3, or any later version + * See top-level LICENSE file for more information + */ + +let url; +let origin; + +function makeOriginSaveRequest(originType, originUrl) { + cy.get('#swh-input-origin-type') + .select(originType) + .get('#swh-input-origin-url') + .type(originUrl) + .get('#swh-save-origin-form') + .submit(); +} + +function checkAlertVisible(alertType) { + cy.get('#swh-origin-save-request-status') + .should('be.visible') + .find(`.alert-${alertType}`) + .should('be.visible'); +} + +function stubSaveRequest(requestUrl, objectType, status, originUrl, taskStatus, responseStatus = 200) { + cy.route({ + method: 'POST', + status: responseStatus, + url: requestUrl, + response: genOriginSaveResponse(objectType, status, originUrl, Date().toString(), taskStatus) + }).as('saveRequest'); +} + +// Mocks API response : /save/(:object_type)/(:origin_url) +// object_type : {'git', 'hg', 'svn'} +function genOriginSaveResponse(objectType, saveRequestStatus, originUrl, saveRequestDate, saveTaskStatus) { + return { + 'origin_type': objectType, + 'save_request_status': saveRequestStatus, + 'origin_url': originUrl, + 'id': 1, + 'save_request_date': saveRequestDate, + 'save_task_status': saveTaskStatus, + 'visit_date': null + }; +}; + +describe('Origin Save Tests', function() { + before(function() { + url = this.Urls.origin_save(); + origin = this.origin[0]; + this.originSaveUrl = this.Urls.origin_save_request(origin.type, origin.url); + }); + + beforeEach(function() { + cy.server(); + }); + + it('should display accepted message when accepted', function() { + cy.visit(url); + + stubSaveRequest(this.originSaveUrl, origin.type, 'accepted', + origin.url, 'not yet scheduled'); + + makeOriginSaveRequest(origin.type, origin.url); + + cy.wait('@saveRequest').then(() => { + checkAlertVisible('success'); + }); + }); + + it('should display warning message when pending', function() { + cy.visit(url); + + stubSaveRequest(this.originSaveUrl, origin.type, 'pending', + origin.url, 'not created'); + + makeOriginSaveRequest(origin.type, origin.url); + + cy.wait('@saveRequest').then(() => { + checkAlertVisible('warning'); + }); + }); + + it('should show error when origin is rejected', function() { + cy.visit(url); + + stubSaveRequest(this.originSaveUrl, origin.type, 'rejected', + origin.url, 'not created', 403); + + makeOriginSaveRequest(origin.type, origin.url); + + cy.wait('@saveRequest').then(() => { + checkAlertVisible('danger'); + }); + }); + +}); diff --git a/cypress/support/index.js b/cypress/support/index.js --- a/cypress/support/index.js +++ b/cypress/support/index.js @@ -16,6 +16,7 @@ before(function() { this.unarchivedRepo = { url: 'https://github.com/SoftwareHeritage/swh-web', + type: 'git', revision: '7bf1b2f489f16253527807baead7957ca9e8adde', snapshot: 'd9829223095de4bb529790de8ba4e4813e38672d', rootDirectory: '7d887d96c0047a77e2e8c4ee9bb1528463677663', @@ -26,6 +27,7 @@ this.origin = [{ url: 'https://github.com/memononen/libtess2', + type: 'git', content: [{ path: 'Source/tess.h' }, { @@ -39,6 +41,7 @@ invalidSubDir: 'Source1' }, { url: 'https://github.com/wcoder/highlightjs-line-numbers.js', + type: 'git', content: [], directory: [], revisions: ['1c480a4573d2a003fc2630c21c2b25829de49972']