diff --git a/api/preprints/serializers.py b/api/preprints/serializers.py index e07f79ca8570..5c6d72588831 100644 --- a/api/preprints/serializers.py +++ b/api/preprints/serializers.py @@ -206,6 +206,16 @@ class PreprintSerializer(TaxonomizableSerializerMixin, MetricsSerializerMixin, J ), ) + affiliated_institutions = RelationshipField( + related_view='preprints:preprints-institutions', + related_view_kwargs={'preprint_id': '<_id>'}, + self_view='preprints:preprints-institutions', + self_view_kwargs={'preprint_id': '<_id>'}, + read_only=False, + required=False, + allow_null=True, + ) + links = LinksField( { 'self': 'get_preprint_url', diff --git a/api_tests/preprints/views/test_preprint_detail.py b/api_tests/preprints/views/test_preprint_detail.py index 7112a1a21e6f..2b915796d3d9 100644 --- a/api_tests/preprints/views/test_preprint_detail.py +++ b/api_tests/preprints/views/test_preprint_detail.py @@ -28,6 +28,7 @@ ProjectFactory, SubjectFactory, PreprintProviderFactory, + InstitutionFactory ) from website.settings import DOI_FORMAT, CROSSREF_URL @@ -58,6 +59,10 @@ class TestPreprintDetail: def preprint(self, user): return PreprintFactory(creator=user) + @pytest.fixture() + def institution(self): + return InstitutionFactory(creator=user) + @pytest.fixture() def preprint_pre_mod(self, user): return PreprintFactory(reviews_workflow='pre-moderation', is_published=False, creator=user) @@ -215,6 +220,16 @@ def test_preprint_embed_identifiers(self, app, user, preprint, url): link = res.json['data']['relationships']['identifiers']['links']['related']['href'] assert f'{url}identifiers/' in link + def test_return_affiliated_institutions(self, app, user, preprint, institution, url): + """ + Confirmation test for the the new preprint affiliated institutions feature + """ + preprint.affiliated_institutions.add(institution) + res = app.get(url) + assert res.status_code == 200 + relationship_link = res.json['data']['relationships']['affiliated_institutions']['links']['related']['href'] + assert f'/v2/preprints/{preprint._id}/institutions/' in relationship_link + @pytest.mark.django_db class TestPreprintDelete: diff --git a/api_tests/preprints/views/test_preprint_list.py b/api_tests/preprints/views/test_preprint_list.py index 81fdde05f797..305dca450d5c 100644 --- a/api_tests/preprints/views/test_preprint_list.py +++ b/api_tests/preprints/views/test_preprint_list.py @@ -26,6 +26,7 @@ AuthUserFactory, SubjectFactory, PreprintProviderFactory, + InstitutionFactory, ) from tests.base import ApiTestCase, capture_signals from website.project import signals as project_signals @@ -145,6 +146,7 @@ class TestPreprintList(ApiTestCase): def setUp(self): super().setUp() self.user = AuthUserFactory() + self.institution = InstitutionFactory() self.preprint = PreprintFactory(creator=self.user) self.url = f'/{API_BASE}preprints/' @@ -184,6 +186,18 @@ def test_withdrawn_preprints_list(self): assert pp._id not in user_res_ids assert pp._id in mod_res_ids + def test_return_affiliated_institutions(self): + """ + Confirmation test for the the new preprint affiliated institutions feature + """ + self.preprint.affiliated_institutions.add(self.institution) + res = self.app.get(self.url) + assert len(res.json['data']) == 1 + assert res.status_code == 200 + assert res.content_type == 'application/vnd.api+json' + relationship_link = res.json['data'][0]['relationships']['affiliated_institutions']['links']['related']['href'] + assert f'/v2/preprints/{self.preprint._id}/institutions/' in relationship_link + class TestPreprintsListFiltering(PreprintsListFilteringMixin):