diff --git a/CONTRIBUTORS b/CONTRIBUTORS --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -2,4 +2,5 @@ Ishan Bhanuka Kalpit Kothari Katrin Leinweber -Siddharth Ravikumar +Shankhadeep Dey +Siddharth Ravikumar \ No newline at end of file diff --git a/cypress/integration/origin-search.spec.js b/cypress/integration/origin-search.spec.js --- a/cypress/integration/origin-search.spec.js +++ b/cypress/integration/origin-search.spec.js @@ -1,5 +1,5 @@ /** - * Copyright (C) 2019 The Software Heritage developers + * Copyright (C) 2019-2020 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 @@ -65,12 +65,12 @@ .find('i') .should('have.class', 'fa-check') .and('have.attr', 'title', - 'Origin has at least one full visit by Software Heritage'); + 'Origin has at least one full visit by Software Heritage'); }); it('should show not found message when no repo matches', function() { searchShouldShowNotFound(nonExistentText, - 'No origins matching the search criteria were found.'); + 'No origins matching the search criteria were found.'); }); it('should add appropriate URL parameters', function() { @@ -100,6 +100,30 @@ }); }); + it('should not send request to the resolve endpoint', function() { + cy.server(); + + cy.route({ + method: 'GET', + url: `${this.Urls.api_1_resolve()}**`, + }).as('resolvePid'); + + cy.route({ + method: 'GET', + url: `${this.Urls.api_1_origin_search()}**`, + }).as('searchOrigin'); + + cy.get('#origins-url-patterns') + .type(origin.url); + cy.get('.swh-search-icon') + .click(); + + cy.wait('@searchOrigin'); + + cy.xhrShouldBeCalled('resolvePid', 0); + cy.xhrShouldBeCalled('searchOrigin', 1); + }); + context('Test pagination', function() { it('should not paginate if there are not many results', function() { // Setup search @@ -345,6 +369,31 @@ searchShouldRedirect(persistentId, redirectUrl); }); + + it('should not send request to the search endpoint', function() { + cy.server(); + const persistentId = `swh:1:rev:${origin.revisions[0]}`; + + cy.route({ + method: 'GET', + url: `${this.Urls.api_1_resolve()}**`, + }).as('resolvePid'); + + cy.route({ + method: 'GET', + url: `${this.Urls.api_1_origin_search()}**`, + }).as('searchOrigin'); + + cy.get('#origins-url-patterns') + .type(persistentId); + cy.get('.swh-search-icon') + .click(); + + cy.wait('@resolvePid'); + + cy.xhrShouldBeCalled('resolvePid', 1); + cy.xhrShouldBeCalled('searchOrigin', 0); + }); }); context('Test invalid persistent ids', function() { @@ -377,4 +426,4 @@ }); }); -}); +}); \ No newline at end of file diff --git a/cypress/support/index.js b/cypress/support/index.js --- a/cypress/support/index.js +++ b/cypress/support/index.js @@ -11,6 +11,13 @@ screenshotOnRunFailure: false }); +Cypress.Commands.add('xhrShouldBeCalled', (alias, timesCalled) => { + expect( + cy.state('requests').filter(call => call.alias === alias), + `${alias} should have been called ${timesCalled} times` + ).to.have.length(timesCalled); +}); + before(function() { this.unarchivedRepo = { url: 'https://github.com/SoftwareHeritage/swh-web', diff --git a/swh/web/assets/src/bundles/browse/origin-search.js b/swh/web/assets/src/bundles/browse/origin-search.js --- a/swh/web/assets/src/bundles/browse/origin-search.js +++ b/swh/web/assets/src/bundles/browse/origin-search.js @@ -1,5 +1,5 @@ /** - * Copyright (C) 2018-2019 The Software Heritage developers + * Copyright (C) 2018-2020 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 @@ -138,19 +138,18 @@ $('#swh-no-result').hide(); let searchQueryText = $('#origins-url-patterns').val(); inSearch = true; - // first try to resolve a swh persistent identifier - let resolvePidUrl = Urls.api_1_resolve_swh_pid(searchQueryText); - fetch(resolvePidUrl) - .then(handleFetchError) - .then(response => response.json()) - .then(data => { - // pid has been successfully resolved, - // so redirect to browse page - window.location = data.browse_url; - }) - .catch(response => { - // pid resolving failed - if (searchQueryText.startsWith('swh:')) { + if (searchQueryText.startsWith('swh:')) { + // searchQueryText may be a PID so sending search queries to PID resolve endpoint + let resolvePidUrl = Urls.api_1_resolve_swh_pid(searchQueryText); + fetch(resolvePidUrl) + .then(handleFetchError) + .then(response => response.json()) + .then(data => { + // pid has been successfully resolved, + // so redirect to browse page + window.location = data.browse_url; + }) + .catch(response => { // display a useful error message if the input // looks like a swh pid response.json().then(data => { @@ -159,13 +158,14 @@ $('#swh-no-result').text(data.reason); $('#swh-no-result').show(); }); - } else { - // otherwise, proceed with origins search - $('#swh-origin-search-results').show(); - $('.swh-search-pagination').show(); - searchOriginsFirst(searchQueryText, limit); - } - }); + + }); + } else { + // otherwise, proceed with origins search + $('#swh-origin-search-results').show(); + $('.swh-search-pagination').show(); + searchOriginsFirst(searchQueryText, limit); + } } export function initOriginSearch() {