diff --git a/sonar/modules/documents/dojson/rerodoc/model.py b/sonar/modules/documents/dojson/rerodoc/model.py index 8c0497db..76fa2ca4 100644 --- a/sonar/modules/documents/dojson/rerodoc/model.py +++ b/sonar/modules/documents/dojson/rerodoc/model.py @@ -27,7 +27,8 @@ from sonar.modules.collections.api import Record as CollectionRecord from sonar.modules.documents.dojson.rerodoc.overdo import Overdo from sonar.modules.organisations.api import OrganisationRecord -from sonar.modules.subdivisions.api import RecordSearch, Record as SubdivisionRecord +from sonar.modules.subdivisions.api import Record as SubdivisionRecord +from sonar.modules.subdivisions.api import RecordSearch from sonar.modules.utils import remove_trailing_punctuation overdo = Overdo() diff --git a/tests/api/test_api_simple_flow.py b/tests/api/test_api_simple_flow.py index 20f297c2..6e4e3f81 100644 --- a/tests/api/test_api_simple_flow.py +++ b/tests/api/test_api_simple_flow.py @@ -27,7 +27,7 @@ @mock.patch('invenio_records_rest.views.verify_record_permission', mock.MagicMock(return_value=VerifyRecordPermissionPatch)) -def test_simple_flow(client, document_json, admin): +def test_simple_flow(client, document_json, admin, embargo_date): """Test simple flow using REST API.""" headers = [('Content-Type', 'application/json')] @@ -46,7 +46,7 @@ def test_simple_flow(client, document_json, admin): 'value'] == 'Title of the document' -def test_add_files_restrictions(client, document_with_file, superuser): +def test_add_files_restrictions(client, document_with_file, superuser, embargo_date): """Test adding file restrictions before dumping object.""" login_user_via_session(client, email=superuser['email']) res = client.get( @@ -57,5 +57,5 @@ def test_add_files_restrictions(client, document_with_file, superuser): assert res.status_code == 200 assert res.json['metadata']['_files'][0]['restriction'] == { 'restricted': True, - 'date': '01/01/2022' + 'date': embargo_date.strftime('%d/%m/%Y') } diff --git a/tests/conftest.py b/tests/conftest.py index 10e08373..becb05a4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,6 +20,7 @@ import copy import os import tempfile +from datetime import date from io import BytesIO import pytest @@ -38,6 +39,13 @@ from sonar.proxies import sonar +@pytest.fixture(scope='module') +def embargo_date(): + """Embargo date in one year from now.""" + today = date.today() + return today.replace(year=today.year+1) + + @pytest.fixture(scope='function') def mock_ark(app, monkeypatch): """Mock for the ARK module.""" @@ -405,7 +413,7 @@ def document_json(app, db, bucket_location, organisation): @pytest.fixture() -def make_document(db, document_json, make_organisation, pdf_file): +def make_document(db, document_json, make_organisation, pdf_file, embargo_date): """Factory for creating document.""" def _make_document(organisation='org', with_file=False, pid=None): @@ -435,7 +443,7 @@ def _make_document(organisation='org', with_file=False, pid=None): order=1, access='coar:c_f1cf', restricted_outside_organisation=False, - embargo_date='2022-01-01') + embargo_date=embargo_date.isoformat()) record.commit() db.session.commit() @@ -614,7 +622,7 @@ def deposit_json(collection, subdivision): @pytest.fixture() -def make_deposit(db, deposit_json, bucket_location, pdf_file, make_user): +def make_deposit(db, deposit_json, bucket_location, pdf_file, make_user, embargo_date): """Factory for creating deposit.""" def _make_deposit(role='submitter', organisation=None): @@ -638,7 +646,7 @@ def _make_deposit(role='submitter', organisation=None): record.files['main.pdf']['category'] = 'main' record.files['main.pdf']['type'] = 'file' record.files['main.pdf']['embargo'] = True - record.files['main.pdf']['embargoDate'] = '2022-01-01' + record.files['main.pdf']['embargoDate'] = embargo_date.isoformat() record.files['main.pdf']['exceptInOrganisation'] = True record.files['additional.pdf'] = BytesIO(content) @@ -658,7 +666,8 @@ def _make_deposit(role='submitter', organisation=None): @pytest.fixture() -def deposit(app, db, user, pdf_file, bucket_location, deposit_json): +def deposit( + app, db, user, pdf_file, bucket_location, deposit_json, embargo_date): """Deposit fixture.""" json = copy.deepcopy(deposit_json) json['user'] = { @@ -675,7 +684,7 @@ def deposit(app, db, user, pdf_file, bucket_location, deposit_json): deposit.files['main.pdf']['category'] = 'main' deposit.files['main.pdf']['type'] = 'file' deposit.files['main.pdf']['embargo'] = True - deposit.files['main.pdf']['embargoDate'] = '2022-01-01' + deposit.files['main.pdf']['embargoDate'] = embargo_date.isoformat() deposit.files['main.pdf']['exceptInOrganisation'] = True deposit.files['additional.pdf'] = BytesIO(content) diff --git a/tests/ui/deposits/test_deposits_api.py b/tests/ui/deposits/test_deposits_api.py index 6c763b58..d9ea080b 100644 --- a/tests/ui/deposits/test_deposits_api.py +++ b/tests/ui/deposits/test_deposits_api.py @@ -21,7 +21,7 @@ def test_create_document(app, db, project, client, deposit, submitter, - subdivision): + subdivision, embargo_date): """Test create document based on it.""" submitter['subdivision'] = { '$ref': f'https://sonar.ch/api/subdivisions/{subdivision["pid"]}' @@ -199,7 +199,7 @@ def test_create_document(app, db, project, client, deposit, submitter, assert document.files['main.pdf']['access'] == 'coar:c_f1cf' assert document.files['main.pdf']['restricted_outside_organisation'] - assert document.files['main.pdf']['embargo_date'] == '2022-01-01' + assert document.files['main.pdf']['embargo_date'] == embargo_date.isoformat() assert len(document.files) == 6 # Test without affiliation diff --git a/tests/ui/documents/test_dc_schema.py b/tests/ui/documents/test_dc_schema.py index 005ea6b2..ede4065e 100644 --- a/tests/ui/documents/test_dc_schema.py +++ b/tests/ui/documents/test_dc_schema.py @@ -124,7 +124,7 @@ def test_creators(minimal_document, contributors): ] -def test_dates(app, minimal_document): +def test_dates(app, minimal_document, embargo_date): result = dc_v1.transform_record(minimal_document['pid'], minimal_document) assert result['dates'] == [] @@ -155,15 +155,17 @@ def test_dates(app, minimal_document): result = dc_v1.transform_record(minimal_document['pid'], minimal_document) assert result['dates'] == [] + iso_embargo_date = embargo_date.isoformat() with app.test_request_context() as req: req.request.args = {'view': 'global'} minimal_document.files['test.pdf']['type'] = 'file' minimal_document.files['test.pdf']['access'] = 'coar:c_f1cf' minimal_document.files['test.pdf']['restricted'] = 'full' - minimal_document.files['test.pdf']['embargo_date'] = '2022-01-01' + minimal_document.files['test.pdf']['embargo_date'] = iso_embargo_date result = dc_v1.transform_record(minimal_document['pid'], minimal_document) - assert result['dates'] == ['info:eu-repo/date/embargoEnd/2022-01-01'] + assert result['dates'] == [ + f'info:eu-repo/date/embargoEnd/{iso_embargo_date}'] def test_descriptions(minimal_document): @@ -308,7 +310,7 @@ def test_relations(minimal_document): ] -def test_rights(app, minimal_document): +def test_rights(app, minimal_document, embargo_date): """Test rights serialization.""" result = dc_v1.transform_record(minimal_document['pid'], minimal_document) assert result['rights'] == [] @@ -341,7 +343,8 @@ def test_rights(app, minimal_document): assert result['rights'] == ['info:eu-repo/semantics/restrictedAccess'] minimal_document.files['test.pdf']['access'] = 'coar:c_f1cf' - minimal_document.files['test.pdf']['embargo_date'] = '2022-01-01' + minimal_document.files['test.pdf'][ + 'embargo_date'] = embargo_date.isoformat() result = dc_v1.transform_record(minimal_document['pid'], minimal_document) assert result['rights'] == ['info:eu-repo/semantics/embargoedAccess'] diff --git a/tests/ui/documents/test_documents_api.py b/tests/ui/documents/test_documents_api.py index a199a253..d38faf5a 100644 --- a/tests/ui/documents/test_documents_api.py +++ b/tests/ui/documents/test_documents_api.py @@ -107,7 +107,7 @@ def test_get_documents_by_project(db, project, document): 'permalink'] == f'http://localhost/global/documents/{document["pid"]}' -def test_is_open_access(document): +def test_is_open_access(document, embargo_date): """Test if document is open access.""" assert not document.is_open_access() @@ -129,7 +129,7 @@ def test_is_open_access(document): # Embargo access with future date --> not open access document.files['test1.pdf']['access'] = 'coar:c_f1cf' - document.files['test1.pdf']['embargo_date'] = '2025-01-01' + document.files['test1.pdf']['embargo_date'] = embargo_date.isoformat() assert not document.is_open_access() # Embargo access with past date --> open access diff --git a/tests/ui/documents/test_documents_utils.py b/tests/ui/documents/test_documents_utils.py index 574a0467..4c305064 100644 --- a/tests/ui/documents/test_documents_utils.py +++ b/tests/ui/documents/test_documents_utils.py @@ -61,7 +61,8 @@ def test_publication_statement_text(): }) == '31.12.1990' -def test_get_file_restriction(app, organisation, admin, monkeypatch): +def test_get_file_restriction(app, organisation, admin, monkeypatch, + embargo_date): """Test if a file is restricted by embargo date and/or organisation.""" # No view arg, file is allowed assert utils.get_file_restriction({}, [organisation]) == { @@ -123,9 +124,9 @@ def test_get_file_restriction(app, organisation, admin, monkeypatch): assert utils.get_file_restriction( { 'access': 'coar:c_f1cf', - 'embargo_date': '2022-01-01' + 'embargo_date': embargo_date.isoformat() }, [organisation]) == { - 'date': '01/01/2022', + 'date': embargo_date.strftime('%d/%m/%Y'), 'restricted': True } @@ -134,9 +135,9 @@ def test_get_file_restriction(app, organisation, admin, monkeypatch): { 'access': 'coar:c_f1cf', 'restricted_outside_organisation': False, - 'embargo_date': '2022-01-01' + 'embargo_date': embargo_date.isoformat() }, [organisation]) == { - 'date': '01/01/2022', + 'date': embargo_date.strftime('%d/%m/%Y'), 'restricted': True } @@ -146,9 +147,9 @@ def test_get_file_restriction(app, organisation, admin, monkeypatch): { 'access': 'coar:c_f1cf', 'restricted_outside_organisation': True, - 'embargo_date': '2022-01-01' + 'embargo_date': embargo_date.isoformat() }, []) == { - 'date': '01/01/2022', + 'date': embargo_date.strftime('%d/%m/%Y'), 'restricted': True } @@ -158,9 +159,9 @@ def test_get_file_restriction(app, organisation, admin, monkeypatch): { 'access': 'coar:c_f1cf', 'restricted_outside_organisation': True, - 'embargo_date': '2022-01-01' + 'embargo_date': embargo_date.isoformat() }, []) == { - 'date': '01/01/2022', + 'date': embargo_date.strftime('%d/%m/%Y'), 'restricted': True } @@ -173,9 +174,9 @@ def test_get_file_restriction(app, organisation, admin, monkeypatch): { 'access': 'coar:c_f1cf', 'restricted_outside_organisation': True, - 'embargo_date': '2022-01-01' + 'embargo_date': embargo_date.isoformat() }, [organisation]) == { - 'date': '01/01/2022', + 'date': embargo_date.strftime('%d/%m/%Y'), 'restricted': True } @@ -188,7 +189,7 @@ def test_get_file_restriction(app, organisation, admin, monkeypatch): { 'access': 'coar:c_f1cf', 'restricted_outside_organisation': True, - 'embargo_date': '2022-01-01' + 'embargo_date': embargo_date.isoformat() }, [organisation]) == { 'date': None, 'restricted': False @@ -204,9 +205,9 @@ def test_get_file_restriction(app, organisation, admin, monkeypatch): { 'access': 'coar:c_f1cf', 'restricted_outside_organisation': True, - 'embargo_date': '2022-01-01' + 'embargo_date': embargo_date.isoformat() }, [organisation]) == { - 'date': '01/01/2022', + 'date': embargo_date.strftime('%d/%m/%Y'), 'restricted': True } @@ -217,7 +218,7 @@ def test_get_file_restriction(app, organisation, admin, monkeypatch): { 'access': 'coar:c_f1cf', 'restricted_outside_organisation': True, - 'embargo_date': '2022-01-01' + 'embargo_date': embargo_date.isoformat() }, [organisation]) == { 'date': None, 'restricted': False diff --git a/tests/ui/test_query.py b/tests/ui/test_query.py index 53575a04..39c59cd9 100644 --- a/tests/ui/test_query.py +++ b/tests/ui/test_query.py @@ -49,7 +49,7 @@ def test_get_operator_and_query_type(app): 'simple_query_string') -def test_open_access_filter(app, document): +def test_open_access_filter(app, document, embargo_date): """Test open access filter.""" # No filter search = Search(index='documents', using=current_search_client) @@ -91,7 +91,7 @@ def test_open_access_filter(app, document): # Files with document, access property is embargo access and date is in the # future --> not open access. document.files['test1.pdf']['access'] = 'coar:c_f1cf' - document.files['test1.pdf']['embargo_date'] = '2025-01-01' + document.files['test1.pdf']['embargo_date'] = embargo_date.isoformat() document.commit() document.reindex() search = search.query(and_term_filter('isOpenAccess')([True]))