Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api_tests/base/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from tests.base import ApiTestCase, DbTestCase
from osf_tests import factories
from tests.utils import make_drf_request_with_version
from tests.utils import make_drf_request_with_version, capture_notifications

from osf.models import RegistrationSchema

Expand Down Expand Up @@ -142,7 +142,8 @@ def test_deprecation_warning_for_snake_case(self):
}
}
}
res = self.app.post_json_api(url, payload, auth=user_auth.auth)
with capture_notifications():
res = self.app.post_json_api(url, payload, auth=user_auth.auth)
assert res.json['data']['type'] == 'draft-registrations'
assert res.json['meta']['warning'] == 'As of API Version {0}, all types are now Kebab-case. {0} will accept snake_case, but this will be deprecated in future versions.'.format(KEBAB_CASE_VERSION)

Expand Down
28 changes: 14 additions & 14 deletions api_tests/draft_registrations/views/test_draft_registration_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ def test_draft_registration_attributes_copied_from_node(
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_alt, auth=user.auth)
with capture_notifications():
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 Down Expand Up @@ -336,9 +337,6 @@ def test_logged_in_non_contributor_cannot_create_draft(
)
assert res.status_code == 403

def test_create_project_based_draft_does_not_email_initiator(self, app, user, url_draft_registrations, payload):
app.post_json_api(f'{url_draft_registrations}?embed=branched_from&embed=initiator', payload, auth=user.auth)

def test_affiliated_institutions_are_copied_from_node_no_institutions(self, app, user, url_draft_registrations, payload):
"""
Draft registrations that are based on projects get those project's user institutional affiliation,
Expand All @@ -349,11 +347,12 @@ def test_affiliated_institutions_are_copied_from_node_no_institutions(self, app,
"""
project = ProjectFactory(is_public=True, creator=user)
payload['data']['relationships']['branched_from']['data']['id'] = project._id
res = app.post_json_api(
url_draft_registrations,
payload,
auth=user.auth,
)
with capture_notifications():
res = app.post_json_api(
url_draft_registrations,
payload,
auth=user.auth,
)
assert res.status_code == 201
draft_registration = DraftRegistration.load(res.json['data']['id'])
assert not draft_registration.affiliated_institutions.exists()
Expand All @@ -371,11 +370,12 @@ def test_affiliated_institutions_are_copied_from_node(self, app, user, url_draft
project = ProjectFactory(is_public=True, creator=user)
project.affiliated_institutions.add(institution)
payload['data']['relationships']['branched_from']['data']['id'] = project._id
res = app.post_json_api(
url_draft_registrations,
payload,
auth=user.auth,
)
with capture_notifications():
res = app.post_json_api(
url_draft_registrations,
payload,
auth=user.auth,
)
assert res.status_code == 201
draft_registration = DraftRegistration.load(res.json['data']['id'])
assert list(draft_registration.affiliated_institutions.all()) == list(project.affiliated_institutions.all())
Expand Down
96 changes: 52 additions & 44 deletions api_tests/nodes/views/test_node_draft_registration_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from osf.utils import permissions
from website.project.metadata.utils import create_jsonschema_from_metaschema
from website import settings
from tests.utils import capture_notifications

OPEN_ENDED_SCHEMA_VERSION = 3
SCHEMA_VERSION = 2
Expand Down Expand Up @@ -334,11 +335,12 @@ def test_type_is_draft_registrations(self, app, user, metaschema_open_ended, url
def test_admin_can_create_draft(
self, app, user, project_public, url_draft_registrations, payload, metaschema_open_ended
):
res = app.post_json_api(
f'{url_draft_registrations}&embed=branched_from&embed=initiator',
payload,
auth=user.auth
)
with capture_notifications():
res = app.post_json_api(
f'{url_draft_registrations}&embed=branched_from&embed=initiator',
payload,
auth=user.auth
)
assert res.status_code == 201
data = res.json['data']
assert metaschema_open_ended._id in data['relationships']['registration_schema']['links']['related']['href']
Expand Down Expand Up @@ -407,11 +409,11 @@ def test_schema_validation(
assert res.status_code == 400

payload['data']['relationships']['registration_schema']['data']['id'] = schema._id

res = app.post_json_api(
url_draft_registrations,
payload,
auth=user.auth)
with capture_notifications():
res = app.post_json_api(
url_draft_registrations,
payload,
auth=user.auth)
assert res.status_code == 201

# Non-Default provider does not accept everything
Expand Down Expand Up @@ -590,10 +592,11 @@ def test_supply_registration_responses_on_creation(
'datacompletion': 'No, data collection has not begun',
'comments': ''
}
res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
with capture_notifications():
res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)

attributes = res.json['data']['attributes']
assert attributes['registration_responses'] == {
Expand Down Expand Up @@ -641,10 +644,11 @@ def test_registration_metadata_question_values_must_be_dictionaries(
payload['data']['attributes']['registration_metadata'] = {}
payload['data']['attributes']['registration_metadata']['datacompletion'] = 'No, data collection has not begun'

res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
with capture_notifications():
res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
errors = res.json['errors'][0]
assert res.status_code == 400
assert errors['detail'] == 'For your registration your response to the \'Data collection status\' field' \
Expand All @@ -661,10 +665,11 @@ def test_registration_metadata_question_keys_must_be_value(
payload['data']['attributes']['registration_metadata']['datacompletion'] = {
'incorrect_key': 'No, data collection has not begun'}

res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
with capture_notifications():
res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
errors = res.json['errors'][0]
assert res.status_code == 400
assert errors['detail'] == 'For your registration your response to the \'Data collection status\' ' \
Expand All @@ -682,10 +687,11 @@ def test_question_in_registration_metadata_must_be_in_schema(
'value': 'No, data collection has not begun'
}

res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
with capture_notifications():
res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
errors = res.json['errors'][0]
assert res.status_code == 400
assert errors['detail'] == 'For your registration the \'datacompletion\' field is extraneous and not' \
Expand All @@ -701,11 +707,11 @@ def test_multiple_choice_question_value_must_match_value_in_schema(
payload['data']['attributes']['registration_metadata'] = {}
payload['data']['attributes']['registration_metadata']['datacompletion'] = {
'value': 'Nope, data collection has not begun'}

res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
with capture_notifications():
res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
errors = res.json['errors'][0]
assert res.status_code == 400
assert errors['detail'] == 'For your registration your response to the \'Data collection status\'' \
Expand Down Expand Up @@ -733,10 +739,11 @@ def test_registration_responses_question_values_must_not_be_dictionaries(
payload['data']['attributes']['registration_responses'] = {}
payload['data']['attributes']['registration_responses']['datacompletion'] = {'value': 'No, data collection has not begun'}

res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
with capture_notifications():
res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
errors = res.json['errors'][0]
assert res.status_code == 400
assert errors['detail'] == 'For your registration, your response to the \'Data collection status\' field' \
Expand All @@ -752,10 +759,11 @@ def test_question_in_registration_responses_must_be_in_schema(
payload['data']['attributes']['registration_responses'] = {}
payload['data']['attributes']['registration_responses']['q11'] = 'No, data collection has not begun'

res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
with capture_notifications():
res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
errors = res.json['errors'][0]
assert res.status_code == 400
assert errors['detail'] == 'Additional properties are not allowed (\'q11\' was unexpected)'
Expand All @@ -769,11 +777,11 @@ def test_registration_responses_multiple_choice_question_value_must_match_value_
payload['data']['relationships']['registration_schema']['data']['id'] = schema._id
payload['data']['attributes']['registration_responses'] = {}
payload['data']['attributes']['registration_responses']['datacompletion'] = 'Nope, data collection has not begun'

res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
with capture_notifications():
res = app.post_json_api(
url_draft_registrations,
payload, auth=user.auth,
expect_errors=True)
errors = res.json['errors'][0]
assert res.status_code == 400
assert errors['detail'] == 'For your registration, your response to the \'Data collection status\'' \
Expand Down
12 changes: 7 additions & 5 deletions osf/models/registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1374,13 +1374,15 @@ def create_from_node(

if not node:
draft.affiliated_institutions.add(*draft.creator.get_affiliated_institutions())
initiator_permissions = draft.contributor_set.get(user=user).permission

current_contributors = draft.contributor_set.all()
for contributor in current_contributors:
signals.contributor_added.send(
draft,
contributor=user,
auth=None,
notification_type=notification_type,
permissions=initiator_permissions
contributor=contributor.user,
auth=Auth(user) if user != contributor.user else None,
notification_type=notification_type if contributor.user.is_confirmed else NotificationType.Type.USER_INVITE_DRAFT_REGISTRATION,
permissions=contributor.permission
)

return draft
Expand Down
16 changes: 8 additions & 8 deletions osf_tests/test_draft_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ def license():
def make_complex_draft_registration(title, institution, description, category,
write_contrib, license, subject, user):
def make_draft_registration(node=None):
draft_registration = DraftRegistration.create_from_node(
user=user,
schema=get_default_metaschema(),
data={},
node=node if node else None
)
with capture_notifications():
draft_registration = DraftRegistration.create_from_node(
user=user,
schema=get_default_metaschema(),
data={},
node=node if node else None
)
user.add_or_update_affiliated_institution(institution)
draft_registration.set_title(title, Auth(user))
draft_registration.set_description(description, Auth(user))
Expand Down Expand Up @@ -147,8 +148,7 @@ def test_register_draft_node(self, user, draft_node, draft_registration):

def test_draft_registration_fields_are_copied_back_to_draft_node(self, user, institution,
subject, write_contrib, title, description, category, license, make_complex_draft_registration):
with capture_notifications():
draft_registration = make_complex_draft_registration()
draft_registration = make_complex_draft_registration()
draft_node = draft_registration.branched_from

with disconnected_from_listeners(after_create_registration):
Expand Down
30 changes: 15 additions & 15 deletions osf_tests/test_draft_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ def test_create_from_node_existing(self, user):
node.set_subjects([[subject._id]], auth=Auth(node.creator))
node.affiliated_institutions.add(institution)
node.save()

draft = DraftRegistration.create_from_node(
node=node,
user=user,
schema=factories.get_default_metaschema(),
)
with capture_notifications():
draft = DraftRegistration.create_from_node(
node=node,
user=user,
schema=factories.get_default_metaschema(),
)

# Assert existing metadata-like node attributes are copied to the draft
assert draft.title == title
Expand Down Expand Up @@ -292,15 +292,15 @@ def test_can_view_property(self, user):
write_contrib = factories.UserFactory()
read_contrib = factories.UserFactory()
non_contrib = factories.UserFactory()

draft = DraftRegistration.create_from_node(
user=user,
node=project,
schema=factories.get_default_metaschema()
)
project.add_contributor(non_contrib, ADMIN, save=True)
draft.add_contributor(write_contrib, WRITE, save=True)
draft.add_contributor(read_contrib, READ, save=True)
with capture_notifications():
draft = DraftRegistration.create_from_node(
user=user,
node=project,
schema=factories.get_default_metaschema()
)
project.add_contributor(non_contrib, ADMIN, save=True)
draft.add_contributor(write_contrib, WRITE, save=True)
draft.add_contributor(read_contrib, READ, save=True)

assert draft.get_permissions(user) == [READ, WRITE, ADMIN]
assert draft.get_permissions(write_contrib) == [READ, WRITE]
Expand Down
13 changes: 7 additions & 6 deletions osf_tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2565,12 +2565,13 @@ def test_create_from_node(self):
user = proj.creator
schema = RegistrationSchema.objects.first()
data = {'some': 'data'}
draft = DraftRegistration.create_from_node(
node=proj,
user=user,
schema=schema,
data=data,
)
with capture_notifications():
draft = DraftRegistration.create_from_node(
node=proj,
user=user,
schema=schema,
data=data,
)
assert user == draft.initiator
assert schema == draft.registration_schema
assert data == draft.registration_metadata
Expand Down
14 changes: 8 additions & 6 deletions osf_tests/test_private_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .factories import PrivateLinkFactory, NodeFactory
from osf.models import RegistrationSchema, DraftRegistration, NodeLog
from tests.utils import capture_notifications

@pytest.mark.django_db
def test_factory():
Expand Down Expand Up @@ -50,12 +51,13 @@ def test_create_from_node(self):
user = proj.creator
schema = RegistrationSchema.objects.first()
data = {'some': 'data'}
draft = DraftRegistration.create_from_node(
node=proj,
user=user,
schema=schema,
data=data,
)
with capture_notifications():
draft = DraftRegistration.create_from_node(
node=proj,
user=user,
schema=schema,
data=data,
)
assert user == draft.initiator
assert schema == draft.registration_schema
assert data == draft.registration_metadata
Expand Down
2 changes: 1 addition & 1 deletion osf_tests/test_registration_moderation_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_submit_notifications(self, registration, moderator, admin, contrib, pro
assert notification['emits'][1]['type'] == NotificationType.Type.PROVIDER_REVIEWS_SUBMISSION_CONFIRMATION
assert notification['emits'][1]['kwargs']['user'] == contrib
assert notification['emits'][2]['type'] == NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS
assert NotificationSubscription.objects.count() == 6
assert NotificationSubscription.objects.count() == 7
digest = NotificationSubscription.objects.last()
assert digest.user == moderator

Expand Down
Loading
Loading