Skip to content

Commit

Permalink
Add alt fixtures to prevent both siblings & parent/child conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
cslzchen committed Oct 4, 2022
1 parent b311d18 commit 227ee3a
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions api_tests/draft_registrations/views/test_draft_registration_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ def test_cannot_view_draft_list(


class TestDraftRegistrationCreateWithNode(TestDraftRegistrationCreate):

# Overrides `url_draft_registrations` in `TestDraftRegistrationCreate`
@pytest.fixture()
def url_draft_registrations(self, project_public):
return '/{}draft_registrations/?'.format(API_BASE)

# Overrides `payload` in TestDraftRegistrationCreate`
@pytest.fixture()
def payload(self, metaschema_open_ended, provider, project_public):
return {
Expand Down Expand Up @@ -105,39 +108,55 @@ def payload(self, metaschema_open_ended, provider, project_public):
}
}

# Temporary alternative provider that supports `metaschema_open_ended` in `TestDraftRegistrationCreate`
# This provider is created to fix the first 3 tests in this test class due to test DB changes with the
# Django 3 Upgrade. A long-term solution is to create and/or use dedicated schemas for testing.
@pytest.fixture()
def provider_alt(self, metaschema_open_ended):
default_provider = RegistrationProvider.get_default()
default_provider.schemas.add(metaschema_open_ended)
default_provider.save()
return default_provider

# Similarly, this is a temporary alternative payload that uses the above `provider_alt`.
@pytest.fixture()
def payload_alt(self, payload, provider_alt):
new_payload = payload.copy()
new_payload['data']['relationships']['provider']['data']['id'] = provider_alt._id
return new_payload

# Overrides TestDraftRegistrationList
def test_cannot_create_draft_errors(
self, app, user, project_public, payload, url_draft_registrations):
def test_cannot_create_draft_errors(self, app, user, payload_alt, project_public, url_draft_registrations):
# test_cannot_create_draft_from_a_registration
registration = RegistrationFactory(
project=project_public, creator=user)
payload['data']['relationships']['branched_from']['data']['id'] = registration._id
payload_alt['data']['relationships']['branched_from']['data']['id'] = registration._id
res = app.post_json_api(
url_draft_registrations, payload, auth=user.auth,
url_draft_registrations, payload_alt, auth=user.auth,
expect_errors=True)
assert res.status_code == 404

# test_cannot_create_draft_from_deleted_node
project = ProjectFactory(is_public=True, creator=user)
project.is_deleted = True
project.save()
payload['data']['relationships']['branched_from']['data']['id'] = project._id
payload_alt['data']['relationships']['branched_from']['data']['id'] = project._id
res = app.post_json_api(
url_draft_registrations, payload,
url_draft_registrations, payload_alt,
auth=user.auth, expect_errors=True)
assert res.status_code == 410
assert res.json['errors'][0]['detail'] == 'The requested node is no longer available.'

# test_cannot_create_draft_from_collection
collection = CollectionFactory(creator=user)
payload['data']['relationships']['branched_from']['data']['id'] = collection._id
payload_alt['data']['relationships']['branched_from']['data']['id'] = collection._id
res = app.post_json_api(
url_draft_registrations, payload, auth=user.auth,
url_draft_registrations, payload_alt, auth=user.auth,
expect_errors=True)
assert res.status_code == 404

def test_draft_registration_attributes_copied_from_node(self, app, project_public,
url_draft_registrations, user, payload):
url_draft_registrations, user, payload_alt):

write_contrib = AuthUserFactory()
read_contrib = AuthUserFactory()
Expand All @@ -156,12 +175,12 @@ def test_draft_registration_attributes_copied_from_node(self, app, project_publi
project_public.add_contributor(write_contrib, WRITE)
project_public.add_contributor(read_contrib, READ)

res = app.post_json_api(url_draft_registrations, payload, auth=write_contrib.auth, expect_errors=True)
res = app.post_json_api(url_draft_registrations, payload_alt, auth=write_contrib.auth, expect_errors=True)
assert res.status_code == 201
res = app.post_json_api(url_draft_registrations, payload, auth=read_contrib.auth, expect_errors=True)
res = app.post_json_api(url_draft_registrations, payload_alt, auth=read_contrib.auth, expect_errors=True)
assert res.status_code == 403

res = app.post_json_api(url_draft_registrations, payload, auth=user.auth)
res = app.post_json_api(url_draft_registrations, payload_alt, auth=user.auth)
assert res.status_code == 201
attributes = res.json['data']['attributes']
assert attributes['title'] == project_public.title
Expand All @@ -180,14 +199,14 @@ def test_draft_registration_attributes_copied_from_node(self, app, project_publi
def test_cannot_create_draft(
self, app, user_write_contrib,
user_read_contrib, user_non_contrib,
project_public, payload, group,
project_public, payload_alt, group,
url_draft_registrations, group_mem):

# test_write_only_contributor_cannot_create_draft
assert user_write_contrib in project_public.contributors.all()
res = app.post_json_api(
url_draft_registrations,
payload,
payload_alt,
auth=user_write_contrib.auth,
expect_errors=True)
assert res.status_code == 201
Expand All @@ -196,29 +215,29 @@ def test_cannot_create_draft(
assert user_read_contrib in project_public.contributors.all()
res = app.post_json_api(
url_draft_registrations,
payload,
payload_alt,
auth=user_read_contrib.auth,
expect_errors=True)
assert res.status_code == 403

# test_non_authenticated_user_cannot_create_draft
res = app.post_json_api(
url_draft_registrations,
payload, expect_errors=True)
payload_alt, expect_errors=True)
assert res.status_code == 401

# test_logged_in_non_contributor_cannot_create_draft
res = app.post_json_api(
url_draft_registrations,
payload,
payload_alt,
auth=user_non_contrib.auth,
expect_errors=True)
assert res.status_code == 403

# test_group_admin_cannot_create_draft
res = app.post_json_api(
url_draft_registrations,
payload,
payload_alt,
auth=group_mem.auth,
expect_errors=True)
assert res.status_code == 201
Expand All @@ -228,7 +247,7 @@ def test_cannot_create_draft(
project_public.add_osf_group(group, WRITE)
res = app.post_json_api(
url_draft_registrations,
payload,
payload_alt,
auth=group_mem.auth,
expect_errors=True)
assert res.status_code == 201
Expand Down

0 comments on commit 227ee3a

Please sign in to comment.