Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions hepdata/modules/doi_banner/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def resolve_doi_data(doi):
:return: redirect to HEPData record (or 404 if it doesn't exist)
"""
matching = get_records_matching_field('doi', doi, source={"includes": ['inspire_id']})
if matching.get('hits').get('total') > 0:
if matching.get('hits').get('total').get('value') > 0:
_returned = matching.get('hits').get('hits')[0].get('_source').get('inspire_id')
return redirect('/record/ins{0}'.format(_returned))
return abort(404)
Expand All @@ -43,8 +43,7 @@ def get_doi_banner(doi):
"""

matching = get_records_matching_field('doi', doi, source={"includes": ['inspire_id']})
if matching.get('hits').get('total') > 0:
print(matching)
if matching.get('hits').get('total').get('value') > 0:
return send_file(os.path.join(base_dir, 'static/img/hepdata-doi-banner.png'))
else:
return send_file(os.path.join(base_dir, 'static/img/1px.png'))
2 changes: 1 addition & 1 deletion hepdata/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
and parsed by ``setup.py``.
"""

__version__ = "0.9.4dev20260803"
__version__ = "0.9.4dev20260806"
81 changes: 81 additions & 0 deletions tests/doi_banner_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# coding=utf-8

import os
from unittest.mock import patch

from hepdata.modules.doi_banner import views as doi_banner_views


def _mock_search_result(total, inspire_id='1245023'):
hits = []
if total > 0:
hits = [{'_source': {'inspire_id': inspire_id}}]

return {
'hits': {
'total': {'value': total},
'hits': hits
}
}


def test_resolve_doi_data_redirects_to_record(client):
doi = '10.1093/ptep/ptt097'

with patch('hepdata.modules.doi_banner.views.get_records_matching_field') as mock_search:
mock_search.return_value = _mock_search_result(total=1, inspire_id='1245023')

response = client.get(f'/doidata/{doi}', follow_redirects=False)

assert response.status_code == 302
assert response.headers['Location'] == '/record/ins1245023'
mock_search.assert_called_once_with('doi', doi, source={"includes": ['inspire_id']})


def test_resolve_doi_data_returns_404_when_not_found(client):
doi = '10.1093/ptep/ptt404'

with patch('hepdata.modules.doi_banner.views.get_records_matching_field') as mock_search:
mock_search.return_value = _mock_search_result(total=0)

response = client.get(f'/doidata/{doi}')

assert response.status_code == 404
mock_search.assert_called_once_with('doi', doi, source={"includes": ['inspire_id']})


def test_get_doi_banner_returns_hepdata_banner_if_record_exists(client):
doi = '10.1093/ptep/ptt097'

with patch('hepdata.modules.doi_banner.views.get_records_matching_field') as mock_search:
mock_search.return_value = _mock_search_result(total=1)

response = client.get(f'/doibanner/{doi}')

expected_path = os.path.join(doi_banner_views.base_dir, 'static/img/hepdata-doi-banner.png')
with open(expected_path, 'rb') as banner:
expected_content = banner.read()

assert response.status_code == 200
assert response.mimetype == 'image/png'
assert response.data == expected_content
mock_search.assert_called_once_with('doi', doi, source={"includes": ['inspire_id']})


def test_get_doi_banner_returns_1px_if_record_does_not_exist(client):
doi = '10.1093/ptep/ptt404'

with patch('hepdata.modules.doi_banner.views.get_records_matching_field') as mock_search:
mock_search.return_value = _mock_search_result(total=0)

response = client.get(f'/doibanner/{doi}')

expected_path = os.path.join(doi_banner_views.base_dir, 'static/img/1px.png')
with open(expected_path, 'rb') as one_px:
expected_content = one_px.read()

assert response.status_code == 200
assert response.mimetype == 'image/png'
assert response.data == expected_content
mock_search.assert_called_once_with('doi', doi, source={"includes": ['inspire_id']})

Loading