From f7fda0abbb6e645bd992dc3ee89ab6a03e14af8b Mon Sep 17 00:00:00 2001 From: Longze Chen Date: Mon, 16 Sep 2024 18:33:22 -0400 Subject: [PATCH] CR Response * Fixed self url for PreprintsInstitutionsRelationshipSerializer * Removed the superfluous 'html' link that is not part of JSON:API * Fixed incorrect tests --- api/preprints/serializers.py | 6 +----- .../test_preprint_institutions_relationship.py | 16 ++++++++++------ osf/models/preprint.py | 4 ++++ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/api/preprints/serializers.py b/api/preprints/serializers.py index ca3a516477c..97cc3f3fb7c 100644 --- a/api/preprints/serializers.py +++ b/api/preprints/serializers.py @@ -625,14 +625,10 @@ class PreprintsInstitutionsRelationshipSerializer(BaseAPISerializer): links = LinksField({ 'self': 'get_self_url', - 'html': 'get_related_url', }) def get_self_url(self, obj): - return obj['self'].absolute_api_v2_url - - def get_related_url(self, obj): - return f"{obj['self'].absolute_api_v2_url}institutions/" + return obj['self'].institutions_relationship_url class Meta: type_ = 'institutions' diff --git a/api_tests/preprints/views/test_preprint_institutions_relationship.py b/api_tests/preprints/views/test_preprint_institutions_relationship.py index cd00750c0eb..336ae2cd8ea 100644 --- a/api_tests/preprints/views/test_preprint_institutions_relationship.py +++ b/api_tests/preprints/views/test_preprint_institutions_relationship.py @@ -167,18 +167,22 @@ def test_update_affiliated_institutions_remove_write_user(self, app, write_user_ preprint.affiliated_institutions.add(institution_A) preprint.save() update_institutions_payload = {'data': []} - res = app.put_json_api(url, update_institutions_payload, auth=write_user_with_institutional_affiliation.auth, expect_errors=True) + res = app.put_json_api(url, update_institutions_payload, auth=write_user_with_institutional_affiliation.auth) assert res.status_code == 200 + preprint.reload() + assert institution_A in preprint.affiliated_institutions.all() def test_update_affiliated_institutions_remove_admin_without_affiliation(self, app, admin_without_institutional_affiliation, preprint, url, institution_A): """ - Test that admins without affiliation can remove institutions. + Test that admins without affiliation cannot remove institutions. """ preprint.affiliated_institutions.add(institution_A) preprint.save() update_institutions_payload = {'data': []} res = app.put_json_api(url, update_institutions_payload, auth=admin_without_institutional_affiliation.auth) assert res.status_code == 200 + preprint.reload() + assert institution_A in preprint.affiliated_institutions.all() def test_update_affiliated_institutions_remove_admin_with_affiliation(self, app, admin_with_institutional_affiliation, preprint, url, institution_A): """ @@ -198,16 +202,16 @@ def test_update_affiliated_institutions_remove_admin_with_affiliation(self, app, def test_preprint_institutions_list_get_unauthenticated(self, app, url): """ - Test that unauthenticated users cannot retrieve the list of affiliated institutions for a preprint. + Test that unauthenticated users can retrieve the list of affiliated institutions for a preprint. """ - res = app.get(url, expect_errors=True) + res = app.get(url) assert res.status_code == 200 def test_preprint_institutions_list_get_no_permissions(self, app, user, url): """ - Test that users without permissions cannot retrieve the list of affiliated institutions for a preprint. + Test that users without permissions can retrieve the list of affiliated institutions for a preprint. """ - res = app.get(url, auth=user.auth, expect_errors=True) + res = app.get(url, auth=user.auth) assert res.status_code == 200 def test_preprint_institutions_list_get_read_user(self, app, read_user_with_institutional_affiliation, preprint, url): diff --git a/osf/models/preprint.py b/osf/models/preprint.py index ff7a5f88c5e..4ce426ccaed 100644 --- a/osf/models/preprint.py +++ b/osf/models/preprint.py @@ -433,6 +433,10 @@ def display_absolute_url(self): def linked_nodes_self_url(self): return self.absolute_api_v2_url + 'relationships/node/' + @property + def institutions_relationship_url(self): + return self.absolute_api_v2_url + 'relationships/institutions/' + @property def admin_contributor_or_group_member_ids(self): # Overrides ContributorMixin