Esempio n. 1
0
    def test_group_index(self):
        res = self.app.get(url_for(controller="group", action="index"))
        assert 'href="/group?page=2"' in res, res
        grp_numbers = scrape_search_results(res, "group")
        assert_equal(
            [
                "00",
                "01",
                "02",
                "03",
                "04",
                "05",
                "06",
                "07",
                "08",
                "09",
                "10",
                "11",
                "12",
                "13",
                "14",
                "15",
                "16",
                "17",
                "18",
                "19",
                "20",
            ],
            grp_numbers,
        )

        res = self.app.get(url_for(controller="group", action="index", page=2))
        assert 'href="/group?page=1"' in res
        grp_numbers = scrape_search_results(res, "group")
        assert_equal(["21"], grp_numbers)
Esempio n. 2
0
    def test_users_index(self):
        # allow for 2 extra users shown on user listing, 'logged_in' and 'visitor'
        res = self.app.get(url_for(controller="user", action="index"))
        assert 'href="/user?q=&order_by=name&page=2"' in res
        user_numbers = scrape_search_results(res, "user")
        assert_equal(
            [
                "00",
                "01",
                "02",
                "03",
                "04",
                "05",
                "06",
                "07",
                "08",
                "09",
                "10",
                "11",
                "12",
                "13",
                "14",
                "15",
                "16",
                "17",
                "18",
                "19",
            ],
            user_numbers,
        )

        res = self.app.get(url_for(controller="user", action="index", page=2))
        assert 'href="/user?q=&order_by=name&page=1"' in res
        user_numbers = scrape_search_results(res, "user")
        assert_equal(["20"], user_numbers)
Esempio n. 3
0
    def test_auth_form(self):
        url = url_for('storage_api_auth_form', label='abc')
        res = self.app.get(url, extra_environ=self.extra_environ, status=200)
        assert res.json['action'] == u'/storage/upload_handle', res.json
        assert res.json['fields'][-1]['value'] == 'abc', res

        url = url_for('storage_api_auth_form', label='abc/xxx')
        res = self.app.get(url, extra_environ=self.extra_environ, status=200)
        assert res.json['fields'][-1]['value'] == 'abc/xxx'
Esempio n. 4
0
    def test_users_index(self):
        res = self.app.get(url_for(controller='user', action='index'))
        assert 'href="/user?q=&order_by=name&page=2"' in res
        user_numbers = scrape_search_results(res, 'user')
        assert_equal(['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'], user_numbers)

        res = self.app.get(url_for(controller='user', action='index', page=2))
        assert 'href="/user?q=&order_by=name&page=1"' in res
        user_numbers = scrape_search_results(res, 'user')
        assert_equal(['20'], user_numbers)
Esempio n. 5
0
    def test_group_index(self):
        res = self.app.get(url_for(controller='group', action='index'))
        assert 'href="/group?q=&sort=&page=2"' in res, res
        grp_numbers = scrape_search_results(res, 'group')
        assert_equal(['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20'], grp_numbers)

        res = self.app.get(url_for(controller='group', action='index', page=2))
        assert 'href="/group?q=&sort=&page=1"' in res
        grp_numbers = scrape_search_results(res, 'group')
        assert_equal(['21'], grp_numbers)
Esempio n. 6
0
    def test_metadata(self):
        url = url_for('storage_api_get_metadata', label='abc')
        res = self.app.get(url, status=404)

        # TODO: test get metadata on real setup ...
        label = 'abc'
        url = url_for('storage_api_set_metadata',
            extra_environ=self.extra_environ,
            label=label,
            data=dict(
                label=label
                )
            )
Esempio n. 7
0
    def test_auth_form(self):
        url = url_for('storage_api_auth_form', label='abc')
        res = self.app.get(url, extra_environ=self.extra_environ, status=200)
        assert res.json['fields'][-1]['value'] == 'abc', res

        url = url_for('storage_api_auth_form', label='abc/xxx')
        res = self.app.get(url, extra_environ=self.extra_environ, status=200)
        assert res.json['fields'][-1]['value'] == 'abc/xxx'

        url = url_for('storage_api_auth_form', label='abc',
                success_action_redirect='abc')
        res = self.app.get(url, extra_environ=self.extra_environ, status=200)
        fields = dict([ (x['name'], x['value']) for x in res.json['fields'] ])
        assert fields['success_action_redirect'] == u'http://localhost/storage/upload/success_empty?label=abc'
Esempio n. 8
0
    def test_contact(self):

        data = copy.deepcopy(TEST_DATADICT)
        data['private'] = False
        data['contact'][0]['email'] = '*****@*****.**'

        model.User(name="test_sysadmin", sysadmin=True).save()

        organisation = get_action('organization_create')({'user': '******'}, {'name': 'test-organization',
                                                                                     'title': "Test organization"})
        data['owner_org'] = organisation.get('name')
        package = get_action('package_create')({'user': '******'}, data)
        name = package['name']
        id = package['id']
        package_contact_id = utils.get_package_contacts(id)[0].get('id')

        send_contact_offset = url_for("/contact/send/{0}".format(id))
        res = self.app.post(send_contact_offset, params={'recipient': package_contact_id})
        assert res.status == 302

        dataset_offset = url_for("/dataset/{0}".format(name))
        res = self.app.post(dataset_offset)
        assert 'Message not sent' in res

        import base64
        import time

        cc = ContactController()
        _time = base64.b64encode(cc.crypto.encrypt(cc._pad(str(int(time.time())))))

        params = {
            'recipient': package_contact_id,
            'accept_logging': 'True'
        }

        self.app.post(send_contact_offset, params=params, status=302)
        res = self.app.post(dataset_offset)
        assert 'spam bot' in res

        _time = base64.b64encode(cc.crypto.encrypt(cc._pad(str(int(time.time())-21))))
        params = {
            'recipient': package_contact_id,
            'accept_logging': 'True'
        }
        self.app.post(send_contact_offset, params=params, status=302)
        offset = url_for("/dataset/{0}".format(name))
        res = self.app.post(offset)

        assert 'Message not sent' in res
Esempio n. 9
0
    def test_list_format_atom(self):
        self.create_40_revisions()
        self.create_updating_revision(u'warandpeace',
            title=u"My Updated 'War and Peace' Title",
        )
        self.create_updating_revision(u'annakarenina',
            title=u"My Updated 'Annakarenina' Title",
            resources=[{
                'url': u'http://datahub.io/download3',
                'format': u'zip file',
                'description': u'Full text. Needs escaping: " Umlaut: \xfc',
                'hash': u'def456',
            }],
        )
        self.create_updating_revision(u'warandpeace',
            title=u"My Doubly Updated 'War and Peace' Title",
            extras={
                'date_updated': u'2010',
            }
        )
        self.create_deleting_revision(u'annakarenina')
        revisions = model.repo.history().all()
        revision1 = revisions[0]
        # Revisions are most recent first, with first rev on last page.
        # Todo: Look at the model to see which revision is last.
        # Todo: Test for last revision on first page.
        # Todo: Test for first revision on last page.
        # Todo: Test for last revision minus 50 on second page.
        # Page 1.   (Implied id=1)
        offset = url_for(controller='revision', action='list', format='atom')
        res = self.app.get(offset)
        assert '<feed' in res, res
        assert 'xmlns="http://www.w3.org/2005/Atom"' in res, res
        assert '</feed>' in res, res
        # Todo: Better test for 'days' request param.
        #  - fake some older revisions and check they aren't included.
        offset = url_for(controller='revision', action='list', format='atom',
                days=30)
        res = self.app.get(offset)
        assert '<feed' in res, res
        assert 'xmlns="http://www.w3.org/2005/Atom"' in res, res
        assert '</feed>' in res, res

        # Tests for indications about what happened.
        assert 'warandpeace:created' in res, res
        assert 'annakarenina:created' in res, res
        assert 'warandpeace:updated:date_updated' in res, res
        assert 'annakarenina:updated:resources' in res, res
        assert 'annakarenina:deleted' in res, res
Esempio n. 10
0
 def test_group_datasets_read_p1(self):
     res = self.app.get(url_for(controller="group", action="read", id="group_00"))
     assert 'href="/group/group_00?page=2' in res, res
     pkg_numbers = scrape_search_results(res, "group_dataset")
     assert_equal(
         [
             "50",
             "49",
             "48",
             "47",
             "46",
             "45",
             "44",
             "43",
             "42",
             "41",
             "40",
             "39",
             "38",
             "37",
             "36",
             "35",
             "34",
             "33",
             "32",
             "31",
         ],
         pkg_numbers,
     )
Esempio n. 11
0
 def test_group_datasets_read_p2(self):
     res = self.app.get(url_for(controller="group", action="read", id="group_00", page=2))
     assert 'href="/group/group_00?page=1' in res, res
     pkg_numbers = scrape_search_results(res, "group_dataset")
     assert_equal(
         [
             "30",
             "29",
             "28",
             "27",
             "26",
             "25",
             "24",
             "23",
             "22",
             "21",
             "20",
             "19",
             "18",
             "17",
             "16",
             "15",
             "14",
             "13",
             "12",
             "11",
         ],
         pkg_numbers,
     )
Esempio n. 12
0
 def test_munge_tag(self):
     response = self.app.get(
         url=url_for(controller='api', action='munge_tag', ver=2),
         params={'name': 'Test subject'},
         status=200,
     )
     assert_equal(response.body, '"test-subject"')
Esempio n. 13
0
 def test_package_search_p1(self):
     res = self.app.get(url_for(controller="package", action="search", q="groups:group_00"))
     assert 'href="/dataset?q=groups%3Agroup_00&amp;page=2"' in res
     pkg_numbers = scrape_search_results(res, "dataset")
     assert_equal(
         [
             "50",
             "49",
             "48",
             "47",
             "46",
             "45",
             "44",
             "43",
             "42",
             "41",
             "40",
             "39",
             "38",
             "37",
             "36",
             "35",
             "34",
             "33",
             "32",
             "31",
         ],
         pkg_numbers,
     )
Esempio n. 14
0
 def test_package_search_p2(self):
     res = self.app.get(url_for(controller="package", action="search", q="groups:group_00", page=2))
     assert 'href="/dataset?q=groups%3Agroup_00&amp;page=1"' in res
     pkg_numbers = scrape_search_results(res, "dataset")
     assert_equal(
         [
             "30",
             "29",
             "28",
             "27",
             "26",
             "25",
             "24",
             "23",
             "22",
             "21",
             "20",
             "19",
             "18",
             "17",
             "16",
             "15",
             "14",
             "13",
             "12",
             "11",
         ],
         pkg_numbers,
     )
Esempio n. 15
0
 def test_read(self):
     anna = model.Package.by_name(u"annakarenina")
     rev_id = anna.revision.id
     offset = url_for(controller="revision", action="read", id="%s" % rev_id)
     res = self.app.get(offset)
     assert "Revision %s" % rev_id in res
     assert "Revision: %s" % rev_id in res
Esempio n. 16
0
 def test_read(self):
     anna = model.Package.by_name(u'annakarenina')
     rev_id = anna.revision.id
     offset = url_for(controller='revision', action='read', id='%s' % rev_id)
     res = self.app.get(offset)
     assert 'Revision %s' % rev_id in res
     assert 'Revision: %s' % rev_id in res
Esempio n. 17
0
 def test_munge_title_to_package_name(self):
     response = self.app.get(
         url=url_for(controller='api', action='munge_title_to_package_name', ver=2),
         params={'name': 'Test title'},
         status=200,
     )
     assert_equal(response.body, '"test-title"')
Esempio n. 18
0
 def test_contact_controller_no_user(self):
     """
     Test that we get a redirect when there is no user
     """
     offset = url_for("/contact/warandpeace")
     res = self.app.get(offset)
     assert res.status == 200, 'Everyone is allowed to contact'
Esempio n. 19
0
    def test_delete_authorized_own(self):
        """
            A non-admin should be able to delete its own created dataset.
        """

        org = copy.deepcopy(TEST_ORGANIZATION_COMMON)
        org['name'] = 'testdeleteauthorizedown'
        organization = get_action('organization_create')({'user': '******'}, org)

        data = copy.deepcopy(TEST_DATADICT)
        data['owner_org'] = organization['name']
        data['name'] = 'test-delete-own-dataset'

        package = get_action('package_create')({'user': '******'}, data)

        get_action('package_delete')({'user': '******'}, {'id': package['id']})

        offset = url_for(controller='package', action='read', id=package['id'])
        res = self.app.get(offset)

        assert res.status == 302, 'The user should be redirected to login page, since the package is deleted'
        res = res.follow()

        assert 'Unauthorized to read package' in res, \
                "The package should be deleted and not shown to an anonymous user."
Esempio n. 20
0
    def test_delete_authorized_external(self):
        """
            An editor should be able to delete a package from an external organization
        """

        # tester - organization editor (has a right to delete packages)
        org = copy.deepcopy(TEST_ORGANIZATION_COMMON)
        org['name'] = 'testdeleteauthorizedexternal'
        organization = get_action('organization_create')({'user': '******'}, org)

        # create a dataset under that organization
        data = copy.deepcopy(TEST_DATADICT)
        data['owner_org'] = organization['name']
        data['name'] = 'test-authorized-external-deletion'

        package = get_action('package_create')({'user': '******'}, data)

        # delete the package as 'tester'
        get_action('package_delete')({'user': '******'}, {'id': package['id']})

        # assert, that the package is not found
        offset = url_for(controller='package', action='read', id=package['id'])
        res = self.app.get(offset)

        assert res.status == 302, 'The user should be redirected to login page, since the package is deleted'
        res = res.follow()

        assert 'Unauthorized to read package' in res, \
                "The package should be deleted and not shown to an anonymous user."
Esempio n. 21
0
    def test_resource_read_redirect(self):
        """
        resource_read should redirect to dataset page.
        """
        res_id = None

        pkg = model.Package.get(u'annakarenina')
        for resource in pkg.resources:
            if 'Full text.' in resource.description:
                revision = model.repo.new_revision()
                resource.resource_type = settings.RESOURCE_TYPE_DATASET
                model.Session.commit()
                res_id = resource.id

        offset = '/en' + url_for(controller='package', action='resource_read',
                                 id=u'annakarenina', resource_id=res_id)

        extra_environ = {'REMOTE_USER': '******'}
        result = self.app.get(offset, extra_environ=extra_environ)

        # Redirect should redirect to dataset page
        result = result.follow()

        assert result.body.count('Full text.') == 0
        assert len(etree.fromstring(result.body, parser=self.html_parser))
Esempio n. 22
0
 def test_markdown(self):
     markdown = '''##Title'''
     response = self.app.get(
         url=url_for(controller='api', action='markdown', ver=2),
         params={'q': markdown},
         status=200,
     )
     assert_equal(response.body, '"<h2>Title</h2>"')
Esempio n. 23
0
    def test_authz(self):
        url = url_for('storage_api_auth_form', label='abc')

        # Non logged in users can not upload
        res = self.app.get(url, status=[302,401])

        # Logged in users can upload
        res = self.app.get(url, status=[200], extra_environ={'REMOTE_USER':'******'})
Esempio n. 24
0
    def test_list_format_atom(self):
        self.create_40_revisions()
        self.create_updating_revision(u"warandpeace", title=u"My Updated 'War and Peace' Title")
        self.create_updating_revision(
            u"annakarenina",
            title=u"My Updated 'Annakarenina' Title",
            resources=[
                {
                    "url": u"http://datahub.io/download3",
                    "format": u"zip file",
                    "description": u'Full text. Needs escaping: " Umlaut: \xfc',
                    "hash": u"def456",
                }
            ],
        )
        self.create_updating_revision(
            u"warandpeace", title=u"My Doubly Updated 'War and Peace' Title", extras={"date_updated": u"2010"}
        )
        self.create_deleting_revision(u"annakarenina")
        revisions = model.repo.history().all()
        revision1 = revisions[0]
        # Revisions are most recent first, with first rev on last page.
        # Todo: Look at the model to see which revision is last.
        # Todo: Test for last revision on first page.
        # Todo: Test for first revision on last page.
        # Todo: Test for last revision minus 50 on second page.
        # Page 1.   (Implied id=1)
        offset = url_for(controller="revision", action="list", format="atom")
        res = self.app.get(offset)
        assert "<feed" in res, res
        assert 'xmlns="http://www.w3.org/2005/Atom"' in res, res
        assert "</feed>" in res, res
        # Todo: Better test for 'days' request param.
        #  - fake some older revisions and check they aren't included.
        offset = url_for(controller="revision", action="list", format="atom", days=30)
        res = self.app.get(offset)
        assert "<feed" in res, res
        assert 'xmlns="http://www.w3.org/2005/Atom"' in res, res
        assert "</feed>" in res, res

        # Tests for indications about what happened.
        assert "warandpeace:created" in res, res
        assert "annakarenina:created" in res, res
        assert "warandpeace:updated:date_updated" in res, res
        assert "annakarenina:updated:resources" in res, res
        assert "annakarenina:deleted" in res, res
Esempio n. 25
0
 def test_group_page(self):
     """
     Test that help page is found and rendered.
     """
     offset = url_for('/group')
     res = self.app.get(offset)
     assert res.status == 200, 'Wrong HTTP status code (expecting 200)'
     assert len(etree.fromstring(res.body, parser=self.html_parser))
Esempio n. 26
0
    def test_edit_not_available(self):
        '''
        Test that edit page is not available for random user
        '''
        offset = url_for("/dataset/edit/annakarenina")

        extra_environ = {'REMOTE_USER': '******'}
        res = self.app.get(offset, extra_environ=extra_environ, status=401)
Esempio n. 27
0
 def test_edit_not_available(self):
     '''
     Test that edit page is not available for random user
     '''
     TestAuthorisation.TEST_DATADICT['pids'][0]['id'] = 'primary_pid_1'
     package = TestAuthorisation.api_test_user.action.package_create(**TestAuthorisation.TEST_DATADICT)
     offset = url_for("/dataset/edit/{0}".format(package['name']))
     extra_environ = {'REMOTE_USER': '******'}
     res = self.app.get(offset, extra_environ=extra_environ, status=401)
     TestAuthorisation.api_test_user.call_action('package_delete', data_dict={'id': package['id']})
Esempio n. 28
0
 def test_autocomplete_multiple(self):
     response = self.app.get(
         url=url_for(controller='api', action='user_autocomplete', ver=2),
         params={
            'q': u'tes',
         },
         status=200,
     )
     print response.json
     assert_equal(len(response.json), 2)
Esempio n. 29
0
 def test_index(self):
     url = url_for('admin.index')
     response = self.app.get(url, status=[403])
     # random username
     response = self.app.get(url, status=[403],
             extra_environ={'REMOTE_USER': '******'})
     # now test real access
     username = u'testsysadmin'.encode('utf8')
     response = self.app.get(url,
             extra_environ={'REMOTE_USER': username})
     assert 'Administration' in response, response
Esempio n. 30
0
 def test_autocomplete_limit(self):
     response = self.app.get(
         url=url_for(controller='api', action='user_autocomplete', ver=2),
         params={
             'q': u'tes',
             'limit': 1
         },
         status=200,
     )
     print response.json
     assert_equal(len(response.json), 1)
Esempio n. 31
0
    def test_group_datasets_read_p1(self):

        with self.app.flask_app.test_request_context():
            url = url_for(controller='group', action='read', id='group_00')

        res = self.app.get(url)
        assert 'href="/group/group_00?page=2' in res, res
        pkg_numbers = scrape_search_results(res, 'group_dataset')
        assert_equal([
            '50', '49', '48', '47', '46', '45', '44', '43', '42', '41', '40',
            '39', '38', '37', '36', '35', '34', '33', '32', '31'
        ], pkg_numbers)
Esempio n. 32
0
 def test_edit_not_available(self):
     '''
     Test that edit page is not available for random user
     '''
     TestAuthorisation.TEST_DATADICT['pids'][0]['id'] = 'primary_pid_1'
     package = TestAuthorisation.api_test_user.action.package_create(
         **TestAuthorisation.TEST_DATADICT)
     offset = url_for("/dataset/edit/{0}".format(package['name']))
     extra_environ = {'REMOTE_USER': '******'}
     res = self.app.get(offset, extra_environ=extra_environ, status=401)
     TestAuthorisation.api_test_user.call_action(
         'package_delete', data_dict={'id': package['id']})
Esempio n. 33
0
 def test_new_plugin_hook(self, env_user, app):
     plugin = plugins.get_plugin("test_package_controller_plugin")
     offset = url_for("dataset.new")
     res = app.get(offset, extra_environ=env_user)
     new_name = u"plugged"
     fv = res.forms["dataset-edit"]
     prefix = ""
     fv[prefix + "name"] = new_name
     res = fv.submit("save", extra_environ=env_user)
     # get redirected ...
     assert plugin.calls["edit"] == 0, plugin.calls
     assert plugin.calls["create"] == 1, plugin.calls
Esempio n. 34
0
def test_index(app):
    CreateTestData.create()
    url = url_for("admin.index")
    response = app.get(url, status=403)
    # random username
    response = app.get(url,
                       status=403,
                       extra_environ={"REMOTE_USER": "******"})
    # now test real access
    username = u"testsysadmin".encode("utf8")
    response = app.get(url, extra_environ={"REMOTE_USER": username})
    assert "Administration" in response, response
Esempio n. 35
0
    def test_markdown(self):
        markdown = '''##Title'''

        with self.app.flask_app.test_request_context():
            url = url_for(controller='api', action='markdown', ver=2)

        response = self.app.get(
            url,
            params={'q': markdown},
            status=200,
        )
        assert_equal(response.body, '"<h2>Title</h2>"')
Esempio n. 36
0
 def test_autocomplete(self):
     response = self.app.get(
         url=url_for(controller='api', action='user_autocomplete', ver=2),
         params={
            'q': u'sysadmin',
         },
         status=200,
     )
     print response.json
     assert set(response.json[0].keys()) == set(['id', 'name', 'fullname'])
     assert_equal(response.json[0]['name'], u'testsysadmin')
     assert_equal(response.header('Content-Type'), 'application/json;charset=utf-8')
Esempio n. 37
0
 def test_package_search_p2(self):
     res = self.app.get(
         url_for(controller='package',
                 action='search',
                 q='groups:group_00',
                 page=2))
     assert 'href="/dataset?q=groups%3Agroup_00&amp;page=1"' in res
     pkg_numbers = scrape_search_results(res, 'dataset')
     assert_equal([
         '30', '29', '28', '27', '26', '25', '24', '23', '22', '21', '20',
         '19', '18', '17', '16', '15', '14', '13', '12', '11'
     ], pkg_numbers)
Esempio n. 38
0
 def test_autocomplete(self):
     response = self.app.get(
         url=url_for(controller='api', action='user_autocomplete', ver=2),
         params={
            'q': u'sysadmin',
         },
         status=200,
     )
     print response.json
     assert set(response.json[0].keys()) == set(['id', 'name', 'fullname'])
     assert_equal(response.json[0]['name'], u'testsysadmin')
     assert_equal(response.header('Content-Type'), 'application/json;charset=utf-8')
Esempio n. 39
0
 def test_index(self):
     url = url_for('ckanadmin', action='index')
     response = self.app.get(url, status=[403])
     # random username
     response = self.app.get(
         url,
         status=[403],
         extra_environ={'REMOTE_USER': '******'})
     # now test real access
     username = u'testsysadmin'.encode('utf8')
     response = self.app.get(url, extra_environ={'REMOTE_USER': username})
     assert 'Administration' in response, response
Esempio n. 40
0
    def test_munge_title_to_package_name(self):

        with self.app.flask_app.test_request_context():
            url = url_for(controller='api',
                          action='munge_title_to_package_name',
                          ver=2)

        response = self.app.get(
            url,
            params={'name': 'Test title'},
            status=200,
        )
        assert_equal(response.body, '"test-title"')
Esempio n. 41
0
 def test_new_plugin_hook(self, env_user, app):
     plugin = plugins.get_plugin("test_package_controller_plugin")
     offset = url_for("dataset.new")
     new_name = u"plugged"
     res = app.post(offset,
                    extra_environ=env_user,
                    data={
                        "name": new_name,
                        "save": ""
                    },
                    follow_redirects=False)
     assert plugin.calls["edit"] == 0, plugin.calls
     assert plugin.calls["create"] == 1, plugin.calls
Esempio n. 42
0
    def test_delete(self, app, users):
        plugin = plugins.get_plugin("test_package_controller_plugin")

        offset = url_for("dataset.delete", id="warandpeace")
        # Since organizations, any owned dataset can be edited/deleted by any
        # user
        app.post(offset, extra_environ=users["tester"])

        app.post(offset, extra_environ=users["admin"])

        assert model.Package.get("warandpeace").state == u"deleted"

        assert plugin.calls["delete"] == 2
        assert plugin.calls["after_delete"] == 2
Esempio n. 43
0
    def test_package_search_p1(self):

        with self.app.flask_app.test_request_context():
            url = url_for(controller='package',
                          action='search',
                          q='groups:group_00')

        res = self.app.get(url)
        assert 'href="/dataset?q=groups%3Agroup_00&amp;page=2"' in res
        pkg_numbers = scrape_search_results(res, 'dataset')
        assert_equal([
            '50', '49', '48', '47', '46', '45', '44', '43', '42', '41', '40',
            '39', '38', '37', '36', '35', '34', '33', '32', '31'
        ], pkg_numbers)
Esempio n. 44
0
 def test_tag_autocomplete(self):
     url = url_for(controller='api', action='tag_autocomplete', ver=2)
     assert_equal(url, '/api/2/util/tag/autocomplete')
     response = self.app.get(
         url=url,
         params={
             'incomplete': u'ru',
         },
         status=200,
     )
     assert_equal(response.body,
                  '{"ResultSet": {"Result": [{"Name": "russian"}]}}')
     assert_equal(response.header('Content-Type'),
                  'application/json;charset=utf-8')
Esempio n. 45
0
    def test_autocomplete_multiple(self):

        with self.app.flask_app.test_request_context():
            url = url_for(controller='api', action='user_autocomplete', ver=2)

        response = self.app.get(
            url,
            params={
               'q': u'tes',
            },
            status=200,
        )
        print response.json
        assert_equal(len(response.json), 2)
Esempio n. 46
0
    def test_package_slug_invalid(self):
        response = self.app.get(
            url=url_for(controller='api', action='is_slug_valid', ver=2),
            params={
                'type': u'package',
                'slug': u'edit',
            },
            status=200,
        )
        assert_equal(response.body, '{"valid": false}')
        assert_equal(response.header('Content-Type'),
                     'application/json;charset=utf-8')

        response = self.app.get(
            url=url_for(controller='api', action='is_slug_valid', ver=2),
            params={
                'type': u'package',
                'slug': u'new',
            },
            status=200,
        )
        assert_equal(response.body, '{"valid": false}')
        assert_equal(response.header('Content-Type'),
                     'application/json;charset=utf-8')
Esempio n. 47
0
    def test_package_slug_valid(self):
        response = self.app.get(
            url=url_for(controller='api', action='is_slug_valid', ver=2),
            params={
                'type': u'package',
                'slug': u'A New Title * With & Funny CHARacters',
            },
            status=200,
        )
        assert_equal(response.body, '{"valid": true}')
        assert_equal(response.header('Content-Type'),
                     'application/json;charset=utf-8')

        response = self.app.get(
            url=url_for(controller='api', action='is_slug_valid', ver=2),
            params={
                'type': u'package',
                'slug': u'warandpeace',
            },
            status=200,
        )
        assert_equal(response.body, '{"valid": false}')
        assert_equal(response.header('Content-Type'),
                     'application/json;charset=utf-8')
Esempio n. 48
0
    def test_group_datasets_read_p2(self):

        with self.app.flask_app.test_request_context():
            url = url_for(controller='group',
                          action='read',
                          id='group_00',
                          page=2)

        res = self.app.get(url)
        assert 'href="/group/group_00?page=1' in res, res
        pkg_numbers = scrape_search_results(res, 'group_dataset')
        assert_equal([
            '30', '29', '28', '27', '26', '25', '24', '23', '22', '21', '20',
            '19', '18', '17', '16', '15', '14', '13', '12', '11'
        ], pkg_numbers)
Esempio n. 49
0
 def test_group_autocomplete(self):
     url = url_for(controller='api', action='group_autocomplete', ver=2)
     assert_equal(url, '/api/2/util/group/autocomplete')
     response = self.app.get(
         url=url,
         params={
             'q': u'dave',
         },
         status=200,
     )
     results = json.loads(response.body)
     assert_equal(len(results), 1)
     assert_equal(results[0]['name'], 'david')
     assert_equal(results[0]['title'], 'Dave\'s books')
     assert_equal(response.header('Content-Type'),
                  'application/json;charset=utf-8')
Esempio n. 50
0
 def test_dataset_autocomplete_match_title(self):
     url = url_for(controller='api', action='dataset_autocomplete', ver=2)
     assert_equal(url, '/api/2/util/dataset/autocomplete')
     response = self.app.get(
         url=url,
         params={
             'incomplete': u'a n',
         },
         status=200,
     )
     assert_equal(
         response.body,
         '{"ResultSet": {"Result": [{"match_field": "title", "match_displayed": "A Novel By Tolstoy (annakarenina)", "name": "annakarenina", "title": "A Novel By Tolstoy"}]}}'
     )
     assert_equal(response.header('Content-Type'),
                  'application/json;charset=utf-8')
Esempio n. 51
0
    def test_status(self):
        response = self.app.get(
            url=url_for(controller='api', action='status', ver=2),
            params={},
            status=200,
        )
        res = json.loads(response.body)
        assert_equal(res['ckan_version'], __version__)
        assert_equal(res['site_url'], 'http://test.ckan.net')
        assert_equal(res['site_title'], 'CKAN')
        assert_equal(res['site_description'], '')
        assert_equal(res['locale_default'], 'en')

        assert_equal(type(res['extensions']), list)
        expected_extensions = set(('stats', ))
        assert_equal(set(res['extensions']), expected_extensions)
Esempio n. 52
0
    def test_new_indexerror(self, env_user, app):
        bad_solr_url = "http://example.com/badsolrurl"
        solr_url = SolrSettings.get()[0]
        try:
            SolrSettings.init(bad_solr_url)
            new_package_name = u"new-package-missing-solr"

            offset = url_for("dataset.new")
            res = app.get(offset, extra_environ=env_user)
            fv = res.forms["dataset-edit"]
            fv["name"] = new_package_name

            res = fv.submit("save", status=500, extra_environ=env_user)
            assert "Unable to add package to search index" in res, res
        finally:
            SolrSettings.init(solr_url)
Esempio n. 53
0
    def test_new_indexerror(self, env_user, app):
        bad_solr_url = "http://example.com/badsolrurl"
        solr_url = SolrSettings.get()[0]
        try:
            SolrSettings.init(bad_solr_url)
            new_package_name = u"new-package-missing-solr"

            offset = url_for("dataset.new")
            res = app.post(offset,
                           extra_environ=env_user,
                           data={
                               "save": "",
                               "name": new_package_name,
                           })
            assert "Unable to add package to search index" in res, res
        finally:
            SolrSettings.init(solr_url)
Esempio n. 54
0
    def test_contact_controller_user_logged_in(self):
        '''
        Test that we get the contact form when user is logged in

        Note: Form should probably be only visible if the dataset can be requested via the form?
        '''

        extra_environ = {'REMOTE_USER': '******'}

        offset = url_for("/contact/warandpeace")
        res = self.app.post(offset, extra_environ=extra_environ)

        print offset
        print res

        assert all(piece in res.body
                   for piece in ['<form', '/contact/send/', '</form>'
                                 ]), 'Contact form not rendered'
Esempio n. 55
0
 def test_resource_list(self, app):
     # TODO restore this test. It doesn't make much sense with the
     # present resource list design.
     name = "annakarenina"
     cache_url = "http://thedatahub.org/test_cache_url.csv"
     # add a cache_url to the first resource in the package
     context = {
         "model": model,
         "session": model.Session,
         "user": "******",
     }
     data = {"id": "annakarenina"}
     pkg = get.package_show(context, data)
     pkg["resources"][0]["cache_url"] = cache_url
     # FIXME need to pretend to be called by the api
     context["api_version"] = 3
     update.package_update(context, pkg)
     # check that the cache url is included on the dataset view page
     offset = url_for("dataset.read", id=name)
     res = app.get(offset)
Esempio n. 56
0
    def test_01_button(self):
        '''
        Test that button renders
        '''
        organization = get_action('organization_create')(
            {
                'user': '******'
            }, {
                'name': 'unseen-academy',
                'title': "Unseen Academy"
            })

        data = copy.deepcopy(TEST_DATADICT)
        data['owner_org'] = organization['name']

        package = get_action('package_create')({'user': '******'}, data)
        offset = url_for(controller='package', action='read', id=package['id'])
        extra_environ = {'REMOTE_USER': '******'}
        res = self.app.get(offset, extra_environ=extra_environ)
        assert '/dataset/new_resource/' in res
Esempio n. 57
0
    def test_delete_not_available(self):
        '''
        Test that deletion of a dataset is not available
        for an unauthorised user
        '''
        package = TestAuthorisation.api_test_user.action.package_create(
            **TestAuthorisation.TEST_DATADICT)
        offset = url_for("/dataset/delete/{0}".format(package['name']))
        extra_environ = {'REMOTE_USER': '******'}

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

        assert res.status == 200, 'Wrong status code (expecting 200)'  # Confirmation page
        res = res.forms[0].submit('delete')

        assert res.status == 302, 'Wrong status code (expecting 302)'  # Redirect to login page
        res = res.follow()

        assert res.status == 200, 'Wrong status code (expecting 200)'  # Login page
        assert 'Unauthorized to delete package' in res, \
            'The login page should alert the user about deleting being unauthorized'

        TestAuthorisation.api_test_user.call_action(
            'package_delete', data_dict={'id': package['id']})
Esempio n. 58
0
    def test_edit_pkg_with_relationships(self, app):

        # add a relationship to a package
        pkg = model.Package.by_name(self.editpkg_name)
        anna = model.Package.by_name(u"annakarenina")
        pkg.add_relationship(u"depends_on", anna)
        model.repo.commit_and_remove()

        # check relationship before the test
        rels = model.Package.by_name(self.editpkg_name).get_relationships()
        assert (str(rels) ==
                "[<*PackageRelationship editpkgtest depends_on annakarenina>]")

        # edit the package
        self.offset = url_for("dataset.edit", id=self.editpkg_name)
        res = app.get(self.offset, extra_environ=self.extra_environ_admin)
        fv = res.forms["dataset-edit"]
        fv["title"] = u"New Title"
        fv.submit("save", extra_environ=self.extra_environ_admin)

        # check relationship still exists
        rels = model.Package.by_name(self.editpkg_name).get_relationships()
        assert (str(rels) ==
                "[<*PackageRelationship editpkgtest depends_on annakarenina>]")
Esempio n. 59
0
 def test_index(self):
     url = url_for('storage_api')
     res = self.app.get(url)
     out = res.json
     assert len(res.json) == 3
Esempio n. 60
0
 def _test_auth_request(self):
     url = url_for('storage_api_auth_request', label='abc')
     res = self.app.get(url, extra_environ=self.extra_environ, status=200)
     assert res.json['method'] == 'POST'
     assert res.json['headers']['Authorization']