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_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 test_archive_success_with_components(self): ensure_schemas() node = factories.NodeFactory(creator=self.user) comp1 = factories.NodeFactory(parent=node, creator=self.user) factories.NodeFactory(parent=comp1, creator=self.user) factories.NodeFactory(parent=node, creator=self.user) nodes = [n for n in node.node_and_primary_descendants()] file_trees = { n._id: file_tree_factory(3, 3, 3) for n in nodes } selected_files = {} selected_file_node_index = {} for n in nodes: file_tree = file_trees[n._id] selected = select_files_from_tree(file_tree) selected_file_node_index.update({ sha256: n._id for sha256 in selected.keys() }) selected_files.update(selected) # select files from each Node prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', 2) ) data = { ('q_' + selected_file['name']): { 'value': fake.word(), 'extra': { 'sha256': sha256, 'viewUrl': '/project/{0}/files/osfstorage{1}'.format( selected_file_node_index[sha256], selected_file['path'] ), 'selectedFileName': selected_file['name'], 'nodeId': selected_file_node_index[sha256] } } for sha256, selected_file in selected_files.items() } object_types = { ('q_' + selected_file['name'] + '_obj'): { 'value': { name_factory(): { 'value': fake.word(), 'extra': { 'sha256': sha256, 'viewUrl': '/project/{0}/files/osfstorage{1}'.format( selected_file_node_index[sha256], selected_file['path'] ), 'selectedFileName': selected_file['name'], 'nodeId': selected_file_node_index[sha256] } }, name_factory(): { 'value': fake.word() } } } for sha256, selected_file in selected_files.items() } data.update(object_types) other_questions = { 'q{}'.format(i): { 'value': fake.word() } for i in range(5) } data.update(other_questions) with test_utils.mock_archive(node, schema=prereg_schema, data=copy.deepcopy(data), autocomplete=True, autoapprove=True) as registration: patches = [] for n in registration.node_and_primary_descendants(): file_tree = file_trees[n.registered_from._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) job = factories.ArchiveJobFactory() archive_success(registration._id, job._id) for key, question in registration.registered_meta[prereg_schema._id].items(): target = None if isinstance(question['value'], dict): target = [v for v in question['value'].values() if 'extra' in v and 'sha256' in v['extra']][0] elif 'extra' in question and 'sha256' in question['extra']: target = question if target: node_id = re.search( r'^/project/(?P<node_id>\w{5}).+$', target['extra']['viewUrl'] ).groupdict()['node_id'] assert_in( node_id, [r._id for r in registration.node_and_primary_descendants()] ) if target['extra']['sha256'] in selected_files: del selected_files[target['extra']['sha256']] else: # check non-file questions are unmodified assert_equal(data[key]['value'], question['value']) # ensure each selected file was checked assert_false(selected_files) for patch in patches: patch.stop()
def test_archive_success(self): ensure_schemas() file_tree = file_tree_factory(3, 3, 3) selected_files = select_files_from_tree(file_tree) node = factories.NodeFactory(creator=self.user) prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', 2) ) data = { ('q_' + selected_file['name']): { 'value': fake.word(), 'extra': { 'selectedFileName': selected_file['name'], 'nodeId': node._id, 'sha256': sha256, 'viewUrl': '/project/{0}/files/osfstorage{1}'.format(node._id, selected_file['path']) } } for sha256, selected_file in selected_files.items() } object_types = { ('q_' + selected_file['name'] + '_obj'): { 'value': { name_factory(): { 'value': fake.word(), 'extra': { 'selectedFileName': selected_file['name'], 'nodeId': node._id, 'sha256': sha256, 'viewUrl': '/project/{0}/files/osfstorage{1}'.format(node._id, selected_file['path']) } }, name_factory(): { 'value': fake.word() } } } for sha256, selected_file in selected_files.items() } data.update(copy.deepcopy(object_types)) other_questions = { 'q{}'.format(i): { 'value': fake.word() } for i in range(5) } data.update(other_questions) 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(): target = None if isinstance(question['value'], dict): target = [v for v in question['value'].values() if 'extra' in v and 'sha256' in v['extra']][0] elif 'extra' in question and 'hashes' in question['extra']: target = question if target: assert_in(registration._id, target['extra']['viewUrl']) assert_not_in(node._id, target['extra']['viewUrl']) del selected_files[target['extra']['sha256']] else: # check non-file questions are unmodified assert_equal(data[key]['value'], question['value']) assert_false(selected_files)