def test_get_file_map_memoization(self): node = factories.NodeFactory() comp1 = factories.NodeFactory(parent=node) comp1a = factories.NodeFactory(parent=comp1) comp2 = factories.NodeFactory(parent=node) file_trees = { n._id: file_tree_factory(3, 3, 3) for n in [node, comp1, comp1a, comp2] } patches = {} mocks = {} for n in [node, comp1, comp1a, comp2]: file_tree = file_trees[n._id] osfstorage = n.get_addon('osfstorage') mocked = mock.Mock(return_value=file_tree) patch = mock.patch.object(osfstorage, '_get_file_tree', mocked) patch.start() patches[n._id] = patch mocks[n._id] = mocked # first call file_map = archiver_utils.get_file_map(node) file_map = {sha256: value for sha256, value, _ in file_map} for mocked in mocks.values(): mocked.assert_called_once() # second call file_map = archiver_utils.get_file_map(node) file_map = {sha256: value for sha256, value, _ in file_map} for mocked in mocks.values(): mocked.assert_called_once() for patch in patches.values(): patch.stop()
def test_archive_callback_on_tree_sends_only_one_email(self, mock_send_success, mock_arhive_success): proj = factories.NodeFactory() child = factories.NodeFactory(parent=proj) factories.NodeFactory(parent=child) reg = factories.RegistrationFactory(project=proj) rchild = reg.nodes[0] rchild2 = rchild.nodes[0] for node in [reg, rchild, rchild2]: for addon in ['dropbox', 'osfstorage']: node.archive_job._set_target(addon) for node in [reg, rchild, rchild2]: for addon in ['dropbox', 'osfstorage']: node.archive_job.update_target(addon, ARCHIVER_INITIATED) for addon in ['dropbox', 'osfstorage']: rchild.archive_job.update_target(addon, ARCHIVER_SUCCESS) rchild.save() listeners.archive_callback(rchild) mock_send_success.assert_not_called() for addon in ['dropbox', 'osfstorage']: reg.archive_job.update_target(addon, ARCHIVER_SUCCESS) reg.save() listeners.archive_callback(reg) mock_send_success.assert_not_called() for addon in ['dropbox', 'osfstorage']: rchild2.archive_job.update_target(addon, ARCHIVER_SUCCESS) rchild2.save() listeners.archive_callback(rchild2) mock_send_success.assert_called()
def test_get_file_map_with_components(self): node = factories.NodeFactory() comp1 = factories.NodeFactory(parent=node) comp1a = factories.NodeFactory(parent=comp1) comp2 = factories.NodeFactory(parent=node) file_trees = { n._id: file_tree_factory(3, 3, 3) for n in [node, comp1, comp1a, comp2] } patches = [] for n in [node, comp1, comp1a, comp2]: file_tree = file_trees[n._id] osfstorage = n.get_addon('osfstorage') patch = mock.patch.object(osfstorage, '_get_file_tree', mock.Mock(return_value=file_tree)) patch.start() patches.append(patch) file_map = archiver_utils.get_file_map(node) stack = file_trees.values() file_map = {sha256: value for sha256, value, _ in file_map} while len(stack): item = stack.pop(0) if item['kind'] == 'file': sha256 = item['extra']['hashes']['sha256'] assert_in(sha256, file_map) map_file = file_map[sha256] assert_equal(item, map_file) else: stack = stack + item['children'] for patch in patches: patch.stop()
def test_delete_registration_tree_deletes_backrefs(self): proj = factories.NodeFactory() factories.NodeFactory(parent=proj) comp2 = factories.NodeFactory(parent=proj) factories.NodeFactory(parent=comp2) reg = factories.RegistrationFactory(project=proj) archiver_utils.delete_registration_tree(reg) assert_false(proj.node__registrations)
def test_is_pending_embargo_searches_parents(self): user = factories.AuthUserFactory() node = factories.NodeFactory(creator=user) child = factories.NodeFactory(creator=user, parent=node) factories.NodeFactory(creator=user, parent=child) with mock_archive(node, embargo=True) as registration: sub_reg = registration.nodes[0].nodes[0] assert_true(sub_reg.is_pending_embargo)
def test_sanction_searches_parents(self): user = factories.AuthUserFactory() node = factories.NodeFactory(creator=user) child = factories.NodeFactory(creator=user, parent=node) factories.NodeFactory(creator=user, parent=child) with mock_archive(node) as registration: approval = registration.registration_approval sub_reg = registration.nodes[0].nodes[0] assert_equal(sub_reg.sanction, approval)
def test_delete_registration_tree(self): proj = factories.NodeFactory() factories.NodeFactory(parent=proj) comp2 = factories.NodeFactory(parent=proj) factories.NodeFactory(parent=comp2) reg = factories.RegistrationFactory(project=proj) reg_ids = [reg._id] + [r._id for r in reg.get_descendants_recursive()] archiver_utils.delete_registration_tree(reg) assert_false(Node.find(Q('_id', 'in', reg_ids) & Q('is_deleted', 'eq', False)).count())
def test_is_registration_approved_searches_parents(self): user = factories.AuthUserFactory() node = factories.NodeFactory(creator=user) child = factories.NodeFactory(creator=user, parent=node) factories.NodeFactory(creator=user, parent=child) with mock_archive(node) as registration: with mock.patch('website.project.sanctions.Sanction.is_approved', mock.Mock(return_value=True)): sub_reg = registration.nodes[0].nodes[0] assert_true(sub_reg.is_registration_approved)
def test_archive_callback_updates_archiving_state_when_done(self): proj = factories.NodeFactory() factories.NodeFactory(parent=proj) reg = factories.RegistrationFactory(project=proj) reg.archive_job.update_target('osfstorage', ARCHIVER_INITIATED) child = reg.nodes[0] child.archive_job.update_target('osfstorage', ARCHIVER_SUCCESS) child.save() listeners.archive_callback(child) assert_false(child.archiving)
def test_is_retracted_searches_parents(self): user = factories.AuthUserFactory() node = factories.NodeFactory(creator=user) child = factories.NodeFactory(creator=user, parent=node) factories.NodeFactory(creator=user, parent=child) with mock_archive(node, autoapprove=True, retraction=True, autoapprove_retraction=True) as registration: sub_reg = registration.nodes[0].nodes[0] assert_true(sub_reg.is_retracted)
def test_archive_tree_finished_d3(self): proj = factories.NodeFactory() child = factories.NodeFactory(parent=proj) factories.NodeFactory(parent=child) reg = factories.RegistrationFactory(project=proj) rchild = reg.nodes[0] rchild2 = rchild.nodes[0] for node in [reg, rchild, rchild2]: for addon in ['osfstorage', 'dropbox']: node.archive_job._set_target(addon) for node in [reg, rchild, rchild2]: for addon in ['osfstorage', 'dropbox']: node.archive_job.update_target(addon, ARCHIVER_SUCCESS) for node in [reg, rchild, rchild2]: assert_true(node.archive_job.archive_tree_finished())
def setUp(self): super(TestMigrateSchemas, self).setUp() MetaSchema.remove() ensure_schemas() self.regular_old_node = factories.NodeFactory() self.open_ended_schema = MetaSchema.find_one( Q('name', 'eq', SCHEMA_NAMES[0]) & Q('schema_version', 'eq', 1)) self.open_ended = self._make_registration(self.open_ended_schema) self.standard_schema = MetaSchema.find_one( Q('name', 'eq', SCHEMA_NAMES[1]) & Q('schema_version', 'eq', 1)) self.standard = self._make_registration(self.standard_schema) self.brandt_pre_schema = MetaSchema.find_one( Q('name', 'eq', SCHEMA_NAMES[2]) & Q('schema_version', 'eq', 1)) self.brandt_pre = self._make_registration(self.brandt_pre_schema) self.brandt_post_schema = MetaSchema.find_one( Q('name', 'eq', SCHEMA_NAMES[3]) & Q('schema_version', 'eq', 1)) self.brandt_post = self._make_registration(self.brandt_post_schema) self.multiple = self._make_registration( [self.brandt_pre_schema, self.brandt_post_schema]) self.confirmatory_schema = MetaSchema.find_one( Q('name', 'eq', 'Confirmatory - General')) self.confirmatory = self._make_registration(self.confirmatory_schema) self.db['node'].update({}, {'$set': { 'registered_schema': None }}, multi=True)
def test_field_with_callable_related_attrs(self): req = make_drf_request_with_version(version='2.0') project = factories.ProjectFactory() node = factories.NodeFactory(parent=project) data = self.BasicNodeSerializer(node, context={ 'request': req }).data['data'] assert_not_in('registered_from', data['relationships']) registration = factories.RegistrationFactory(project=node) data = self.BasicNodeSerializer(registration, context={ 'request': req }).data['data'] field = data['relationships']['registered_from']['links'] assert_in('/v2/nodes/{}/'.format(node._id), field['related']['href']) registration_registration = factories.RegistrationFactory( project=registration) data = self.BasicNodeSerializer(registration_registration, context={ 'request': req }).data['data'] field = data['relationships']['registered_from']['links'] assert_in('/v2/registrations/{}/'.format(registration._id), field['related']['href'])
def test_field_with_two_kwargs(self): req = make_drf_request() project = factories.ProjectFactory() node = factories.NodeFactory(parent=project) data = self.BasicNodeSerializer(node, context={'request': req}).data['data'] field = data['relationships']['two_url_kwargs']['links'] assert_in('/v2/nodes/{}/node_links/{}/'.format(node._id, node._id), field['related']['href'])
def test_archive_success_different_name_same_sha(self): ensure_schemas() file_tree = file_tree_factory(0, 0, 0) fake_file = file_factory() fake_file2 = file_factory(sha256=fake_file['extra']['hashes']['sha256']) file_tree['children'] = [fake_file, fake_file2] node = factories.NodeFactory(creator=self.user) prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', 2) ) data = { ('q_' + fake_file['name']): { 'value': fake.word(), 'extra': { 'sha256': fake_file['extra']['hashes']['sha256'], 'viewUrl': '/project/{0}/files/osfstorage{1}'.format( node._id, fake_file['path'] ), 'selectedFileName': fake_file['name'], 'nodeId': node._id } } } with test_utils.mock_archive(node, schema=prereg_schema, data=data, autocomplete=True, autoapprove=True) as registration: with mock.patch.object(StorageAddonBase, '_get_file_tree', mock.Mock(return_value=file_tree)): job = factories.ArchiveJobFactory() archive_success(registration._id, job._id) for key, question in registration.registered_meta[prereg_schema._id].items(): assert_equal(question['extra']['selectedFileName'], fake_file['name'])
def test_field_with_non_attribute(self): req = make_drf_request() project = factories.ProjectFactory() node = factories.NodeFactory(parent=project) data = self.BasicNodeSerializer(node, context={'request': req}).data['data'] field = data['relationships']['not_attribute_on_target']['links'] assert_in('/v2/nodes/{}/children/'.format('12345'), field['related']['href'])
def test_self_and_related_fields(self): req = make_drf_request() project = factories.ProjectFactory() node = factories.NodeFactory(parent=project) data = self.BasicNodeSerializer(node, context={'request': req}).data['data'] relationship_field = data['relationships']['self_and_related_field']['links'] assert_in('/v2/nodes/{}/contributors/'.format(node._id), relationship_field['self']['href']) assert_in('/v2/nodes/{}/'.format(node._id), relationship_field['related']['href'])
def test_serializing_meta(self): req = make_drf_request() project = factories.ProjectFactory() node = factories.NodeFactory(parent=project) data = self.BasicNodeSerializer(node, context={'request': req}).data['data'] meta = data['relationships']['parent_with_meta']['links']['related']['meta'] assert_not_in('count', meta) assert_in('extra', meta) assert_equal(meta['extra'], 'foo')
def test_autoload(self): target = factories.NodeFactory() def fn(node, *args, **kwargs): return node wrapped = autoload(Node, 'node_id', 'node', fn) found = wrapped(node_id=target._id) assert_equal(found, target)
def setUp(self): super(ArchiverTestCase, self).setUp() handlers.celery_before_request() self.user = factories.UserFactory() self.auth = Auth(user=self.user) self.src = factories.NodeFactory(creator=self.user) self.dst = factories.RegistrationFactory(user=self.user, project=self.src, send_signals=False) archiver_utils.before_archive(self.dst, self.user) self.archive_job = self.dst.archive_job
def test_archive_tree_finished(self): proj = factories.NodeFactory() factories.NodeFactory(parent=proj) comp2 = factories.NodeFactory(parent=proj) factories.NodeFactory(parent=comp2) reg = factories.RegistrationFactory(project=proj) rchild1 = reg.nodes[0] rchild2 = reg.nodes[1] rchild2a = rchild2.nodes[0] regs = itertools.chain([reg], reg.get_descendants_recursive()) for node in regs: assert_false(node.archive_job.archive_tree_finished()) for node in regs: assert_false(node.archive_job.archive_tree_finished()) for node in [reg, rchild2]: for target in node.archive_job.target_addons: node.archive_job.update_target(target.name, ARCHIVER_SUCCESS) for node in regs: assert_true(node.archive_job.archive_tree_finished())
def test_archive_success_same_file_in_component(self): ensure_schemas() file_tree = file_tree_factory(3, 3, 3) selected = select_files_from_tree(file_tree).values()[0] child_file_tree = file_tree_factory(0, 0, 0) child_file_tree['children'] = [selected] node = factories.NodeFactory(creator=self.user) child = factories.NodeFactory(creator=self.user, parent=node) prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', 2)) data = { ('q_' + selected['name']): { 'value': fake.word(), 'extra': { 'sha256': selected['extra']['hashes']['sha256'], 'viewUrl': '/project/{0}/files/osfstorage{1}'.format( child._id, selected['path']), 'selectedFileName': selected['name'], 'nodeId': child._id } } } with test_utils.mock_archive(node, schema=prereg_schema, data=data, autocomplete=True, autoapprove=True) as registration: with mock.patch.object(StorageAddonBase, '_get_file_tree', mock.Mock(return_value=file_tree)): job = factories.ArchiveJobFactory() archive_success(registration._id, job._id) child_reg = registration.nodes[0] for key, question in registration.registered_meta[ prereg_schema._id].items(): assert_in(child_reg._id, question['extra']['viewUrl'])
def setUp(self): super(TestApiBaseSerializers, self).setUp() self.user = factories.AuthUserFactory() self.auth = factories.Auth(self.user) self.node = factories.ProjectFactory(is_public=True) for i in range(5): factories.ProjectFactory(is_public=True, parent=self.node) self.linked_node = factories.NodeFactory(creator=self.user, is_public=True) self.node.add_pointer(self.linked_node, auth=self.auth) self.url = '/{}nodes/{}/'.format(API_BASE, self.node._id)
def test_field_with_two_filters(self): req = make_drf_request_with_version(version='2.0') project = factories.ProjectFactory() node = factories.NodeFactory(parent=project) data = self.BasicNodeSerializer(node, context={ 'request': req }).data['data'] field = data['relationships']['field_with_filters']['links'] assert_in(urllib.quote('filter[target]=hello', safe='?='), field['related']['href']) assert_in(urllib.quote('filter[woop]=yea', safe='?='), field['related']['href'])
def setUp(self): super(TestFileSerializer, self).setUp() self.user = factories.UserFactory() self.node = factories.NodeFactory(creator=self.user) self.file = utils.create_test_file(self.node, self.user) self.date_created = self.file.versions[0].date_created self.date_modified = self.file.versions[-1].date_created self.date_created_tz_aware = self.date_created.replace(tzinfo=utc) self.date_modified_tz_aware = self.date_modified.replace(tzinfo=utc) self.new_format = '%Y-%m-%dT%H:%M:%S.%fZ'
def setUp(self): super(TestDateByVersion, self).setUp() self.node = factories.NodeFactory() self.old_date = datetime.utcnow() # naive dates before django-osf self.old_date_without_microseconds = self.old_date.replace( microsecond=0) self.new_date = datetime.utcnow().replace( tzinfo=utc) # non-naive after django-osf self.new_date_without_microseconds = self.new_date.replace( microsecond=0) self.old_format = '%Y-%m-%dT%H:%M:%S.%f' self.old_format_without_microseconds = '%Y-%m-%dT%H:%M:%S' self.new_format = '%Y-%m-%dT%H:%M:%S.%fZ'
def test_get_file_map(self): node = factories.NodeFactory(creator=self.user) file_tree = file_tree_factory(3, 3, 3) with mock.patch.object(StorageAddonBase, '_get_file_tree', mock.Mock(return_value=file_tree)): file_map = archiver_utils.get_file_map(node) stack = [file_tree] file_map = {sha256: value for sha256, value, _ in file_map} while len(stack): item = stack.pop(0) if item['kind'] == 'file': sha256 = item['extra']['hashes']['sha256'] assert_in(sha256, file_map) map_file = file_map[sha256] assert_equal(item, map_file) else: stack = stack + item['children']
def setUp(self): with run_celery_tasks(): super(TestPublicNodes, self).setUp() self.user = factories.UserFactory(usename='Doug Bogie') self.title = 'Red Special' self.consolidate_auth = Auth(user=self.user) self.project = factories.ProjectFactory( title=self.title, creator=self.user, is_public=True, ) self.component = factories.NodeFactory(parent=self.project, title=self.title, creator=self.user, is_public=True) self.registration = factories.ProjectFactory(title=self.title, creator=self.user, is_public=True, is_registration=True)
def test_prereg(self): meta = {'q1': {'value': fake.sentence()}} node = factories.NodeFactory(creator=self.osf_user) prereg_pending = [] for i in range(3): draft = factories.DraftRegistrationFactory( branched_from=node, registration_schema=self.prereg_schema, registration_metadata=meta) draft.submit_for_review(self.osf_user, {'registration_choice': 'immediate'}, save=True) prereg_pending.append(draft) non_prereg_pending = [] for i in range(3): draft = factories.DraftRegistrationFactory( branched_from=node, registration_schema=self.other_schema) draft.submit_for_review(self.osf_user, {'registration_choice': 'immediate'}, save=True) non_prereg_pending.append(draft) prereg_not_pending = [] for i in range(3): draft = factories.DraftRegistrationFactory( branched_from=node, registration_schema=self.prereg_schema) prereg_not_pending.append(draft) non_prereg_not_pending = [] for i in range(3): draft = factories.DraftRegistrationFactory( branched_from=node, registration_schema=self.other_schema) non_prereg_not_pending.append(draft) url = reverse('pre_reg:prereg') res = self.client.get(url) assert_equal(res.status_code, http.OK) assert_in('drafts', res.context) assert_equal(len(res.context['drafts']), len(prereg_pending)) for draft in res.context['drafts']: assert_in(draft['pk'], [d._id for d in prereg_pending])
def setUp(self): super(TestCheckPreregAuth, self).setUp() ensure_schemas() self.prereg_challenge_admin_user = AuthUserFactory() self.prereg_challenge_admin_user.system_tags.append( settings.PREREG_ADMIN_TAG) self.prereg_challenge_admin_user.save() prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', 2)) self.user = AuthUserFactory() self.node = factories.ProjectFactory(creator=self.user) self.parent = factories.ProjectFactory() self.child = factories.NodeFactory(parent=self.parent) self.draft_registration = factories.DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, branched_from=self.parent)