def test_archive_success_escaped_file_names(self): file_tree = file_tree_factory(0, 0, 0) fake_file = file_factory(name='>and&and<') fake_file_name = strip_html(fake_file['name']) file_tree['children'] = [fake_file] node = factories.NodeFactory(creator=self.user) 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 }] } } schema = generate_schema_from_data(data) draft = factories.DraftRegistrationFactory(branched_from=node, registration_schema=schema, registered_metadata=data) with test_utils.mock_archive(node, schema=schema, data=data, autocomplete=True, autoapprove=True) as registration: with mock.patch.object(BaseStorageAddon, '_get_file_tree', mock.Mock(return_value=file_tree)): job = factories.ArchiveJobFactory(initiator=registration.creator) archive_success(registration._id, job._id) registration.reload() for key, question in registration.registered_meta[schema._id].items(): assert_equal(question['extra'][0]['selectedFileName'], fake_file_name)
def test_archive_failure_different_name_same_sha(self): 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_file2] node = factories.NodeFactory(creator=self.user) 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 }] } } schema = generate_schema_from_data(data) draft = factories.DraftRegistrationFactory(branched_from=node, registration_schema=schema, registered_metadata=data) with test_utils.mock_archive(node, schema=schema, data=data, autocomplete=True, autoapprove=True) as registration: with mock.patch.object(BaseStorageAddon, '_get_file_tree', mock.Mock(return_value=file_tree)): job = factories.ArchiveJobFactory(initiator=registration.creator) draft.registered_node = registration draft.save() with assert_raises(ArchivedFileNotFound): archive_success(registration._id, job._id)
def test_archive_success_with_deeply_nested_schema(self): node = factories.NodeFactory(creator=self.user) file_trees, selected_files, node_index = generate_file_tree([node]) 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() } schema = generate_schema_from_data(data) with test_utils.mock_archive(node, schema=schema, data=data, autocomplete=True, autoapprove=True) as registration: with mock.patch.object(BaseStorageAddon, '_get_file_tree', mock.Mock(return_value=file_trees[node._id])): job = factories.ArchiveJobFactory(initiator=registration.creator) archive_success(registration._id, job._id) registration.reload() for key, question in registration.registered_meta[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]][0] elif 'extra' in question and 'sha256' in question['extra'][0]: target = question if target: assert_in(registration._id, target['extra'][0]['viewUrl']) assert_not_in(node._id, target['extra'][0]['viewUrl']) del selected_files[target['extra'][0]['sha256']] else: # check non-file questions are unmodified assert_equal(data[key]['value'], question['value']) assert_false(selected_files)
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_search_serializer_mixed_model(self): user = AuthUserFactory() project = ProjectFactory(creator=user, is_public=True) component = NodeFactory(parent=project, creator=user, is_public=True) file_component = utils.create_test_file(component, user) context = {'request': make_drf_request_with_version(version='2.0')} schema = MetaSchema.objects.filter( name='Replication Recipe (Brandt et al., 2013): Post-Completion', schema_version=LATEST_SCHEMA_VERSION).first() #test_search_serializer_mixed_model_project result = SearchSerializer(project, context=context).data assert result['data']['type'] == 'nodes' #test_search_serializer_mixed_model_component result = SearchSerializer(component, context=context).data assert result['data']['type'] == 'nodes' #test_search_serializer_mixed_model_registration with mock_archive(project, autocomplete=True, autoapprove=True, schema=schema) as registration: result = SearchSerializer(registration, context=context).data assert result['data']['type'] == 'registrations' #test_search_serializer_mixed_model_file result = SearchSerializer(file_component, context=context).data assert result['data']['type'] == 'files' #test_search_serializer_mixed_model_user result = SearchSerializer(user, context=context).data assert result['data']['type'] == 'users'
def test_search_serializer_mixed_model(self): user = AuthUserFactory() project = ProjectFactory(creator=user, is_public=True) component = NodeFactory(parent=project, creator=user, is_public=True) file_component = utils.create_test_file(component, user) context = {'request': make_drf_request_with_version(version='2.0')} schema = RegistrationSchema.objects.filter( name='Replication Recipe (Brandt et al., 2013): Post-Completion', schema_version=LATEST_SCHEMA_VERSION).first() # test_search_serializer_mixed_model_project result = SearchSerializer(project, context=context).data assert result['data']['type'] == 'nodes' # test_search_serializer_mixed_model_component result = SearchSerializer(component, context=context).data assert result['data']['type'] == 'nodes' # test_search_serializer_mixed_model_registration with mock_archive(project, autocomplete=True, autoapprove=True, schema=schema) as registration: result = SearchSerializer(registration, context=context).data assert result['data']['type'] == 'registrations' # test_search_serializer_mixed_model_file result = SearchSerializer(file_component, context=context).data assert result['data']['type'] == 'files' # test_search_serializer_mixed_model_user result = SearchSerializer(user, context=context).data assert result['data']['type'] == 'users'
def test_cannot_request_termination_on_component_of_embargo(self): node = ProjectFactory() child = ProjectFactory(parent=node, creator=node.creator) with utils.mock_archive(node, embargo=True, autocomplete=True, autoapprove=True) as reg: with assert_raises(NodeStateError): reg.nodes[0].request_embargo_termination(Auth(node.creator))
def test_archive_success(self): node = factories.NodeFactory(creator=self.user) file_trees, selected_files, node_index = generate_file_tree([node]) data = generate_metadata( file_trees, selected_files, node_index ) schema = generate_schema_from_data(data) with test_utils.mock_archive(node, schema=schema, data=data, autocomplete=True, autoapprove=True) as registration: with mock.patch.object(StorageAddonBase, '_get_file_tree', mock.Mock(return_value=file_trees[node._id])): job = factories.ArchiveJobFactory() archive_success(registration._id, job._id) for key, question in registration.registered_meta[schema._id].items(): target = None if isinstance(question.get('value'), dict): target = [v for v in question['value'].values() if 'extra' in v and 'sha256' in v['extra'][0]][0] elif 'extra' in question and 'hashes' in question['extra'][0]: target = question if target: assert_in(registration._id, target['extra'][0]['viewUrl']) assert_not_in(node._id, target['extra'][0]['viewUrl']) del selected_files[target['extra'][0]['sha256']] else: # check non-file questions are unmodified assert_equal(data[key]['value'], question['value']) assert_false(selected_files)
def test_archive_success_different_name_same_sha(self): 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) 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 }] } } schema = generate_schema_from_data(data) with test_utils.mock_archive(node, schema=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[schema._id].items(): assert_equal(question['extra'][0]['selectedFileName'], fake_file['name'])
def test_archive_success_same_file_in_component(self): 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) 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 }] } } schema = generate_schema_from_data(data) with test_utils.mock_archive(node, schema=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[schema._id].items(): assert_in(child_reg._id, question['extra'][0]['viewUrl'])
def test_cannot_request_termination_on_component_of_embargo(self): node = ProjectFactory() ProjectFactory(parent=node, creator=node.creator) # child project with utils.mock_archive(node, embargo=True, autocomplete=True, autoapprove=True) as reg: with assert_raises(NodeStateError): reg._nodes.first().request_embargo_termination(Auth(node.creator))
def test_embargo_end_date_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_equal(sub_reg.embargo_end_date, registration.embargo_end_date)
def test_is_embargoed_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, autoapprove=True) as registration: sub_reg = registration.nodes[0].nodes[0] assert_true(sub_reg.is_embargoed)
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_is_pending_registration_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: sub_reg = registration.nodes[0].nodes[0] assert_true(sub_reg.is_pending_registration)
def test_migrate_registration_extra(self): with utils.mock_archive( self.node, schema=self.prereg_schema, data=self.data, autocomplete=True, autoapprove=True ) as reg: migrate() reg.reload() data = reg.registered_meta[self.prereg_schema._id] assert_true( isinstance(data['uploader']['extra'], list) ) assert_true( isinstance( data['nested']['value']['uploader']['extra'], list ) ) assert_true( isinstance( data['nested']['value']['question']['extra'], list ) ) assert_equal( self.data['uploader']['extra'], data['uploader']['extra'][0] ) assert_equal( self.data['nested']['value']['uploader']['extra'], data['nested']['value']['uploader']['extra'][0] ) assert_equal( self.data['nested']['value']['question']['value'], data['nested']['value']['question']['value'] ) assert_equal( self.data['nested']['value']['other'], data['nested']['value']['other'] ) assert_equal( self.data['other'], data['other'] ) assert_true( data['bad']['extra'][0].get('data', False) ) assert_true( isinstance(data['bad']['extra'][0]['data'], dict) ) assert_equal( data['bad']['extra'][0]['data']['name'], 'file.txt' ) assert_equal( data['bad']['extra'][0]['data']['sha256'], '2413fb3709b05939f04cf2e92f7d0897fc2596f9ad0b8a9ea855c7bfebaae892' )
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 setUp(self): super(TestNodeEmbargoTerminations, self).setUp() self.user = factories.AuthUserFactory() self.node = factories.ProjectFactory(creator=self.user) with mock_archive(self.node, embargo=True, autoapprove=True) as registration: self.registration = registration self.not_embargoed = factories.RegistrationFactory()
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 setUp(self): super(TestApproveEmbargoTerminations, self).setUp() self.user = AuthUserFactory() self.node = NodeFactory(creator=self.user) NodeFactory( creator=self.user, parent=NodeFactory(creator=self.user, parent=self.node) ) # requesting termination but less than 48 hours old with mock_archive(self.node, embargo=True, autoapprove=True) as registration: registration.request_embargo_termination(auth=Auth(self.user)) self.registration1 = registration # requesting termination and older than 48 hours with mock_archive(self.node, embargo=True, autoapprove=True) as registration: old_time = datetime.now() - timedelta(days=5) approval = registration.request_embargo_termination(auth=Auth(self.user)) EmbargoTerminationApproval._storage[0].store.update( {'_id': approval._id}, {'$set': {'initiation_date': old_time}}, ) self.registration2 = registration # requesting termination and older than 48 hours, but approved with mock_archive(self.node, embargo=True, autoapprove=True) as registration: old_time = datetime.now() - timedelta(days=5) approval = registration.request_embargo_termination(auth=Auth(self.user)) EmbargoTerminationApproval._storage[0].store.update( {'_id': approval._id}, {'$set': {'initiation_date': old_time}}, ) approval.state = Sanction.APPROVED approval.save() self.registration3 = registration # embargoed but not requesting termination with mock_archive(self.node, embargo=True, autoapprove=True) as registration: self.registration4 = registration EmbargoTerminationApproval._clear_caches()
def setUp(self): super(TestEmbargoTerminationApprovals, self).setUp() self.user = AuthUserFactory() self.project = ProjectFactory(creator=self.user) with utils.mock_archive(self.project, embargo=True, autocomplete=True, autoapprove=True) as registration: registration.request_embargo_termination(Auth(self.user)) self.registration = registration
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_success_with_components(self): 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, selected_files, node_index = generate_file_tree(nodes) data = generate_metadata(file_trees, selected_files, node_index) schema = generate_schema_from_data(data) with test_utils.mock_archive(node, schema=schema, data=copy.deepcopy(data), autocomplete=True, autoapprove=True) as registration: def mock_get_file_tree(self, *args, **kwargs): return file_trees[self.owner.registered_from._id] with mock.patch.object(BaseStorageAddon, '_get_file_tree', mock_get_file_tree): job = factories.ArchiveJobFactory( initiator=registration.creator) archive_success(registration._id, job._id) registration.reload() for key, question in registration.registered_meta[ 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[0]['extra'][0] ['viewUrl']).groupdict()['node_id'] assert_in(node_id, [ r._id for r in registration.node_and_primary_descendants() ]) if target[0]['extra'][0]['sha256'] in selected_files: del selected_files[target[0]['extra'][0]['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)
def test_archive_success_with_deeply_nested_schema(self): node = factories.NodeFactory(creator=self.user) file_trees, selected_files, node_index = generate_file_tree([node]) 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()} schema = generate_schema_from_data(data) with test_utils.mock_archive(node, schema=schema, data=data, autocomplete=True, autoapprove=True) as registration: with mock.patch.object( BaseStorageAddon, '_get_file_tree', mock.Mock(return_value=file_trees[node._id])): job = factories.ArchiveJobFactory( initiator=registration.creator) archive_success(registration._id, job._id) registration.reload() for key, question in registration.registered_meta[ 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] ][0] elif 'extra' in question and 'sha256' in question['extra'][ 0]: target = question if target: assert_in(registration._id, target['extra'][0]['viewUrl']) assert_not_in(node._id, target['extra'][0]['viewUrl']) del selected_files[target['extra'][0]['sha256']] else: # check non-file questions are unmodified assert_equal(data[key]['value'], question['value']) assert_false(selected_files)
def setUp(self): super(TestRegistrationRetractions, self).setUp() self.user = UserFactory(usename='Doug Bogie') self.title = 'Red Special' self.consolidate_auth = Auth(user=self.user) self.project = ProjectFactory( title=self.title, creator=self.user, is_public=True, ) with mock_archive(self.project, autocomplete=True, autoapprove=True) as registration: self.registration = registration
def setUp(self): super(TestSearchRegistrations, self).setUp() self.url = '/{}search/registrations/'.format(API_BASE) 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) ) with mock_archive(self.project, autocomplete=True, autoapprove=True, schema=self.schema) as registration: self.registration = registration with mock_archive(self.project_two, autocomplete=True, autoapprove=True, schema=self.schema) as registration_two: self.registration_two = registration_two with mock_archive(self.private_project, autocomplete=True, autoapprove=True, schema=self.schema) as private_registration: self.private_registration = private_registration self.private_registration.is_public = False self.private_registration.save()
def setUp(self): super(TestSearchSerializer, self).setUp() self.user = AuthUserFactory() self.project = ProjectFactory(creator=self.user, is_public=True) self.component = NodeFactory(parent=self.project, creator=self.user, is_public=True) self.file = utils.create_test_file(self.component, self.user) self.schema = MetaSchema.find_one( Q('name', 'eq', 'Replication Recipe (Brandt et al., 2013): Post-Completion') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) with mock_archive(self.project, autocomplete=True, autoapprove=True, schema=self.schema) as registration: self.registration = registration
def setUp(self): super(TestSearchSerializer, self).setUp() self.user = AuthUserFactory() self.project = ProjectFactory(creator=self.user, is_public=True) self.component = NodeFactory(parent=self.project, creator=self.user, is_public=True) self.file = utils.create_test_file(self.component, self.user) 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) ) with mock_archive(self.project, autocomplete=True, autoapprove=True, schema=self.schema) as registration: self.registration = registration
def setUp(self): super(TestRegistrationRetractions, 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, ) with mock_archive( self.project, autocomplete=True, autoapprove=True ) as registration: self.registration = registration
def test_archive_success_with_components(self): 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, selected_files, node_index = generate_file_tree(nodes) data = generate_metadata( file_trees, selected_files, node_index ) schema = generate_schema_from_data(data) with test_utils.mock_archive(node, schema=schema, data=copy.deepcopy(data), autocomplete=True, autoapprove=True) as registration: def mock_get_file_tree(self, *args, **kwargs): return file_trees[self.owner.registered_from._id] with mock.patch.object(BaseStorageAddon, '_get_file_tree', mock_get_file_tree): job = factories.ArchiveJobFactory(initiator=registration.creator) archive_success(registration._id, job._id) registration.reload() for key, question in registration.registered_meta[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[0]['extra'][0]['viewUrl'] ).groupdict()['node_id'] assert_in( node_id, [r._id for r in registration.node_and_primary_descendants()] ) if target[0]['extra'][0]['sha256'] in selected_files: del selected_files[target[0]['extra'][0]['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)
def test_archive_success_same_file_in_component(self): 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) 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 }] } } schema = generate_schema_from_data(data) with test_utils.mock_archive(node, schema=schema, data=data, autocomplete=True, autoapprove=True) as registration: with mock.patch.object(BaseStorageAddon, '_get_file_tree', mock.Mock(return_value=file_tree)): job = factories.ArchiveJobFactory( initiator=registration.creator) archive_success(registration._id, job._id) registration.reload() child_reg = registration.nodes[0] for key, question in registration.registered_meta[ schema._id].items(): assert_in(child_reg._id, question['extra'][0]['viewUrl'])
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_different_name_same_sha(self): 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) 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 }] } } schema = generate_schema_from_data(data) with test_utils.mock_archive(node, schema=schema, data=data, autocomplete=True, autoapprove=True) as registration: with mock.patch.object(BaseStorageAddon, '_get_file_tree', mock.Mock(return_value=file_tree)): job = factories.ArchiveJobFactory( initiator=registration.creator) archive_success(registration._id, job._id) for key, question in registration.registered_meta[ schema._id].items(): assert_equal(question['extra'][0]['selectedFileName'], fake_file['name'])
def test_archive_success(self): node = factories.NodeFactory(creator=self.user) file_trees, selected_files, node_index = generate_file_tree([node]) data = generate_metadata(file_trees, selected_files, node_index) schema = generate_schema_from_data(data) with test_utils.mock_archive(node, schema=schema, data=data, autocomplete=True, autoapprove=True) as registration: with mock.patch.object( BaseStorageAddon, '_get_file_tree', mock.Mock(return_value=file_trees[node._id])): job = factories.ArchiveJobFactory( initiator=registration.creator) archive_success(registration._id, job._id) registration.reload() for key, question in registration.registered_meta[ schema._id].items(): target = None if isinstance(question.get('value'), dict): target = [ v for v in question['value'].values() if 'extra' in v and 'sha256' in v['extra'][0] ][0] elif 'extra' in question and 'hashes' in question['extra'][ 0]: target = question if target: assert_in(registration._id, target['extra'][0]['viewUrl']) assert_not_in(node._id, target['extra'][0]['viewUrl']) del selected_files[target['extra'][0]['sha256']] else: # check non-file questions are unmodified assert_equal(data[key]['value'], question['value']) assert_false(selected_files)
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)
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()