|
| 1 | +import pytest |
| 2 | + |
| 3 | +from api.base.settings.defaults import API_BASE |
| 4 | +from osf_tests.factories import ( |
| 5 | + AuthUserFactory, |
| 6 | + ProjectFactory, |
| 7 | + RegistrationFactory, |
| 8 | +) |
| 9 | +from osf.utils import permissions |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture() |
| 13 | +def user(): |
| 14 | + return AuthUserFactory() |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.django_db |
| 18 | +class TestRegistrationContributorsList: |
| 19 | + |
| 20 | + @pytest.fixture() |
| 21 | + def user_two(self): |
| 22 | + return AuthUserFactory() |
| 23 | + |
| 24 | + @pytest.fixture() |
| 25 | + def user_three(self): |
| 26 | + return AuthUserFactory() |
| 27 | + |
| 28 | + @pytest.fixture() |
| 29 | + def project_public(self, user): |
| 30 | + return ProjectFactory( |
| 31 | + title='Public Project', |
| 32 | + description='A public project', |
| 33 | + category='data', |
| 34 | + is_public=True, |
| 35 | + creator=user, |
| 36 | + ) |
| 37 | + |
| 38 | + @pytest.fixture() |
| 39 | + def registration_public(self, project_public, user): |
| 40 | + return RegistrationFactory(project=project_public, creator=user, is_public=True) |
| 41 | + |
| 42 | + @pytest.fixture() |
| 43 | + def url_public(self, registration_public): |
| 44 | + return f'/{API_BASE}registrations/{registration_public._id}/contributors/?send_email=false' |
| 45 | + |
| 46 | + @pytest.fixture() |
| 47 | + def payload_one(self, user_two): |
| 48 | + return { |
| 49 | + 'type': 'contributors', |
| 50 | + 'attributes': {'bibliographic': True, 'permission': permissions.ADMIN}, |
| 51 | + 'relationships': {'users': {'data': {'id': user_two._id, 'type': 'users'}}}, |
| 52 | + } |
| 53 | + |
| 54 | + @pytest.fixture() |
| 55 | + def payload_two(self, user_three): |
| 56 | + return { |
| 57 | + 'type': 'contributors', |
| 58 | + 'attributes': {'bibliographic': False, 'permission': permissions.READ}, |
| 59 | + 'relationships': { |
| 60 | + 'users': {'data': {'id': user_three._id, 'type': 'users'}} |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + @pytest.fixture() |
| 65 | + def make_contrib_id(self): |
| 66 | + def contrib_id(registration_id, user_id): |
| 67 | + return f'{registration_id}-{user_id}' |
| 68 | + return contrib_id |
| 69 | + |
| 70 | + @pytest.fixture() |
| 71 | + def registration_with_contributors(self, project_public, user, user_two, user_three): |
| 72 | + registration = RegistrationFactory(project=project_public, creator=user, is_public=True) |
| 73 | + |
| 74 | + registration.add_contributor( |
| 75 | + user_two, permissions=permissions.READ, visible=True, save=True |
| 76 | + ) |
| 77 | + registration.add_contributor( |
| 78 | + user_three, permissions=permissions.READ, visible=True, save=True |
| 79 | + ) |
| 80 | + return registration |
| 81 | + |
| 82 | + @pytest.fixture() |
| 83 | + def url_with_contributors(self, registration_with_contributors): |
| 84 | + return f'/{API_BASE}registrations/{registration_with_contributors._id}/contributors/' |
| 85 | + |
| 86 | + @pytest.fixture() |
| 87 | + def update_payload_one(self, user_two, registration_with_contributors, make_contrib_id): |
| 88 | + return { |
| 89 | + 'id': make_contrib_id(registration_with_contributors._id, user_two._id), |
| 90 | + 'type': 'contributors', |
| 91 | + 'attributes': {'bibliographic': True, 'permission': permissions.ADMIN}, |
| 92 | + } |
| 93 | + |
| 94 | + @pytest.fixture() |
| 95 | + def update_payload_two(self, user_three, registration_with_contributors, make_contrib_id): |
| 96 | + return { |
| 97 | + 'id': make_contrib_id(registration_with_contributors._id, user_three._id), |
| 98 | + 'type': 'contributors', |
| 99 | + 'attributes': {'bibliographic': False, 'permission': permissions.WRITE}, |
| 100 | + } |
| 101 | + |
| 102 | + def test_bulk_post_new_contributors( |
| 103 | + self, app, user, registration_public, payload_one, payload_two, url_public |
| 104 | + ): |
| 105 | + res = app.post_json_api( |
| 106 | + url_public, |
| 107 | + {'data': [payload_one, payload_two]}, |
| 108 | + auth=user.auth, |
| 109 | + bulk=True, |
| 110 | + ) |
| 111 | + assert res.status_code == 201 |
| 112 | + assert len(res.json['data']) == 2 |
| 113 | + assert [ |
| 114 | + res.json['data'][0]['attributes']['bibliographic'], |
| 115 | + res.json['data'][1]['attributes']['bibliographic'], |
| 116 | + ] == [True, False] |
| 117 | + assert [ |
| 118 | + res.json['data'][0]['attributes']['permission'], |
| 119 | + res.json['data'][1]['attributes']['permission'], |
| 120 | + ] == [permissions.ADMIN, permissions.READ] |
| 121 | + assert res.content_type == 'application/vnd.api+json' |
| 122 | + |
| 123 | + res = app.get(url_public, auth=user.auth) |
| 124 | + assert len(res.json['data']) == 3 |
| 125 | + |
| 126 | + def test_bulk_patch_existing_contributors( |
| 127 | + self, app, user, registration_with_contributors, update_payload_one, update_payload_two, url_with_contributors |
| 128 | + ): |
| 129 | + res = app.patch_json_api( |
| 130 | + url_with_contributors, |
| 131 | + {'data': [update_payload_one, update_payload_two]}, |
| 132 | + auth=user.auth, |
| 133 | + bulk=True, |
| 134 | + ) |
| 135 | + assert res.status_code == 200 |
| 136 | + assert len(res.json['data']) == 2 |
| 137 | + assert [ |
| 138 | + res.json['data'][0]['attributes']['bibliographic'], |
| 139 | + res.json['data'][1]['attributes']['bibliographic'], |
| 140 | + ] == [True, False] |
| 141 | + assert [ |
| 142 | + res.json['data'][0]['attributes']['permission'], |
| 143 | + res.json['data'][1]['attributes']['permission'], |
| 144 | + ] == [permissions.ADMIN, permissions.WRITE] |
| 145 | + |
| 146 | + res = app.get(url_with_contributors, auth=user.auth) |
| 147 | + data = res.json['data'] |
| 148 | + contrib_dict = {contrib['id']: contrib for contrib in data} |
| 149 | + assert contrib_dict[update_payload_one['id']]['attributes']['permission'] == permissions.ADMIN |
| 150 | + assert contrib_dict[update_payload_two['id']]['attributes']['permission'] == permissions.WRITE |
0 commit comments