Example #1
0
    def test_create_trigger_applies_to_records(self):
        ds = factories.Dataset()

        helpers.call_action(
            u'datastore_function_create',
            name=u'spamify_trigger',
            rettype=u'trigger',
            definition=u'''
                BEGIN
                NEW.spam := 'spam spam ' || NEW.spam || ' spam';
                RETURN NEW;
                END;''')

        app = self._get_test_app()
        with app.flask_app.test_request_context():
            res = helpers.call_action(
                u'datastore_create',
                resource={u'package_id': ds['id']},
                fields=[{u'id': u'spam', u'type': u'text'}],
                records=[{u'spam': u'SPAM'}, {u'spam': u'EGGS'}],
                triggers=[{u'function': u'spamify_trigger'}])
        assert_equal(
            helpers.call_action(
                u'datastore_search',
                fields=[u'spam'],
                resource_id=res['resource_id'])['records'],
            [
                {u'spam': u'spam spam SPAM spam'},
                {u'spam': u'spam spam EGGS spam'}])
Example #2
0
    def test_default_views_created_on_resource_update(self):

        dataset_dict = factories.Dataset(
            resources=[{
                'url': 'http://not.for.viewing',
                'format': 'xxx',
            }]
        )

        resource_id = dataset_dict['resources'][0]['id']

        views_list = helpers.call_action('resource_view_list', id=resource_id)

        eq_(len(views_list), 0)

        resource_dict = {
            'id': resource_id,
            'package_id': dataset_dict['id'],
            'url': 'http://some.image.png',
            'format': 'png',
        }

        updated_resource_dict = helpers.call_action('resource_update', **resource_dict)

        views_list = helpers.call_action('resource_view_list', id=updated_resource_dict['id'])

        eq_(len(views_list), 1)
        eq_(views_list[0]['view_type'], 'image_view')
Example #3
0
    def test_dataset_showcase_page_add_to_showcase_dropdown_list(self):
        '''
        Add to showcase dropdown only lists showcases that aren't already
        associated with dataset.
        '''
        app = self._get_test_app()
        sysadmin = factories.Sysadmin()
        dataset = factories.Dataset(name='my-dataset')
        showcase_one = factories.Dataset(name='my-first-showcase', type='showcase')
        showcase_two = factories.Dataset(name='my-second-showcase', type='showcase')
        showcase_three = factories.Dataset(name='my-third-showcase', type='showcase')

        context = {'user': sysadmin['name']}
        helpers.call_action('ckanext_showcase_package_association_create',
                            context=context, package_id=dataset['id'],
                            showcase_id=showcase_one['id'])

        response = app.get(
            url=url_for(controller='ckanext.showcase.controller:ShowcaseController',
                        action='dataset_showcase_list', id=dataset['id']),
            extra_environ={'REMOTE_USER': str(sysadmin['name'])}
        )

        showcase_add_form = response.forms['showcase-add']
        showcase_added_options = [value for (value, _) in showcase_add_form['showcase_added'].options]
        nosetools.assert_true(showcase_one['id'] not in showcase_added_options)
        nosetools.assert_true(showcase_two['id'] in showcase_added_options)
        nosetools.assert_true(showcase_three['id'] in showcase_added_options)
Example #4
0
 def setup(self):
     super(TestProjectBase, self).setup()
     self.user = factories.User()
     context = {
         'model': model,
         'session': model.Session,
         'user': self.user['name'],
     }
     org_create_context = context.copy()
     org_create_context['schema'] = schema.default_group_schema()
     self.organization = helpers.call_action(
         'organization_create',
         context=context,
         id='1',
         name='organization'
     )
     project_context = context.copy()
     project_context['schema'] = schema.default_create_package_schema()
     self.project = helpers.call_action(
         'package_create',
         context=project_context,
         type='project',
         id='1',
         name='test',
         title='Test',
         owner_org=self.organization['name'],
     )
Example #5
0
    def test_dataset_showcase_page_remove_showcase_button_submit(self):
        '''
        Submitting 'Remove' form with selected showcase value deletes a sc/pkg
        association.
        '''
        app = self._get_test_app()
        sysadmin = factories.Sysadmin()
        dataset = factories.Dataset(name='my-dataset')
        showcase_one = factories.Dataset(name='my-first-showcase', type='showcase')

        context = {'user': sysadmin['name']}
        helpers.call_action('ckanext_showcase_package_association_create',
                            context=context, package_id=dataset['id'],
                            showcase_id=showcase_one['id'])

        nosetools.assert_equal(model.Session.query(ShowcasePackageAssociation).count(), 1)

        env = {'REMOTE_USER': sysadmin['name'].encode('ascii')}
        response = app.get(
            url=url_for(controller='ckanext.showcase.controller:ShowcaseController',
                        action='dataset_showcase_list', id=dataset['id']),
            extra_environ=env
        )

        # Submit the remove form.
        form = response.forms[1]
        nosetools.assert_equal(form['remove_showcase_id'].value, showcase_one['id'])
        showcase_remove_response = submit_and_follow(app, form, env)

        # returns to the correct page
        nosetools.assert_equal(showcase_remove_response.request.path, "/dataset/showcases/my-dataset")
        # the association is deleted
        nosetools.assert_equal(model.Session.query(ShowcasePackageAssociation).count(), 0)
Example #6
0
    def test_not_authorized_if_user_has_no_permissions_on_dataset(self):

        org = factories.Organization()

        user = factories.User()

        member = {'username': user['name'],
                  'role': 'admin',
                  'id': org['id']}
        helpers.call_action('organization_member_create', **member)

        user_2 = factories.User()

        dataset = factories.Dataset(owner_org=org['id'])

        resource = factories.Resource(package_id=dataset['id'])

        resource_view = {'resource_id': resource['id'],
                         'title': u'Resource View',
                         'view_type': u'image_view',
                         'image_url': 'url'}

        context = {'user': user_2['name'], 'model': core_model}
        nose.tools.assert_raises(logic.NotAuthorized, helpers.call_auth,
                                 'resource_view_create', context=context,
                                 **resource_view)
Example #7
0
    def test_dataset_showcase_page_lists_showcases_two_associations(self):
        '''
        Two showcases are listed for dataset with two showcase associations.
        '''

        app = self._get_test_app()
        sysadmin = factories.Sysadmin()
        dataset = factories.Dataset(name='my-dataset')
        showcase_one = factories.Dataset(name='my-first-showcase', type='showcase')
        showcase_two = factories.Dataset(name='my-second-showcase', type='showcase')
        factories.Dataset(name='my-third-showcase', type='showcase')

        context = {'user': sysadmin['name']}
        helpers.call_action('ckanext_showcase_package_association_create',
                            context=context, package_id=dataset['id'],
                            showcase_id=showcase_one['id'])
        helpers.call_action('ckanext_showcase_package_association_create',
                            context=context, package_id=dataset['id'],
                            showcase_id=showcase_two['id'])

        response = app.get(
            url=url_for(controller='ckanext.showcase.controller:ShowcaseController',
                        action='dataset_showcase_list', id=dataset['id'])
        )

        nosetools.assert_equal(len(response.html.select('li.media-item')), 2)
        nosetools.assert_true('my-first-showcase' in response)
        nosetools.assert_true('my-second-showcase' in response)
        nosetools.assert_true('my-third-showcase' not in response)
Example #8
0
    def test_user_update_name(self):
        '''Test that updating a user's name works successfully.'''

        # The canonical form of a test has four steps:
        # 1. Setup any preconditions needed for the test.
        # 2. Call the function that's being tested, once only.
        # 3. Make assertions about the return value and/or side-effects of
        #    of the function that's being tested.
        # 4. Do nothing else!

        # 1. Setup.
        user = factories.User()

        # 2. Call the function that's being tested, once only.
        # FIXME we have to pass the email address and password to user_update
        # even though we're not updating those fields, otherwise validation
        # fails.
        helpers.call_action('user_update', id=user['name'],
                            email=user['email'],
                            password=factories.User.attributes()['password'],
                            name='updated',
                            )

        # 3. Make assertions about the return value and/or side-effects.
        updated_user = helpers.call_action('user_show', id=user['id'])
        # Note that we check just the field we were trying to update, not the
        # entire dict, only assert what we're actually testing.
        assert updated_user['name'] == 'updated'
Example #9
0
    def test_resource_view_list_reorder(self):

        resource_view_1 = factories.ResourceView(title='View 1')

        resource_id = resource_view_1['resource_id']

        resource_view_2 = factories.ResourceView(resource_id=resource_id,
                                                 title='View 2')

        resource_view_list = helpers.call_action('resource_view_list', id=resource_id)

        assert_equals(resource_view_list[0]['title'], 'View 1')
        assert_equals(resource_view_list[1]['title'], 'View 2')

        # Reorder views

        result = helpers.call_action('resource_view_reorder',
                                     id=resource_id,
                                     order=[resource_view_2['id'], resource_view_1['id']])
        assert_equals(result['order'], [resource_view_2['id'], resource_view_1['id']])

        resource_view_list = helpers.call_action('resource_view_list', id=resource_id)

        assert_equals(resource_view_list[0]['title'], 'View 2')
        assert_equals(resource_view_list[1]['title'], 'View 1')
Example #10
0
    def test_add_default_views_to_dataset_resources(self):

        # New resources have no views
        dataset_dict = factories.Dataset(resources=[
            {
                'url': 'http://some.image.png',
                'format': 'png',
                'name': 'Image 1',
            },
            {
                'url': 'http://some.image.png',
                'format': 'png',
                'name': 'Image 2',
            },
        ])

        # Change default views config setting
        config['ckan.views.default_views'] = 'image_view'

        context = {
            'user': helpers.call_action('get_site_user')['name']
        }
        created_views = helpers.call_action(
            'package_create_default_resource_views',
            context,
            package=dataset_dict)

        assert_equals(len(created_views), 2)

        assert_equals(created_views[0]['view_type'], 'image_view')
        assert_equals(created_views[1]['view_type'], 'image_view')
Example #11
0
    def test_add_default_views_to_resource(self):

        # New resources have no views
        dataset_dict = factories.Dataset()
        resource_dict = factories.Resource(
            package_id=dataset_dict['id'],
            url='http://some.image.png',
            format='png',
        )

        # Change default views config setting
        config['ckan.views.default_views'] = 'image_view'

        context = {
            'user': helpers.call_action('get_site_user')['name']
        }
        created_views = helpers.call_action(
            'resource_create_default_resource_views',
            context,
            resource=resource_dict,
            package=dataset_dict)

        assert_equals(len(created_views), 1)

        assert_equals(created_views[0]['view_type'], 'image_view')
Example #12
0
    def test_error_from_cadasta_api_raises_validation_error(self):
        for action, cadasta_endpoint in get_api_map.items():
            print "testing {action}".format(action=action),

            # add the expected parameters (everything is a 1)
            api_url = cadasta_endpoint.url
            url_args = dict([(a[1], 1) for a in string.Formatter().parse(api_url) if a[1]])

            # make sure the point parameters are filled out
            endpoint = urljoin(self.test_api, api_url).format(**url_args)

            # fake out our response
            responses.add(
                responses.GET,
                endpoint,
                body='{"error": {"code": 1}, "message": "err msg"}',
                content_type="application/json",
            )

            with assert_raises(toolkit.ValidationError) as cm:
                helpers.call_action(action, **url_args)

            assert_dict_equal({u"code": [1], "message": [u"err msg"], "type": [""]}, cm.exception.error_dict)

            print "\t[OK]"
Example #13
0
    def test_package_project_list_project_has_two_packages(self):
        '''
        Calling ckanext_package_project_list with a package that has two
        projects should return the projects.
        '''
        sysadmin = factories.User(sysadmin=True)

        package = factories.Dataset()
        project_one = factories.Dataset(type='project')
        project_two = factories.Dataset(type='project')
        context = {'user': sysadmin['name']}
        # create first association
        helpers.call_action('ckanext_project_package_association_create',
                            context=context, package_id=package['id'],
                            project_id=project_one['id'])
        # create second association
        helpers.call_action('ckanext_project_package_association_create',
                            context=context, package_id=package['id'],
                            project_id=project_two['id'])

        project_list = helpers.call_action('ckanext_package_project_list',
                                            package_id=package['id'])

        # We've got two items in the project_list
        nosetools.assert_equal(len(project_list), 2)
    def test_package_purge_deletes_associations(self):
        '''
        Purging a package (actually deleting it from the database) deletes
        associated ShowcasePackageAssociation objects.
        '''
        sysadmin = factories.Sysadmin()
        context = {'user': sysadmin['name']}
        showcase = factories.Dataset(type='showcase', name='my-showcase')
        dataset_one = factories.Dataset(name='dataset-one')
        dataset_two = factories.Dataset(name='dataset-two')

        helpers.call_action('ckanext_showcase_package_association_create',
                            context=context, package_id=dataset_one['id'],
                            showcase_id=showcase['id'])
        helpers.call_action('ckanext_showcase_package_association_create',
                            context=context, package_id=dataset_two['id'],
                            showcase_id=showcase['id'])

        nosetools.assert_equal(model.Session.query(ShowcasePackageAssociation).count(), 2)

        # purge the first package, should also delete the
        # ShowcasePackageAssociation associated with it.
        pkg = model.Session.query(model.Package).get(dataset_one['id'])
        pkg.purge()
        model.repo.commit_and_remove()

        nosetools.assert_equal(model.Session.query(ShowcasePackageAssociation).count(), 1)
Example #15
0
    def test_all_no_parameters_fail(self):
        for action, cadasta_endpoint in get_api_map.items():
            print "testing {action}".format(action=action),
            # read our expected json output as <action_name>.json
            filepath = os.path.join(self.data_dir, ".".join([action, "json"]))
            body = open(filepath).read()

            # add the expected parameters (everything is a 1)
            api_url = cadasta_endpoint.url
            url_args = dict([(a[1], 1) for a in string.Formatter().parse(api_url) if a[1]])

            # if this endpoint needs no parameters, quit early, test does not
            # apply
            if not url_args:
                return

            # make sure the point parameters are filled out
            endpoint = urljoin(self.test_api, api_url).format(**url_args)

            # fake out our response
            responses.add(responses.GET, endpoint, body=body, content_type="application/json")

            # call our action with no arguments
            with assert_raises(toolkit.ValidationError) as cm:
                helpers.call_action(action)
            for error in cm.exception.error_dict.values():
                assert_equal(["Missing value"], error)
            print "\t[OK]"
Example #16
0
    def test_purged_org_does_not_show(self):
        org = factories.Organization()

        helpers.call_action('organization_purge', id=org['name'])

        assert_raises(logic.NotFound, helpers.call_action, 'organization_show',
                      context={}, id=org['name'])
Example #17
0
    def test_user_delete_removes_memberships(self):
        user = factories.User()
        factories.Organization(
            users=[{u'name': user[u'id'], u'capacity': u'admin'}])

        factories.Group(
            users=[{u'name': user[u'id'], u'capacity': u'admin'}])

        user_memberships = model.Session.query(model.Member).filter(
            model.Member.table_id == user[u'id']).all()

        assert_equals(len(user_memberships), 2)

        assert_equals([m.state for m in user_memberships],
                      [u'active', u'active'])

        context = {}
        params = {u'id': user[u'id']}

        helpers.call_action(u'user_delete', context, **params)

        user_memberships = model.Session.query(model.Member).filter(
            model.Member.table_id == user[u'id']).all()

        # Member objects are still there, but flagged as deleted
        assert_equals(len(user_memberships), 2)

        assert_equals([m.state for m in user_memberships],
                      [u'deleted', u'deleted'])
Example #18
0
    def test_bulk_delete(self):

        org = factories.Organization()

        dataset1 = factories.Dataset(owner_org=org['id'])
        dataset2 = factories.Dataset(owner_org=org['id'])

        helpers.call_action('bulk_update_delete', {},
                            datasets=[dataset1['id'], dataset2['id']],
                            org_id=org['id'])

        # Check search index
        datasets = helpers.call_action('package_search', {},
                                       q='owner_org:{0}'.format(org['id']))

        eq_(datasets['results'], [])

        # Check DB
        datasets = model.Session.query(model.Package) \
            .filter(model.Package.owner_org == org['id']).all()
        for dataset in datasets:
            eq_(dataset.state, 'deleted')

        revisions = model.Session.query(model.PackageRevision) \
            .filter(model.PackageRevision.owner_org == org['id']) \
            .filter(model.PackageRevision.current is True) \
            .all()
        for revision in revisions:
            eq_(revision.state, 'deleted')
Example #19
0
    def test_purged_group_does_not_show(self):
        group = factories.Group()

        helpers.call_action('group_purge', id=group['name'])

        assert_raises(logic.NotFound, helpers.call_action, 'group_show',
                      context={}, id=group['name'])
    def test_datastore_delete_custom_filters_have_the_correct_operator_precedence(self):
        '''
        We're testing that the WHERE clause becomes:
            (age < 50 OR age > 60) AND age = 30
        And not:
            age < 50 OR age > 60 AND age = 30
        '''
        records = [
            {'age': 20}, {'age': 30}, {'age': 40}
        ]
        resource = self._create_datastore_resource(records)
        filters = {
            'age_not_between': [50, 60],
            'age': 30
        }

        helpers.call_action(DATASTORE_DELETE,
                            resource_id=resource['id'],
                            force=True,
                            filters=filters)

        result = helpers.call_action(DATASTORE_SEARCH,
                                     resource_id=resource['id'])

        new_records_ages = [r['age'] for r in result['records']]
        new_records_ages.sort()
        assert_equals(new_records_ages, [20, 40])
Example #21
0
    def test_bulk_make_public(self):

        org = factories.Organization()

        dataset1 = factories.Dataset(owner_org=org['id'], private=True)
        dataset2 = factories.Dataset(owner_org=org['id'], private=True)

        helpers.call_action('bulk_update_public', {},
                            datasets=[dataset1['id'], dataset2['id']],
                            org_id=org['id'])

        # Check search index
        datasets = helpers.call_action('package_search', {},
                                       q='owner_org:{0}'.format(org['id']))

        for dataset in datasets['results']:
            eq_(dataset['private'], False)

        # Check DB
        datasets = model.Session.query(model.Package) \
            .filter(model.Package.owner_org == org['id']).all()
        for dataset in datasets:
            eq_(dataset.private, False)

        revisions = model.Session.query(model.PackageRevision) \
            .filter(model.PackageRevision.owner_org == org['id']) \
            .filter(model.PackageRevision.current is True) \
            .all()
        for revision in revisions:
            eq_(revision.private, False)
Example #22
0
 def test_all_params_work_with_fields_with_whitespaces(self):
     resource = factories.Resource()
     data = {
         'resource_id': resource['id'],
         'force': True,
         'records': [
             {'the year': 2014},
             {'the year': 2013},
         ],
     }
     result = helpers.call_action('datastore_create', **data)
     search_data = {
         'resource_id': resource['id'],
         'fields': 'the year',
         'sort': 'the year',
         'filters': {
             'the year': 2013
         },
         'q': {
             'the year': '2013'
         },
     }
     result = helpers.call_action('datastore_search', **search_data)
     result_years = [r['the year'] for r in result['records']]
     assert_equals(result_years, [2013])
Example #23
0
 def test_dry_run_trigger_error(self):
     ds = factories.Dataset()
     helpers.call_action(
         u'datastore_function_create',
         name=u'spamexception_trigger',
         rettype=u'trigger',
         definition=u'''
             BEGIN
             IF NEW.spam != 'spam' THEN
                 RAISE EXCEPTION '"%"? Yeeeeccch!', NEW.spam;
             END IF;
             RETURN NEW;
             END;''')
     table = helpers.call_action(
         u'datastore_create',
         resource={u'package_id': ds['id']},
         fields=[{u'id': u'spam', u'type': u'text'}],
         primary_key=u'spam',
         triggers=[{u'function': u'spamexception_trigger'}])
     try:
         helpers.call_action(
             u'datastore_upsert',
             resource_id=table['resource_id'],
             records=[{u'spam': u'EGGS'}],
             dry_run=True)
     except ValidationError as ve:
         assert_equal(ve.error_dict['records'],
             [u'"EGGS"? Yeeeeccch!'])
     else:
         assert 0, 'error not raised'
    def test_custom_search(self):
        app = helpers._get_test_app()

        helpers.call_action('package_create', name='test_package_a',
                            custom_text='z')
        helpers.call_action('package_create', name='test_package_b',
                            custom_text='y')

        response = app.get('/dataset/')

        # change the sort by form to our custom_text ascending
        response.forms[1].fields['sort'][0].value = 'custom_text asc'

        response = response.forms[1].submit()
        # check that package_b appears before package a (y < z)
        a = response.body.index('test_package_a')
        b = response.body.index('test_package_b')
        nt.assert_true(b < a)

        response = app.get('/dataset/')
        response.forms[1].fields['sort'][0].value = 'custom_text desc'
        # check that package_a appears before package b (z is first in
        # descending order)
        response = response.forms[1].submit()
        a = response.body.index('test_package_a')
        b = response.body.index('test_package_b')
        nt.assert_true(a < b)
    def test_it_allows_uploading_a_zipfile(self):
        dataset = helpers.call_action(
            "create_dataset_from_mapaction_zip", upload=_UploadFile(custom_helpers.get_test_zip())
        )

        nose.tools.assert_equal(
            dataset["title"], "Central African Republic:  Example Map- Reference (as of 3 Feb 2099)"
        )

        nose.tools.assert_equal(dataset["name"], "189-ma001-aptivate-example")

        # Expect the JPEG And PDF referenced in the XML Metadata
        dataset = helpers.call_action("package_show", id=dataset["id"])
        resources = dataset["resources"]
        extras = dataset["extras"]

        nose.tools.assert_true(len(resources) == 2)
        nose.tools.assert_true(len(extras) > 0)

        sorted_resources = sorted(resources, key=lambda k: k["format"])

        self._check_uploaded_resource(
            sorted_resources[0], "JPEG", "MA001_Aptivate_Example-300dpi.jpeg", "ma001aptivateexample-300dpi.jpeg"
        )
        self._check_uploaded_resource(
            sorted_resources[1], "PDF", "MA001_Aptivate_Example-300dpi.pdf", "ma001aptivateexample-300dpi.pdf"
        )
    def test_group_image_upload(self):
        '''Test a group image file upload'''
        sysadmin = factories.Sysadmin(apikey="my-test-key")

        file_path = os.path.join(os.path.dirname(__file__), 'data.csv')
        file_name = 'somename.png'

        img_uploader = Uploader(file_name, file=open(file_path))

        with mock.patch('ckanext.s3filestore.uploader.datetime') as mock_date:
            mock_date.datetime.utcnow.return_value = \
                datetime.datetime(2001, 1, 29)
            context = {'user': sysadmin['name']}
            helpers.call_action('group_create', context=context,
                                name="my-group",
                                image_upload=img_uploader,
                                image_url=file_name,
                                save='save')

        key = '{0}/storage/uploads/group/2001-01-29-000000{1}' \
            .format(config.get('ckanext.s3filestore.aws_storage_path'), file_name)

        conn = boto.connect_s3()
        bucket = conn.get_bucket('my-bucket')
        # test the key exists
        assert_true(bucket.lookup(key))

        # requesting image redirects to s3
        app = self._get_test_app()
        # attempt redirect to linked url
        image_file_url = '/uploads/group/{0}'.format(file_name)
        r = app.get(image_file_url, status=[302, 301])
        assert_equal(r.location, 'https://my-bucket.s3.amazonaws.com/my-path/storage/uploads/group/{0}'
                                 .format(file_name))
    def test_group_image_upload_then_clear(self):
        '''Test that clearing an upload removes the S3 key'''

        sysadmin = factories.Sysadmin(apikey="my-test-key")

        file_path = os.path.join(os.path.dirname(__file__), 'data.csv')
        file_name = "somename.png"

        img_uploader = Uploader(file_name, file=open(file_path))

        with mock.patch('ckanext.s3filestore.uploader.datetime') as mock_date:
            mock_date.datetime.utcnow.return_value = \
                datetime.datetime(2001, 1, 29)
            context = {'user': sysadmin['name']}
            helpers.call_action('group_create', context=context,
                                name="my-group",
                                image_upload=img_uploader,
                                image_url=file_name)

        key = '{0}/storage/uploads/group/2001-01-29-000000{1}' \
            .format(config.get('ckanext.s3filestore.aws_storage_path'), file_name)

        conn = boto.connect_s3()
        bucket = conn.get_bucket('my-bucket')
        # test the key exists
        assert_true(bucket.lookup(key))

        # clear upload
        helpers.call_action('group_update', context=context,
                            id='my-group', name='my-group',
                            image_url="http://asdf", clear_upload=True)

        # key shouldn't exist
        assert_false(bucket.lookup(key))
Example #28
0
    def test_group_patch_preserve_datasets(self):
        user = factories.User()
        group = factories.Group(
            name='economy',
            description='some test now',
            user=user)
        factories.Dataset(groups=[{'name': group['name']}])

        group2 = helpers.call_action('group_show', id=group['id'])
        assert_equals(1, group2['package_count'])

        group = helpers.call_action(
            'group_patch',
            id=group['id'],
            context={'user': user['name']})

        group3 = helpers.call_action('group_show', id=group['id'])
        assert_equals(1, group3['package_count'])

        group = helpers.call_action(
            'group_patch',
            id=group['id'],
            packages=[],
            context={'user': user['name']})

        group4 = helpers.call_action(
            'group_show', id=group['id'], include_datasets=True
        )
        assert_equals(0, group4['package_count'])
Example #29
0
    def test_delete_basic(self):
        resource = factories.Resource()
        data = {
            'resource_id': resource['id'],
            'force': True,
            'aliases': u'b\xfck2',
            'fields': [{'id': 'book', 'type': 'text'},
                       {'id': 'author', 'type': 'text'},
                       {'id': 'rating with %', 'type': 'text'}],
            'records': [{'book': 'annakarenina', 'author': 'tolstoy',
                         'rating with %': '90%'},
                        {'book': 'warandpeace', 'author': 'tolstoy',
                         'rating with %': '42%'}]
        }
        helpers.call_action('datastore_create', **data)
        data = {
            'resource_id': resource['id'],
            'force': True,
        }
        helpers.call_action('datastore_delete', **data)

        results = execute_sql(u'select 1 from pg_views where viewname = %s', u'b\xfck2')
        assert results.rowcount == 0

        # check the table is gone
        results = execute_sql(
            u'''SELECT table_name
            FROM information_schema.tables
            WHERE table_name=%s;''',
            resource['id'])
        assert results.rowcount == 0
    def test_showcase_package_list_showcase_has_two_packages(self):
        '''
        Calling ckanext_showcase_package_list with a showcase that has two
        packages should return the packages.
        '''
        sysadmin = factories.User(sysadmin=True)

        package_one = factories.Dataset()
        package_two = factories.Dataset()
        showcase_id = factories.Dataset(type='showcase')['id']
        context = {'user': sysadmin['name']}
        # create first association
        helpers.call_action('ckanext_showcase_package_association_create',
                            context=context, package_id=package_one['id'],
                            showcase_id=showcase_id)
        # create second association
        helpers.call_action('ckanext_showcase_package_association_create',
                            context=context, package_id=package_two['id'],
                            showcase_id=showcase_id)

        pkg_list = helpers.call_action('ckanext_showcase_package_list',
                                       showcase_id=showcase_id)

        # We've got two items in the pkg_list
        nosetools.assert_equal(len(pkg_list), 2)
Example #31
0
    def test_org_member_creation_raises_validation_error_if_role_missing(self):

        with pytest.raises(logic.ValidationError):
            helpers.call_action(
                "organization_member_create", id="someid", username="******"
            )
Example #32
0
 def test_resource_version_current_no_versions(self):
     resource = factories.Resource(name='First name')
     assert helpers.call_action('resource_version_current', {},
                                resource_id=resource['id']) is None
Example #33
0
def test_config_option_update_action_works_on_flask(reset_db, ckan_config):
    params = {u"ckan.site_title": u"Example title action"}
    helpers.call_action(u"config_option_update", {}, **params)
    assert ckan_config[u"ckan.site_title"] == u"Example title action"
    reset_db()
    def test_showcase_anon_user_can_see_package_list_when_showcase_association_was_deleted(
            self):
        '''
        When a showcase is deleted, the remaining associations with formerly associated
        packages or showcases can still be displayed.
        '''
        app = self._get_test_app()

        sysadmin = factories.User(sysadmin=True)

        showcase_one = factories.Dataset(type='showcase', name='showcase-one')
        showcase_two = factories.Dataset(type='showcase', name='showcase-two')
        package_one = factories.Dataset()
        package_two = factories.Dataset()

        admin_context = {'user': sysadmin['name']}

        # create the associations
        helpers.call_action('ckanext_showcase_package_association_create',
                            context=admin_context,
                            package_id=package_one['id'],
                            showcase_id=showcase_one['id'])
        helpers.call_action('ckanext_showcase_package_association_create',
                            context=admin_context,
                            package_id=package_one['id'],
                            showcase_id=showcase_two['id'])
        helpers.call_action('ckanext_showcase_package_association_create',
                            context=admin_context,
                            package_id=package_two['id'],
                            showcase_id=showcase_one['id'])
        helpers.call_action('ckanext_showcase_package_association_create',
                            context=admin_context,
                            package_id=package_two['id'],
                            showcase_id=showcase_two['id'])

        # delete one of the associated showcases
        helpers.call_action('package_delete',
                            context=admin_context,
                            id=showcase_two['id'])

        # the anon user can still see the associated packages of remaining showcase
        associated_packages = helpers.call_action(
            'ckanext_showcase_package_list', showcase_id=showcase_one['id'])

        nosetools.assert_equal(len(associated_packages), 2)

        # overview of packages can still be seen
        app.get("/dataset", status=200)
Example #35
0
    def test_return_id_only(self):
        dataset = helpers.call_action(
            "package_create", name="test-id", context={"return_id_only": True}
        )

        assert isinstance(dataset, string_types)
Example #36
0
 def test_missing_id(self):
     with pytest.raises(logic.ValidationError):
         helpers.call_action("package_create")
Example #37
0
 def test_name_not_changed_during_deletion(self):
     dataset = factories.Dataset()
     helpers.call_action("package_delete", id=dataset["id"])
     deleted_dataset = helpers.call_action("package_show", id=dataset["id"])
     assert deleted_dataset["name"] == dataset["name"]
Example #38
0
    def test_it_requires_package_id(self):

        data_dict = {"url": "http://data"}

        with pytest.raises(logic.ValidationError):
            helpers.call_action("resource_create", **data_dict)
Example #39
0
 def test_raises_if_couldnt_find_view_extension(self):
     context = {}
     params = self._default_resource_view_attributes(view_type="unknown")
     with pytest.raises(logic.ValidationError):
         helpers.call_action("resource_view_create", context, **params)
Example #40
0
    def test_group_member_creation_raises_validation_error_if_id_missing(self):

        with pytest.raises(logic.ValidationError):
            helpers.call_action(
                "group_member_create", username="******", role="member"
            )
Example #41
0
 def submit(res, user):
     return helpers.call_action('shift_submit',
                                context=dict(user=user['name']),
                                resource_id=res['id'])
Example #42
0
 def test_non_filterable_views_dont_accept_filter_fields_and_values(self):
     context = {}
     filters = {"filter_fields": "country", "filter_values": "Brazil"}
     params = self._default_resource_view_attributes(**filters)
     with pytest.raises(logic.ValidationError):
         helpers.call_action("resource_view_create", context, **params)
Example #43
0
 def test_create_delete(self):
     helpers.call_action(u'datastore_function_create',
                         name=u'test_nop',
                         rettype=u'trigger',
                         definition=u'BEGIN RETURN NEW; END;')
     helpers.call_action(u'datastore_function_delete', name=u'test_nop')
Example #44
0
 def _create(cls, target_class, *args, **kwargs):
     if args:
         assert False, "Positional args aren't supported, use keyword args."
     return helpers.call_action("activity_create", **kwargs)
 def test_package_show(self):
     helpers.call_action('package_create',
                         name='test_package',
                         custom_text='this is my custom text')
     result = helpers.call_action('package_show', name_or_id='test_package')
     nt.assert_equals('this is my custom text', result['custom_text'])
Example #46
0
 def test_delete_if_exitst(self):
     helpers.call_action(u'datastore_function_delete',
                         name=u'test_not_there_either',
                         if_exists=True)
Example #47
0
 def test_disabled_update_to_next_stage(self):
     pkg = th.call_action('package_create', name='test-package1')
     nt.assert_raises(
         tk.ValidationError, th.call_action,
         'package_patch', id=pkg['id'], workflow_stage='pending')
Example #48
0
 def submit(res, user):
     return helpers.call_action(
         "xloader_submit",
         context=dict(user=user["name"]),
         resource_id=res["id"],
     )
Example #49
0
 def test_creation(self):
     pkg = th.call_action('package_create', name='test-package')
     nt.assert_equal(pkg['workflow_type'], 'base')
     nt.assert_equal(pkg['workflow_stage'], 'unpublished')
Example #50
0
 def test_move_to_pending_stage(self):
     pkg = factories.Dataset()
     data = th.call_action('move_to_next_stage', id=pkg['id'])
     nt.assert_equal(data['workflow_stage'], 'pending')
Example #51
0
 def test_works_with_semicolons_inside_strings(self):
     sql = 'SELECT * FROM public."{0}" WHERE "author" = \'foo; bar\''.format(self.data['resource_id'])
     helpers.call_action('datastore_ts_search_sql', sql=sql)
Example #52
0
 def test_patch(self):
     pkg = th.call_action('package_create', name='test-package')
     pkg = th.call_action('package_patch', id=pkg['id'], name='test-kage2')
     nt.assert_equal(pkg['workflow_type'], 'base')
     nt.assert_equal(pkg['workflow_stage'], 'unpublished')
     nt.assert_equal(pkg['name'], 'test-kage2')
Example #53
0
    def test_delete_wrong_id_raises_not_found_error(self):

        params = {"id": "does_not_exist"}

        with pytest.raises(logic.NotFound):
            helpers.call_action("resource_view_delete", context={}, **params)
Example #54
0
 def test_move_to_unpublished_stage_from_published(self):
     pkg = factories.Dataset()
     data = th.call_action('move_to_next_stage', id=pkg['id'])
     data = th.call_action('move_to_next_stage', id=pkg['id'])
     data = th.call_action('move_to_previous_stage', id=pkg['id'])
     nt.assert_equal(data['workflow_stage'], 'unpublished')
Example #55
0
 def test_no_id(self):
     with pytest.raises(logic.ValidationError):
         helpers.call_action("vocabulary_delete")
Example #56
0
    def test_theme_to_group_mapping(self):
        # multilang requires lang to be set
        # class dummyreq(object):
        #     class p(object):
        #         translator = object()
        #     environ = {'pylons.pylons': p()}

        # CKANRequest(dummyreq)
        # pylons.request = dummyreq()
        # pylons.translator.pylons_lang = ['en_GB']

        #set_lang('en_GB')
        #assert get_lang() == ['en_GB']
        assert 'dcatapit_theme_group_mapper' in config[
            'ckan.plugins'], 'No dcatapit_theme_group_mapper plugin in config'

        with open(get_example_file('dataset.rdf'), 'r') as f:
            contents = f.read()

        p = RDFParser(profiles=['it_dcat_ap'])

        p.parse(contents)
        datasets = [d for d in p.datasets()]
        self.assertEqual(len(datasets), 1)
        package_dict = datasets[0]

        user = User.get('dummy')

        if not user:
            user = call_action('user_create',
                               name='dummy',
                               password='******',
                               email='*****@*****.**')
            user_name = user['name']
        else:
            user_name = user.name
        org = Group.by_name('dummy')
        if org is None:
            org = call_action('organization_create',
                              context={'user': user_name},
                              name='dummy',
                              identifier='aaaaaa')
        existing_g = Group.by_name('existing-group')
        if existing_g is None:
            existing_g = call_action('group_create',
                                     context={'user': user_name},
                                     name='existing-group')

        context = {'user': '******', 'ignore_auth': True, 'defer_commit': False}
        package_schema = schema.default_create_package_schema()
        context['schema'] = package_schema
        _p = {
            'frequency': 'manual',
            'publisher_name': 'dummy',
            'extras': [{
                'key': 'theme',
                'value': ['non-mappable', 'thememap1']
            }],
            'groups': [],  #  [{'name':existing_g.name}],
            'title': 'dummy',
            'holder_name': 'dummy',
            'holder_identifier': 'dummy',
            'name': 'dummy-' + uuid4().hex,
            'identifier': 'dummy' + uuid4().hex,
            'notes': 'dummy',
            'owner_org': 'dummy',
            'modified': datetime.now(),
            'publisher_identifier': 'dummy',
            'metadata_created': datetime.now(),
            'metadata_modified': datetime.now(),
            'guid': str(uuid.uuid4),
        }

        package_dict.update(_p)

        config[DCATAPIT_THEME_TO_MAPPING_SOURCE] = ''
        config[DCATAPIT_THEME_TO_MAPPING_ADD_NEW_GROUPS] = 'false'

        package_data = call_action('package_create',
                                   context=context,
                                   **package_dict)

        p = Package.get(package_data['id'])

        # no groups should be assigned at this point (no map applied)
        assert {
            'theme': ['non-mappable', 'thememap1']
        } == p.extras, '{} vs {}'.format(_p['extras'], p.extras)
        assert [] == p.get_groups(
            group_type='group'), 'should be {}, got {}'.format(
                [], p.get_groups(group_type='group'))

        package_data = call_action('package_show',
                                   context=context,
                                   id=package_data['id'])

        # use test mapping, which replaces thememap1 to thememap2 and thememap3
        test_map_file = os.path.join(os.path.dirname(__file__), '..', '..',
                                     '..', 'examples', 'test_map.ini')

        config[DCATAPIT_THEME_TO_MAPPING_SOURCE] = test_map_file
        config[DCATAPIT_THEME_TO_MAPPING_ADD_NEW_GROUPS] = 'false'

        # package_dict['theme'] = ['non-mappable', 'thememap1']

        package_dict.pop('extras', None)
        p = Package.get(package_data['id'])
        context['package'] = p

        package_data = call_action('package_update',
                                   context=context,
                                   **package_dict)

        # check - only existing group should be assigned
        p = Package.get(package_data['id'])
        groups = [g.name for g in p.get_groups(group_type='group')]

        # the map file maps ECON to existing group, and 2 other unexisting groups that will not be created
        expected_groups = ['existing-group']
        self.assertSetEqual(set(expected_groups), set(groups),
                            'Error in assigned groups')

        config[DCATAPIT_THEME_TO_MAPPING_SOURCE] = test_map_file
        config[DCATAPIT_THEME_TO_MAPPING_ADD_NEW_GROUPS] = 'true'

        # package_dict['theme'] = ['non-mappable', 'thememap1']
        package_data = call_action('package_update',
                                   context=context,
                                   **package_dict)

        meta.Session.flush()

        # recheck - this time, new groups should appear
        p = Package.get(package_data['id'])
        groups = [g.name for g in p.get_groups(group_type='group')]

        # the map file maps ECON to existing group and 2 other groups that have been automatically created
        expected_groups = expected_groups + ['somegroup1', 'somegroup2']
        self.assertSetEqual(set(expected_groups), set(groups), 'Groups differ')

        # package_dict['theme'] = ['non-mappable', 'thememap1', 'thememap-multi']
        aggr = json.loads(package_dict[FIELD_THEMES_AGGREGATE])
        aggr.append({'theme': 'thememap-multi', 'subthemes': []})
        package_dict[FIELD_THEMES_AGGREGATE] = json.dumps(aggr)

        package_data = call_action('package_update',
                                   context=context,
                                   **package_dict)

        meta.Session.flush()

        # recheck - there should be no duplicates
        p = Package.get(package_data['id'])
        groups = [g.name for g in p.get_groups(group_type='group')]

        # added theme 'thememap-multi', that maps to 'othergroup' and other already exisintg groups
        expected_groups = expected_groups + ['othergroup']
        self.assertEqual(len(expected_groups), len(groups),
                         'New groups differ - there may be duplicated groups')
        self.assertSetEqual(set(expected_groups), set(groups),
                            'New groups differ')

        package_data = call_action('package_update',
                                   context=context,
                                   **package_dict)

        meta.Session.flush()

        # recheck - there still should be no duplicates
        p = Package.get(package_data['id'])
        groups = [g.name for g in p.get_groups(group_type='group')]

        self.assertEqual(len(expected_groups), len(groups),
                         'New groups differ - there may be duplicated groups')
        self.assertSetEqual(set(expected_groups), set(groups),
                            'New groups differ')

        meta.Session.rollback()
Example #57
0
    def test_basic(self):
        vocab = factories.Vocabulary()
        helpers.call_action("vocabulary_delete", id=vocab["id"])

        assert helpers.call_action("vocabulary_list") == []
Example #58
0
    def test_delete_no_id_raises_validation_error(self):

        params = {}

        with pytest.raises(logic.ValidationError):
            helpers.call_action("resource_view_delete", context={}, **params)
Example #59
0
 def test_not_existing_job(self):
     with pytest.raises(logic.NotFound):
         helpers.call_action(u"job_cancel", id=u"does-not-exist")
Example #60
0
 def test_not_existing(self):
     with pytest.raises(logic.NotFound):
         helpers.call_action("vocabulary_delete", id="does-not-exist")