-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add institutional affiliation relationship to preprints
- Loading branch information
John Tordoff
committed
Jun 24, 2024
1 parent
b6cecc0
commit 889ae38
Showing
9 changed files
with
350 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
api_tests/preprints/views/test_preprint_institutions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import pytest | ||
|
||
from api.base.settings.defaults import API_BASE | ||
from osf_tests.factories import ( | ||
PreprintFactory, | ||
AuthUserFactory, | ||
InstitutionFactory, | ||
) | ||
|
||
|
||
@pytest.fixture() | ||
def user(): | ||
return AuthUserFactory() | ||
|
||
|
||
@pytest.fixture() | ||
def admin_with_institutional_affilation(institution): | ||
user = AuthUserFactory() | ||
user.add_or_update_affiliated_institution(institution) | ||
return user | ||
|
||
|
||
@pytest.fixture() | ||
def no_auth_with_institutional_affilation(institution): | ||
user = AuthUserFactory() | ||
user.add_or_update_affiliated_institution(institution) | ||
return user | ||
|
||
|
||
@pytest.fixture() | ||
def admin_without_institutional_affilation(institution, preprint): | ||
user = AuthUserFactory() | ||
preprint.add_permission(user, 'admin') | ||
return user | ||
|
||
|
||
@pytest.fixture() | ||
def institution(): | ||
return InstitutionFactory() | ||
|
||
|
||
@pytest.fixture() | ||
def preprint(admin_with_institutional_affilation): | ||
return PreprintFactory(creator=admin_with_institutional_affilation) | ||
|
||
|
||
@pytest.fixture() | ||
def url(preprint): | ||
return '/{}preprints/{}/institutions/'.format(API_BASE, preprint._id) | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestPreprintInstitutionsList: | ||
|
||
def test_update_affiliated_institutions_add(self, app, user, admin_with_institutional_affilation, admin_without_institutional_affilation, preprint, url, | ||
institution): | ||
update_institutions_payload = { | ||
'data': [{'type': 'institutions', 'id': institution._id}] | ||
} | ||
|
||
res = app.put_json_api( | ||
url, | ||
update_institutions_payload, | ||
auth=user.auth, | ||
expect_errors=True | ||
) | ||
assert res.status_code == 403 | ||
|
||
res = app.put_json_api( | ||
url, | ||
update_institutions_payload, | ||
auth=admin_without_institutional_affilation.auth, | ||
expect_errors=True | ||
) | ||
assert res.status_code == 400 | ||
assert res.json['errors'][0]['detail'] == f'User is not affiliated with {institution.name},' | ||
|
||
res = app.put_json_api( | ||
url, | ||
update_institutions_payload, | ||
auth=admin_with_institutional_affilation.auth | ||
) | ||
assert res.status_code == 201 | ||
|
||
preprint.reload() | ||
assert institution in preprint.affiliated_institutions.all() | ||
|
||
log = preprint.logs.latest() | ||
assert log.action == 'affiliated_institution_added' | ||
assert log.params['institution'] == { | ||
'id': institution._id, | ||
'name': institution.name | ||
} | ||
|
||
def test_update_affiliated_institutions_remove(self, app, user, admin_with_institutional_affilation, no_auth_with_institutional_affilation, admin_without_institutional_affilation, preprint, url, | ||
institution): | ||
|
||
preprint.affiliated_institutions.add(institution) | ||
preprint.save() | ||
|
||
update_institutions_payload = { | ||
'data': [] | ||
} | ||
|
||
res = app.put_json_api( | ||
url, | ||
update_institutions_payload, | ||
auth=user.auth, | ||
expect_errors=True | ||
) | ||
assert res.status_code == 403 | ||
|
||
res = app.put_json_api( | ||
url, | ||
update_institutions_payload, | ||
auth=no_auth_with_institutional_affilation.auth, | ||
expect_errors=True | ||
) | ||
assert res.status_code == 403 | ||
|
||
res = app.put_json_api( | ||
url, | ||
update_institutions_payload, | ||
auth=admin_without_institutional_affilation.auth, | ||
expect_errors=True | ||
) | ||
assert res.status_code == 201 # you can always remove it you are an admin | ||
|
||
res = app.put_json_api( | ||
url, | ||
update_institutions_payload, | ||
auth=admin_with_institutional_affilation.auth | ||
) | ||
assert res.status_code == 201 | ||
|
||
preprint.reload() | ||
assert institution not in preprint.affiliated_institutions.all() | ||
|
||
log = preprint.logs.latest() | ||
assert log.action == 'affiliated_institution_removed' | ||
assert log.params['institution'] == { | ||
'id': institution._id, | ||
'name': institution.name | ||
} | ||
|
||
def test_preprint_institutions_list_get(self, app, user, admin_with_institutional_affilation, preprint, url, | ||
institution): | ||
# For testing purposes | ||
preprint.is_public = False | ||
preprint.save() | ||
|
||
res = app.get(url, expect_errors=True) | ||
assert res.status_code == 401 | ||
|
||
res = app.get(url, auth=user.auth, expect_errors=True) | ||
assert res.status_code == 403 | ||
|
||
res = app.get(url, auth=admin_with_institutional_affilation.auth, expect_errors=True) | ||
assert res.status_code == 200 | ||
|
||
assert res.status_code == 200 | ||
assert not res.json['data'] | ||
|
||
preprint.add_affiliated_institution(institution, admin_with_institutional_affilation) | ||
|
||
res = app.get(url, auth=admin_with_institutional_affilation.auth, expect_errors=True) | ||
assert res.status_code == 200 | ||
|
||
assert res.json['data'][0]['id'] == institution._id | ||
assert res.json['data'][0]['type'] == 'institution' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 3.2.17 on 2024-06-21 16:25 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('osf', '0020_abstractprovider_advertise_on_discover_page'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='preprint', | ||
name='affiliated_institutions', | ||
field=models.ManyToManyField(related_name='preprints', to='osf.Institution'), | ||
), | ||
] |
Oops, something went wrong.