Skip to content
Snippets Groups Projects
Commit 911f82d2 authored by Shankhadeep Dey's avatar Shankhadeep Dey
Browse files

assets/origin-search: Resolve swh PID only when required

When a search query does not look like a PID, do not try to resolve it by
sending a request to the dedicated endpoint and directly search for origins.

Closes T1850
parent f0cbc527
No related branches found
No related tags found
1 merge request!241Resolve search query which does not look like PID
......@@ -2,4 +2,5 @@ Daniele Serafini
Ishan Bhanuka
Kalpit Kothari
Katrin Leinweber
Shankhadeep Dey
Siddharth Ravikumar
/**
* 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 @@ describe('Test origin-search', function() {
.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 @@ describe('Test origin-search', function() {
});
});
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 @@ describe('Test origin-search', function() {
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 @@ describe('Test origin-search', function() {
});
});
});
});
\ No newline at end of file
/**
* 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
......@@ -11,6 +11,13 @@ Cypress.Screenshot.defaults({
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',
......
/**
* 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 @@ function doSearch() {
$('#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 @@ function doSearch() {
$('#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() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment