Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: fix hardcoded embargo date #750

Merged
merged 1 commit into from
Jan 18, 2022
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
3 changes: 2 additions & 1 deletion sonar/modules/documents/dojson/rerodoc/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions tests/api/test_api_simple_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')]

Expand All @@ -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(
Expand All @@ -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')
}
21 changes: 15 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import copy
import os
import tempfile
from datetime import date
from io import BytesIO

import pytest
Expand All @@ -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."""
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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'] = {
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/deposits/test_deposits_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}'
Expand Down Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions tests/ui/documents/test_dc_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'] == []

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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'] == []
Expand Down Expand Up @@ -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']
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/documents/test_documents_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand Down
31 changes: 16 additions & 15 deletions tests/ui/documents/test_documents_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]) == {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]))
Expand Down