def test_purge_youngest_revision(self): as_testsysadmin = {"REMOTE_USER": "******"} id = u"warandpeace" log_message = "test_1234" edit_url = url_for(controller="package", action="edit", id=id) # Manually create a revision res = self.app.get(edit_url) fv = res.forms["dataset-edit"] fv["title"] = "RevisedTitle" fv["log_message"] = log_message res = fv.submit("save") # Delete that revision rev = model.repo.youngest_revision() assert rev.message == log_message rev.state = model.State.DELETED model.Session.commit() # Run a purge url = url_for("ckanadmin", action="trash") res = self.app.get(url, extra_environ=as_testsysadmin) form = res.forms["form-purge-revisions"] res = form.submit("purge-revisions", status=[302], extra_environ=as_testsysadmin) res = res.follow(extra_environ=as_testsysadmin) # Verify the edit page can be loaded (ie. does not 404) res = self.app.get(edit_url)
def _test_via_wui(self, action, user, entity_name, entity='package'): # Test action on WUI str_required_in_response = entity_name if action in (model.Action.EDIT, model.Action.READ): offset = url_for(controller=entity, action=action, id=unicode(entity_name)) elif action == 'search': offset = '/%s/search?q=%s' % (entity, entity_name) str_required_in_response = '/%s"' % entity_name elif action == 'list': if entity == 'group': offset = '/group' else: offset = '/%s/list' % entity elif action == 'create': offset = '/%s/new' % entity str_required_in_response = 'New' elif action == 'delete': offset = url_for(controller=entity, action=model.Action.EDIT, id=unicode(entity_name)) str_required_in_response = 'state' else: raise NotImplementedError res = self.app.get(offset, extra_environ={'REMOTE_USER': user.name.encode('utf8')}, expect_errors=True) tests = {} tests['str_required (%s)' % str_required_in_response] = bool(str_required_in_response in res) tests['error string'] = bool('error' not in res) tests['status'] = bool(res.status in (200, 201)) tests['0 packages found'] = bool(u'0 packages found' not in res) is_ok = False not in tests.values() # clear flash messages - these might make the next page request # look like it has an error self.app.reset() return is_ok, [offset, user.name, tests, res.status, res.body]
def test_purge_youngest_revision(self): as_testsysadmin = {'REMOTE_USER': '******'} id = u'warandpeace' log_message = 'test_1234' edit_url = url_for(controller='package', action='edit', id=id) # Manually create a revision res = self.app.get(edit_url) fv = res.forms['dataset-edit'] fv['title'] = 'RevisedTitle' fv['log_message'] = log_message res = fv.submit('save') # Delete that revision rev = model.repo.youngest_revision() assert rev.message == log_message rev.state = model.State.DELETED model.Session.commit() # Run a purge url = url_for('ckanadmin', action='trash') res = self.app.get(url, extra_environ=as_testsysadmin) form = res.forms['form-purge-revisions'] res = form.submit('purge-revisions', status=[302], extra_environ=as_testsysadmin) res = res.follow(extra_environ=as_testsysadmin) # Verify the edit page can be loaded (ie. does not 404) res = self.app.get(edit_url)
def test_group_read(self): res = self.app.get(url_for(controller='group', action='read', id='group_00')) assert 'href="/group/group_00?page=2' in res assert 'href="/dataset/package_29"' in res res = self.app.get(url_for(controller='group', action='read', id='group_00', page=2)) assert 'href="/group/group_00?page=1' in res assert 'href="/dataset/package_30"' in res
def test_search(self): res = self.app.get(url_for(controller='package', action='search', q='groups:group_00')) assert 'href="/dataset?q=groups%3Agroup_00&page=2"' in res assert 'href="/dataset/package_19"' in res res = self.app.get(url_for(controller='package', action='search', q='groups:group_00', page=2)) assert 'href="/dataset?q=groups%3Agroup_00&page=1"' in res assert 'href="/dataset/package_20"' in res
def test_group_index(self): res = self.app.get(url_for(controller='group', action='index')) assert 'href="/group?page=2"' in res assert 'href="/group/group_19"' in res res = self.app.get(url_for(controller='group', action='index', page=2)) assert 'href="/group?page=1"' in res assert 'href="/group/group_20"' in res
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/user_18"' in res assert 'href="/user?q=&order_by=name&page=2"' in res res = self.app.get(url_for(controller='user', action='index', page=2)) assert 'href="/user/user_20"' in res assert 'href="/user?q=&order_by=name&page=1"' in res
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'http://localhost/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'
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'
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))
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)
def test_group_read(self): res = self.app.get( url_for(controller='group', action='read', id='group_00')) assert 'href="/group/group_00?page=2' in res assert 'href="/dataset/package_49"' in res res = self.app.get( url_for(controller='group', action='read', id='group_00', page=2)) assert 'href="/group/group_00?page=1' in res assert 'href="/dataset/package_50"' in res
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)
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://www.annakarenina.com/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
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 ) )
def _test_via_wui(self, action, user, entity_name, entity='dataset'): # Test action on WUI str_required_in_response = entity_name controller_name = 'package' if entity == 'dataset' else entity if action in (model.Action.EDIT, model.Action.READ): offset = url_for(controller=controller_name, action=action, id=unicode(entity_name)) elif action == 'search': offset = '/%s/search?q=%s' % (entity, entity_name) str_required_in_response = '%s"' % entity_name elif action == 'list': if entity == 'group': offset = '/group' else: offset = '/%s/list' % entity elif action == 'create': offset = '/%s/new' % entity str_required_in_response = 'Add' elif action == 'delete': offset = url_for(controller=controller_name, action=model.Action.EDIT, id=unicode(entity_name)) # this is ludicrously sensitive (we have to improve html testing!) # str_required_in_response = 'state' str_required_in_response = '<select id="state"' else: raise NotImplementedError res = self.app.get( offset, extra_environ={'REMOTE_USER': user.name.encode('utf8')}, expect_errors=True) tests = {} tests['str_required (%s)' % str_required_in_response] = bool(str_required_in_response in res) r = res.body r = r.replace('form_errors', '') # Commenting as it seems a very ineffective way of checking for errors # (e.g. tests fail if there is a JS string with the word 'error' on it) # tests['error string'] = bool('error' not in r) tests['status'] = bool(res.status in (200, 201)) tests['0 packages found'] = bool( u'<strong>0</strong> packages found' not in res) is_ok = False not in tests.values() # clear flash messages - these might make the next page request # look like it has an error self.app.reset() return is_ok, [offset, user.name, tests, res.status, res.body]
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'
def test_04_index(self): extra_environ = {'REMOTE_USER': '******'} url = url_for('storage_upload') out = self.app.get(url, extra_environ=extra_environ) assert 'Upload' in out, out #assert 'action="https://commondatastorage.googleapis.com/ckan' in out, out #assert 'key" value="' in out, out #assert 'policy" value="' in out, out #assert 'failure_action_redirect' in out, out #assert 'success_action_redirect' in out, out url = url_for('storage_upload', filepath='xyz.txt') out = self.app.get(url, extra_environ=extra_environ) assert 'file/xyz.txt' in out, out
def assert_radio_buttons(self, checked='all'): others = ['all', 'any', 'exact'] others.pop(others.index(checked)) urls = [tests.url_for(controller='group', action='read', id='publisher1'), tests.url_for('home'), tests.url_for(controller='package', action='search')] for url in urls: res = self.app.get(url) assert "Show results with" in res assert 'value="{0}" checked="checked" />'.format(checked) in res for other in others: assert 'value="{0}" />'.format(other) in res
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://www.annakarenina.com/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
def test_js_inserted_resource_view(self): from nose import SkipTest raise SkipTest("Test won't work until CKAN 1.5.2") from ckan.logic.action import get from ckan import model context = {"model": model, "ignore_auth": True} data = {"id": "annakarenina"} pkg = get.package_show(context, data) resource_id = pkg["resources"][0]["id"] command = LoadAnalytics("loadanalytics") command.TEST_HOST = MockClient("localhost", 6969) command.CONFIG = self.config command.run([]) response = self.app.get( url_for( controller="package", action="resource_read", id="annakarenina", resource_id=resource_id, )) assert 'onclick="javascript: _gaq.push(' in response.body
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"')
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")
def test_munge_tag(self): response = self.app.get( url=url_for(controller='api', action='munge_tag'), params={'name': 'Test subject'}, status=200, ) assert_equal(response.body, '"test-subject"')
def _test_inspire_metadata_with_upload(self, metadata_file): target_url = url_for(**self.action_url_args) metadata_file = os.path.realpath(os.path.join(self.samples_dir, metadata_file)) metadata_fname = os.path.basename(metadata_file) metadata = None with open(metadata_file, 'r') as ifp: metadata = ifp.read() params = OrderedDict(( ('dataset_type', 'inspire'), ('owner_org', self.owner_org['name']), ('rename', 'n'), ('force_create', 'n'), )) res = self.app.post(target_url, params = params, upload_files = [('source-upload', metadata_fname, metadata)], extra_environ = self.extra_environ) assert res.status in [301, 302] # Assert a package with expected identifier is created identifier = os.path.splitext(metadata_fname)[0] res = self.app.get('/api/action/dataset_show', params = {'id': identifier}, status=200) assert res.json r = res.json['result'] assert r['id'] == identifier assert r['dataset_type'] == 'inspire' assert ('inspire' in r) and (r['inspire']['identifier'] == identifier)
def test_2_edit_publisher_does_not_affect_others(self): publisher_name = 'national-health-service' def check_related_publisher_properties(): group = model.Group.by_name(publisher_name) # datasets assert_equal(set([grp.name for grp in group.active_packages()]), set([u'directgov-cota'])) # parents doh = model.Group.by_name('dept-health') child_groups_of_doh = [ grp.name for grp in list(publib.go_down_tree(doh)) ] assert publisher_name in child_groups_of_doh, child_groups_of_doh # children child_groups = set( [grp.name for grp in list(publib.go_down_tree(group))]) assert set([ u'newham-primary-care-trust', u'barnsley-primary-care-trust' ]) <= child_groups, child_groups # admins & editors assert_equal( set([ user.name for user in group.members_of_type(model.User, capacity='admin') ]), set(('nhsadmin', ))) assert_equal( set([ user.name for user in group.members_of_type(model.User, capacity='editor') ]), set(('nhseditor', 'user_d101'))) check_related_publisher_properties() # Load form group = model.Group.by_name(publisher_name) offset = url_for('/publisher/edit/%s' % publisher_name) res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) assert 'Edit: %s' % group.title in res, res form = res.forms[0] # Make edit form['description'] = 'New description' res = form.submit('save', status=302, extra_environ={'REMOTE_USER': '******'}) assert_equal(res.header_dict['Location'], 'http://localhost/publisher/national-health-service') # Check saved object publisher = model.Group.by_name(publisher_name) assert_equal(publisher.description, 'New description') check_related_publisher_properties()
def test_01_viewing_datasets(self): '''No recent datasets displayed on home when summary table is empty''' rows = helpers.most_viewed_datasets(10) self.assert_equal(rows, []) home_url = tests.url_for('home') res = self.app.get(home_url) self.assert_equal("Most viewed datasets" in res, False)
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"')
def test_analytics_snippet(self): response = self.app.get(url_for(controller="tag", action="index")) code = gasnippet.header_code % ( self.config["googleanalytics.id"], "auto", ) assert code in response.body
def test_read_redirect_at_base(self): # have to put None as o/w seems to still be at url set in previous test offset = url_for(controller="revision", action="read", id=None) res = self.app.get(offset) # redirect res = res.follow() assert "Revision History" in res
def test_0_basic_application(self): # need to randomize the user name because otherwise repeated runs cause # a flash message "A request for this publisher is already in the # system." and I can't see how to clear flash messages for the thread # inside the self.app: # helpers.flash.pop_messages() # *** TypeError: No object (name: session) has been registered for this # thread user_name = 'user%s' % int(random.random() * 10000) factories.User(name=user_name) # Load form publisher_name = 'dept-health' offset = url_for('/publisher/apply/%s' % publisher_name) res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': user_name}) assert 'Apply for membership' in res, res form = res.forms[1] print form parent_publisher_id = form['parent'].value parent_publisher_name = model.Group.get(parent_publisher_id).name assert_equal(parent_publisher_name, publisher_name) assert_equal(form['reason'].value, '') # Fill in form form['reason'] = 'I am the director' res = form.submit('save', status=302, extra_environ={'REMOTE_USER': user_name}) assert_equal(res.header_dict['Location'], 'http://localhost/publisher/%s?__no_cache__=True' % publisher_name) # Check email sent msgs = SmtpServerHarness.smtp_thread.get_smtp_messages() assert_equal(len(msgs), 1) msg = msgs[0] assert_equal(msg[1], '*****@*****.**') # from (ckan.mail_from in ckan/test-core.ini) assert_equal(msg[2], ["*****@*****.**"]) # to (dgu.admin.name/email in dgu/test-core.ini)
def test_0_basic_application(self): # Load form publisher_name = 'dept-health' group = model.Group.by_name(unicode(publisher_name)) offset = url_for('/publisher/apply/%s' % publisher_name) res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) assert 'Apply for membership' in res, res form = res.forms[1] print form parent_publisher_id = form['parent'].value parent_publisher_name = model.Group.get(parent_publisher_id).name assert_equal(parent_publisher_name, publisher_name) assert_equal(form['reason'].value, '') # Fill in form form['reason'] = 'I am the director' res = form.submit('save', status=302, extra_environ={'REMOTE_USER': '******'}) assert_equal(res.header_dict['Location'], 'http://localhost/publisher/%s?__no_cache__=True' % publisher_name) # Check email sent msgs = SmtpServerHarness.smtp_thread.get_smtp_messages() assert_equal(len(msgs), 1) msg = msgs[0] assert_equal(msg[1], '*****@*****.**') # from (ckan.mail_from in ckan/test-core.ini) assert_equal(msg[2], ["*****@*****.**"]) # to (dgu.admin.name/email in dgu/test-core.ini)
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
def test_0_basic_application(self): # Load form publisher_name = 'dept-health' group = model.Group.by_name(unicode(publisher_name)) offset = url_for('/publisher/apply/%s' % publisher_name) res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) assert 'Apply for membership' in res, res form = res.forms[0] parent_publisher_id = form['parent'].value parent_publisher_name = model.Group.get(parent_publisher_id).name assert_equal(parent_publisher_name, publisher_name) assert_equal(form['reason'].value, '') # Fill in form form['reason'] = 'I am the director' res = form.submit('save', status=302, extra_environ={'REMOTE_USER': '******'}) assert_equal( res.header_dict['Location'], 'http://localhost/publisher/%s?__no_cache__=True' % publisher_name) # Check email sent msgs = SmtpServerHarness.smtp_thread.get_smtp_messages() assert_equal(len(msgs), 1) msg = msgs[0] assert_equal(msg[1], '*****@*****.**' ) # from (ckan.mail_from in ckan/test-core.ini) assert_equal(msg[2], ["*****@*****.**" ]) # to (dgu.admin.name/email in dgu/test-core.ini)
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
def test_0_new_publisher(self): # Load form offset = url_for('/publisher/new') res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) assert 'Add A Publisher' in res, res form = res.forms['publisher-edit'] # Fill in form form['title'] = 'New publisher' publisher_name = 'test-name' form['name'] = publisher_name form['description'] = 'New description' form['contact-name'] = 'Head of Comms' form['contact-email'] = '*****@*****.**' form['contact-phone'] = '01234 4567890' form['foi-name'] = 'Head of FOI Comms' form['foi-email'] = '*****@*****.**' form['foi-phone'] = '0845 4567890' form['category'] = 'grouping' res = form.submit('save', status=302, extra_environ={'REMOTE_USER': '******'}) assert_equal(res.header_dict['Location'], 'http://localhost/publisher/test-name') # Check saved object publisher = model.Group.by_name(publisher_name) assert_equal(publisher.title, 'New publisher') assert_equal(publisher.description, 'New description') assert_equal(publisher.extras['contact-name'], 'Head of Comms') assert_equal(publisher.extras['contact-email'], '*****@*****.**') assert_equal(publisher.extras['contact-phone'], '01234 4567890') assert_equal(publisher.extras['foi-name'], 'Head of FOI Comms') assert_equal(publisher.extras['foi-email'], '*****@*****.**') assert_equal(publisher.extras['foi-phone'], '0845 4567890') assert_equal(publisher.extras['category'], 'grouping')
def _test_inspire_metadata_with_upload(self, metadata_file): target_url = url_for(**self.action_url_args) metadata_file = os.path.realpath( os.path.join(self.samples_dir, metadata_file)) metadata_fname = os.path.basename(metadata_file) metadata = None with open(metadata_file, 'r') as ifp: metadata = ifp.read() params = OrderedDict(( ('dataset_type', 'inspire'), ('owner_org', self.owner_org['name']), ('rename', 'n'), ('force_create', 'n'), )) res = self.app.post(target_url, params=params, upload_files=[('source-upload', metadata_fname, metadata)], extra_environ=self.extra_environ) assert res.status in [301, 302] # Assert a package with expected identifier is created identifier = os.path.splitext(metadata_fname)[0] res = self.app.get('/api/action/dataset_show', params={'id': identifier}, status=200) assert res.json r = res.json['result'] assert r['id'] == identifier assert r['dataset_type'] == 'inspire' assert ('inspire' in r) and (r['inspire']['identifier'] == identifier)
def test_2_new_validation_error(self): # Load form offset = url_for('/publisher/new') res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) assert 'Add A Publisher' in res, res form = res.forms['publisher-edit'] # Fill in form form['title'] = 'New publisher' form['name'] = '' # cause validation error form['description'] = 'New description' form['contact-name'] = 'Head of Comms' form['contact-email'] = '*****@*****.**' form['contact-phone'] = '01234 4567890' form['foi-name'] = 'Head of FOI Comms' form['foi-email'] = '*****@*****.**' form['foi-phone'] = '0845 4567890' form['category'] = 'grouping' form['abbreviation'] = 'tn2' res = form.submit('save', status=200, extra_environ={'REMOTE_USER': '******'}) assert 'Errors in form' in res.body # Check redisplayed form form = res.forms['publisher-edit'] assert_equal(form['title'].value, 'New publisher') assert_equal(form['description'].value, 'New description') assert_equal(form['contact-name'].value, 'Head of Comms') assert_equal(form['contact-email'].value, '*****@*****.**') assert_equal(form['contact-phone'].value, '01234 4567890') assert_equal(form['foi-name'].value, 'Head of FOI Comms') assert_equal(form['foi-email'].value, '*****@*****.**') assert_equal(form['foi-phone'].value, '0845 4567890') assert_equal(form['category'].value, 'grouping') assert_equal(form['abbreviation'].value, 'tn2')
def test_06_searchcloud_displays_when_approved_data_present(self): #Put some data into the cloud searchcloud.generate_unapproved_list(model.Session, days=30) latest_rows = searchcloud.get_latest(model.Session) self.assert_equal(latest_rows, [[u'Health', 3L], [u"Water<>&\"{}'", 2L], [u'Tax\u6c49\u5b57\u6f22\u5b57', 1L]]) searchcloud.update_approved(model.Session, latest_rows) approved_rows = searchcloud.get_approved(model.Session) self.assert_equal(latest_rows, approved_rows) #We'll use this test data later, so save it. model.Session.commit() # Test that the JSON text is what we expect. self.assert_equal( expected_json, searchcloud.approved_to_json(approved_rows) ) # Now we should get a cloud home_url = tests.url_for('home') res = self.app.get(home_url) self.assert_equal( 'var word_array = '+(cgi.escape(expected_json)) in res, True, ) self.assert_equal('Popular terms' in res, True) self.assert_equal('jqcloud-1.0.4.min.js' in res, True) self.assert_equal('jqcloud.css' in res, True) self.assert_equal('<div id="searchcloud"' in res, True)
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"')
def test_munge_title_to_package_name(self): response = self.app.get( url=url_for(controller='api', action='munge_title_to_package_name'), params={'name': 'Test title'}, status=200, ) assert_equal(response.body, '"test-title"')
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")
def test_4_delete_publisher(self): group_name = 'deletetest' CreateTestData.create_groups( [{ 'name': group_name, 'packages': [u'cabinet-office-energy-use'] }], admin_user_name='nhsadmin') group = model.Group.by_name(group_name) offset = url_for(controller=self.publisher_controller, action='edit', id=group_name) res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) main_res = self.main_div(res) assert 'Edit: %s' % group.title in main_res, main_res assert 'value="active" selected' in main_res, main_res # delete form = res.forms[0] form['state'] = 'deleted' form['category'] = 'private' # to get it to validate res = form.submit('save', status=302, extra_environ={'REMOTE_USER': '******'}) group = model.Group.by_name(group_name) assert_equal(group.state, 'deleted') res = self.app.get(offset, status=401)
def test_search(self): res = self.app.get( url_for(controller='package', action='search', q='groups:group_00')) assert 'href="/dataset?q=groups%3Agroup_00&page=2"' in res assert 'href="/dataset/package_00"' in res, res assert 'href="/dataset/package_19"' in res, res res = self.app.get( url_for(controller='package', action='search', q='groups:group_00', page=2)) assert 'href="/dataset?q=groups%3Agroup_00&page=1"' in res assert 'href="/dataset/package_20"' in res assert 'href="/dataset/package_39"' in res
def test_package_openness_scores(self): context = {'model': model, 'session': model.Session} for p in model.Session.query(model.Package): context['id'] = p.id p = package_dictize(p, context) url = url_for('qa_dataset_action', action='five_stars') response = self.app.get(url) assert 'openness scores' in response, response
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>"')
def test_follow_get(self): """ Tests to ensure that get requests to the follow action returns the same content as index action. """ f = self.app.get(url_for('follow')) i = self.app.get(url_for('follower')) # compare content-type headers assert f.header('Content-Type') == i.header('Content-Type') f_js = json.loads(f.body) i_js = json.loads(i.body) # make sure json responses are same length assert len(f_js) == len(i_js) # make sure json response key/values are equal for key in f_js: assert key in i_js assert f_js[key] == i_js[key]
def test_download_count_inserted(self): command = LoadAnalytics("loadanalytics") command.TEST_HOST = MockClient('localhost', 6969) command.CONFIG = self.config command.run([]) response = self.app.get( url_for(controller='package', action='read', id='annakarenina')) assert "[downloaded 4 times]" in response.body
def test_publisher_cannot_edit_a_UKLP_dataset(self): """Test that a publisher cannot access the edit page of a UKLP dataset""" offset = url_for(controller='package', action='edit', id=self.uklp_dataset['id']) response = None try: response = self.app.get(offset, extra_environ={'REMOTE_USER': self.tester}) except paste.fixture.AppError, e: assert_in('401 Unauthorized', str(e))
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)
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)