def test_deposited_dataset_owner_org_dest(self): deposit = factories.DataContainer(id='data-deposit') target = factories.DataContainer(id='data-target') user = core_factories.User() result = validators.deposited_dataset_owner_org_dest( 'data-target', {'user': user['name']}) assert result == 'data-target'
def test_deposited_dataset_owner_org_dest_invalid_not_existent(self): deposit = factories.DataContainer(id='data-deposit') target = factories.DataContainer(id='data-target') user = core_factories.User() with pytest.raises(toolkit.Invalid): validators.deposited_dataset_owner_org_dest( 'not-existent', {'user': user['name']})
def test_get_all_data_containers(self): container1 = factories.DataContainer(title='container1') container2 = factories.DataContainer(title='container2') result = helpers.get_all_data_containers() assert_equals(len(result), 2) assert_equals(result[0]['title'], container1['title']) assert_equals(result[1]['title'], container2['title'])
def setup(self): super(TestDepositedDatasetController, self).setup() # Users self.sysadmin = core_factories.Sysadmin(name='sysadmin', id='sysadmin') self.depadmin = core_factories.User(name='depadmin', id='depadmin') self.curator = core_factories.User(name='curator', id='curator') self.creator = core_factories.User(name='creator', id='creator') self.depositor = core_factories.User(name='depositor', id='depositor') self.editor = core_factories.User(name='editor', id='editor') # Containers self.deposit = factories.DataContainer( users=[ {'name': 'curator', 'capacity': 'editor'}, {'name': 'depadmin', 'capacity': 'admin'}, ], name='data-deposit', id='data-deposit' ) self.target = factories.DataContainer( name='data-target', id='data-target', users=[ {'name': 'editor', 'capacity': 'editor'}, ], ) # Dataset self.dataset = factories.DepositedDataset( name='dataset', owner_org='data-deposit', owner_org_dest='data-target', user=self.creator)
def test_get_all_data_containers_exclude(self): container1 = factories.DataContainer(title='container1') container2 = factories.DataContainer(title='container2') result = helpers.get_all_data_containers( exclude_ids=[container2['id']]) assert_equals(len(result), 1) assert_equals(result[0]['title'], 'container1')
def test_get_linked_datasets_for_form_many_selected_ids(self): user = core_factories.User(id='user_selected_ids', name='user_selected_ids') container1 = factories.DataContainer(title='container1', users=[user]) container2 = factories.DataContainer(title='container2', users=[user]) dataset1 = factories.Dataset(id='id1', title='dataset1', owner_org=container1['id']) dataset2 = factories.Dataset(id='id2', title='dataset2', owner_org=container2['id']) context = {'model': model, 'user': user['name']} linked_datasets = get_linked_datasets_for_form(context=context, user_id=user['id'], selected_ids=['id2']) assert_equals(linked_datasets, [ { 'text': 'container1', 'children': [{ 'text': 'dataset1', 'value': 'id1' }] }, { 'text': 'container2', 'children': [{ 'text': 'dataset2', 'value': 'id2', 'selected': 'selected' }] }, ])
def test_user_request_access_body(self): container1 = factories.DataContainer( name='brasil-container', title='Brazil', ) container2 = factories.DataContainer( name='arg-container', title='Argentina', ) user1 = factories.ExternalUser( name='user1', id='user1', email='*****@*****.**', focal_point='Maria', default_containers=[container1['id'], container2['id']]) user_message = 'I can haz access?\nkthxbye' email_body = mailer.compose_request_access_email_body( 'user', self.sysadmin, user1, user1, user_message) regularised_body = regularise_html(email_body) expected = regularise_html( 'External user <a href="{user_link}">Mr. Test User</a> has requested access to deposit datasets.' .format(user_link=toolkit.url_for('user.read', id=user1['id'], qualified=True), )) assert expected in regularised_body assert '<p>I can haz access?<br> kthxbye</p>' in regularised_body assert 'Data container(s): Brazil, Argentina' in regularised_body assert 'User\'s email address: [email protected]' in regularised_body assert 'Focal point: Maria' in regularised_body
def test_package_update(self): # Create users depadmin = core_factories.User(name='depadmin') curator = core_factories.User(name='curator') depositor = core_factories.User(name='depositor') creator = core_factories.User(name='creator') # Create containers deposit = factories.DataContainer( id='data-deposit', name='data-deposit', users=[ { 'name': 'depadmin', 'capacity': 'admin' }, { 'name': 'curator', 'capacity': 'editor' }, ], ) target = factories.DataContainer( id='data-target', name='data-target', ) # Create dataset dataset = factories.DepositedDataset(name='dataset', owner_org='data-deposit', owner_org_dest='data-target', user=creator) # Forbidden depadmin/curator/depositor with pytest.raises(toolkit.NotAuthorized): toolkit.check_access( 'package_update', context={'user': '******'}, data_dict={'id': dataset['id']}, ) with pytest.raises(toolkit.NotAuthorized): toolkit.check_access( 'package_update', context={'user': '******'}, data_dict={'id': dataset['id']}, ) with pytest.raises(toolkit.NotAuthorized): toolkit.check_access( 'package_update', context={'user': '******'}, data_dict={'id': dataset['id']}, ) # Granted creator assert (True == toolkit.check_access('package_update', {'user': '******'}, {'id': dataset['id']}))
def setup(self): factories.DataContainer( id='data-deposit', name='data-deposit', ) factories.DataContainer( id='data-target', name='data-target', )
def test_external_user_approved_deposit(self): sysadmin = core_factories.Sysadmin() external_user = factories.ExternalUser() target = factories.DataContainer( id='data-target', name='data-target', ) deposit = factories.DataContainer(id='data-deposit', name='data-deposit') deposited_dataset = factories.DepositedDataset( name='dataset1', owner_org='data-deposit', owner_org_dest='data-target', user=external_user) tmp = deposited_dataset.copy() tmp.update({ 'unit_of_measurement': 'individual', 'keywords': ['3', '4'], 'archived': 'False', 'data_collector': ['acf'], 'data_collection_technique': 'f2f', 'external_access_level': 'open_access', 'geographies': [DEFAULT_GEOGRAPHY_CODE] }) deposited_dataset = helpers.call_action('package_update', {'user': sysadmin['name']}, **tmp) # While the dataset is in deposited state, external_user can view it assert (True == toolkit.check_access( 'package_show', {'user': external_user['name']}, {'id': deposited_dataset['id']}, )) # Approve the dataset approved_dataset = convert_deposited_dataset_to_regular_dataset( deposited_dataset) approved_dataset = helpers.call_action('package_update', context={ 'user': sysadmin['name'], 'type': approved_dataset['type'] }, **approved_dataset) # Now that the dataset has moved out of the data-deposit, # external_user can not view it anymore with pytest.raises(toolkit.NotAuthorized): toolkit.check_access( 'package_show', context={'user': external_user['name']}, data_dict={'id': approved_dataset['id']}, )
def setup_class(self): core_helpers.reset_db() # Users self.sysadmin = core_factories.Sysadmin(name='sysadmin', id='sysadmin') self.depadmin = core_factories.User(name='depadmin', id='depadmin') self.curator = core_factories.User(name='curator', id='curator') self.creator = core_factories.User(name='creator', id='creator') self.depositor = core_factories.User(name='depositor', id='depositor') self.editor = core_factories.User(name='editor', id='editor') self.target_container_admin = core_factories.User( name='target_container_admin', id='target_container_admin') self.target_container_member = core_factories.User( name='target_container_member', id='target_container_member') self.other_container_admin = core_factories.User( name='other_container_admin', id='other_container_admin') app = core_helpers._get_test_app() self.deposit = factories.DataContainer(users=[ { 'name': 'curator', 'capacity': 'editor' }, { 'name': 'depadmin', 'capacity': 'admin' }, ], name='data-deposit', id='data-deposit') self.target = factories.DataContainer( name='data-target', id='data-target', users=[ { 'name': 'editor', 'capacity': 'editor' }, { 'name': 'target_container_admin', 'capacity': 'admin' }, { 'name': 'target_container_member', 'capacity': 'member' }, ], ) container = factories.DataContainer(users=[ { 'name': 'other_container_admin', 'capacity': 'admin' }, ])
def setup(self): # Users self.sysadmin = core_factories.Sysadmin(name='sysadmin', id='sysadmin') self.user1 = core_factories.User(name='user1', id='user1') self.user2 = core_factories.User(name='user2', id='user2') self.user3 = core_factories.User(name='user3', id='user3') # Containers self.container1 = factories.DataContainer( name='container1', id='container1', users=[ { 'name': 'user1', 'capacity': 'admin' }, ], ) self.container2 = factories.DataContainer( name='container2', id='container2', users=[ { 'name': 'user2', 'capacity': 'admin' }, ], ) # Datasets self.dataset1 = factories.Dataset( name='dataset1', title='Test Dataset 1', owner_org='container1', data_collection_technique='f2f', sampling_procedure='nonprobability', operational_purpose_of_data='cartography', user=self.user1, tags=[{ 'name': 'Keyword1' }, { 'name': 'Keyword2' }], ) # Resources self.resource1 = factories.Resource( name='resource1', package_id='dataset1', url_type='upload', visibility='restricted', upload=mocks.FakeFileStorage(), )
def setup(self): # Users self.normal_user = core_factories.User() self.org_user = core_factories.User() self.sysadmin = core_factories.Sysadmin() # Containers factories.DataContainer(name='data-deposit', id='data-deposit') self.container = factories.DataContainer( users=[{ 'name': self.org_user['name'], 'capacity': 'member' }])
def test_resource_download_deposited_dataset(self): depadmin = core_factories.User() curator = core_factories.User() target_container_admin = core_factories.User() target_container_member = core_factories.User() other_container_admin = core_factories.User() deposit = factories.DataContainer(id='data-deposit', users=[ { 'name': depadmin['name'], 'capacity': 'admin' }, { 'name': curator['name'], 'capacity': 'editor' }, ]) target = factories.DataContainer(users=[ { 'name': target_container_admin['name'], 'capacity': 'admin' }, { 'name': target_container_member['name'], 'capacity': 'member' }, ]) container = factories.DataContainer(users=[ { 'name': other_container_admin['name'], 'capacity': 'admin' }, ]) dataset = factories.DepositedDataset(owner_org=deposit['id'], owner_org_dest=target['id']) resource = factories.Resource( package_id=dataset['id'], url_type='upload', ) for user in [depadmin, curator, target_container_admin]: assert ({ 'success': True } == auth.resource_download({'user': user['name']}, resource)) for user in [target_container_member, other_container_admin]: assert ({ 'success': False } == auth.resource_download({'user': user['name']}, resource))
def test_deposited_dataset_owner_org_dest_not_visible_external(self): deposit = factories.DataContainer(id='data-deposit') target = factories.DataContainer(id='data-target', visible_external=False) internal_user = core_factories.User() external_user = factories.ExternalUser() with pytest.raises(toolkit.Invalid): validators.deposited_dataset_owner_org_dest( 'data-target', {'user': external_user['name']}) assert (validators.deposited_dataset_owner_org_dest( "data-target", {"user": internal_user["name"]}, ) == "data-target")
def setup(self): self.external_user = factories.ExternalUser() user = core_factories.User() target = factories.DataContainer( id='data-target', name='data-target', ) deposit = factories.DataContainer(id='data-deposit', name='data-deposit') self.external_user_dataset = factories.DepositedDataset( description='deposited dataset created by external user', owner_org='data-deposit', owner_org_dest='data-target', user=self.external_user) self.internal_user_dataset = factories.DepositedDataset( description='deposited dataset created by internal user', owner_org='data-deposit', owner_org_dest='data-target', user=user) self.dataset = factories.Dataset(owner_org=target['id'], ) self.external_dataset_resources = [ factories.Resource( description= 'resource created by external_user attached to deposited dataset created by external_user', package_id=self.external_user_dataset['id'], url_type='upload', user=self.external_user, visibility='restricted'), factories.Resource( description= 'resource created by someone else attached to deposited dataset created by external_user', package_id=self.external_user_dataset['id'], url_type='upload', user=user, visibility='restricted'), ] self.internal_dataset_resources = [ factories.Resource(package_id=self.internal_user_dataset['id'], url_type='upload', user=user), ] self.arbitrary_resource = factories.Resource( package_id=self.dataset['id'], url_type='upload', )
def test_logged_in_users(self): user = core_factories.User() actions = [ 'package_search', 'package_list', 'organization_list', 'group_list', 'user_list', 'organization_list_for_user', ] context = {'user': user['name'], 'ignore_auth': False} for action in actions: helpers.call_action(action, context=context) data_container = factories.DataContainer(users=[{ 'name': user['name'], 'capacity': 'admin' }]) dataset = factories.Dataset(owner_org=data_container['id']) helpers.call_action('package_show', context=context, id=dataset['name']) helpers.call_action('organization_show', context=context, id=data_container['name']) helpers.call_action('user_show', context=context, id=user['id'])
def test_recipients_with_org_admins(self): editor = core_factories.User() admin = core_factories.User() external = core_factories.User() container = factories.DataContainer(users=[ { 'name': editor['name'], 'capacity': 'editor' }, { 'name': admin['name'], 'capacity': 'admin' }, ], name='container1', id='container1') dataset1 = factories.Dataset( name='new-dataset', title='New Dataset', owner_org=container['id'], ) dataset_recipients = mailer.get_dataset_request_access_email_recipients( dataset1) container_recipients = mailer.get_container_request_access_email_recipients( container) assert dataset_recipients == container_recipients assert len(container_recipients) == 1 assert admin['name'] == container_recipients[0]['name']
def setup(self): self.container1_admin = core_factories.User() self.container1 = factories.DataContainer( users=[{ "name": self.container1_admin["name"], "capacity": "admin" }])
def test_requesting_user_is_not_admin_of_required_container(self): target_user = factories.ExternalUser(state=model.State.PENDING) requesting_user = core_factories.User() container2 = factories.DataContainer( users=[{ "name": requesting_user["name"], "capacity": "admin" }]) access_request_data_dict = { 'object_id': target_user['id'], 'object_type': 'user', 'message': 'asdf', 'role': 'member', 'data': { 'default_containers': [self.container1['id']] } } toolkit.get_action(u'access_request_create')({ 'user': target_user['id'], 'ignore_auth': True }, access_request_data_dict) action = toolkit.get_action("external_user_update_state") with pytest.raises(toolkit.NotAuthorized): action({"user": requesting_user["name"]}, { 'id': target_user['id'], 'state': model.State.ACTIVE })
def setup(self): # Users self.sysadmin = core_factories.Sysadmin(name='sysadmin', id='sysadmin') self.user1 = core_factories.User(name='user1', id='user1') self.user2 = core_factories.User(name='user2', id='user2') self.user3 = core_factories.User(name='user3', id='user3') # Containers self.container1 = factories.DataContainer( name='container1', id='container1', ) self.container2 = factories.DataContainer( name='container2', id='container2', )
def setup(self): self.deposit = factories.DataContainer( name='data-deposit', id='data-deposit', ) self.user = core_factories.User(name='user1') self.admin = core_factories.User(name='admin') self.container = factories.DataContainer(name='container1', title='Test Container', users=[ { 'name': self.admin['name'], 'capacity': 'admin' }, ])
def test_get_data_curation_users(self): depadmin = core_factories.User(name='depadmin') curator1 = core_factories.User(name='curator1') curator2 = core_factories.User(name='curator2') deposit = factories.DataContainer( name='data-deposit', users=[ { 'name': 'depadmin', 'capacity': 'admin' }, { 'name': 'curator1', 'capacity': 'editor' }, { 'name': 'curator2', 'capacity': 'editor' }, ], ) curators = helpers.get_data_curation_users( context={'user': '******'}) curator_names = sorted([ curator['name'] for curator in curators # Added to org by ckan if not curator['sysadmin'] ]) assert_equals(len(curator_names), 3) assert_equals(curator_names[0], 'curator1') assert_equals(curator_names[1], 'curator2') assert_equals(curator_names[2], 'depadmin')
def setup(self): super(TestDatastoreAuthRestrictedDownloads, self).setup() # Users self.normal_user = core_factories.User() self.org_user = core_factories.User() self.sysadmin = core_factories.Sysadmin() # Containers self.container = factories.DataContainer(users=[ { 'name': self.org_user['name'], 'capacity': 'member' }, ]) # Datasets self.dataset = factories.Dataset( visibility='restricted', owner_org=self.container['id'], ) self.resource = factories.Resource( package_id=self.dataset['id'], url_type='datastore', ) # Actions core_helpers.call_action('datastore_create', resource_id=self.resource['id'], records=[{ 'a': 1, 'b': 2 }])
def setup(self): # Users self.normal_user = core_factories.User() self.org_user = core_factories.User() self.sysadmin = core_factories.Sysadmin() # Containers self.container = factories.DataContainer( users=[ {'name': self.org_user['name'], 'capacity': 'member'}, ] ) # Datasets self.dataset = factories.Dataset( owner_org=self.container['id'], ) self.resource = factories.Resource( package_id=self.dataset['id'], url_type='datastore', upload=mocks.FakeFileStorage(), visibility='restricted', ) # Actions core_helpers.call_action('datastore_create', resource_id=self.resource['id'], records=[{'a': 1, 'b': 2}], force=True, )
def test_resource_download(self): container_member = core_factories.User() dataset_member = core_factories.User() another_user = core_factories.User() data_container = factories.DataContainer( users=[{ 'name': container_member['name'], 'capacity': 'admin' }]) dataset = factories.Dataset(owner_org=data_container['id'], ) resource = factories.Resource(package_id=dataset['id'], url_type='upload', visibility='restricted') helpers.call_action( 'package_collaborator_create', id=dataset['id'], user_id=dataset_member['id'], capacity='member', ) for user in [container_member, dataset_member]: assert ({ 'success': True } == auth.resource_download({'user': user['name']}, resource)) assert ({ 'success': False } == auth.resource_download({'user': another_user['name']}, resource))
def test_get_pending_requests(self): sysadmin = core_factories.Sysadmin(name='sysadmin', id='sysadmin') container1 = factories.DataContainer( name='container1', id='container1', state='approval_needed', ) container2 = factories.DataContainer( name='container2', id='container2', state='approval_needed', ) context = {'model': model, 'user': '******'} requests = helpers.get_pending_requests(context=context) assert_equals(requests['count'], 2) assert_equals(requests['containers'], [container1['id'], container2['id']])
def test_mail_data_container_request_to_sysadmins(self, mock_render_jinja2, mock_mail_user): sysadmin1 = core_factories.Sysadmin() sysadmin2 = core_factories.Sysadmin() data_container = factories.DataContainer() mail_data_container_request_to_sysadmins(self.context, data_container) assert_equals(mock_mail_user.call_count, 2) assert_equals(mock_render_jinja2.call_args[0][0], 'emails/data_container_request.txt')
def test_unit_user_is_not_container_admin(self): creator = core_factories.User(name='creator') container1 = factories.DataContainer(users=[ { 'name': 'creator', 'capacity': 'member' }, ], ) with pytest.raises(toolkit.NotAuthorized): toolkit.check_access('package_create', context={'user': '******'}, data_dict={'owner_org': container1['name']}) container2 = factories.DataContainer() with pytest.raises(toolkit.NotAuthorized): toolkit.check_access('package_create', context={'user': '******'}, data_dict={'owner_org': container2['name']})
def setup(self): super(TestDataContainerController, self).setup() # Users self.sysadmin = core_factories.Sysadmin(name='sysadmin', id='sysadmin') self.user1 = core_factories.User(name='user1', id='user1') self.user2 = core_factories.User(name='user2', id='user2') self.user3 = core_factories.User(name='user3', id='user3') # Containers self.container1 = factories.DataContainer( name='container1', id='container1', ) self.container2 = factories.DataContainer( name='container2', id='container2', )