-
Notifications
You must be signed in to change notification settings - Fork 321
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
Changes from all commits
bfc5b8c
b927676
a61cd1b
21bfede
ca45a2a
0375a5a
b3aa28d
9b30044
75a99e2
b311d18
227ee3a
16f7a3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,8 +103,8 @@ def test_get_object(self, req, preprint, plain_view): | |
def test_no_user_permissions_raises_error(self, user, preprint, plain_view): | ||
request = RequestFactory().get(reverse('preprints:preprint', kwargs={'guid': preprint._id})) | ||
request.user = user | ||
resp = plain_view.as_view()(request, guid=preprint._id) | ||
assert resp._headers['location'][1] == f'/accounts/login/?next=/preprints/{preprint._id}/' | ||
with pytest.raises(PermissionDenied): | ||
plain_view.as_view()(request, guid=preprint._id) | ||
Comment on lines
-106
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When there is no permission or when permission is insufficient, django admin either redirect user to login page ( if the user is not authenticated) or raise the permission error (if authenticated). |
||
|
||
def test_get_flagged_spam(self, superuser, preprint, ham_preprint, spam_preprint, flagged_preprint): | ||
request = RequestFactory().get(reverse('preprints:flagged-spam')) | ||
|
@@ -175,16 +175,16 @@ def test_confirm_ham(self, preprint, superuser, mock_akismet): | |
assert preprint.spam_status == SpamStatus.HAM | ||
assert preprint.is_public | ||
|
||
def test_correct_view_permissions(self, user, preprint, plain_view): | ||
def test_valid_but_insufficient_view_permissions(self, user, preprint, plain_view): | ||
view_permission = Permission.objects.get(codename='view_preprint') | ||
user.user_permissions.add(view_permission) | ||
user.save() | ||
|
||
request = RequestFactory().get(reverse('preprints:preprint', kwargs={'guid': preprint._id})) | ||
request.user = user | ||
|
||
response = plain_view.as_view()(request, guid=preprint._id) | ||
assert response.status_code == 302 | ||
with pytest.raises(PermissionDenied): | ||
plain_view.as_view()(request, guid=preprint._id) | ||
Comment on lines
-178
to
+187
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto, also renamed the test name to avoid confusion. |
||
|
||
def test_change_preprint_provider(self, user, preprint, plain_view): | ||
change_permission = Permission.objects.get(codename='change_preprint') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This issue is caused by our factories relies on test DB (i.e. adding the |
||
|
||
# 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() | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
import mock | ||
import pytest | ||
from django.utils import timezone | ||
from future.moves.urllib.parse import urlparse | ||
import mock | ||
from nose.tools import * # noqa: | ||
|
||
import pytest | ||
from rest_framework import exceptions | ||
|
||
from addons.wiki.tests.factories import WikiFactory, WikiVersionFactory | ||
from api.base.settings.defaults import API_BASE | ||
|
@@ -33,7 +34,6 @@ | |
WithdrawnRegistrationFactory, | ||
DraftNodeFactory, | ||
) | ||
from rest_framework import exceptions | ||
from tests.base import fake | ||
from tests.utils import assert_latest_log, assert_latest_log_not | ||
from website.views import find_bookmark_collection | ||
|
@@ -480,8 +480,12 @@ def test_node_shows_related_count_for_linked_by_relationships(self, app, user, p | |
assert res.json['data']['relationships']['linked_by_nodes']['links']['related']['meta']['count'] == 1 | ||
assert res.json['data']['relationships']['linked_by_registrations']['links']['related']['meta']['count'] == 1 | ||
|
||
log_date = timezone.now() | ||
project_private.deleted_date = log_date | ||
project_private.deleted = log_date | ||
project_private.is_deleted = True | ||
Comment on lines
+483
to
486
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: somehow, when I tried |
||
project_private.save() | ||
registration.reload() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Must reload the |
||
project_public.reload() | ||
|
||
res = app.get(url) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue of duplicate
view_node
permission is caused by the upgraded django added new defaultview_
permissions fornode
. Need to specify the content_type to point to the right one.