Beispiel #1
0
    def test_auth_normal_user_cant_view_addtoshowcase_dropdown_dataset_showcase_list(
            self):
        '''
        A normal (logged in) user can't view the 'Add to showcase' dropdown
        selector from a datasets showcase list page.
        '''
        user = factories.User()
        app = self._get_test_app()

        factories.Dataset(name='my-showcase', type='showcase')
        factories.Dataset(name='my-dataset')

        showcase_list_response = app.get(
            '/dataset/showcases/my-dataset',
            status=200,
            extra_environ={'REMOTE_USER': str(user['name'])})

        nosetools.assert_false('showcase-add' in showcase_list_response.forms)
Beispiel #2
0
    def test_org_user_can_delete(self):
        user = factories.User()
        org_users = [{'name': user['name'], 'capacity': 'editor'}]
        org = factories.Organization(users=org_users)
        dataset = factories.Dataset(owner_org=org['id'],
                                    resources=[factories.Resource()],
                                    user=user)

        resource_view = factories.ResourceView(
            resource_id=dataset['resources'][0]['id']
        )

        context = {'user': user['name'], 'model': model}

        response = helpers.call_auth('resource_view_delete', context=context,
                                     id=resource_view['id'])

        assert_equals(response, True)
Beispiel #3
0
    def test_resource_create_with_empty_format(self):
        '''If an empty format is given when creating a resource, the resource's
        format should be set to CSV.

        '''
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        for format_ in ('', ' '):
            resource = api.action.resource_create(
                package_id=dataset['id'],
                format=format_,
                upload=custom_helpers.get_csv_file(
                    'test-data/lahmans-baseball-database/AllstarFull.csv'))

            resource = api.action.resource_show(id=resource['id'])
            assert resource['format'] == 'CSV', resource['format']
Beispiel #4
0
    def test_resource_create_with_custom_format(self):
        '''If a format other than CSV is given when creating a resource, it
        should be accepted.

        '''
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        for format_ in ('jpeg', 'image/png', 'foobar'):
            resource = api.action.resource_create(
                package_id=dataset['id'],
                format=format_,
                upload=custom_helpers.get_csv_file(
                    'test-data/lahmans-baseball-database/AllstarFull.csv'))

            resource = api.action.resource_show(id=resource['id'])
            assert resource['format'] == format_
Beispiel #5
0
    def test_authorized_if_user_has_permissions_on_dataset(self):

        user = factories.User()

        dataset = factories.Dataset(user=user)

        resource = {
            'package_id': dataset['id'],
            'title': 'Resource',
            'url': 'http://test',
            'format': 'csv'
        }

        context = {'user': user['name'], 'model': core_model}
        response = helpers.call_auth('resource_create',
                                     context=context,
                                     **resource)
        assert_equals(response, True)
Beispiel #6
0
    def test_user_update_without_email_address(self):
        '''You have to pass an email address when you call user_update.

        Even if you don't want to change the user's email address, you still
        have to pass their current email address to user_update.

        FIXME: The point of this feature seems to be to prevent people from
        removing email addresses from user accounts, but making them post the
        current email address every time they post to user update is just
        annoying, they should be able to post a dict with no 'email' key to
        user_update and it should simply not change the current email.

        '''
        user = factories.User()
        del user['email']

        assert_raises(logic.ValidationError, helpers.call_action,
                      'user_update', **user)
Beispiel #7
0
    def test_user_update_with_empty_password(self):
        '''If an empty password is passed to user_update, nothing should
        happen.

        No error (e.g. a validation error) is raised, but the password is not
        changed either.

        '''
        user_dict = factories.User.attributes()
        original_password = user_dict['password']
        user_dict = factories.User(**user_dict)

        user_dict['password'] = ''
        helpers.call_action('user_update', **user_dict)

        import ckan.model as model
        updated_user = model.User.get(user_dict['id'])
        assert updated_user.validate_password(original_password)
Beispiel #8
0
    def test_spatial_extra_edit(self):
        app = self._get_test_app()

        user = factories.User()
        env = {'REMOTE_USER': user['name'].encode('ascii')}
        dataset = factories.Dataset(user=user)

        offset = url_for(controller='package', action='edit', id=dataset['id'])
        res = app.get(offset, extra_environ=env)

        form = res.forms[1]
        form['extras__0__key'] = u'spatial'
        form['extras__0__value'] = self.geojson_examples['point']

        res = helpers.submit_and_follow(app, form, env, 'save')

        assert 'Error' not in res, res

        res = app.get(offset, extra_environ=env)

        form = res.forms[1]
        form['extras__0__key'] = u'spatial'
        form['extras__0__value'] = self.geojson_examples['polygon']

        res = helpers.submit_and_follow(app, form, env, 'save')

        assert 'Error' not in res, res

        package_extent = Session.query(PackageExtent) \
            .filter(PackageExtent.package_id == dataset['id']).first()

        assert_equals(package_extent.package_id, dataset['id'])
        if legacy_geoalchemy:
            assert_equals(
                Session.scalar(package_extent.the_geom.geometry_type),
                'ST_Polygon')
            assert_equals(Session.scalar(package_extent.the_geom.srid),
                          self.db_srid)
        else:
            from sqlalchemy import func
            assert_equals(
                Session.query(func.ST_GeometryType(
                    package_extent.the_geom)).first()[0], 'ST_Polygon')
            assert_equals(package_extent.the_geom.srid, self.db_srid)
Beispiel #9
0
    def test_showcase_package_list_showcase_only_contains_active_datasets(
            self):
        '''
        Calling ckanext_showcase_package_list will only return active datasets
        (not deleted ones).
        '''
        sysadmin = factories.User(sysadmin=True)

        package_one = factories.Dataset()
        package_two = factories.Dataset()
        package_three = 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)
        # create third association
        helpers.call_action('ckanext_showcase_package_association_create',
                            context=context,
                            package_id=package_three['id'],
                            showcase_id=showcase_id)

        # delete the first package
        helpers.call_action('package_delete',
                            context=context,
                            id=package_one['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)

        pkg_list_ids = [pkg['id'] for pkg in pkg_list]
        nosetools.assert_true(package_two['id'] in pkg_list_ids)
        nosetools.assert_true(package_three['id'] in pkg_list_ids)
        nosetools.assert_false(package_one['id'] in pkg_list_ids)
Beispiel #10
0
    def test_resource_update_with_missing_name(self):
        '''If a user updates a resource and does not pass a name field, the
        name should be changed to the name of the resource's uploaded file.

        '''
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource = api.action.resource_create(
            package_id=dataset['id'],
            name='test-resource',
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))

        del resource['name']
        helpers.call_action('resource_update', **resource)

        resource = helpers.call_action('resource_show', id=resource['id'])
        assert resource['name'] == 'AllstarFull.csv'
    def test_spatial_extra(self):
        ''' '''
        app = self._get_test_app()

        user = factories.User()
        env = {u'REMOTE_USER': user[u'name'].encode(u'ascii')}
        dataset = factories.Dataset(user=user)

        offset = toolkit.url_for(controller=u'package',
                                 action=u'edit',
                                 id=dataset[u'id'])
        res = app.get(offset, extra_environ=env)

        form = res.forms[1]
        form[u'extras__0__key'] = u'spatial'
        form[u'extras__0__value'] = self.geojson_examples[u'point']

        res = helpers.submit_and_follow(app, form, env, u'save')

        assert u'Error' not in res, res

        package_extent = Session.query(PackageExtent) \
            .filter(PackageExtent.package_id == dataset[u'id']).first()

        geojson = json.loads(self.geojson_examples[u'point'])

        assert_equals(package_extent.package_id, dataset[u'id'])
        if legacy_geoalchemy:
            assert_equals(Session.scalar(package_extent.the_geom.x),
                          geojson[u'coordinates'][0])
            assert_equals(Session.scalar(package_extent.the_geom.y),
                          geojson[u'coordinates'][1])
            assert_equals(Session.scalar(package_extent.the_geom.srid),
                          self.db_srid)
        else:
            from sqlalchemy import func
            assert_equals(
                Session.query(func.ST_X(package_extent.the_geom)).first()[0],
                geojson[u'coordinates'][0])
            assert_equals(
                Session.query(func.ST_Y(package_extent.the_geom)).first()[0],
                geojson[u'coordinates'][1])
            assert_equals(package_extent.the_geom.srid, self.db_srid)
Beispiel #12
0
    def test_create_comment_on_issue(self):
        user = factories.User()
        dataset = factories.Dataset()

        issue = issue_factories.Issue(user=user,
                                      user_id=user['id'],
                                      dataset_id=dataset['id'])

        helpers.call_action('issue_comment_create',
                            context={'user': user['name']},
                            issue_id=issue['id'],
                            comment='some comment')

        result = helpers.call_action('issue_show',
                                     context={'user': user['name']},
                                     id=issue['id'])
        comments = result['comments']
        assert_equals(len(comments), 1)
        assert_equals(comments[0]['comment'], 'some comment')
Beispiel #13
0
 def test_reset_spam_state(self):
     owner = factories.User()
     org = factories.Organization(user=owner)
     dataset = factories.Dataset(owner_org=org['name'])
     issue = issue_factories.Issue(user_id=owner['id'],
                                   dataset_id=dataset['id'],
                                   spam_state='hidden',
                                   spam_count='20')
     context = {
         'user': owner['name'],
         'model': model,
     }
     helpers.call_action('issue_reset_spam_state',
                         context=context,
                         dataset_id=dataset['id'],
                         issue_id=issue['id'])
     result = helpers.call_action('issue_show', id=issue['id'])
     assert_equals(0, result['spam_count'])
     assert_equals('visible', result['spam_state'])
Beispiel #14
0
    def test_publisher_mark_spam(self):
        owner = factories.User()
        org = factories.Organization(user=owner)
        dataset = factories.Dataset(owner_org=org['name'])
        issue = issue_factories.Issue(user=owner,
                                      user_id=owner['id'],
                                      dataset_id=dataset['id'])

        context = {
            'user': owner['name'],
            'model': model,
        }
        helpers.call_action('issue_report_spam',
                            context=context,
                            dataset_id=dataset['id'],
                            issue_id=issue['id'])

        result = helpers.call_action('issue_show', id=issue['id'])
        assert_equals('hidden', result['spam_state'])
Beispiel #15
0
    def test_spatial_extra_bad_geojson(self):
        app = self._get_test_app()

        user = factories.User()
        env = {'REMOTE_USER': user['name'].encode('ascii')}
        dataset = factories.Dataset(user=user)

        offset = url_for(controller='package', action='edit', id=dataset['id'])
        res = app.get(offset, extra_environ=env)

        form = res.forms[1]
        form['extras__0__key'] = u'spatial'
        form['extras__0__value'] = u'{"Type":"Bad_GeoJSON","a":2}'

        res = helpers.webtest_submit(form, extra_environ=env, name='save')

        assert 'Error' in res, res
        assert 'Spatial' in res
        assert 'Error creating geometry' in res
Beispiel #16
0
    def test_does_not_return_editors(self):
        """

        By default organization_list_for_user() should not return organizations
        that the user is just an editor (not an admin) of.

        """
        user = factories.User()
        context = {'user': user['name']}
        organization = factories.Organization()

        helpers.call_action('member_create', id=organization['id'],
                            object=user['id'], object_type='user',
                            capacity='editor')

        organizations = helpers.call_action('organization_list_for_user',
                                            context=context)

        assert organizations == []
Beispiel #17
0
    def test_dataset_edit_org_dropdown_not_visible_to_normal_user_with_no_orgs_available(
            self):
        '''
        The 'Organization' dropdown is not available on the dataset
        create/edit page to normal (non-sysadmin) users who have no
        organizations available to them.
        '''
        user = factories.User()
        # user isn't admin of org.
        org = factories.Organization(name="my-org")

        app = self._get_test_app()
        env = {'REMOTE_USER': user['name'].encode('ascii')}
        response = app.get(
            url=url_for(controller='package', action='new'),
            extra_environ=env,
        )

        # organization dropdown not in create page.
        form = response.forms['dataset-edit']
        assert 'owner_org' not in form.fields

        # create dataset
        form['name'] = u'my-dataset'
        response = submit_and_follow(app, form, env, 'save')

        # add a resource to make the pkg active
        resource_form = response.forms['resource-edit']
        resource_form['url'] = u'http://example.com/resource'
        submit_and_follow(app, resource_form, env, 'save', 'go-metadata')
        pkg = model.Package.by_name(u'my-dataset')
        assert_equal(pkg.state, 'active')

        # edit package response
        url = url_for(controller='package',
                      action='edit',
                      id=model.Package.by_name(u'my-dataset').id)
        pkg_edit_response = app.get(url=url, extra_environ=env)
        # A field with the correct id is in the response
        form = pkg_edit_response.forms['dataset-edit']
        assert 'owner_org' not in form.fields
        # The organization id is in the response in a value attribute
        assert 'value="{0}"'.format(org['id']) not in pkg_edit_response
Beispiel #18
0
    def test_user_show_include_datasets_includes_draft_myself(self):
        # a user viewing his own user should see the draft and private datasets

        user = factories.User()
        org = factories.Organization(user=user)
        factories.Dataset(user=user)
        dataset_deleted = factories.Dataset(user=user, state='deleted')
        factories.Dataset(user=user, state='draft')
        factories.Dataset(user=user, private=True, owner_org=org['name'])

        got_user = helpers.call_action('user_show',
                                       context={'user': user['name']},
                                       include_datasets=True,
                                       id=user['id'])

        assert len(got_user['datasets']) == 3
        datasets_got = set([user_['name'] for user_ in got_user['datasets']])
        assert dataset_deleted['name'] not in datasets_got
        assert got_user['number_created_packages'] == 3
Beispiel #19
0
 def setup(self):
     super(TestDatasetList, self).setup()
     self.owner = factories.User()
     self.org = factories.Organization(user=self.owner)
     self.dataset = factories.Dataset(user=self.owner,
                                      owner_org=self.org['name'],
                                      name='test-dataset',
                                      )
     self.dataset_1 = factories.Dataset(user=self.owner,
                                      extras=[{
                                          'key': 'issues_enabled',
                                          'value': True
                                          }],
                                      owner_org=self.org['name'])
     self.issue = issue_factories.Issue(user=self.owner,
                                        user_id=self.owner['id'],
                                        dataset_id=self.dataset['id'],
                                        spam_state='hidden')
     self.app = self._get_test_app()
Beispiel #20
0
    def test_package_patch_updating_single_field(self):
        user = factories.User()
        dataset = factories.Dataset(
            name='annakarenina',
            notes='some test now',
            user=user)

        dataset = helpers.call_action(
            'package_patch',
            id=dataset['id'],
            name='somethingnew')

        assert_equals(dataset['name'], 'somethingnew')
        assert_equals(dataset['notes'], 'some test now')

        dataset2 = helpers.call_action('package_show', id=dataset['id'])

        assert_equals(dataset2['name'], 'somethingnew')
        assert_equals(dataset2['notes'], 'some test now')
Beispiel #21
0
    def test_update_organization_cant_change_type(self):
        user = factories.User()
        context = {'user': user['name']}
        org = factories.Organization(
            type='organization',
            name='unchanging',
            user=user)

        org = helpers.call_action(
            'organization_update',
            context=context,
            id=org['id'],
            name='unchanging',
            type='ragtagband')

        assert_equals(org['type'], 'organization')
        assert_equals(helpers.call_action(
            'organization_show', id='unchanging')['type'],
            'organization')
Beispiel #22
0
    def test_user_update_password(self):
        '''Test that updating a user's password works successfully.'''

        user = factories.User()

        # FIXME we have to pass the email address to user_update even though
        # we're not updating it, otherwise validation fails.
        helpers.call_action(
            'user_update',
            id=user['name'],
            email=user['email'],
            password='******',
        )

        # user_show() never returns the user's password, so we have to access
        # the model directly to test it.
        import ckan.model as model
        updated_user = model.User.get(user['id'])
        assert updated_user.validate_password('new password')
    def test_showcase_admin_remove_retains_user(self):
        '''
        Deleting a ShowcaseAdmin object doesn't delete the corresponding user.
        '''

        user = factories.User()

        helpers.call_action('ckanext_showcase_admin_add', context={},
                            username=user['name'])

        # We have a user
        user_obj = model.Session.query(model.User).get(user['id'])
        nosetools.assert_true(user_obj is not None)

        helpers.call_action('ckanext_showcase_admin_remove', context={},
                            username=user['name'])

        # We still have a user
        user_obj = model.Session.query(model.User).get(user['id'])
        nosetools.assert_true(user_obj is not None)
Beispiel #24
0
    def test_user_show_default_values(self):

        user = factories.User()

        got_user = helpers.call_action('user_show', id=user['id'])

        assert got_user['id'] == user['id']
        assert got_user['name'] == user['name']
        assert got_user['fullname'] == user['fullname']
        assert got_user['display_name'] == user['display_name']
        assert got_user['created'] == user['created']
        assert got_user['about'] == user['about']
        assert got_user['sysadmin'] == user['sysadmin']
        assert got_user['number_of_edits'] == 0
        assert got_user['number_created_packages'] == 0
        assert 'password' not in got_user
        assert 'reset_key' not in got_user
        assert 'apikey' not in got_user
        assert 'email' not in got_user
        assert 'datasets' not in got_user
Beispiel #25
0
    def test_group_show_packages_returned(self):

        user_name = helpers.call_action('get_site_user')['name']

        group = factories.Group(user=factories.User())

        datasets = [
            {'name': 'dataset_1', 'groups': [{'name': group['name']}]},
            {'name': 'dataset_2', 'groups': [{'name': group['name']}]},
        ]

        for dataset in datasets:
            helpers.call_action('package_create',
                                context={'user': user_name},
                                **dataset)

        group_dict = helpers.call_action('group_show', id=group['id'])

        assert len(group_dict['packages']) == 2
        assert group_dict['package_count'] == 2
Beispiel #26
0
    def test_group_create_with_no_curators_group(self):
        '''Test that group_create returns a 404 when there's no curators group.

        With this version of the plugin group_create returns a spurious 404
        when a user _is_ logged-in but the site has no curators group.

        '''
        assert 'curators' not in tests.call_action_api(self.app, 'group_list')

        user = factories.User()

        response = tests.call_action_api(self.app,
                                         'group_create',
                                         name='test_group',
                                         apikey=user['apikey'],
                                         status=404)
        assert response == {
            '__type': 'Not Found Error',
            'message': 'Not found'
        }
Beispiel #27
0
    def test_group_patch_updating_single_field(self):
        user = factories.User()
        group = factories.Group(
            name='economy',
            description='some test now',
            user=user)

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

        assert_equals(group['name'], 'economy')
        assert_equals(group['description'], 'somethingnew')

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

        assert_equals(group2['name'], 'economy')
        assert_equals(group2['description'], 'somethingnew')
Beispiel #28
0
    def test_member_permission(self):
        """

        organization_list_for_user() should return organizations that the user
        is a member of if passed a permission that belongs to the member role.

        """
        user = factories.User()
        context = {'user': user['name']}
        organization = factories.Organization()

        helpers.call_action('member_create', id=organization['id'],
                            object=user['id'], object_type='user',
                            capacity='member')

        organizations = helpers.call_action('organization_list_for_user',
                                            permission='read',
                                            context=context)

        assert [org['id'] for org in organizations] == [organization['id']]
Beispiel #29
0
    def test_authorized_if_user_has_permissions_on_dataset(self):

        user = factories.User()

        dataset = factories.Dataset(user=user)

        resource = factories.Resource(user=user, 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['name'], 'model': core_model}
        response = helpers.call_auth('resource_view_create',
                                     context=context,
                                     **resource_view)
        assert_equals(response, True)
Beispiel #30
0
    def test_org_editor_can_update_an_issue(self):
        org_editor = factories.User()
        org = factories.Organization()
        helpers.call_action('member_create',
                            object=org_editor['name'],
                            id=org['id'],
                            object_type='user',
                            capacity='editor')
        dataset = factories.Dataset(owner_org=org['name'], private=True)
        user = helpers.call_action('get_site_user')
        issue = issue_factories.Issue(user=user, dataset_id=dataset['id'])

        context = {
            'user': org_editor['name'],
            'model': model,
        }
        assert_true(
            helpers.call_auth('issue_update',
                              context,
                              dataset_id=dataset['id']))