Esempio n. 1
0
    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,
        )
Esempio n. 2
0
    def test_create(self):

        source_dict = self._get_source_dict()

        sysadmin = ckan_factories.Sysadmin()
        result = call_action_api('harvest_source_create',
                                 apikey=sysadmin['apikey'], **source_dict)

        for key in source_dict.keys():
            assert_equal(source_dict[key], result[key])

        # Check that source was actually created
        source = harvest_model.HarvestSource.get(result['id'])
        assert_equal(source.url, source_dict['url'])
        assert_equal(source.type, source_dict['source_type'])

        # Trying to create a source with the same URL fails
        source_dict = self._get_source_dict()
        source_dict['name'] = 'test-source-action-new'

        result = call_action_api('harvest_source_create',
                                 apikey=sysadmin['apikey'], status=409,
                                 **source_dict)

        assert 'url' in result
        assert u'There already is a Harvest Source for this URL' in result['url'][0]
Esempio n. 3
0
    def test_update(self):
        source_dict = self._get_source_dict()
        source_dict.update({
            "url": "http://test.action.updated.com",
            "name": "test-source-action-updated",
            "title": "Test source action updated",
            "notes": "Test source action desc updated",
            "source_type": "test-nose",
            "frequency": "MONTHLY",
            "config": json.dumps({"custom_option": ["c", "d"]})
        })

        sysadmin = ckan_factories.Sysadmin()
        result = call_action_api('harvest_source_update',
                                 apikey=sysadmin['apikey'],
                                 **source_dict)

        for key in set(('url', 'name', 'title', 'notes', 'source_type',
                        'frequency', 'config')):
            assert_equal(source_dict[key], result[key], "Key: %s" % key)

        # Check that source was actually updated
        source = harvest_model.HarvestSource.get(result['id'])
        assert_equal(source.url, source_dict['url'])
        assert_equal(source.type, source_dict['source_type'])
    def test_unicode(self, app):
        user = factories.Sysadmin()
        env = {'REMOTE_USER': user['name'].encode('ascii')}
        page = 'test_html_page_div'
        page = '/' + page if not ckan_29_or_higher else page
        response = app.post(
            url=toolkit.url_for('pages_edit', page=page),
            params={
                'title': u'Tïtlé'.encode('utf-8'),
                'name': 'page_unicode',
                'content': u'Çöñtéñt'.encode('utf-8'),
                'order': 1,
                'private': False,
            },
            extra_environ=env,
        )

        if not ckan_29_or_higher:
            response = response.follow(extra_environ=env)
        if toolkit.check_ckan_version(min_version='2.9.0'):
            assert u'<p>&#199;&#246;&#241;t&#233;&#241;t</p>' in response.get_data(as_text=True)
            assert u'<title>Tïtlé - CKAN</title>' in response.get_data(as_text=True)
            assert u'<a href="/pages/page_unicode">Tïtlé</a>' in response.get_data(as_text=True)
            assert u'<h1 class="page-heading">Tïtlé</h1>' in response.get_data(as_text=True)
        else:
            assert u'<title>Tïtlé - CKAN</title>' in response.unicode_body
            assert u'<a href="/pages/page_unicode">Tïtlé</a>' in response.unicode_body
            assert u'<h1 class="page-heading">Tïtlé</h1>' in response.unicode_body
            try:
                assert u'<p>&#199;&#246;&#241;t&#233;&#241;t</p>' in response.unicode_body
            except AssertionError:
                assert u'<p>Çöñtéñt</p>' in response.unicode_body
Esempio n. 5
0
    def test_sysadmin_revoke_success(self):
        admin = core_factories.Sysadmin()

        # create another sysadmin
        user = core_factories.Sysadmin(fullname='Bob')

        # revoke their status
        action = toolkit.get_action("user_update_sysadmin")
        action({'user': admin['name']}, {
            'id': user['name'],
            'is_sysadmin': False
        })

        # now they are not a sysadmin any more
        userobj = model.User.get(user['id'])
        assert False == userobj.sysadmin
Esempio n. 6
0
    def test_user_autocomplete(self):
        sysadmin = core_factories.Sysadmin(name='sysadmin', id='sysadmin')
        factories.ExternalUser(
            fullname='Alice External',
            email='*****@*****.**',
        )
        core_factories.User(fullname='Bob Internal')
        core_factories.User(fullname='Carlos Internal')
        core_factories.User(fullname='David Internal')

        action = toolkit.get_action('user_autocomplete')
        context = {'user': sysadmin['name']}

        result = action(context, {'q': 'alic'})
        assert 0 == len(result)

        result = action(context, {'q': 'alic', 'include_external': True})
        assert 'Alice External' == result[0]['fullname']

        result = action(context, {'q': 'nal'})
        fullnames = [r['fullname'] for r in result]
        assert 'Bob Internal' in fullnames
        assert 'Carlos Internal' in fullnames
        assert 'David Internal' in fullnames

        result = action(context, {'q': 'foobar'})
        assert 0 == len(result)
    def setup_class(cls):

        helpers.reset_db()
        super(TestController, cls).setup_class()
        harvest_model.setup()
        sysadmin = factories.Sysadmin()
        cls.extra_environ = {'REMOTE_USER': sysadmin['name'].encode('ascii')}
 def test_pages_index(self, app):
     user = factories.Sysadmin()
     env = {'REMOTE_USER': user['name'].encode('ascii')}
     url = toolkit.url_for('pages_index')
     response = app.get(url, status=200, extra_environ=env)
     assert '<h2>Pages</h2>' in response.body
     assert 'Add page</a>' in response.body
 def test_blog_index(self, app):
     user = factories.Sysadmin()
     env = {'REMOTE_USER': user['name'].encode('ascii')}
     endpoint = 'pages.blog_index' if ckan_29_or_higher else 'blog_index'
     url = toolkit.url_for(endpoint)
     response = app.get(url, status=200, extra_environ=env)
     assert '<h2>Blog</h2>' in response.body
     assert 'Add Article</a>' in response.body
Esempio n. 10
0
 def setup(self):
     self.sysadmin = core_factories.Sysadmin()
     self.dataset = factories.Dataset()
     self.resource = factories.Resource(
         package_id=self.dataset['id'],
         url_type='upload',
         last_modified=datetime.datetime.utcnow(),
     )
Esempio n. 11
0
    def test_user_show(self):
        sysadmin = core_factories.Sysadmin()
        external_user = factories.ExternalUser()
        internal_user = core_factories.User()

        action = toolkit.get_action('user_show')
        context = {'user': sysadmin['name']}
        assert action(context, {'id': external_user['id']})['external']
        assert not action(context, {'id': internal_user['id']})['external']
Esempio n. 12
0
 def test_group_pages_index(self, app):
     user = factories.Sysadmin()
     env = {'REMOTE_USER': user['name'].encode('ascii')}
     group = factories.Group()
     endpoint = 'pages.group_pages_index' if ckan_29_or_higher else 'group_pages_index'
     url = toolkit.url_for(endpoint, id=group['id'])
     response = app.get(url, status=200, extra_environ=env)
     assert '<h2>Pages</h2>' in response.body
     assert 'Add page</a>' in response.body
Esempio n. 13
0
    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']},
            )
Esempio n. 14
0
    def test_dataset_form_is_rendered(self, app):
        sysadmin = factories.Sysadmin()
        env = {'REMOTE_USER': sysadmin['name'].encode('ascii')}
        url = url_for('dataset.new')

        response = app.get(url, extra_environ=env)

        content = response.body

        assert '<input id="field-title"' in content
Esempio n. 15
0
 def test_sysadmin_invalid_user(self, app):
     user = core_factories.Sysadmin()
     env = {'REMOTE_USER': user['name'].encode('ascii')}
     app.post('/user/sysadmin',
              data={
                  'id': 'fred',
                  'status': '1'
              },
              extra_environ=env,
              status=404)
Esempio n. 16
0
    def setup(self):
        # Config
        toolkit.config['ckanext.unhcr.microdata_api_key'] = 'API-KEY'

        # Users
        self.sysadmin = core_factories.Sysadmin(name='sysadmin', id='sysadmin')
        self.user = core_factories.User(name='user', id='user')

        # Datasets
        self.dataset = factories.Dataset(name='dataset')
Esempio n. 17
0
    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'
            },
        ])
Esempio n. 18
0
    def test_invalid_unknown_type(self):
        source_dict = self._get_source_dict()
        source_dict['source_type'] = 'unknown'

        sysadmin = ckan_factories.Sysadmin()
        result = call_action_api(self.action,
                                 apikey=sysadmin['apikey'], status=409,
                                 **source_dict)

        assert 'source_type' in result
        assert u'Unknown harvester type' in result['source_type'][0]
Esempio n. 19
0
    def test_job_show_last_page_rendered(self, app):

        job = harvest_factories.HarvestJob()

        sysadmin = factories.Sysadmin()
        env = {"REMOTE_USER": sysadmin['name'].encode('ascii')}

        url = url_for('harvest_job_show_last', source=job['source_id'])

        response = app.get(url, extra_environ=env)

        _assert_in_body(job['id'], response)
Esempio n. 20
0
    def test_external_user(self):
        sysadmin = core_factories.Sysadmin(name='sysadmin', id='sysadmin')
        external_user = factories.ExternalUser()
        dataset = factories.Dataset()

        action = toolkit.get_action("package_collaborator_create")
        with pytest.raises(toolkit.ValidationError):
            action({'user': sysadmin['name']}, {
                'id': dataset['id'],
                'user_id': external_user['id'],
                'capacity': 'member',
            })
Esempio n. 21
0
    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(),
        )
Esempio n. 22
0
    def test_invalid_unknown_frequency(self):
        wrong_frequency = 'ANNUALLY'
        source_dict = self._get_source_dict()
        source_dict['frequency'] = wrong_frequency

        sysadmin = ckan_factories.Sysadmin()
        result = call_action_api(self.action,
                                 apikey=sysadmin['apikey'], status=409,
                                 **source_dict)

        assert 'frequency' in result
        assert u'Frequency {0} not recognised'.format(wrong_frequency) in result['frequency'][0]
Esempio n. 23
0
    def test_user_list_query(self):
        sysadmin = core_factories.Sysadmin()
        external_user = factories.ExternalUser()
        internal_user = core_factories.User()
        default_user = toolkit.get_action('get_site_user')({
            'ignore_auth': True
        })

        action = toolkit.get_action('user_list')
        context = {'user': sysadmin['name'], 'return_query': True}
        users = action(context, {})
        assert users.count() == 4
Esempio n. 24
0
    def setup(self):
        # these geographies are all related in a partent/child hierarchy
        self.hierarchy = {
            'IRQ': factories.Geography(
                pcode='IRQ',
                iso3='IRQ',
                gis_name='Iraq',
                hierarchy_pcode='IRQ'
            ),
            '20IRQ015': factories.Geography(
                pcode='20IRQ015',
                iso3='IRQ',
                gis_name='Ninewa',
                hierarchy_pcode='20IRQ015',
            ),
            '20IRQ015004': factories.Geography(
                pcode='20IRQ015004',
                iso3='IRQ',
                gis_name='Mosul',
                hierarchy_pcode='20IRQ015004',
            ),
            '20IRQ015004159': factories.Geography(
                pcode='IRQr000019225',
                iso3='IRQ',
                gis_name='Mosul',
                hierarchy_pcode='20IRQ015004159',
            ),
        }
        # some other geographies that aren't related to our 'main' hierarchy
        self.unrelated = {
            'BRZ': factories.Geography(
                pcode='BRZ', iso3='BRZ', gis_name='Brazil', hierarchy_pcode='BRZ'
            ),
            '20DEU010004': factories.Geography(
                pcode='20DEU010004',
                iso3='DEU',
                gis_name='Regierungsbezirk Dusseldorf',
                hierarchy_pcode='20DEU010004',
            ),
        }
        self.geogs = {**self.hierarchy, **self.unrelated}

        self.no_gis_dataset = factories.Dataset()
        self.gis_dataset1 = factories.Dataset(
            name="gis1",
            geographies=','.join([self.hierarchy['20IRQ015004159'].pcode, self.unrelated['BRZ'].pcode])
        )
        self.gis_dataset2 = factories.Dataset(
            name="gis2",
            geographies=self.unrelated['20DEU010004'].pcode
        )
        self.sysadmin = core_factories.Sysadmin(name='sysadmin', id='sysadmin')
Esempio n. 25
0
    def test_user_delete(self, mock_hook, mail_user_by_id):
        sysadmin = core_factories.Sysadmin()
        external_user = factories.ExternalUser(name='to-delete',
                                               id='to-delete')

        # User will leave this container orphaned after being deleted
        container = factories.DataContainer(user=sysadmin,
                                            users=[
                                                {
                                                    'name':
                                                    external_user['name'],
                                                    'capacity': 'admin'
                                                },
                                            ])
        # the container creatopr will be added as admin
        toolkit.get_action('member_delete')({
            'ignore_auth': True
        }, {
            'id': container['id'],
            'object_type': 'user',
            'object': sysadmin['id']
        })

        toolkit.get_action('user_delete')({
            'ignore_auth': True
        }, {
            'id': external_user['id']
        })

        mock_hook.assert_called_once()

        assert mock_hook.call_args_list[0][0][
            0].__name__ == 'process_last_admin_on_delete'
        assert mock_hook.call_args_list[0][0][1][0] == container['id']

        # run the job
        process_last_admin_on_delete(container['id'])
        # and validate we have a new admin
        container = toolkit.get_action('organization_show')(
            {
                'ignore_auth': True
            }, {
                'id': container['id']
            })
        assert container['users'][0]['capacity'] == 'admin'
        assert container['users'][0]['name'] == sysadmin['name']

        # Test the extra comment in the email
        mail_user_by_id.assert_called_once()
        args_list = mail_user_by_id.call_args_list
        mail_body = args_list[0][0][2]
        assert 'You have been assigned as admin' in mail_body
Esempio n. 26
0
    def test_user_list_query_empty(self):
        sysadmin = core_factories.Sysadmin()
        external_user = factories.ExternalUser()
        internal_user = core_factories.User()
        default_user = toolkit.get_action('get_site_user')({
            'ignore_auth': True
        })

        action = toolkit.get_action('user_list')
        context = {'user': sysadmin['name'], 'return_query': True}
        # add a filter to get 0 results
        users = action(context, {'email': '*****@*****.**'})
        assert users.count() == 0
Esempio n. 27
0
    def test_sysadmin_revoke_success(self, app):
        admin = core_factories.Sysadmin()
        env = {'REMOTE_USER': admin['name'].encode('ascii')}

        # create another sysadmin
        user = core_factories.Sysadmin(fullname='Bob')

        # revoke their status
        resp = app.post(
            '/user/sysadmin',
            data={
                'id': user['id'],
                'status': '0'
            },
            extra_environ=env,
            status=200,
        )
        assert ('Revoked sysadmin permission from Bob' in resp.body)

        # now they are not a sysadmin any more
        userobj = model.User.get(user['id'])
        assert not userobj.sysadmin
Esempio n. 28
0
    def test_invalid_missing_values(self):
        source_dict = {}
        test_data = self._get_source_dict()
        if 'id' in test_data:
            source_dict['id'] = test_data['id']

        sysadmin = ckan_factories.Sysadmin()
        result = call_action_api(self.action,
                                 apikey=sysadmin['apikey'], status=409,
                                 **source_dict)

        for key in ('name', 'title', 'url', 'source_type'):
            assert_equal(result[key], [u'Missing value'])
Esempio n. 29
0
 def setup(self):
     self.sysadmin = core_factories.Sysadmin()
     self.container = factories.DataContainer()
     self.payload = {
         'name': 'externaluser',
         'fullname': 'New External User',
         'email': '*****@*****.**',
         'password1': 'TestPassword1',
         'password2': 'TestPassword1',
         'message': 'I can haz access?',
         'focal_point': 'REACH',
         'container': self.container['id'],
     }
Esempio n. 30
0
    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'
            }])