Skip to content

[ENG-8185] Unit tests for Manual GUID and DOI #11175

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

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
28 changes: 27 additions & 1 deletion api_tests/preprints/views/test_preprint_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest
from django.utils import timezone
from waffle.testutils import override_switch
from waffle.testutils import override_switch, override_flag

from addons.github.models import GithubFile
from api.base.settings.defaults import API_BASE
Expand Down Expand Up @@ -351,6 +351,8 @@ def setUp(self):

self.user_two = AuthUserFactory()
self.url = f'/{API_BASE}preprints/'
self.manual_guid = 'abcde'
self.manual_doi = '10.70102/FK2osf.io/abcde'

def publish_preprint(self, preprint, user, expect_errors=False):
preprint_file = test_utils.create_test_preprint_file(preprint, user, 'coffee_manuscript.pdf')
Expand All @@ -362,6 +364,30 @@ def publish_preprint(self, preprint, user, expect_errors=False):
)
return res

@property
def manual_guid_payload(self):
return {
'manual_doi': self.manual_doi,
'manual_guid': self.manual_guid,
}

def test_fail_create_prerprint_with_manual_guid(self):
public_project_payload = build_preprint_create_payload(self.public_project._id, self.provider._id, attrs=self.manual_guid_payload)
res = self.app.post_json_api(self.url, public_project_payload, auth=self.user.auth, expect_errors=True)
assert res.status_code == 400
print(res.status_code)

def test_create_preprint_with_manual_guid(self):
public_project_payload = build_preprint_create_payload(self.public_project._id, self.provider._id, attrs=self.manual_guid_payload)
with override_flag(features.MANUAL_DOI_AND_GUID, True):
res = self.app.post_json_api(self.url, public_project_payload, auth=self.user.auth, )
data = res.json['data']
assert res.status_code == 201
assert data['id'] == f'{self.manual_guid}_v1', 'manual guid was not assigned'
identifiers_response = self.app.get(data['relationships']['identifiers']['links']['related']['href'], auth=self.user.auth)
assert identifiers_response.status_code == 200
assert identifiers_response.json['data'][0]['attributes']['value'] == self.manual_doi

def test_create_preprint_with_supplemental_public_project(self):
public_project_payload = build_preprint_create_payload(self.public_project._id, self.provider._id)

Expand Down
38 changes: 38 additions & 0 deletions api_tests/registrations/views/test_registration_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

from urllib.parse import urljoin, urlparse

from waffle import testutils

from api.base.settings.defaults import API_BASE
from api.base.versioning import CREATE_REGISTRATION_FIELD_CHANGE_VERSION
from api_tests.nodes.views.test_node_draft_registration_list import AbstractDraftRegistrationTestCase
from api_tests.subjects.mixins import SubjectsFilterMixin
from api_tests.registrations.filters.test_filters import RegistrationListFilteringMixin
from api_tests.utils import create_test_file
from framework.auth.core import Auth
from osf import features
from osf.models import RegistrationSchema, Registration
from osf_tests.factories import (
EmbargoFactory,
Expand Down Expand Up @@ -1559,6 +1562,41 @@ def test_registration_draft_must_be_draft_of_current_node(
self, mock_enqueue, app, user, schema, url_registrations_ver):
# Overrides TestNodeRegistrationCreate - node is not in URL in this workflow
return
@pytest.fixture
def manual_guid(self):
return 'abcde'

@pytest.fixture
def manual_doi(self):
return '10.70102/FK2osf.io/abcde'

@pytest.fixture
def enable_flag(self):
with testutils.override_flag(features.MANUAL_DOI_AND_GUID, True):
yield

@pytest.fixture
def manual_guid_payload(self, payload, manual_guid, manual_doi):
payload['data']['attributes'] |= {
'manual_doi': manual_doi,
'manual_guid': manual_guid,
}

return payload

def test_fail_create_registration_with_manual_guid(self, app, user, schema, url_registrations, manual_guid_payload, manual_guid, manual_doi):
res = app.post_json_api(url_registrations, manual_guid_payload, auth=user.auth, expect_errors=True)
assert res.status_code == 400
print(res.status_code)

def test_create_registration_with_manual_guid(self, app, user, schema, url_registrations, manual_guid_payload, manual_guid, manual_doi, enable_flag):
res = app.post_json_api(url_registrations, manual_guid_payload, auth=user.auth)
data = res.json['data']
assert res.status_code == 201
assert data['id'] == manual_guid, 'manual guid was not assigned'
identifiers_response = app.get(data['relationships']['identifiers']['links']['related']['href'], auth=user.auth)
assert identifiers_response.status_code == 200
assert identifiers_response.json['data'][0]['attributes']['value'] == manual_doi

@mock.patch('framework.celery_tasks.handlers.enqueue_task')
def test_need_admin_perms_on_draft(
Expand Down
Loading