def setUp(self): super(TestRegistrationCreate, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'Replication Recipe (Brandt et al., 2013): Post-Completion') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project, registration_metadata = { 'item29': {'value': 'Yes'}, 'item33': {'value': 'success'} } ) self.url = '/{}nodes/{}/registrations/'.format(API_BASE, self.public_project._id) self.payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "immediate" } } }
def test_required_third_level_questions_must_be_answered_on_draft(self, mock_enqueue): prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, branched_from=self.public_project ) registration_metadata = self.prereg_metadata(prereg_draft_registration) registration_metadata['q11'] = {'value': {"question": {}}} prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": prereg_draft_registration._id, } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "'value' is a required property")
class TestDraftListView(AdminTestCase): def setUp(self): super(TestDraftListView, self).setUp() self.user = AuthUserFactory() schema = utils.draft_reg_util() self.dr1 = DraftRegistrationFactory( initiator=self.user, registration_schema=schema, registration_metadata=utils.SCHEMA_DATA) self.dr1.submit_for_review(self.user, {}, save=True) self.dr2 = DraftRegistrationFactory( initiator=self.user, registration_schema=schema, registration_metadata=utils.SCHEMA_DATA) self.dr2.submit_for_review(self.user, {}, save=True) self.request = RequestFactory().get('/fake_path') self.view = DraftListView() self.view = setup_view(self.view, self.request) def test_get_queryset(self): res = list(self.view.get_queryset()) nt.assert_equal(len(res), 2) nt.assert_is_instance(res[0], DraftRegistration) def test_get_context_data(self): self.view.object_list = self.view.get_queryset() res = self.view.get_context_data() nt.assert_is_instance(res, dict) nt.assert_is_instance(res['drafts'], list) nt.assert_equal(len(res['drafts']), 2)
def setUp(self): super(TestPreregFiles, self).setUp() self.prereg_user = AuthUserFactory() self.user = AuthUserFactory() self.node = ProjectFactory(creator=self.user) ensure_schemas() prereg_schema = get_prereg_schema() self.d_of_qs = { 'q7': OsfStorageFileNode(node=self.node, name='7'), 'q11': OsfStorageFileNode(node=self.node, name='11'), 'q16': OsfStorageFileNode(node=self.node, name='16'), 'q12': OsfStorageFileNode(node=self.node, name='12'), 'q13': OsfStorageFileNode(node=self.node, name='13'), 'q19': OsfStorageFileNode(node=self.node, name='19'), 'q26': OsfStorageFileNode(node=self.node, name='26') } data = {} for q, f in self.d_of_qs.iteritems(): guid = f.get_guid(create=True)._id f.save() if q == 'q26': data[q] = { 'comments': [], 'value': '26', 'extra': [ { 'data': { 'provider': 'osfstorage', 'path': f.path, }, 'fileId': guid, 'nodeId': self.node._id, } ] } continue data[q] = { 'value': { 'uploader': { 'extra': [ { 'data': { 'provider': 'osfstorage', 'path': f.path, }, 'fileId': guid, 'nodeId': self.node._id, } ] } } } self.draft = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, registration_metadata=data ) self.prereg_user.save() self.admin_user = UserFactory(osf_id=self.prereg_user.pk)
def test_required_metaschema_questions_not_required_on_post(self): prereg_schema = MetaSchema.find_one( Q("name", "eq", "Prereg Challenge") & Q("schema_version", "eq", LATEST_SCHEMA_VERSION) ) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema._id, branched_from=self.public_project ) url = "/{}nodes/{}/draft_registrations/?embed=initiator&embed=branched_from".format( API_BASE, self.public_project._id ) registration_metadata = self.prereg_metadata(prereg_draft_registration) del registration_metadata["q1"] prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "draft_registrations", "attributes": { "registration_supplement": prereg_schema._id, "registration_metadata": registration_metadata, }, } } res = self.app.post_json_api(url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 201) data = res.json["data"] assert_equal(res.json["data"]["attributes"]["registration_metadata"]["q2"]["value"], "Test response") assert_equal(data["attributes"]["registration_supplement"], prereg_schema._id) assert_equal(data["embeds"]["branched_from"]["data"]["id"], self.public_project._id) assert_equal(data["embeds"]["initiator"]["data"]["id"], self.user._id)
class TestDraftListView(AdminTestCase): def setUp(self): super(TestDraftListView, self).setUp() self.user = AuthUserFactory() schema = utils.draft_reg_util() self.dr1 = DraftRegistrationFactory( initiator=self.user, registration_schema=schema, registration_metadata=utils.SCHEMA_DATA ) self.dr1.submit_for_review(self.user, {}, save=True) self.dr2 = DraftRegistrationFactory( initiator=self.user, registration_schema=schema, registration_metadata=utils.SCHEMA_DATA ) self.dr2.submit_for_review(self.user, {}, save=True) self.request = RequestFactory().get('/fake_path') self.view = DraftListView() self.view = setup_view(self.view, self.request) def test_get_queryset(self): res = list(self.view.get_queryset()) nt.assert_equal(len(res), 2) nt.assert_is_instance(res[0], DraftRegistration) def test_get_context_data(self): self.view.object_list = self.view.get_queryset() res = self.view.get_context_data() nt.assert_is_instance(res, dict) nt.assert_is_instance(res['drafts'], list) nt.assert_equal(len(res['drafts']), 2)
def test_required_third_level_questions_must_be_answered_on_draft(self, mock_enqueue): prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, branched_from=self.public_project ) registration_metadata = self.prereg_metadata(prereg_draft_registration) registration_metadata['q11'] = {'value': {"question": {}}} prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": prereg_draft_registration._id, } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "'value' is a required property")
def test_get_draft_registrations_only_gets_drafts_for_that_node(self): dummy = NodeFactory() # Drafts for dummy node for i in range(5): d = DraftRegistrationFactory(initiator=self.user, branched_from=dummy, meta_schema=self.meta_schema, schema_data={}) found = [self.draft] # Drafts for self.node for i in range(3): d = DraftRegistrationFactory(initiator=self.user, branched_from=self.node, meta_schema=self.meta_schema, schema_data={}) found.append(d) url = self.node.api_url_for('get_draft_registrations') res = self.app.get(url, auth=self.user.auth) assert_equal(res.status_code, http.OK) # 3 new, 1 from setUp assert_equal(len(res.json['drafts']), 4) for draft in res.json['drafts']: assert_in(draft['pk'], [f._id for f in found])
class TestDraftRegistrationDelete(DraftRegistrationTestCase): def setUp(self): super(TestDraftRegistrationDelete, self).setUp() ensure_schemas() schema = MetaSchema.find_one( Q('name', 'eq', 'OSF-Standard Pre-Data Collection Registration') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=schema, branched_from=self.public_project ) self.other_project = ProjectFactory(creator=self.user) self.url = '/{}nodes/{}/draft_registrations/{}/'.format(API_BASE, self.public_project._id, self.draft_registration._id) def test_admin_can_delete_draft(self): res = self.app.delete_json_api(self.url, auth=self.user.auth) assert_equal(res.status_code, 204) def test_read_only_contributor_cannot_delete_draft(self): res = self.app.delete_json_api(self.url, auth=self.read_only_user.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_read_write_contributor_cannot_delete_draft(self): res = self.app.delete_json_api(self.url, auth=self.read_write_user.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_logged_in_non_contributor_cannot_delete_draft(self): res = self.app.delete_json_api(self.url, auth=self.non_contributor.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_unauthenticated_user_cannot_delete_draft(self): res = self.app.delete_json_api(self.url, expect_errors=True) assert_equal(res.status_code, 401) def test_draft_that_has_been_registered_cannot_be_deleted(self): reg = RegistrationFactory(project=self.public_project) self.draft_registration.registered_node = reg self.draft_registration.save() res = self.app.delete_json_api(self.url, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_equal(res.json['errors'][0]['detail'], 'This draft has already been registered and cannot be modified.') def test_reviewer_cannot_delete_draft_registration(self): user = AuthUserFactory() user.system_tags.append(PREREG_ADMIN_TAG) user.save() res = self.app.delete_json_api(self.url, auth=user.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_equal(res.json['errors'][0]['detail'], 'You do not have permission to perform this action.')
def setUp(self): super(TestDraftDetailView, self).setUp() self.user = AuthUserFactory() schema = utils.draft_reg_util() self.dr1 = DraftRegistrationFactory( initiator=self.user, registration_schema=schema, registration_metadata=utils.SCHEMA_DATA) self.dr1.submit_for_review(self.user, {}, save=True) self.request = RequestFactory().get('/fake_path') self.view = DraftDetailView() self.view = setup_view(self.view, self.request, draft_pk=self.dr1._id)
def setUp(self): super(TestCommentUpdateView, self).setUp() self.user = AuthUserFactory() self.dr1 = DraftRegistrationFactory( initiator=self.user, registration_schema=utils.draft_reg_util(), registration_metadata=utils.SCHEMA_DATA) self.dr1.submit_for_review(self.user, {}, save=True) self.request = RequestFactory().post('/fake_path', data={'blah': 'arg'}) self.request.user = UserFactory() self.view = CommentUpdateView() self.view = setup_view(self.view, self.request, draft_pk=self.dr1._id)
def setUp(self): super(TestDraftRegistrationList, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'Open-Ended Registration') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION)) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project) self.url = '/{}nodes/{}/draft_registrations/'.format( API_BASE, self.public_project._id)
def setUp(self): super(TestDraftRegistrationUpdate, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'OSF-Standard Pre-Data Collection Registration') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project ) self.prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.prereg_schema, branched_from=self.public_project ) self.registration_metadata = self.prereg_metadata(self.prereg_draft_registration) self.other_project = ProjectFactory(creator=self.user) self.url = '/{}nodes/{}/draft_registrations/{}/'.format(API_BASE, self.public_project._id, self.draft_registration._id) self.payload = { "data": { "id": self.draft_registration._id, "type": "draft_registrations", "attributes": { "registration_metadata": { "datacompletion": { "value": "No, data collection has not begun" }, "looked": { "value": "No" }, "comments": { "value": "This is my first registration." } } } } }
def setUp(self): super(TestDraftPreregChallengeRegistrationMetadataValidation, self).setUp() ensure_schemas() self.prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.prereg_schema, branched_from=self.public_project ) self.other_project = ProjectFactory(creator=self.user) self.url = '/{}nodes/{}/draft_registrations/{}/'.format(API_BASE, self.public_project._id, self.prereg_draft_registration._id) self.payload = { "data": { "id": self.prereg_draft_registration._id, "type": "draft_registrations", "attributes": { "registration_metadata": {} } } }
def test_registration_draft_must_be_draft_of_current_node( self, mock_enqueue): new_project = ProjectFactory(creator=self.user) draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=new_project, registration_metadata={ 'item29': { 'value': 'Yes' }, 'item33': { 'value': 'success' } }) payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": draft_registration._id } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal( res.json['errors'][0]['detail'], 'This draft registration is not created from the given node.')
def test_multiple_choice_in_registration_schema_must_match_one_of_choices( self, mock_enqueue): draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project, registration_metadata={ 'item29': { 'value': 'Yes' }, 'item33': { 'value': 'success!' } }) self.payload['data']['attributes'][ 'draft_registration'] = draft_registration._id res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal( res.json['errors'][0]['detail'], "u'success!' is not one of [u'success', u'informative failure to replicate'," " u'practical failure to replicate', u'inconclusive']")
def setUp(self): super(TestRegistrationCreate, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'Replication Recipe (Brandt et al., 2013): Post-Completion') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project, registration_metadata = { 'item29': {'value': 'Yes'}, 'item33': {'value': 'success'} } ) self.url = '/{}nodes/{}/registrations/'.format(API_BASE, self.public_project._id) self.payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "immediate" } } }
def setUp(self): super(RegistrationEmbargoViewsTestCase, self).setUp() ensure_schemas() self.user = AuthUserFactory() self.project = ProjectFactory(creator=self.user) self.draft = DraftRegistrationFactory(branched_from=self.project) self.registration = RegistrationFactory(project=self.project, creator=self.user) current_month = datetime.datetime.now().strftime("%B") current_year = datetime.datetime.now().strftime("%Y") self.valid_make_public_payload = json.dumps({ u'embargoEndDate': u'Fri, 01, {month} {year} 00:00:00 GMT'.format( month=current_month, year=current_year ), u'registrationChoice': 'immediate', u'summary': unicode(fake.sentence()) }) valid_date = datetime.datetime.now() + datetime.timedelta(days=180) self.valid_embargo_payload = json.dumps({ u'embargoEndDate': unicode(valid_date.strftime('%a, %d, %B %Y %H:%M:%S')) + u' GMT', u'registrationChoice': 'embargo', u'summary': unicode(fake.sentence()) }) self.invalid_embargo_date_payload = json.dumps({ u'embargoEndDate': u"Thu, 01 {month} {year} 05:00:00 GMT".format( month=current_month, year=str(int(current_year)-1) ), u'registrationChoice': 'embargo', u'summary': unicode(fake.sentence()) })
def setUp(self): super(TestDraftRegistrationDelete, self).setUp() ensure_schemas() schema = MetaSchema.find_one( Q('name', 'eq', 'OSF-Standard Pre-Data Collection Registration') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=schema, branched_from=self.public_project ) self.other_project = ProjectFactory(creator=self.user) self.url = '/{}nodes/{}/draft_registrations/{}/'.format(API_BASE, self.public_project._id, self.draft_registration._id)
class TestDraftDetailView(AdminTestCase): def setUp(self): super(TestDraftDetailView, self).setUp() self.user = AuthUserFactory() schema = utils.draft_reg_util() self.dr1 = DraftRegistrationFactory( initiator=self.user, registration_schema=schema, registration_metadata=utils.SCHEMA_DATA) self.dr1.submit_for_review(self.user, {}, save=True) self.request = RequestFactory().get('/fake_path') self.view = DraftDetailView() self.view = setup_view(self.view, self.request, draft_pk=self.dr1._id) def test_get_object(self): res = self.view.get_object() nt.assert_is_instance(res, dict) nt.assert_equal(res['pk'], self.dr1._id)
def setUp(self): super(RegistrationEmbargoModelsTestCase, self).setUp() self.user = UserFactory() self.project = ProjectFactory(creator=self.user) self.draft = DraftRegistrationFactory(branched_from=self.project, initiator=self.user) self.registration = RegistrationFactory(project=self.project) self.embargo = EmbargoFactory(user=self.user) self.valid_embargo_end_date = datetime.datetime.utcnow( ) + datetime.timedelta(days=3)
def setUp(self): super(TestDraftListView, self).setUp() self.user = AuthUserFactory() schema = utils.draft_reg_util() self.dr1 = DraftRegistrationFactory( initiator=self.user, registration_schema=schema, registration_metadata=utils.SCHEMA_DATA ) self.dr1.submit_for_review(self.user, {}, save=True) self.dr2 = DraftRegistrationFactory( initiator=self.user, registration_schema=schema, registration_metadata=utils.SCHEMA_DATA ) self.dr2.submit_for_review(self.user, {}, save=True) self.request = RequestFactory().get('/fake_path') self.view = DraftListView() self.view = setup_view(self.view, self.request)
class TestDraftDetailView(AdminTestCase): def setUp(self): super(TestDraftDetailView, self).setUp() self.user = AuthUserFactory() schema = utils.draft_reg_util() self.dr1 = DraftRegistrationFactory( initiator=self.user, registration_schema=schema, registration_metadata=utils.SCHEMA_DATA ) self.dr1.submit_for_review(self.user, {}, save=True) self.request = RequestFactory().get('/fake_path') self.view = DraftDetailView() self.view = setup_view(self.view, self.request, draft_pk=self.dr1._id) def test_get_object(self): res = self.view.get_object() nt.assert_is_instance(res, dict) nt.assert_equal(res['pk'], self.dr1._id)
def setUp(self): super(TestDraftFormView, self).setUp() self.user = AuthUserFactory() self.dr1 = DraftRegistrationFactory( initiator=self.user, registration_schema=utils.draft_reg_util(), registration_metadata=utils.SCHEMA_DATA) self.dr1.submit_for_review(self.user, {}, save=True) self.dr1.flags # sets flags if there aren't any yet. self.request = RequestFactory().get('/fake_path') self.view = DraftFormView() self.view = setup_view(self.view, self.request, draft_pk=self.dr1._id) self.post = RequestFactory().post('/fake_path') self.post.user = UserFactory() self.post_view = DraftFormView() self.form_data = { 'notes': 'Far between', 'proof_of_publication': 'approved', }
def test_required_metaschema_questions_not_required_on_post(self): prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION)) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema._id, branched_from=self.public_project) url = '/{}nodes/{}/draft_registrations/?embed=initiator&embed=branched_from'.format( API_BASE, self.public_project._id) registration_metadata = self.prereg_metadata(prereg_draft_registration) del registration_metadata['q1'] prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "draft_registrations", "attributes": { "registration_supplement": prereg_schema._id, "registration_metadata": registration_metadata } } } res = self.app.post_json_api(url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 201) data = res.json['data'] assert_equal( res.json['data']['attributes']['registration_metadata']['q2'] ['value'], 'Test response') assert_equal(data['attributes']['registration_supplement'], prereg_schema._id) assert_equal(data['embeds']['branched_from']['data']['id'], self.public_project._id) assert_equal(data['embeds']['initiator']['data']['id'], self.user._id)
def setUp(self): super(TestDraftRegistrationList, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q("name", "eq", "Open-Ended Registration") & Q("schema_version", "eq", LATEST_SCHEMA_VERSION) ) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project ) self.url = "/{}nodes/{}/draft_registrations/".format(API_BASE, self.public_project._id)
class TestCommentUpdateView(AdminTestCase): def setUp(self): super(TestCommentUpdateView, self).setUp() self.user = AuthUserFactory() self.dr1 = DraftRegistrationFactory( initiator=self.user, registration_schema=utils.draft_reg_util(), registration_metadata=utils.SCHEMA_DATA ) self.dr1.submit_for_review(self.user, {}, save=True) self.request = RequestFactory().post('/fake_path', data={'blah': 'arg'}) self.request.user = UserFactory() self.view = CommentUpdateView() self.view = setup_view(self.view, self.request, draft_pk=self.dr1._id) @mock.patch('admin.pre_reg.views.json.loads') @mock.patch('admin.pre_reg.views.DraftRegistration.update_metadata') def test_post_comments(self, mock_json, mock_meta): count = OSFLogEntry.objects.count() self.view.post(self.request) nt.assert_equal(OSFLogEntry.objects.count(), count + 1)
class TestCommentUpdateView(AdminTestCase): def setUp(self): super(TestCommentUpdateView, self).setUp() self.user = AuthUserFactory() self.dr1 = DraftRegistrationFactory( initiator=self.user, registration_schema=utils.draft_reg_util(), registration_metadata=utils.SCHEMA_DATA) self.dr1.submit_for_review(self.user, {}, save=True) self.request = RequestFactory().post('/fake_path', data={'blah': 'arg'}) self.request.user = UserFactory() self.view = CommentUpdateView() self.view = setup_view(self.view, self.request, draft_pk=self.dr1._id) @mock.patch('admin.pre_reg.views.json.loads') @mock.patch('admin.pre_reg.views.DraftRegistration.update_metadata') def test_post_comments(self, mock_json, mock_meta): count = OSFLogEntry.objects.count() self.view.post(self.request) nt.assert_equal(OSFLogEntry.objects.count(), count + 1)
def test_factory(self): draft = DraftRegistrationFactory() assert_is_not_none(draft.branched_from) assert_is_not_none(draft.initiator) assert_is_not_none(draft.registration_schema) user = AuthUserFactory() draft = DraftRegistrationFactory(initiator=user) assert_equal(draft.initiator, user) node = ProjectFactory() draft = DraftRegistrationFactory(branched_from=node) assert_equal(draft.branched_from, node) assert_equal(draft.initiator, node.creator) # Pick an arbitrary v2 schema schema = MetaSchema.find(Q('schema_version', 'eq', 2))[0] data = {'some': 'data'} draft = DraftRegistrationFactory(registration_schema=schema, registration_metadata=data) assert_equal(draft.registration_schema, schema) assert_equal(draft.registration_metadata, data)
def setUp(self): super(TestCommentUpdateView, self).setUp() self.user = AuthUserFactory() self.dr1 = DraftRegistrationFactory( initiator=self.user, registration_schema=utils.draft_reg_util(), registration_metadata=utils.SCHEMA_DATA ) self.dr1.submit_for_review(self.user, {}, save=True) self.request = RequestFactory().post('/fake_path', data={'blah': 'arg'}) self.request.user = UserFactory() self.view = CommentUpdateView() self.view = setup_view(self.view, self.request, draft_pk=self.dr1._id)
def setUp(self): super(RegistrationsTestBase, self).setUp() self.user = AuthUserFactory() self.auth = Auth(self.user) self.node = ProjectFactory(creator=self.user) self.non_admin = AuthUserFactory() self.node.add_contributor( self.non_admin, permissions.DEFAULT_CONTRIBUTOR_PERMISSIONS, auth=self.auth, save=True ) self.non_contrib = AuthUserFactory() MetaSchema.remove() ensure_schemas() self.meta_schema = MetaSchema.find_one( Q('name', 'eq', 'Open-Ended Registration') & Q('schema_version', 'eq', 2) ) self.draft = DraftRegistrationFactory( initiator=self.user, branched_from=self.node, registration_schema=self.meta_schema, registration_metadata={ 'summary': {'value': 'Some airy'} } ) current_month = dt.datetime.now().strftime("%B") current_year = dt.datetime.now().strftime("%Y") valid_date = dt.datetime.now() + dt.timedelta(days=180) self.embargo_payload = { u'embargoEndDate': unicode(valid_date.strftime('%a, %d, %B %Y %H:%M:%S')) + u' GMT', u'registrationChoice': 'embargo' } self.invalid_embargo_date_payload = { u'embargoEndDate': u"Thu, 01 {month} {year} 05:00:00 GMT".format( month=current_month, year=str(int(current_year) - 1) ), u'registrationChoice': 'embargo' } self.immediate_payload = { 'registrationChoice': 'immediate' } self.invalid_payload = { 'registrationChoice': 'foobar' }
def setUp(self): super(TestDraftFormView, self).setUp() self.user = AuthUserFactory() self.dr1 = DraftRegistrationFactory( initiator=self.user, registration_schema=utils.draft_reg_util(), registration_metadata=utils.SCHEMA_DATA ) self.dr1.submit_for_review(self.user, {}, save=True) self.dr1.flags # sets flags if there aren't any yet. self.request = RequestFactory().get('/fake_path') self.view = DraftFormView() self.view = setup_view(self.view, self.request, draft_pk=self.dr1._id) self.post = RequestFactory().post('/fake_path') self.post.user = UserFactory() self.post_view = DraftFormView() self.form_data = { 'notes': 'Far between', 'proof_of_publication': 'approved', }
class TestDraftRegistrationList(DraftRegistrationTestCase): def setUp(self): super(TestDraftRegistrationList, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q("name", "eq", "Open-Ended Registration") & Q("schema_version", "eq", LATEST_SCHEMA_VERSION) ) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project ) self.url = "/{}nodes/{}/draft_registrations/".format(API_BASE, self.public_project._id) def test_admin_can_view_draft_list(self): res = self.app.get(self.url, auth=self.user.auth) assert_equal(res.status_code, 200) data = res.json["data"] assert_equal(len(data), 1) assert_equal(data[0]["attributes"]["registration_supplement"], self.schema._id) assert_equal(data[0]["id"], self.draft_registration._id) assert_equal(data[0]["attributes"]["registration_metadata"], {}) def test_read_only_contributor_cannot_view_draft_list(self): res = self.app.get(self.url, auth=self.read_only_user.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_read_write_contributor_cannot_view_draft_list(self): res = self.app.get(self.url, auth=self.read_write_user.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_logged_in_non_contributor_cannot_view_draft_list(self): res = self.app.get(self.url, auth=self.non_contributor.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_unauthenticated_user_cannot_view_draft_list(self): res = self.app.get(self.url, expect_errors=True) assert_equal(res.status_code, 401) def test_draft_with_registered_node_does_not_show_up_in_draft_list(self): reg = RegistrationFactory(project=self.public_project) self.draft_registration.registered_node = reg self.draft_registration.save() res = self.app.get(self.url, auth=self.user.auth) assert_equal(res.status_code, 200) data = res.json["data"] assert_equal(len(data), 0) def test_draft_with_deleted_registered_node_shows_up_in_draft_list(self): reg = RegistrationFactory(project=self.public_project) self.draft_registration.registered_node = reg self.draft_registration.save() reg.is_deleted = True reg.save() res = self.app.get(self.url, auth=self.user.auth) assert_equal(res.status_code, 200) data = res.json["data"] assert_equal(len(data), 1) assert_equal(data[0]["attributes"]["registration_supplement"], self.schema._id) assert_equal(data[0]["id"], self.draft_registration._id) assert_equal(data[0]["attributes"]["registration_metadata"], {})
class TestDraftRegistrationList(DraftRegistrationTestCase): def setUp(self): super(TestDraftRegistrationList, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'Open-Ended Registration') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION)) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project) self.url = '/{}nodes/{}/draft_registrations/'.format( API_BASE, self.public_project._id) def test_admin_can_view_draft_list(self): res = self.app.get(self.url, auth=self.user.auth) assert_equal(res.status_code, 200) data = res.json['data'] assert_equal(len(data), 1) assert_equal(data[0]['attributes']['registration_supplement'], self.schema._id) assert_equal(data[0]['id'], self.draft_registration._id) assert_equal(data[0]['attributes']['registration_metadata'], {}) def test_read_only_contributor_cannot_view_draft_list(self): res = self.app.get(self.url, auth=self.read_only_user.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_read_write_contributor_cannot_view_draft_list(self): res = self.app.get(self.url, auth=self.read_write_user.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_logged_in_non_contributor_cannot_view_draft_list(self): res = self.app.get(self.url, auth=self.non_contributor.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_unauthenticated_user_cannot_view_draft_list(self): res = self.app.get(self.url, expect_errors=True) assert_equal(res.status_code, 401) def test_draft_with_registered_node_does_not_show_up_in_draft_list(self): reg = RegistrationFactory(project=self.public_project) self.draft_registration.registered_node = reg self.draft_registration.save() res = self.app.get(self.url, auth=self.user.auth) assert_equal(res.status_code, 200) data = res.json['data'] assert_equal(len(data), 0) def test_draft_with_deleted_registered_node_shows_up_in_draft_list(self): reg = RegistrationFactory(project=self.public_project) self.draft_registration.registered_node = reg self.draft_registration.save() reg.is_deleted = True reg.save() res = self.app.get(self.url, auth=self.user.auth) assert_equal(res.status_code, 200) data = res.json['data'] assert_equal(len(data), 1) assert_equal(data[0]['attributes']['registration_supplement'], self.schema._id) assert_equal(data[0]['id'], self.draft_registration._id) assert_equal(data[0]['attributes']['registration_metadata'], {})
class TestDraftRegistrationUpdate(DraftRegistrationTestCase): def setUp(self): super(TestDraftRegistrationUpdate, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'OSF-Standard Pre-Data Collection Registration') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project ) self.prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.prereg_schema, branched_from=self.public_project ) self.registration_metadata = self.prereg_metadata(self.prereg_draft_registration) self.other_project = ProjectFactory(creator=self.user) self.url = '/{}nodes/{}/draft_registrations/{}/'.format(API_BASE, self.public_project._id, self.draft_registration._id) self.payload = { "data": { "id": self.draft_registration._id, "type": "draft_registrations", "attributes": { "registration_metadata": { "datacompletion": { "value": "No, data collection has not begun" }, "looked": { "value": "No" }, "comments": { "value": "This is my first registration." } } } } } def test_id_required_in_payload(self): payload = { "data": { "type": "draft_registrations", "attributes": { "registration_metadata": {} } } } res = self.app.put_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) errors = res.json['errors'][0] assert_equal(errors['source']['pointer'], '/data/id') assert_equal(errors['detail'], 'This field may not be null.') def test_admin_can_update_draft(self): res = self.app.put_json_api(self.url, self.payload, auth=self.user.auth) assert_equal(res.status_code, 200) data = res.json['data'] assert_equal(data['attributes']['registration_supplement'], self.schema._id) assert_equal(data['attributes']['registration_metadata'], self.payload['data']['attributes']['registration_metadata']) def test_draft_must_be_branched_from_node(self): url = '/{}nodes/{}/draft_registrations/{}/'.format(API_BASE, self.other_project._id, self.draft_registration._id) res = self.app.put_json_api(url, self.payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) errors = res.json['errors'][0] assert_equal(errors['detail'], 'This draft registration is not created from the given node.') def test_read_only_contributor_cannot_update_draft(self): res = self.app.put_json_api(self.url, self.payload, auth=self.read_only_user.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_read_write_contributor_cannot_update_draft(self): res = self.app.put_json_api(self.url, self.payload, auth=self.read_write_user.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_logged_in_non_contributor_cannot_update_draft(self): res = self.app.put_json_api(self.url, self.payload, auth=self.non_contributor.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_unauthenticated_user_cannot_update_draft(self): res = self.app.put_json_api(self.url, self.payload, expect_errors=True) assert_equal(res.status_code, 401) def test_registration_metadata_must_be_supplied(self): self.payload['data']['attributes'] = {} res = self.app.put_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) errors = res.json['errors'][0] assert_equal(res.status_code, 400) assert_equal(errors['source']['pointer'], '/data/attributes/registration_metadata') assert_equal(errors['detail'], 'This field is required.') def test_registration_metadata_must_be_a_dictionary(self): self.payload['data']['attributes']['registration_metadata'] = 'Registration data' res = self.app.put_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) errors = res.json['errors'][0] assert_equal(res.status_code, 400) assert_equal(errors['source']['pointer'], '/data/attributes/registration_metadata') assert_equal(errors['detail'], 'Expected a dictionary of items but got type "unicode".') def test_registration_metadata_question_values_must_be_dictionaries(self): self.payload['data']['attributes']['registration_metadata']['datacompletion'] = 'No, data collection has not begun' res = self.app.put_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) errors = res.json['errors'][0] assert_equal(res.status_code, 400) assert_equal(errors['detail'], "u'No, data collection has not begun' is not of type 'object'") def test_registration_metadata_question_keys_must_be_value(self): self.payload['data']['attributes']['registration_metadata']['datacompletion'] = { "incorrect_key": "No, data collection has not begun" } res = self.app.put_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) errors = res.json['errors'][0] assert_equal(res.status_code, 400) assert_equal(errors['detail'], "Additional properties are not allowed (u'incorrect_key' was unexpected)") def test_question_in_registration_metadata_must_be_in_schema(self): self.payload['data']['attributes']['registration_metadata']['q11'] = { "value": "No, data collection has not begun" } res = self.app.put_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) errors = res.json['errors'][0] assert_equal(res.status_code, 400) assert_equal(errors['detail'], "Additional properties are not allowed (u'q11' was unexpected)") def test_multiple_choice_question_value_must_match_value_in_schema(self): self.payload['data']['attributes']['registration_metadata']['datacompletion'] = { "value": "Nope, data collection has not begun" } res = self.app.put_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) errors = res.json['errors'][0] assert_equal(res.status_code, 400) assert_equal(errors['detail'], "u'Nope, data collection has not begun' is not one of [u'No, data collection has not begun', u'Yes, data collection is underway or complete']") def test_cannot_update_registration_schema(self): self.payload['data']['attributes']['registration_supplement'] = self.prereg_schema._id res = self.app.put_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 200) assert_equal(res.json['data']['attributes']['registration_supplement'], self.schema._id) def test_required_metaschema_questions_not_required_on_update(self): url = '/{}nodes/{}/draft_registrations/{}/'.format(API_BASE, self.public_project._id, self.prereg_draft_registration._id) del self.registration_metadata['q1'] self.prereg_draft_registration.registration_metadata = self.registration_metadata self.prereg_draft_registration.save() payload = { "data": { "id": self.prereg_draft_registration._id, "type": "draft_registrations", "attributes": { "registration_metadata": { 'q2': { 'value': 'New response' } } } } } res = self.app.put_json_api(url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 200) assert_equal(res.json['data']['attributes']['registration_metadata']['q2']['value'], 'New response') assert_not_in('q1', res.json['data']['attributes']['registration_metadata']) def test_reviewer_can_update_draft_registration(self): user = AuthUserFactory() user.system_tags.append(PREREG_ADMIN_TAG) user.save() payload = { "data": { "id": self.prereg_draft_registration._id, "type": "draft_registrations", "attributes": { "registration_metadata": { 'q2': { 'comments': [{'value': 'This is incomplete.'}] } } } } } url = '/{}nodes/{}/draft_registrations/{}/'.format(API_BASE, self.public_project._id, self.prereg_draft_registration._id) res = self.app.put_json_api(url, payload, auth=user.auth, expect_errors=True) assert_equal(res.status_code, 200) assert_equal(res.json['data']['attributes']['registration_metadata']['q2']['comments'][0]['value'], 'This is incomplete.') assert_not_in('q1', res.json['data']['attributes']['registration_metadata']) def test_reviewer_can_only_update_comment_fields_draft_registration(self): user = AuthUserFactory() user.system_tags.append(PREREG_ADMIN_TAG) user.save() payload = { "data": { "id": self.prereg_draft_registration._id, "type": "draft_registrations", "attributes": { "registration_metadata": { 'q2': { 'value': 'Test response' } } } } } url = '/{}nodes/{}/draft_registrations/{}/'.format(API_BASE, self.public_project._id, self.prereg_draft_registration._id) res = self.app.put_json_api(url, payload, auth=user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "Additional properties are not allowed (u'value' was unexpected)") def test_reviewer_can_update_nested_comment_fields_draft_registration(self): user = AuthUserFactory() user.system_tags.append(PREREG_ADMIN_TAG) user.save() payload = { "data": { "id": self.prereg_draft_registration._id, "type": "draft_registrations", "attributes": { "registration_metadata": { 'q7': { 'value': { 'question': { 'comments': [{'value': 'Add some clarity here.'}] } } } } } } } url = '/{}nodes/{}/draft_registrations/{}/'.format(API_BASE, self.public_project._id, self.prereg_draft_registration._id) res = self.app.put_json_api(url, payload, auth=user.auth, expect_errors=True) assert_equal(res.status_code, 200) assert_equal(res.json['data']['attributes']['registration_metadata']['q7']['value']['question']['comments'][0]['value'], 'Add some clarity here.') def test_reviewer_cannot_update_nested_value_fields_draft_registration(self): user = AuthUserFactory() user.system_tags.append(PREREG_ADMIN_TAG) user.save() payload = { "data": { "id": self.prereg_draft_registration._id, "type": "draft_registrations", "attributes": { "registration_metadata": { 'q7': { 'value': { 'question': { 'value': 'This is the answer' } } } } } } } url = '/{}nodes/{}/draft_registrations/{}/'.format(API_BASE, self.public_project._id, self.prereg_draft_registration._id) res = self.app.put_json_api(url, payload, auth=user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "Additional properties are not allowed (u'value' was unexpected)")
class TestRegistrationCreate(DraftRegistrationTestCase): def setUp(self): super(TestRegistrationCreate, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'Replication Recipe (Brandt et al., 2013): Post-Completion') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project, registration_metadata = { 'item29': {'value': 'Yes'}, 'item33': {'value': 'success'} } ) self.url = '/{}nodes/{}/registrations/'.format(API_BASE, self.public_project._id) self.payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "immediate" } } } @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_admin_can_create_registration(self, mock_enqueue): res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth) data = res.json['data']['attributes'] assert_equal(res.status_code, 201) assert_equal(data['registration'], True) assert_equal(data['pending_registration_approval'], True) assert_equal(data['public'], False) def test_write_only_contributor_cannot_create_registration(self): res = self.app.post_json_api(self.url, self.payload, auth=self.read_write_user.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_read_only_contributor_cannot_create_registration(self): res = self.app.post_json_api(self.url, self.payload, auth=self.read_only_user.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_non_authenticated_user_cannot_create_registration(self): res = self.app.post_json_api(self.url, self.payload, expect_errors=True) assert_equal(res.status_code, 401) @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_registration_draft_must_be_specified(self, mock_enqueue): payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate" } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['source']['pointer'], '/data/attributes/draft_registration') assert_equal(res.json['errors'][0]['detail'], 'This field is required.') @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_registration_draft_must_be_valid(self, mock_enqueue): payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": "12345" } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 404) @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_registration_draft_must_be_draft_of_current_node(self, mock_enqueue): new_project = ProjectFactory(creator=self.user) draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=new_project, registration_metadata = { 'item29': {'value': 'Yes'}, 'item33': {'value': 'success'} } ) payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": draft_registration._id } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], 'This draft registration is not created from the given node.') @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_required_top_level_questions_must_be_answered_on_draft(self, mock_enqueue): prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, branched_from=self.public_project ) registration_metadata = self.prereg_metadata(prereg_draft_registration) del registration_metadata['q1'] prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": prereg_draft_registration._id, } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "u'q1' is a required property") @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_required_top_level_questions_must_be_answered_on_draft(self, mock_enqueue): prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, branched_from=self.public_project ) registration_metadata = self.prereg_metadata(prereg_draft_registration) del registration_metadata['q1'] prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": prereg_draft_registration._id, } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "u'q1' is a required property") @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_required_second_level_questions_must_be_answered_on_draft(self, mock_enqueue): prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, branched_from=self.public_project ) registration_metadata = self.prereg_metadata(prereg_draft_registration) registration_metadata['q11'] = {'value': {}} prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": prereg_draft_registration._id, } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "u'question' is a required property") @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_required_third_level_questions_must_be_answered_on_draft(self, mock_enqueue): prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, branched_from=self.public_project ) registration_metadata = self.prereg_metadata(prereg_draft_registration) registration_metadata['q11'] = {'value': {"question": {}}} prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": prereg_draft_registration._id, } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "'value' is a required property") @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_multiple_choice_in_registration_schema_must_match_one_of_choices(self, mock_enqueue): draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project, registration_metadata = { 'item29': {'value': 'Yes'}, 'item33': {'value': 'success!'} } ) self.payload['data']['attributes']['draft_registration'] = draft_registration._id res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "u'success!' is not one of [u'success', u'informative failure to replicate'," " u'practical failure to replicate', u'inconclusive']") def test_invalid_registration_choice(self): self.payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "tomorrow" } } } res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['source']['pointer'], '/data/attributes/registration_choice') assert_equal(res.json['errors'][0]['detail'], '"tomorrow" is not a valid choice.') @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_embargo_end_date_provided_if_registration_choice_is_embargo(self, mock_enqueue): payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "embargo" } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], 'lift_embargo must be specified.') @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_embargo_must_be_less_than_four_years(self, mock_enqueue): today = datetime.datetime.utcnow() five_years = (today + dateutil.relativedelta.relativedelta(years=5)).strftime('%Y-%m-%dT%H:%M:%S') payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "embargo", "lift_embargo": five_years } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], 'Registrations can only be embargoed for up to four years.') @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_embargo_registration(self, mock_enqueue): today = datetime.datetime.utcnow() next_week = (today + dateutil.relativedelta.relativedelta(months=1)).strftime('%Y-%m-%dT%H:%M:%S') payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "embargo", "lift_embargo": next_week } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 201) data = res.json['data']['attributes'] assert_equal(data['registration'], True) assert_equal(data['pending_embargo_approval'], True) def test_embargo_end_date_must_be_in_the_future(self): today = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S') payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "embargo", "lift_embargo": today } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], 'Embargo end date must be at least three days in the future.') def test_invalid_embargo_end_date_format(self): today = datetime.datetime.utcnow().isoformat() payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "embargo", "lift_embargo": today } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], 'Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm:ss.') @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_cannot_register_draft_that_has_already_been_registered(self, mock_enqueue): self.draft_registration.register(auth=Auth(self.user), save=True) res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_equal(res.json['errors'][0]['detail'], 'This draft has already been registered and cannot be modified.') @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_cannot_register_draft_that_is_pending_review(self, mock_enqueue): with mock.patch.object(DraftRegistration, 'is_pending_review', mock.PropertyMock(return_value=True)): res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_equal(res.json['errors'][0]['detail'], 'This draft is pending review and cannot be modified.') def test_cannot_register_draft_that_has_already_been_approved(self): with mock.patch.object(DraftRegistration, 'requires_approval', mock.PropertyMock(return_value=True)), mock.patch.object(DraftRegistration, 'is_approved', mock.PropertyMock(return_value=True)): res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_equal(res.json['errors'][0]['detail'], 'This draft has already been approved and cannot be modified.')
class TestDraftFormView(AdminTestCase): def setUp(self): super(TestDraftFormView, self).setUp() self.user = AuthUserFactory() self.dr1 = DraftRegistrationFactory( initiator=self.user, registration_schema=utils.draft_reg_util(), registration_metadata=utils.SCHEMA_DATA ) self.dr1.submit_for_review(self.user, {}, save=True) self.dr1.flags # sets flags if there aren't any yet. self.request = RequestFactory().get('/fake_path') self.view = DraftFormView() self.view = setup_view(self.view, self.request, draft_pk=self.dr1._id) self.post = RequestFactory().post('/fake_path') self.post.user = UserFactory() self.post_view = DraftFormView() self.form_data = { 'notes': 'Far between', 'proof_of_publication': 'approved', } def test_dispatch_raise_404(self): view = setup_view(DraftFormView(), self.request, draft_pk='wrong') with nt.assert_raises(Http404): view.dispatch(self.request) def test_get_initial(self): self.view.draft = self.dr1 self.view.get_initial() res = self.view.initial nt.assert_is_instance(res, dict) nt.assert_equal(res['notes'], self.dr1.notes) nt.assert_equal(res['assignee'], self.dr1.flags['assignee']) nt.assert_equal(res['payment_sent'], self.dr1.flags['payment_sent']) nt.assert_equal(res['proof_of_publication'], self.dr1.flags['proof_of_publication']) def test_get_context_data(self): self.view.draft = self.dr1 res = self.view.get_context_data() nt.assert_is_instance(res, dict) nt.assert_in('draft', res) nt.assert_is_instance(res['draft'], dict) nt.assert_in('IMMEDIATE', res) def test_form_valid_notes(self): form = DraftRegistrationForm(data=self.form_data) nt.assert_true(form.is_valid()) view = setup_form_view(self.post_view, self.post, form, draft_pk=self.dr1._id) view.draft = self.dr1 count = OSFLogEntry.objects.count() with transaction.atomic(): view.form_valid(form) nt.assert_equal(count, OSFLogEntry.objects.count()) self.dr1.reload() nt.assert_equal(self.dr1.notes, self.form_data['notes']) @mock.patch('admin.pre_reg.views.DraftRegistration.approve') def test_form_valid_approve(self, mock_approve): self.form_data.update(approve_reject='approve') form = DraftRegistrationForm(data=self.form_data) nt.assert_true(form.is_valid()) view = setup_form_view(self.post_view, self.post, form, draft_pk=self.dr1._id) view.draft = self.dr1 count = OSFLogEntry.objects.count() with transaction.atomic(): view.form_valid(form) nt.assert_true(mock_approve.called) nt.assert_equal(count + 1, OSFLogEntry.objects.count()) @mock.patch('admin.pre_reg.views.DraftRegistration.reject') def test_form_valid_reject(self, mock_reject): self.form_data.update(approve_reject='reject') form = DraftRegistrationForm(data=self.form_data) nt.assert_true(form.is_valid()) view = setup_form_view(self.post_view, self.post, form, draft_pk=self.dr1._id) view.draft = self.dr1 count = OSFLogEntry.objects.count() with transaction.atomic(): view.form_valid(form) nt.assert_true(mock_reject.called) nt.assert_equal(count + 1, OSFLogEntry.objects.count())
class TestMigrateRegistrationExtra(OsfTestCase): def _get_test_file(self): version = models.FileVersion(identifier='1', provider='osfstorage', metadata={'sha256': '2413fb3709b05939f04cf2e92f7d0897fc2596f9ad0b8a9ea855c7bfebaae892'}) version.save() ret = models.FileNode( _id='5723787136b74e1a953d9612', name='file.txt', node=self.node, provider='osfstorage', path='/test/file.txt', materialized_path='/test/file.txt', versions=[version] ) ret.save() return ret def setUp(self): super(TestMigrateRegistrationExtra, self).setUp() self.user = UserFactory() self.node = ProjectFactory(creator=self.user) self.file = self._get_test_file() MetaSchema.remove() self.file_ans = { 'file': { 'data':{ 'kind':'file', 'extra':{ 'checkout': None, 'hashes':{ 'sha256':'1fffe6116ecfa778f9938060d5caab923ba4b8db60bd2dd57f16a72e5ef06292' }, 'downloads':0, 'version':1 }, 'modified':'2016-04-15T18:10:48', 'name':'file.txt', 'provider':'osfstorage', } } } self.complex_metadata = { 'q1': { 'value': 'Answer 1', 'extra': [] }, 'q2': { 'value': 'Answer 2', 'extra': {} }, 'q3': { 'value': 'Answer 3', 'extra': self.file_ans }, 'q4': { 'value': { 'question': { 'value': 'Answer 4', 'extra': {} }, 'uploader': { 'value': '', 'extra': {} } }, }, 'q5': { 'value': 'Answer 5', 'extra': [ { 'viewUrl': '/project/abcdef/files/osfstorage/5723787136b74e1a953d9612/', 'hasSelectedFile': True, 'selectedFileName': 'file.txt' } ] } } self.simple_metadata = { 'Summary': 'Some airy' } self.schema = get_prereg_schema() self.draft1 = DraftRegistrationFactory( registration_metadata=self.complex_metadata, registration_schema=self.schema, approval=None, registered_node=None ) self.draft2 = DraftRegistrationFactory( registration_metadata=self.simple_metadata ) def test_migrate_registration_extra(self): assert_equal(type(self.draft1.registration_metadata['q1']['extra']), list) assert_equal(type(self.draft1.registration_metadata['q2']['extra']), dict) assert_equal(type(self.draft1.registration_metadata['q2']['extra']), dict) assert_equal(type(self.draft1.registration_metadata['q4']['value']['question']['extra']), dict) assert_equal(self.draft2.registration_metadata, self.simple_metadata) main(dry=False) self.draft1.reload() self.draft2.reload() assert_equal(type(self.draft1.registration_metadata['q1']['extra']), list) assert_equal(type(self.draft1.registration_metadata['q2']['extra']), list) assert_equal(type(self.draft1.registration_metadata['q3']['extra']), list) assert_equal(self.draft1.registration_metadata['q3']['extra'][0], self.file_ans) assert_equal(type(self.draft1.registration_metadata['q4']['value']['question']['extra']), list) assert_true(self.draft1.registration_metadata['q5']['extra'][0].get('data', False)) assert_equal(type(self.draft1.registration_metadata['q5']['extra'][0]['data']), dict) assert_equal(self.draft1.registration_metadata['q5']['extra'][0]['data']['name'], 'file.txt') assert_equal(self.draft1.registration_metadata['q5']['extra'][0]['data']['sha256'], '2413fb3709b05939f04cf2e92f7d0897fc2596f9ad0b8a9ea855c7bfebaae892') assert_equal(self.draft2.registration_metadata, self.simple_metadata)
class TestRegistrationCreate(DraftRegistrationTestCase): def setUp(self): super(TestRegistrationCreate, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'Replication Recipe (Brandt et al., 2013): Post-Completion') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project, registration_metadata = { 'item29': {'value': 'Yes'}, 'item33': {'value': 'success'} } ) self.url = '/{}nodes/{}/registrations/'.format(API_BASE, self.public_project._id) self.payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "immediate" } } } @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_admin_can_create_registration(self, mock_enqueue): res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth) data = res.json['data']['attributes'] assert_equal(res.status_code, 201) assert_equal(data['registration'], True) assert_equal(data['pending_registration_approval'], True) assert_equal(data['public'], False) def test_write_only_contributor_cannot_create_registration(self): res = self.app.post_json_api(self.url, self.payload, auth=self.read_write_user.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_read_only_contributor_cannot_create_registration(self): res = self.app.post_json_api(self.url, self.payload, auth=self.read_only_user.auth, expect_errors=True) assert_equal(res.status_code, 403) def test_non_authenticated_user_cannot_create_registration(self): res = self.app.post_json_api(self.url, self.payload, expect_errors=True) assert_equal(res.status_code, 401) @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_registration_draft_must_be_specified(self, mock_enqueue): payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate" } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['source']['pointer'], '/data/attributes/draft_registration') assert_equal(res.json['errors'][0]['detail'], 'This field is required.') @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_registration_draft_must_be_valid(self, mock_enqueue): payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": "12345" } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 404) @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_registration_draft_must_be_draft_of_current_node(self, mock_enqueue): new_project = ProjectFactory(creator=self.user) draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=new_project, registration_metadata = { 'item29': {'value': 'Yes'}, 'item33': {'value': 'success'} } ) payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": draft_registration._id } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], 'This draft registration is not created from the given node.') @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_required_top_level_questions_must_be_answered_on_draft(self, mock_enqueue): prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, branched_from=self.public_project ) registration_metadata = self.prereg_metadata(prereg_draft_registration) del registration_metadata['q1'] prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": prereg_draft_registration._id, } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "u'q1' is a required property") @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_required_top_level_questions_must_be_answered_on_draft(self, mock_enqueue): prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, branched_from=self.public_project ) registration_metadata = self.prereg_metadata(prereg_draft_registration) del registration_metadata['q1'] prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": prereg_draft_registration._id, } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "u'q1' is a required property") @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_required_second_level_questions_must_be_answered_on_draft(self, mock_enqueue): prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, branched_from=self.public_project ) registration_metadata = self.prereg_metadata(prereg_draft_registration) registration_metadata['q11'] = {'value': {}} prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": prereg_draft_registration._id, } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "u'question' is a required property") @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_required_third_level_questions_must_be_answered_on_draft(self, mock_enqueue): prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, branched_from=self.public_project ) registration_metadata = self.prereg_metadata(prereg_draft_registration) registration_metadata['q11'] = {'value': {"question": {}}} prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": prereg_draft_registration._id, } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "'value' is a required property") @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_multiple_choice_in_registration_schema_must_match_one_of_choices(self, mock_enqueue): draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project, registration_metadata = { 'item29': {'value': 'Yes'}, 'item33': {'value': 'success!'} } ) self.payload['data']['attributes']['draft_registration'] = draft_registration._id res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "u'success!' is not one of [u'success', u'informative failure to replicate'," " u'practical failure to replicate', u'inconclusive']") def test_invalid_registration_choice(self): self.payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "tomorrow" } } } res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['source']['pointer'], '/data/attributes/registration_choice') assert_equal(res.json['errors'][0]['detail'], '"tomorrow" is not a valid choice.') @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_embargo_end_date_provided_if_registration_choice_is_embargo(self, mock_enqueue): payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "embargo" } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], 'lift_embargo must be specified.') @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_embargo_must_be_less_than_four_years(self, mock_enqueue): today = datetime.datetime.utcnow() five_years = (today + dateutil.relativedelta.relativedelta(years=5)).strftime('%Y-%m-%dT%H:%M:%S') payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "embargo", "lift_embargo": five_years } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], 'Registrations can only be embargoed for up to four years.') @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_embargo_registration(self, mock_enqueue): today = datetime.datetime.utcnow() next_week = (today + dateutil.relativedelta.relativedelta(months=1)).strftime('%Y-%m-%dT%H:%M:%S') payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "embargo", "lift_embargo": next_week } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 201) data = res.json['data']['attributes'] assert_equal(data['registration'], True) assert_equal(data['pending_embargo_approval'], True) def test_embargo_end_date_must_be_in_the_future(self): today = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S') payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "embargo", "lift_embargo": today } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], 'Embargo end date must be at least three days in the future.') def test_invalid_embargo_end_date_format(self): today = datetime.datetime.utcnow().isoformat() payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "embargo", "lift_embargo": today } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], 'Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm:ss.') @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_cannot_register_draft_that_has_already_been_registered(self, mock_enqueue): self.draft_registration.register(auth=Auth(self.user), save=True) res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_equal(res.json['errors'][0]['detail'], 'This draft has already been registered and cannot be modified.') @mock.patch('framework.celery_tasks.handlers.enqueue_task') def test_cannot_register_draft_that_is_pending_review(self, mock_enqueue): with mock.patch.object(DraftRegistration, 'is_pending_review', mock.PropertyMock(return_value=True)): res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_equal(res.json['errors'][0]['detail'], 'This draft is pending review and cannot be modified.') def test_cannot_register_draft_that_has_already_been_approved(self): with mock.patch.object(DraftRegistration, 'requires_approval', mock.PropertyMock(return_value=True)), mock.patch.object(DraftRegistration, 'is_approved', mock.PropertyMock(return_value=True)): res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_equal(res.json['errors'][0]['detail'], 'This draft has already been approved and cannot be modified.')
class TestPreregFiles(AdminTestCase): def setUp(self): super(TestPreregFiles, self).setUp() self.prereg_user = AuthUserFactory() self.user = AuthUserFactory() self.node = ProjectFactory(creator=self.user) ensure_schemas() prereg_schema = get_prereg_schema() self.d_of_qs = { 'q7': OsfStorageFileNode(node=self.node, name='7'), 'q11': OsfStorageFileNode(node=self.node, name='11'), 'q16': OsfStorageFileNode(node=self.node, name='16'), 'q12': OsfStorageFileNode(node=self.node, name='12'), 'q13': OsfStorageFileNode(node=self.node, name='13'), 'q19': OsfStorageFileNode(node=self.node, name='19'), 'q26': OsfStorageFileNode(node=self.node, name='26') } data = {} for q, f in self.d_of_qs.iteritems(): guid = f.get_guid(create=True)._id f.save() if q == 'q26': data[q] = { 'comments': [], 'value': '26', 'extra': [ { 'data': { 'provider': 'osfstorage', 'path': f.path, }, 'fileId': guid, 'nodeId': self.node._id, } ] } continue data[q] = { 'value': { 'uploader': { 'extra': [ { 'data': { 'provider': 'osfstorage', 'path': f.path, }, 'fileId': guid, 'nodeId': self.node._id, } ] } } } self.draft = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, registration_metadata=data ) self.prereg_user.save() self.admin_user = UserFactory(osf_id=self.prereg_user.pk) def test_checkout_files(self): self.draft.submit_for_review(self.user, {}, save=True) request = RequestFactory().get('/fake_path') view = DraftDetailView() view = setup_user_view(view, request, self.admin_user, draft_pk=self.draft._id) view.checkout_files(self.draft) for q, f in self.d_of_qs.iteritems(): nt.assert_equal(self.prereg_user, f.checkout) def test_checkin_files(self): self.draft.submit_for_review(self.user, {}, save=True) request = RequestFactory().get('/fake_path') view = DraftDetailView() view = setup_user_view(view, request, self.admin_user, draft_pk=self.draft._id) view.checkout_files(self.draft) view2 = DraftFormView() view2 = setup_view(view2, request, draft_pk=self.draft._id) view2.checkin_files(self.draft) for q, f in self.d_of_qs.iteritems(): nt.assert_equal(None, f.checkout) def test_get_meta_data_files(self): for item in get_metadata_files(self.draft): nt.assert_in(type(item), [OsfStorageFile, StoredFileNode]) def test_get_file_questions(self): questions = get_file_questions('prereg-prize.json') nt.assert_equal(7, len(questions)) nt.assert_list_equal( [ (u'q7', u'Data collection procedures'), (u'q11', u'Manipulated variables'), (u'q12', u'Measured variables'), (u'q13', u'Indices'), (u'q16', u'Study design'), (u'q19', u'Statistical models'), (u'q26', u'Upload an analysis script with clear comments') ], questions ) def test_file_id_missing(self): data = self.draft.registration_metadata data['q7']['value']['uploader']['extra'][0].pop('fileId') self.draft.update_metadata(data) for item in get_metadata_files(self.draft): nt.assert_in(type(item), [OsfStorageFile, StoredFileNode]) def test_file_id_missing_odd(self): data = self.draft.registration_metadata data['q26']['extra'][0].pop('fileId') self.draft.update_metadata(data) for item in get_metadata_files(self.draft): nt.assert_in(type(item), [OsfStorageFile, StoredFileNode]) def test_wrong_provider(self): data = self.draft.registration_metadata data['q7']['value']['uploader']['extra'][0]['data']['provider'] = 'box' self.draft.update_metadata(data) with nt.assert_raises(Http404): for item in get_metadata_files(self.draft): pass def test_wrong_provider_odd(self): data = self.draft.registration_metadata data['q26']['extra'][0]['data']['provider'] = 'box' self.draft.update_metadata(data) with nt.assert_raises(Http404): for item in get_metadata_files(self.draft): pass
def setUp(self): super(TestMigrateRegistrationExtra, self).setUp() self.user = UserFactory() self.node = ProjectFactory(creator=self.user) self.file = self._get_test_file() MetaSchema.remove() self.file_ans = { 'file': { 'data':{ 'kind':'file', 'extra':{ 'checkout': None, 'hashes':{ 'sha256':'1fffe6116ecfa778f9938060d5caab923ba4b8db60bd2dd57f16a72e5ef06292' }, 'downloads':0, 'version':1 }, 'modified':'2016-04-15T18:10:48', 'name':'file.txt', 'provider':'osfstorage', } } } self.complex_metadata = { 'q1': { 'value': 'Answer 1', 'extra': [] }, 'q2': { 'value': 'Answer 2', 'extra': {} }, 'q3': { 'value': 'Answer 3', 'extra': self.file_ans }, 'q4': { 'value': { 'question': { 'value': 'Answer 4', 'extra': {} }, 'uploader': { 'value': '', 'extra': {} } }, }, 'q5': { 'value': 'Answer 5', 'extra': [ { 'viewUrl': '/project/abcdef/files/osfstorage/5723787136b74e1a953d9612/', 'hasSelectedFile': True, 'selectedFileName': 'file.txt' } ] } } self.simple_metadata = { 'Summary': 'Some airy' } self.schema = get_prereg_schema() self.draft1 = DraftRegistrationFactory( registration_metadata=self.complex_metadata, registration_schema=self.schema, approval=None, registered_node=None ) self.draft2 = DraftRegistrationFactory( registration_metadata=self.simple_metadata )
class TestDraftFormView(AdminTestCase): def setUp(self): super(TestDraftFormView, self).setUp() self.user = AuthUserFactory() self.dr1 = DraftRegistrationFactory( initiator=self.user, registration_schema=utils.draft_reg_util(), registration_metadata=utils.SCHEMA_DATA) self.dr1.submit_for_review(self.user, {}, save=True) self.dr1.flags # sets flags if there aren't any yet. self.request = RequestFactory().get('/fake_path') self.view = DraftFormView() self.view = setup_view(self.view, self.request, draft_pk=self.dr1._id) self.post = RequestFactory().post('/fake_path') self.post.user = UserFactory() self.post_view = DraftFormView() self.form_data = { 'notes': 'Far between', 'proof_of_publication': 'approved', } def test_dispatch_raise_404(self): view = setup_view(DraftFormView(), self.request, draft_pk='wrong') with nt.assert_raises(Http404): view.dispatch(self.request) def test_get_initial(self): self.view.draft = self.dr1 self.view.get_initial() res = self.view.initial nt.assert_is_instance(res, dict) nt.assert_equal(res['notes'], self.dr1.notes) nt.assert_equal(res['assignee'], self.dr1.flags['assignee']) nt.assert_equal(res['payment_sent'], self.dr1.flags['payment_sent']) nt.assert_equal(res['proof_of_publication'], self.dr1.flags['proof_of_publication']) def test_get_context_data(self): self.view.draft = self.dr1 res = self.view.get_context_data() nt.assert_is_instance(res, dict) nt.assert_in('draft', res) nt.assert_is_instance(res['draft'], dict) nt.assert_in('IMMEDIATE', res) def test_form_valid_notes(self): form = DraftRegistrationForm(data=self.form_data) nt.assert_true(form.is_valid()) view = setup_form_view(self.post_view, self.post, form, draft_pk=self.dr1._id) view.draft = self.dr1 count = OSFLogEntry.objects.count() with transaction.atomic(): view.form_valid(form) nt.assert_equal(count, OSFLogEntry.objects.count()) self.dr1.reload() nt.assert_equal(self.dr1.notes, self.form_data['notes']) @mock.patch('admin.pre_reg.views.DraftRegistration.approve') def test_form_valid_approve(self, mock_approve): self.form_data.update(approve_reject='approve') form = DraftRegistrationForm(data=self.form_data) nt.assert_true(form.is_valid()) view = setup_form_view(self.post_view, self.post, form, draft_pk=self.dr1._id) view.draft = self.dr1 count = OSFLogEntry.objects.count() with transaction.atomic(): view.form_valid(form) nt.assert_true(mock_approve.called) nt.assert_equal(count + 1, OSFLogEntry.objects.count()) @mock.patch('admin.pre_reg.views.DraftRegistration.reject') def test_form_valid_reject(self, mock_reject): self.form_data.update(approve_reject='reject') form = DraftRegistrationForm(data=self.form_data) nt.assert_true(form.is_valid()) view = setup_form_view(self.post_view, self.post, form, draft_pk=self.dr1._id) view.draft = self.dr1 count = OSFLogEntry.objects.count() with transaction.atomic(): view.form_valid(form) nt.assert_true(mock_reject.called) nt.assert_equal(count + 1, OSFLogEntry.objects.count())