Changeset View
Changeset View
Standalone View
Standalone View
cypress/integration/origin-save.spec.js
/** | /** | ||||
* Copyright (C) 2019 The Software Heritage developers | * Copyright (C) 2019 The Software Heritage developers | ||||
* See the AUTHORS file at the top-level directory of this distribution | * See the AUTHORS file at the top-level directory of this distribution | ||||
* License: GNU Affero General Public License version 3, or any later version | * License: GNU Affero General Public License version 3, or any later version | ||||
* See top-level LICENSE file for more information | * See top-level LICENSE file for more information | ||||
*/ | */ | ||||
let url; | let url; | ||||
let origin; | let origin; | ||||
const saveCodeMsg = { | const saveCodeMsg = { | ||||
'success': 'The "save code now" request has been accepted and will be processed as soon as possible.', | 'success': 'The "save code now" request has been accepted and will be processed as soon as possible.', | ||||
'warning': 'The "save code now" request has been put in pending state and may be accepted for processing after manual review.', | 'warning': 'The "save code now" request has been put in pending state and may be accepted for processing after manual review.', | ||||
'rejected': 'The "save code now" request has been rejected because the provided origin url is blacklisted.', | 'rejected': 'The "save code now" request has been rejected because the provided origin url is blacklisted.', | ||||
'rateLimit': 'The rate limit for "save code now" requests has been reached. Please try again later.', | 'rateLimit': 'The rate limit for "save code now" requests has been reached. Please try again later.', | ||||
'unknownErr': 'An unexpected error happened when submitting the "save code now request' | 'unknownError': 'An unexpected error happened when submitting the "save code now request', | ||||
vlorentz: Nitpick: rename `unknownErr` to `unknownError` for consistency | |||||
'csrfError': 'CSRF Failed: Referrer checking failed - no Referrer.' | |||||
}; | }; | ||||
function makeOriginSaveRequest(originType, originUrl) { | function makeOriginSaveRequest(originType, originUrl) { | ||||
cy.get('#swh-input-visit-type') | cy.get('#swh-input-visit-type') | ||||
.select(originType) | .select(originType) | ||||
.get('#swh-input-origin-url') | .get('#swh-input-origin-url') | ||||
.type(originUrl) | .type(originUrl) | ||||
.get('#swh-save-origin-form') | .get('#swh-save-origin-form') | ||||
.submit(); | .submit(); | ||||
} | } | ||||
function checkAlertVisible(alertType, msg) { | function checkAlertVisible(alertType, msg) { | ||||
cy.get('#swh-origin-save-request-status') | cy.get('#swh-origin-save-request-status') | ||||
.should('be.visible') | .should('be.visible') | ||||
.find(`.alert-${alertType}`) | .find(`.alert-${alertType}`) | ||||
.should('be.visible') | .should('be.visible') | ||||
.and('contain', msg); | .and('contain', msg); | ||||
} | } | ||||
// Stub requests to save an origin | // Stub requests to save an origin | ||||
function stubSaveRequest(requestUrl, objectType, status, originUrl, taskStatus, responseStatus = 200) { | function stubSaveRequest(requestUrl, objectType, status, originUrl, taskStatus, | ||||
responseStatus = 200, errorMessage = '') { | |||||
let response; | |||||
if (responseStatus !== 200 && errorMessage) { | |||||
response = {'detail': errorMessage}; | |||||
} else { | |||||
response = genOriginSaveResponse(objectType, status, originUrl, Date().toString(), taskStatus); | |||||
} | |||||
cy.route({ | cy.route({ | ||||
method: 'POST', | method: 'POST', | ||||
status: responseStatus, | status: responseStatus, | ||||
url: requestUrl, | url: requestUrl, | ||||
response: genOriginSaveResponse(objectType, status, originUrl, Date().toString(), taskStatus) | response: response | ||||
}).as('saveRequest'); | }).as('saveRequest'); | ||||
} | } | ||||
// Mocks API response : /save/(:object_type)/(:origin_url) | // Mocks API response : /save/(:object_type)/(:origin_url) | ||||
// object_type : {'git', 'hg', 'svn'} | // object_type : {'git', 'hg', 'svn'} | ||||
function genOriginSaveResponse(objectType, saveRequestStatus, originUrl, saveRequestDate, saveTaskStatus) { | function genOriginSaveResponse(objectType, saveRequestStatus, originUrl, saveRequestDate, saveTaskStatus) { | ||||
return { | return { | ||||
'visit_type': objectType, | 'visit_type': objectType, | ||||
Show All 35 Lines | it('should display warning message when pending', function() { | ||||
makeOriginSaveRequest(origin.type, origin.url); | makeOriginSaveRequest(origin.type, origin.url); | ||||
cy.wait('@saveRequest').then(() => { | cy.wait('@saveRequest').then(() => { | ||||
checkAlertVisible('warning', saveCodeMsg['warning']); | checkAlertVisible('warning', saveCodeMsg['warning']); | ||||
}); | }); | ||||
}); | }); | ||||
it('should show error when csrf validation failed (status: 403)', function() { | |||||
stubSaveRequest(this.originSaveUrl, origin.type, 'rejected', | |||||
origin.url, 'not created', 403, saveCodeMsg['csrfError']); | |||||
makeOriginSaveRequest(origin.type, origin.url); | |||||
cy.wait('@saveRequest').then(() => { | |||||
checkAlertVisible('danger', saveCodeMsg['csrfError']); | |||||
}); | |||||
}); | |||||
it('should show error when origin is rejected (status: 403)', function() { | it('should show error when origin is rejected (status: 403)', function() { | ||||
stubSaveRequest(this.originSaveUrl, origin.type, 'rejected', | stubSaveRequest(this.originSaveUrl, origin.type, 'rejected', | ||||
origin.url, 'not created', 403); | origin.url, 'not created', 403, saveCodeMsg['rejected']); | ||||
makeOriginSaveRequest(origin.type, origin.url); | makeOriginSaveRequest(origin.type, origin.url); | ||||
cy.wait('@saveRequest').then(() => { | cy.wait('@saveRequest').then(() => { | ||||
checkAlertVisible('danger', saveCodeMsg['rejected']); | checkAlertVisible('danger', saveCodeMsg['rejected']); | ||||
}); | }); | ||||
}); | }); | ||||
Show All 11 Lines | describe('Origin Save Tests', function() { | ||||
it('should show error when unknown error occurs (status other than 200, 403, 429)', function() { | it('should show error when unknown error occurs (status other than 200, 403, 429)', function() { | ||||
stubSaveRequest(this.originSaveUrl, origin.type, 'Error', | stubSaveRequest(this.originSaveUrl, origin.type, 'Error', | ||||
origin.url, 'not created', 406); | origin.url, 'not created', 406); | ||||
makeOriginSaveRequest(origin.type, origin.url); | makeOriginSaveRequest(origin.type, origin.url); | ||||
cy.wait('@saveRequest').then(() => { | cy.wait('@saveRequest').then(() => { | ||||
checkAlertVisible('danger', saveCodeMsg['unknownErr']); | checkAlertVisible('danger', saveCodeMsg['unknownError']); | ||||
}); | }); | ||||
}); | }); | ||||
}); | }); |
Nitpick: rename unknownErr to unknownError for consistency