Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Django Upgrade] [ENG-4072] The Rest - Part 2 #10072

Merged
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
Comment on lines +111 to +119
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestDraftRegistrationCreateWithNode inherits from TestDraftRegistrationCreate. Thus modifying the provider in the latter breaks TestDraftRegistrationCreate. In addition, some tests in TestDraftRegistrationCreateWithNode also relied on this version of metaschema_open_ended not in provider.

This issue is caused by our factories relies on test DB (i.e. adding the pk=1 schema when calling RegistrationFactory) instead of adding a schema with specified name and pinned version. Pre and post upgrade test DB are slightly different which breaks the tests.


# 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