Skip to content

Commit

Permalink
Merge pull request #10926 from Vlad0n20/feature/ENG-6928
Browse files Browse the repository at this point in the history
[ENG-6928] Add new tests in `TestPreprintVersionsListRetrieve`
  • Loading branch information
cslzchen authored Jan 24, 2025
2 parents 0d73565 + 07e4e2c commit 3bdca36
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 9 deletions.
202 changes: 198 additions & 4 deletions api_tests/preprints/views/test_preprint_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
from framework.auth.core import Auth
from osf.models import Preprint
from osf.utils import permissions
from osf_tests.factories import ProjectFactory, PreprintFactory, AuthUserFactory
from osf.utils.workflows import DefaultStates, RequestTypes
from osf_tests.factories import ProjectFactory, PreprintFactory, AuthUserFactory, PreprintRequestFactory
from tests.base import ApiTestCase


# TODO: we have good coverage for `POST`; please add new tests for `GET`
class TestPreprintVersion(ApiTestCase):
class TestPreprintVersionsListCreate(ApiTestCase):

def setUp(self):

super().setUp()
self.user = AuthUserFactory()
self.project = ProjectFactory(creator=self.user)
Expand Down Expand Up @@ -154,3 +153,198 @@ def test_permission_error(self):
self.post_mod_preprint.add_contributor(user_write, permissions.WRITE)
res = self.app.post_json_api(self.post_mod_version_create_url, auth=user_write.auth, expect_errors=True)
assert res.status_code == 403


class TestPreprintVersionsListRetrieve(ApiTestCase):

def setUp(self):

super().setUp()

self.creator = AuthUserFactory()
self.read_contrib = AuthUserFactory()
self.write_contrib = AuthUserFactory()
self.admin_contrib = AuthUserFactory()
self.moderator = AuthUserFactory()
self.non_contrib = AuthUserFactory()

def test_list_versions_post_mod(self):
# Post moderation V1 (Accepted)
post_mod_preprint = PreprintFactory(
reviews_workflow='post-moderation',
creator=self.creator,
is_published=True
)
post_mod_preprint.add_contributor(self.admin_contrib, permissions=permissions.ADMIN)
post_mod_preprint.add_contributor(self.write_contrib, permissions=permissions.WRITE)
post_mod_preprint.add_contributor(self.read_contrib, permissions=permissions.READ)
post_mod_preprint.provider.get_group('moderator').user_set.add(self.moderator)
post_mod_versions_list_url = f"/{API_BASE}preprints/{post_mod_preprint.get_guid()._id}/versions/"

# Post moderation V2 (Withdrawn)
post_mod_preprint_v2 = PreprintFactory.create_version(
create_from=post_mod_preprint,
creator=self.creator,
final_machine_state='initial',
set_doi=False
)
post_mod_preprint_v2.run_submit(self.creator)
post_mod_preprint_v2.run_accept(self.moderator, 'comment')
withdrawal_request = PreprintRequestFactory(
creator=self.creator,
target=post_mod_preprint_v2,
request_type=RequestTypes.WITHDRAWAL.value,
machine_state=DefaultStates.INITIAL.value)
withdrawal_request.run_submit(self.creator)
withdrawal_request.run_accept(self.moderator, withdrawal_request.comment)

# Post moderation V3 (Pending)
post_mod_preprint_v3 = PreprintFactory.create_version(
create_from=post_mod_preprint_v2,
creator=self.creator,
final_machine_state='initial',
set_doi=False
)
post_mod_preprint_v3.run_submit(self.creator)
id_set = {post_mod_preprint._id, post_mod_preprint_v2._id, post_mod_preprint_v3._id}
res = self.app.get(post_mod_versions_list_url, auth=self.admin_contrib.auth)
assert res.status_code == 200
data = res.json['data']
assert len(data) == 3
assert set([item['id'] for item in data]) == id_set
res = self.app.get(post_mod_versions_list_url, auth=self.write_contrib.auth)
assert res.status_code == 200
data = res.json['data']
assert len(data) == 3
assert set([item['id'] for item in data]) == id_set
res = self.app.get(post_mod_versions_list_url, auth=self.read_contrib.auth)
assert res.status_code == 200
data = res.json['data']
assert len(data) == 3
assert set([item['id'] for item in data]) == id_set
res = self.app.get(post_mod_versions_list_url, auth=self.non_contrib.auth)
assert res.status_code == 200
data = res.json['data']
assert len(data) == 3
assert set([item['id'] for item in data]) == id_set
res = self.app.get(post_mod_versions_list_url, auth=None)
assert res.status_code == 200
data = res.json['data']
assert len(data) == 3
assert set([item['id'] for item in data]) == id_set

def test_list_versions_pre_mod(self):
# Pre moderation V1 (Accepted)
pre_mod_preprint = PreprintFactory(
reviews_workflow='pre-moderation',
creator=self.creator,
is_published=True
)
pre_mod_preprint.provider.get_group('moderator').user_set.add(self.moderator)
pre_mod_preprint.add_contributor(self.admin_contrib, permissions=permissions.ADMIN)
pre_mod_preprint.add_contributor(self.write_contrib, permissions=permissions.WRITE)
pre_mod_preprint.add_contributor(self.read_contrib, permissions=permissions.READ)
pre_mod_versions_list_url = f"/{API_BASE}preprints/{pre_mod_preprint.get_guid()._id}/versions/"

for contrib in pre_mod_preprint.contributor_set.all():
print(f'>>>> {contrib}:{contrib.permission}')

# Pre moderation V2 (Withdrawn)
pre_mod_preprint_v2 = PreprintFactory.create_version(
create_from=pre_mod_preprint,
creator=self.creator,
final_machine_state='initial',
set_doi=False
)
pre_mod_preprint_v2.run_submit(self.creator)
pre_mod_preprint_v2.run_accept(self.moderator, 'comment')
withdrawal_request = PreprintRequestFactory(
creator=self.creator,
target=pre_mod_preprint_v2,
request_type=RequestTypes.WITHDRAWAL.value,
machine_state=DefaultStates.INITIAL.value)
withdrawal_request.run_submit(self.creator)
withdrawal_request.run_accept(self.moderator, withdrawal_request.comment)

# Pre moderation V3 (Rejected)
pre_mod_preprint_v3 = PreprintFactory.create_version(
create_from=pre_mod_preprint_v2,
creator=self.creator,
final_machine_state='initial',
is_published=False,
set_doi=False,
)
pre_mod_preprint_v3.run_submit(self.creator)
pre_mod_preprint_v3.run_reject(self.moderator, 'comment')

# Pre moderation V4 (Pending)
pre_mod_preprint_v4 = PreprintFactory.create_version(
create_from=pre_mod_preprint_v2,
creator=self.creator,
final_machine_state='initial',
is_published=False,
set_doi=False
)
pre_mod_preprint_v4.run_submit(self.creator)

admin_id_set = {pre_mod_preprint._id, pre_mod_preprint_v2._id, pre_mod_preprint_v3._id, pre_mod_preprint_v4._id}
non_admin_id_set = {pre_mod_preprint._id, pre_mod_preprint_v2._id}
res = self.app.get(pre_mod_versions_list_url, auth=self.admin_contrib.auth)
assert res.status_code == 200
data = res.json['data']
assert len(data) == 4
assert set([item['id'] for item in data]) == admin_id_set
res = self.app.get(pre_mod_versions_list_url, auth=self.write_contrib.auth)
assert res.status_code == 200
data = res.json['data']
assert len(data) == 2
assert set([item['id'] for item in data]) == non_admin_id_set
res = self.app.get(pre_mod_versions_list_url, auth=self.read_contrib.auth)
assert res.status_code == 200
data = res.json['data']
assert len(data) == 2
assert set([item['id'] for item in data]) == non_admin_id_set
res = self.app.get(pre_mod_versions_list_url, auth=self.non_contrib.auth)
assert res.status_code == 200
data = res.json['data']
assert len(data) == 2
assert set([item['id'] for item in data]) == non_admin_id_set
res = self.app.get(pre_mod_versions_list_url, auth=None)
assert res.status_code == 200
data = res.json['data']
assert len(data) == 2
assert set([item['id'] for item in data]) == non_admin_id_set

def test_invalid_preprint_id(self):
versions_url_invalid_guid = f'/{API_BASE}preprints/abcde/versions/'
res = self.app.get(versions_url_invalid_guid, auth=None, expect_errors=True)
assert res.status_code == 404
preprint = PreprintFactory(
reviews_workflow='post-moderation',
creator=self.creator,
is_published=True
)
versions_url_invalid_version = f'/{API_BASE}preprints/{preprint.get_guid()._id}_v2/versions/'
res = self.app.get(versions_url_invalid_version, auth=None, expect_errors=True)
assert res.status_code == 404

def test_version_indifference(self):
latest_version = PreprintFactory(
reviews_workflow='post-moderation',
creator=self.creator,
is_published=True
)
for _ in range(5):
new_version = PreprintFactory.create_version(
create_from=latest_version,
creator=self.creator,
set_doi=False
)
latest_version = new_version
versions_url_base_guid = f'/{API_BASE}preprints/{latest_version.get_guid()._id}/versions/'
res_1 = self.app.get(versions_url_base_guid, auth=self.creator.auth)
assert res_1.status_code == 200
versions_url_valid_version = f'/{API_BASE}preprints/{latest_version._id}/versions/'
res_2 = self.app.get(versions_url_valid_version, auth=self.creator.auth)
assert res_2.status_code == 200
assert len(res_1.json['data']) == len(res_2.json['data'])
17 changes: 12 additions & 5 deletions osf_tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
SchemaResponseTriggers
)
from addons.osfstorage.models import OsfStorageFile, Region
from osf.exceptions import UserStateError, ValidationValueError

fake = Factory.create()
faker = Faker()
# If tests are run on really old processors without high precision this might fail. Unlikely to occur.
Expand Down Expand Up @@ -875,11 +877,16 @@ def create_version(cls, create_from, creator=None, final_machine_state='accepted
preprint_file.create_version(creator, location, metadata=metadata).save()

# Set relationships
contributor_list = latest_version.contributor_set.exclude(
user=latest_version.creator
).values('visible', 'user_id', '_order')
for contributor in contributor_list:
instance.contributor_set.create(**{**contributor, 'preprint_id': instance.id})
for contributor in latest_version.contributor_set.exclude(user=instance.creator):
try:
instance.add_contributor(
contributor.user,
permissions=contributor.permission,
visible=contributor.visible,
save=True
)
except (ValidationValueError, UserStateError):
pass
for institution in latest_version.affiliated_institutions.all():
instance.add_affiliated_institution(institution, auth.user, ignore_user_affiliation=True)

Expand Down

0 comments on commit 3bdca36

Please sign in to comment.