diff --git a/LICENSE.txt b/LICENSE.txt
index 26a96720c5d..42bb1e033ea 100644
--- a/LICENSE.txt
+++ b/LICENSE.txt
@@ -2,7 +2,7 @@ License
+++++++
CKAN - Data Catalogue Software
-Copyright (C) 2007 Open Knowledge Foundation
+Copyright (c) 2006-2017 Open Knowledge International and contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
diff --git a/ckan/config/middleware/__init__.py b/ckan/config/middleware/__init__.py
index 0b14a8c7e05..a6ec785ce4d 100644
--- a/ckan/config/middleware/__init__.py
+++ b/ckan/config/middleware/__init__.py
@@ -39,6 +39,10 @@ def custom_charset__set(self, charset):
# End of webob.requests.BaseRequest monkey patch
+# This is a test Flask request context to be used internally.
+# Do not use it!
+_internal_test_request_context = None
+
def make_app(conf, full_stack=True, static_files=True, **app_conf):
'''
@@ -55,6 +59,11 @@ def make_app(conf, full_stack=True, static_files=True, **app_conf):
app = AskAppDispatcherMiddleware({'pylons_app': pylons_app,
'flask_app': flask_app})
+ # Set this internal test request context with the configured environment so
+ # it can be used when calling url_for from tests
+ global _internal_test_request_context
+ _internal_test_request_context = flask_app._wsgi_app.test_request_context()
+
return app
diff --git a/ckan/controllers/user.py b/ckan/controllers/user.py
index d657330647f..f25b4478294 100644
--- a/ckan/controllers/user.py
+++ b/ckan/controllers/user.py
@@ -397,8 +397,7 @@ def login(self, error=None):
if not c.user:
came_from = request.params.get('came_from')
if not came_from:
- came_from = h.url_for(controller='user', action='logged_in',
- __ckan_no_root=True)
+ came_from = h.url_for(controller='user', action='logged_in')
c.login_handler = h.url_for(
self._get_repoze_handler('login_handler_path'),
came_from=came_from)
@@ -436,10 +435,9 @@ def logout(self):
# Do any plugin logout stuff
for item in p.PluginImplementations(p.IAuthenticator):
item.logout()
- url = h.url_for(controller='user', action='logged_out_page',
- __ckan_no_root=True)
+ url = h.url_for(controller='user', action='logged_out_page')
h.redirect_to(self._get_repoze_handler('logout_handler_path') +
- '?came_from=' + url)
+ '?came_from=' + url, parse_url=True)
def logged_out(self):
# redirect if needed
diff --git a/ckan/lib/alphabet_paginate.py b/ckan/lib/alphabet_paginate.py
index 99e570af055..0fdf8d60cf8 100644
--- a/ckan/lib/alphabet_paginate.py
+++ b/ckan/lib/alphabet_paginate.py
@@ -21,7 +21,7 @@
from sqlalchemy import __version__ as sqav
from sqlalchemy.orm.query import Query
from webhelpers.html.builder import HTML
-from ckan.lib.helpers import url_for
+from routes import url_for
class AlphaPage(object):
diff --git a/ckan/lib/cli.py b/ckan/lib/cli.py
index 7040c8afbdd..1fbe55bc3fb 100644
--- a/ckan/lib/cli.py
+++ b/ckan/lib/cli.py
@@ -28,11 +28,16 @@
import ckan.include.rcssmin as rcssmin
import ckan.plugins as p
from ckan.common import config
+from ckan.tests.helpers import _get_test_app
+# This is a test Flask request context to be used internally.
+# Do not use it!
+_cli_test_request_context = None
-#NB No CKAN imports are allowed until after the config file is loaded.
-# i.e. do the imports in methods, after _load_config is called.
-# Otherwise loggers get disabled.
+
+# NB No CKAN imports are allowed until after the config file is loaded.
+# i.e. do the imports in methods, after _load_config is called.
+# Otherwise loggers get disabled.
def deprecation_warning(message=None):
@@ -224,6 +229,12 @@ def load_config(config, load_site_user=True):
from ckan.config.environment import load_environment
load_environment(conf.global_conf, conf.local_conf)
+ # Set this internal test request context with the configured environment so
+ # it can be used when calling url_for from the CLI.
+ global _cli_test_request_context
+ flask_app = _get_test_app().flask_app
+ _cli_test_request_context = flask_app.test_request_context()
+
registry = Registry()
registry.prepare()
import pylons
diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py
index a701ef5ae81..d6765eab81f 100644
--- a/ckan/lib/helpers.py
+++ b/ckan/lib/helpers.py
@@ -28,6 +28,7 @@
from pylons import url as _pylons_default_url
from ckan.common import config, is_flask_request
from flask import redirect as _flask_redirect
+from flask import _request_ctx_stack, current_app
from routes import redirect_to as _routes_redirect_to
from routes import url_for as _routes_default_url_for
from flask import url_for as _flask_default_url_for
@@ -152,6 +153,12 @@ def redirect_to(*args, **kw):
toolkit.redirect_to('dataset_read', id='changed')
+ If given a single string as argument, this redirects without url parsing
+
+ toolkit.redirect_to('http://example.com')
+ toolkit.redirect_to('/dataset')
+ toolkit.redirect_to('/some/other/path')
+
'''
if are_there_flash_messages():
kw['__no_cache__'] = True
@@ -159,7 +166,19 @@ def redirect_to(*args, **kw):
# Routes router doesn't like unicode args
uargs = map(lambda arg: str(arg) if isinstance(arg, unicode) else arg,
args)
- _url = url_for(*uargs, **kw)
+
+ _url = ''
+ skip_url_parsing = False
+ parse_url = kw.pop('parse_url', False)
+ if uargs and len(uargs) is 1 and isinstance(uargs[0], basestring) \
+ and (uargs[0].startswith('/') or is_url(uargs[0])) \
+ and parse_url is False:
+ skip_url_parsing = True
+ _url = uargs[0]
+
+ if skip_url_parsing is False:
+ _url = url_for(*uargs, **kw)
+
if _url.startswith('/'):
_url = str(config['ckan.site_url'].rstrip('/') + _url)
@@ -201,6 +220,30 @@ def get_site_protocol_and_host():
return (None, None)
+def _get_auto_flask_context():
+ '''
+ Provides a Flask test request context if we are outside the context
+ of a web request (tests or CLI)
+ '''
+
+ from ckan.config.middleware import _internal_test_request_context
+ from ckan.lib.cli import _cli_test_request_context
+
+ # This is a normal web request, there is a request context present
+ if _request_ctx_stack.top:
+ return None
+
+ # We are outside a web request. A test web application was created
+ # (and with it a test request context with the relevant configuration)
+ if _internal_test_request_context:
+ return _internal_test_request_context
+
+ # We are outside a web request. This is a CLI command. A test request
+ # context was created when setting it up
+ if _cli_test_request_context:
+ return _cli_test_request_context
+
+
@core_helper
def url_for(*args, **kw):
'''Return the URL for an endpoint given some parameters.
@@ -252,13 +295,23 @@ def url_for(*args, **kw):
ver = kw.get('ver')
if not ver:
raise Exception('API URLs must specify the version (eg ver=3)')
+
+ _auto_flask_context = _get_auto_flask_context()
+
try:
+ if _auto_flask_context:
+ _auto_flask_context.push()
+
# First try to build the URL with the Flask router
my_url = _url_for_flask(*args, **kw)
except FlaskRouteBuildError:
+
# If it doesn't succeed, fallback to the Pylons router
my_url = _url_for_pylons(*args, **kw)
+ finally:
+ if _auto_flask_context:
+ _auto_flask_context.pop()
# Add back internal params
kw['__ckan_no_root'] = no_root
diff --git a/ckan/lib/mailer.py b/ckan/lib/mailer.py
index c75e3435e96..4979ea8eb77 100644
--- a/ckan/lib/mailer.py
+++ b/ckan/lib/mailer.py
@@ -8,7 +8,6 @@
from email.mime.text import MIMEText
from email.header import Header
from email import Utils
-from urlparse import urljoin
from ckan.common import config
import paste.deploy.converters
@@ -33,7 +32,10 @@ def _mail_recipient(recipient_name, recipient_email,
mail_from = config.get('smtp.mail_from')
msg = MIMEText(body.encode('utf-8'), 'plain', 'utf-8')
for k, v in headers.items():
- msg[k] = v
+ if k in msg.keys():
+ msg.replace_header(k, v)
+ else:
+ msg.add_header(k, v)
subject = Header(subject.encode('utf-8'), 'utf-8')
msg['Subject'] = subject
msg['From'] = _("%s <%s>") % (sender_name, mail_from)
diff --git a/ckan/plugins/interfaces.py b/ckan/plugins/interfaces.py
index f5bf0632487..489ed777501 100644
--- a/ckan/plugins/interfaces.py
+++ b/ckan/plugins/interfaces.py
@@ -1581,10 +1581,7 @@ def organization_facets(self, facets_dict, organization_type, package_type):
class IAuthenticator(Interface):
- u'''EXPERIMENTAL
-
- Allows custom authentication methods to be integrated into CKAN.
- Currently it is experimental and the interface may change.'''
+ u'''Allows custom authentication methods to be integrated into CKAN.'''
def identify(self):
u'''called to identify the user.
diff --git a/ckan/public/base/css/fuchsia.css b/ckan/public/base/css/fuchsia.css
index 61fae4c8804..274b5e6135d 100644
--- a/ckan/public/base/css/fuchsia.css
+++ b/ckan/public/base/css/fuchsia.css
@@ -4220,7 +4220,7 @@ select[multiple].input-group-sm > .input-group-btn > .btn {
}
}
.navbar-static-top {
- z-index: 1000;
+ z-index: 1;
border-width: 0 0 1px;
}
@media (min-width: 768px) {
@@ -10220,6 +10220,12 @@ h4 small {
.alert-error .alert-link {
color: #843534;
}
+.input-group .form-control {
+ z-index: 0;
+}
+.input-group-btn:last-child > .btn {
+ z-index: 0;
+}
body {
background: #E73892 url("../../../base/images/bg.png");
}
diff --git a/ckan/public/base/css/green.css b/ckan/public/base/css/green.css
index e296aac070a..50e8ea9c25b 100644
--- a/ckan/public/base/css/green.css
+++ b/ckan/public/base/css/green.css
@@ -4220,7 +4220,7 @@ select[multiple].input-group-sm > .input-group-btn > .btn {
}
}
.navbar-static-top {
- z-index: 1000;
+ z-index: 1;
border-width: 0 0 1px;
}
@media (min-width: 768px) {
@@ -10220,6 +10220,12 @@ h4 small {
.alert-error .alert-link {
color: #843534;
}
+.input-group .form-control {
+ z-index: 0;
+}
+.input-group-btn:last-child > .btn {
+ z-index: 0;
+}
body {
background: #2F9B45 url("../../../base/images/bg.png");
}
diff --git a/ckan/public/base/css/main.css b/ckan/public/base/css/main.css
index c27f246d99c..45a75e4b1b9 100644
--- a/ckan/public/base/css/main.css
+++ b/ckan/public/base/css/main.css
@@ -4220,7 +4220,7 @@ select[multiple].input-group-sm > .input-group-btn > .btn {
}
}
.navbar-static-top {
- z-index: 1000;
+ z-index: 1;
border-width: 0 0 1px;
}
@media (min-width: 768px) {
@@ -10220,6 +10220,12 @@ h4 small {
.alert-error .alert-link {
color: #843534;
}
+.input-group .form-control {
+ z-index: 0;
+}
+.input-group-btn:last-child > .btn {
+ z-index: 0;
+}
body {
background: #005d7a url("../../../base/images/bg.png");
}
diff --git a/ckan/public/base/css/maroon.css b/ckan/public/base/css/maroon.css
index 60c6fccb6a9..39d602737e1 100644
--- a/ckan/public/base/css/maroon.css
+++ b/ckan/public/base/css/maroon.css
@@ -4220,7 +4220,7 @@ select[multiple].input-group-sm > .input-group-btn > .btn {
}
}
.navbar-static-top {
- z-index: 1000;
+ z-index: 1;
border-width: 0 0 1px;
}
@media (min-width: 768px) {
@@ -10220,6 +10220,12 @@ h4 small {
.alert-error .alert-link {
color: #843534;
}
+.input-group .form-control {
+ z-index: 0;
+}
+.input-group-btn:last-child > .btn {
+ z-index: 0;
+}
body {
background: #810606 url("../../../base/images/bg.png");
}
diff --git a/ckan/public/base/css/red.css b/ckan/public/base/css/red.css
index 2f07f16f870..86b9b5e151e 100644
--- a/ckan/public/base/css/red.css
+++ b/ckan/public/base/css/red.css
@@ -4220,7 +4220,7 @@ select[multiple].input-group-sm > .input-group-btn > .btn {
}
}
.navbar-static-top {
- z-index: 1000;
+ z-index: 1;
border-width: 0 0 1px;
}
@media (min-width: 768px) {
@@ -10220,6 +10220,12 @@ h4 small {
.alert-error .alert-link {
color: #843534;
}
+.input-group .form-control {
+ z-index: 0;
+}
+.input-group-btn:last-child > .btn {
+ z-index: 0;
+}
body {
background: #C14531 url("../../../base/images/bg.png");
}
diff --git a/ckan/public/base/javascript/modules/basic-form.js b/ckan/public/base/javascript/modules/basic-form.js
index a3c57b094a1..37915a6176b 100644
--- a/ckan/public/base/javascript/modules/basic-form.js
+++ b/ckan/public/base/javascript/modules/basic-form.js
@@ -11,7 +11,7 @@ this.ckan.module('basic-form', function (jQuery) {
// consecutive form submissions.
this.el.on('submit', this._onSubmit);
},
- _onSubmit() {
+ _onSubmit: function () {
// The button is not disabled immediately so that its value can be sent
// the first time the form is submitted, because the "save" field is
diff --git a/ckan/public/base/less/ckan.less b/ckan/public/base/less/ckan.less
index fb90acde1ba..f2ed0ef04b9 100644
--- a/ckan/public/base/less/ckan.less
+++ b/ckan/public/base/less/ckan.less
@@ -23,6 +23,7 @@
@import "resource-view.less";
@import "datapusher.less";
@import "alerts.less";
+@import "input-groups.less";
body {
// Using the masthead/footer gradient prevents the color from changing
// at the bottom of the window on pages with little content.
diff --git a/ckan/public/base/less/input-groups.less b/ckan/public/base/less/input-groups.less
new file mode 100644
index 00000000000..b59f48b06be
--- /dev/null
+++ b/ckan/public/base/less/input-groups.less
@@ -0,0 +1,13 @@
+.input-group {
+ .form-control {
+ z-index: 0;
+ }
+}
+
+.input-group-btn {
+ &:last-child {
+ > .btn {
+ z-index: 0;
+ }
+ }
+}
diff --git a/ckan/public/base/less/variables.less b/ckan/public/base/less/variables.less
index 93e5dc36751..5c62374cb0f 100644
--- a/ckan/public/base/less/variables.less
+++ b/ckan/public/base/less/variables.less
@@ -122,3 +122,6 @@
@activityColorNeutral: #767DCE;
@activityColorModify: #767DCE;
@activityColorDelete: #B95252;
+
+// Navigation
+@zindex-navbar: 1;
diff --git a/ckan/templates/page.html b/ckan/templates/page.html
index 208d39176a0..2d20a00f8f0 100644
--- a/ckan/templates/page.html
+++ b/ckan/templates/page.html
@@ -53,6 +53,24 @@
{% block pre_primary %}
{% endblock %}
+ {% block secondary %}
+
+ {% endblock %}
+
{% block primary %}
{#
@@ -93,24 +111,6 @@
My page content
{% endblock %}
{% endblock %}
-
- {% block secondary %}
-
- {% endblock %}
{% endblock %}
diff --git a/ckan/tests/config/test_middleware.py b/ckan/tests/config/test_middleware.py
index e7665877cb0..2e512438cc3 100644
--- a/ckan/tests/config/test_middleware.py
+++ b/ckan/tests/config/test_middleware.py
@@ -3,6 +3,7 @@
import mock
import wsgiref
from nose.tools import assert_equals, assert_not_equals, eq_, assert_raises
+from routes import url_for
from flask import Blueprint
import flask
@@ -29,9 +30,7 @@ def test_homepage_with_middleware_activated(self):
We are just testing the home page renders without any troubles and that
the middleware has not done anything strange to the response string'''
app = self._get_test_app()
- with app.flask_app.test_request_context():
- response = app.get(
- url=h.url_for(controller='home', action='index'))
+ response = app.get(url=url_for(controller='home', action='index'))
assert_equals(200, response.status_int)
# make sure we haven't overwritten the response too early.
diff --git a/ckan/tests/controllers/test_admin.py b/ckan/tests/controllers/test_admin.py
index 7b1c6067839..8915c489d92 100644
--- a/ckan/tests/controllers/test_admin.py
+++ b/ckan/tests/controllers/test_admin.py
@@ -3,7 +3,7 @@
from nose.tools import assert_true, assert_equal
from bs4 import BeautifulSoup
-from ckan.lib.helpers import url_for
+from routes import url_for
from ckan.common import config
import ckan.model as model
@@ -19,10 +19,8 @@
def _get_admin_config_page(app):
user = factories.Sysadmin()
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='admin', action='config')
response = app.get(
- url=url,
+ url=url_for(controller='admin', action='config'),
extra_environ=env,
)
return env, response
@@ -32,10 +30,8 @@ def _reset_config(app):
'''Reset config via action'''
user = factories.Sysadmin()
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='admin', action='reset_config')
app.post(
- url=url,
+ url=url_for(controller='admin', action='reset_config'),
extra_environ=env,
)
@@ -266,9 +262,9 @@ class TestTrashView(helpers.FunctionalTestBase):
def test_trash_view_anon_user(self):
'''An anon user shouldn't be able to access trash view.'''
app = self._get_test_app()
- with app.flask_app.test_request_context():
- trash_url = url_for(controller='admin', action='trash')
- app.get(trash_url, status=403)
+
+ trash_url = url_for(controller='admin', action='trash')
+ trash_response = app.get(trash_url, status=403)
def test_trash_view_normal_user(self):
'''A normal logged in user shouldn't be able to access trash view.'''
@@ -276,9 +272,7 @@ def test_trash_view_normal_user(self):
app = self._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- trash_url = url_for(controller='admin', action='trash')
+ trash_url = url_for(controller='admin', action='trash')
trash_response = app.get(trash_url, extra_environ=env, status=403)
assert_true('Need to be system administrator to administer'
in trash_response)
@@ -289,9 +283,7 @@ def test_trash_view_sysadmin(self):
app = self._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- trash_url = url_for(controller='admin', action='trash')
+ trash_url = url_for(controller='admin', action='trash')
trash_response = app.get(trash_url, extra_environ=env, status=200)
# On the purge page
assert_true('form-purge-packages' in trash_response)
@@ -304,8 +296,7 @@ def test_trash_no_datasets(self):
app = self._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- trash_url = url_for(controller='admin', action='trash')
+ trash_url = url_for(controller='admin', action='trash')
trash_response = app.get(trash_url, extra_environ=env, status=200)
trash_response_html = BeautifulSoup(trash_response.body)
@@ -324,8 +315,7 @@ def test_trash_with_deleted_datasets(self):
app = self._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- trash_url = url_for(controller='admin', action='trash')
+ trash_url = url_for(controller='admin', action='trash')
trash_response = app.get(trash_url, extra_environ=env, status=200)
trash_response_html = BeautifulSoup(trash_response.body)
@@ -348,8 +338,7 @@ def test_trash_purge_deleted_datasets(self):
assert_equal(pkgs_before_purge, 3)
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- trash_url = url_for(controller='admin', action='trash')
+ trash_url = url_for(controller='admin', action='trash')
trash_response = app.get(trash_url, extra_environ=env, status=200)
# submit the purge form
@@ -375,8 +364,7 @@ def _update_config_option(self):
sysadmin = factories.Sysadmin()
env = {'REMOTE_USER': sysadmin['name'].encode('ascii')}
app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='admin', action='config')
+ url = url_for(controller='admin', action='config')
response = app.get(url=url, extra_environ=env)
form = response.forms[1]
diff --git a/ckan/tests/controllers/test_api.py b/ckan/tests/controllers/test_api.py
index def1a1aaea0..d674bb02f66 100644
--- a/ckan/tests/controllers/test_api.py
+++ b/ckan/tests/controllers/test_api.py
@@ -7,7 +7,7 @@
import json
import re
-from ckan.lib.helpers import url_for
+from routes import url_for
from nose.tools import assert_equal, assert_in, eq_
import ckan.tests.helpers as helpers
@@ -30,12 +30,9 @@ def test_unicode_in_error_message_works_ok(self):
def test_dataset_autocomplete_name(self):
dataset = factories.Dataset(name='rivers')
-
- app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='api', action='dataset_autocomplete',
- ver='/2')
+ url = url_for(controller='api', action='dataset_autocomplete', ver='/2')
assert_equal(url, '/api/2/util/dataset/autocomplete')
+ app = self._get_test_app()
response = app.get(
url=url,
@@ -57,11 +54,7 @@ def test_dataset_autocomplete_name(self):
def test_dataset_autocomplete_title(self):
dataset = factories.Dataset(name='test_ri', title='Rivers')
-
- app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='api', action='dataset_autocomplete',
- ver='/2')
+ url = url_for(controller='api', action='dataset_autocomplete', ver='/2')
assert_equal(url, '/api/2/util/dataset/autocomplete')
app = self._get_test_app()
@@ -85,12 +78,9 @@ def test_dataset_autocomplete_title(self):
def test_tag_autocomplete(self):
factories.Dataset(tags=[{'name': 'rivers'}])
-
- app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='api', action='tag_autocomplete',
- ver='/2')
+ url = url_for(controller='api', action='tag_autocomplete', ver='/2')
assert_equal(url, '/api/2/util/tag/autocomplete')
+ app = self._get_test_app()
response = app.get(
url=url,
@@ -106,12 +96,10 @@ def test_tag_autocomplete(self):
'application/json;charset=utf-8')
def test_group_autocomplete_by_name(self):
- factories.Group(name='rivers', title='Bridges')
- app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='api', action='group_autocomplete',
- ver='/2')
+ org = factories.Group(name='rivers', title='Bridges')
+ url = url_for(controller='api', action='group_autocomplete', ver='/2')
assert_equal(url, '/api/2/util/group/autocomplete')
+ app = self._get_test_app()
response = app.get(
url=url,
@@ -129,11 +117,9 @@ def test_group_autocomplete_by_name(self):
'application/json;charset=utf-8')
def test_group_autocomplete_by_title(self):
- factories.Group(name='frogs', title='Bugs')
+ org = factories.Group(name='frogs', title='Bugs')
+ url = url_for(controller='api', action='group_autocomplete', ver='/2')
app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='api', action='group_autocomplete',
- ver='/2')
response = app.get(
url=url,
@@ -149,12 +135,9 @@ def test_group_autocomplete_by_title(self):
def test_organization_autocomplete_by_name(self):
org = factories.Organization(name='simple-dummy-org')
-
- app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='api', action='organization_autocomplete',
- ver='/2')
+ url = url_for(controller='api', action='organization_autocomplete', ver='/2')
assert_equal(url, '/api/2/util/organization/autocomplete')
+ app = self._get_test_app()
response = app.get(
url=url,
@@ -172,12 +155,9 @@ def test_organization_autocomplete_by_name(self):
'application/json;charset=utf-8')
def test_organization_autocomplete_by_title(self):
- factories.Organization(title='Simple dummy org')
-
+ org = factories.Organization(title='Simple dummy org')
+ url = url_for(controller='api', action='organization_autocomplete', ver='/2')
app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='api', action='organization_autocomplete',
- ver='/2')
response = app.get(
url=url,
@@ -193,14 +173,12 @@ def test_organization_autocomplete_by_title(self):
def test_config_option_list_access_sysadmin(self):
user = factories.Sysadmin()
-
+ url = url_for(
+ controller='api',
+ action='action',
+ logic_function='config_option_list',
+ ver='/3')
app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(
- controller='api',
- action='action',
- logic_function='config_option_list',
- ver='/3')
app.get(
url=url,
@@ -211,14 +189,12 @@ def test_config_option_list_access_sysadmin(self):
def test_config_option_list_access_sysadmin_jsonp(self):
user = factories.Sysadmin()
-
+ url = url_for(
+ controller='api',
+ action='action',
+ logic_function='config_option_list',
+ ver='/3')
app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(
- controller='api',
- action='action',
- logic_function='config_option_list',
- ver='/3')
app.get(
url=url,
@@ -232,13 +208,12 @@ def test_jsonp_works_on_get_requests(self):
dataset1 = factories.Dataset()
dataset2 = factories.Dataset()
+ url = url_for(
+ controller='api',
+ action='action',
+ logic_function='package_list',
+ ver='/3')
app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(
- controller='api',
- action='action',
- logic_function='package_list',
- ver='/3')
res = app.get(
url=url,
params={'callback': 'my_callback'},
@@ -255,15 +230,15 @@ def test_jsonp_does_not_work_on_post_requests(self):
dataset1 = factories.Dataset()
dataset2 = factories.Dataset()
+
+ url = url_for(
+ controller='api',
+ action='action',
+ logic_function='package_list',
+ ver='/3',
+ callback='my_callback',
+ )
app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(
- controller='api',
- action='action',
- logic_function='package_list',
- ver='/3',
- callback='my_callback',
- )
res = app.post(
url=url,
)
diff --git a/ckan/tests/controllers/test_feed.py b/ckan/tests/controllers/test_feed.py
index 614eab8befb..ba256d446ba 100644
--- a/ckan/tests/controllers/test_feed.py
+++ b/ckan/tests/controllers/test_feed.py
@@ -17,40 +17,32 @@ def teardown_class(cls):
def test_atom_feed_page_zero_gives_error(self):
group = factories.Group()
-
+ offset = url_for(controller='feed', action='group',
+ id=group['name']) + '?page=0'
app = self._get_test_app()
- with app.flask_app.test_request_context():
- offset = url_for(controller='feed', action='group',
- id=group['name']) + '?page=0'
res = app.get(offset, status=400)
assert '"page" parameter must be a positive integer' in res, res
def test_atom_feed_page_negative_gives_error(self):
group = factories.Group()
-
+ offset = url_for(controller='feed', action='group',
+ id=group['name']) + '?page=-2'
app = self._get_test_app()
- with app.flask_app.test_request_context():
- offset = url_for(controller='feed', action='group',
- id=group['name']) + '?page=-2'
res = app.get(offset, status=400)
assert '"page" parameter must be a positive integer' in res, res
def test_atom_feed_page_not_int_gives_error(self):
group = factories.Group()
-
+ offset = url_for(controller='feed', action='group',
+ id=group['name']) + '?page=abc'
app = self._get_test_app()
- with app.flask_app.test_request_context():
- offset = url_for(controller='feed', action='group',
- id=group['name']) + '?page=abc'
res = app.get(offset, status=400)
assert '"page" parameter must be a positive integer' in res, res
def test_general_atom_feed_works(self):
dataset = factories.Dataset()
-
+ offset = url_for(controller='feed', action='general')
app = self._get_test_app()
- with app.flask_app.test_request_context():
- offset = url_for(controller='feed', action='general')
res = app.get(offset)
assert '{0} '.format(dataset['title']) in res.body
@@ -58,11 +50,9 @@ def test_general_atom_feed_works(self):
def test_group_atom_feed_works(self):
group = factories.Group()
dataset = factories.Dataset(groups=[{'id': group['id']}])
-
+ offset = url_for(controller='feed', action='group',
+ id=group['name'])
app = self._get_test_app()
- with app.flask_app.test_request_context():
- offset = url_for(controller='feed', action='group',
- id=group['name'])
res = app.get(offset)
assert '{0} '.format(dataset['title']) in res.body
@@ -70,11 +60,9 @@ def test_group_atom_feed_works(self):
def test_organization_atom_feed_works(self):
group = factories.Organization()
dataset = factories.Dataset(owner_org=group['id'])
-
+ offset = url_for(controller='feed', action='organization',
+ id=group['name'])
app = self._get_test_app()
- with app.flask_app.test_request_context():
- offset = url_for(controller='feed', action='organization',
- id=group['name'])
res = app.get(offset)
assert '{0} '.format(dataset['title']) in res.body
@@ -86,13 +74,11 @@ def test_custom_atom_feed_works(self):
dataset2 = factories.Dataset(
title='Test daily',
extras=[{'key': 'frequency', 'value': 'daily'}])
-
- app = self._get_test_app()
- with app.flask_app.test_request_context():
- offset = url_for(controller='feed', action='custom')
+ offset = url_for(controller='feed', action='custom')
params = {
'q': 'frequency:weekly'
}
+ app = self._get_test_app()
res = app.get(offset, params=params)
assert '{0} '.format(dataset1['title']) in res.body
diff --git a/ckan/tests/controllers/test_group.py b/ckan/tests/controllers/test_group.py
index 0c92b30d970..a23b38ded3c 100644
--- a/ckan/tests/controllers/test_group.py
+++ b/ckan/tests/controllers/test_group.py
@@ -3,7 +3,7 @@
from bs4 import BeautifulSoup
from nose.tools import assert_equal, assert_true, assert_in
-from ckan.lib.helpers import url_for
+from routes import url_for
import ckan.tests.helpers as helpers
import ckan.model as model
@@ -19,22 +19,17 @@ def setup(self):
model.repo.rebuild_db()
def test_bulk_process_throws_404_for_nonexistent_org(self):
-
app = self._get_test_app()
- with app.flask_app.test_request_context():
- bulk_process_url = url_for(
- controller='organization', action='bulk_process',
- id='does-not-exist')
+ bulk_process_url = url_for(controller='organization',
+ action='bulk_process', id='does-not-exist')
app.get(url=bulk_process_url, status=404)
def test_page_thru_list_of_orgs_preserves_sort_order(self):
orgs = [factories.Organization() for _ in range(35)]
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- org_url = url_for(controller='organization',
- action='index',
- sort='name desc')
+ org_url = url_for(controller='organization',
+ action='index',
+ sort='name desc')
response = app.get(url=org_url)
assert orgs[-1]['name'] in response
assert orgs[0]['name'] not in response
@@ -46,11 +41,9 @@ def test_page_thru_list_of_orgs_preserves_sort_order(self):
def test_page_thru_list_of_groups_preserves_sort_order(self):
groups = [factories.Group() for _ in range(35)]
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- group_url = url_for(controller='group',
- action='index',
- sort='title desc')
+ group_url = url_for(controller='group',
+ action='index',
+ sort='title desc')
response = app.get(url=group_url)
assert groups[-1]['title'] in response
@@ -80,11 +73,8 @@ def test_invalid_sort_param_does_not_crash(self):
def _get_group_new_page(app):
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='group', action='new')
response = app.get(
- url=url,
+ url=url_for(controller='group', action='new'),
extra_environ=env,
)
return env, response
@@ -93,9 +83,7 @@ def _get_group_new_page(app):
class TestGroupControllerNew(helpers.FunctionalTestBase):
def test_not_logged_in(self):
app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='group', action='new')
- app.get(url=url,
+ app.get(url=url_for(controller='group', action='new'),
status=403)
def test_form_renders(self):
@@ -145,11 +133,9 @@ def _get_group_edit_page(app, group_name=None):
group = factories.Group(user=user)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='group',
- action='edit',
- id=group_name)
+ url = url_for(controller='group',
+ action='edit',
+ id=group_name)
response = app.get(url=url, extra_environ=env)
return env, response, group_name
@@ -157,21 +143,16 @@ def _get_group_edit_page(app, group_name=None):
class TestGroupControllerEdit(helpers.FunctionalTestBase):
def test_not_logged_in(self):
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='group', action='new')
- app.get(url=url,
+ app.get(url=url_for(controller='group', action='new'),
status=403)
def test_group_doesnt_exist(self):
app = self._get_test_app()
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='group',
- action='edit',
- id='doesnt_exist')
+ url = url_for(controller='group',
+ action='edit',
+ id='doesnt_exist')
app.get(url=url, extra_environ=env,
status=404)
@@ -208,19 +189,17 @@ def test_all_fields_saved(self):
class TestGroupRead(helpers.FunctionalTestBase):
def setup(self):
super(TestGroupRead, self).setup()
+ self.app = helpers._get_test_app()
self.user = factories.User()
self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')}
self.group = factories.Group(user=self.user)
def test_group_read(self):
-
- app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='group', action='read',
- id=self.group['id'])
- response = app.get(url=url,
- status=200,
- extra_environ=self.user_env)
+ response = self.app.get(url=url_for(controller='group',
+ action='read',
+ id=self.group['id']),
+ status=200,
+ extra_environ=self.user_env)
assert_in(self.group['title'], response)
assert_in(self.group['description'], response)
@@ -234,69 +213,53 @@ def setup(self):
self.group = factories.Group(user=self.user)
def test_owner_delete(self):
-
- app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='group', action='delete',
- id=self.group['id'])
-
- response = app.get(url=url,
- status=200,
- extra_environ=self.user_env)
+ response = self.app.get(url=url_for(controller='group',
+ action='delete',
+ id=self.group['id']),
+ status=200,
+ extra_environ=self.user_env)
form = response.forms['group-confirm-delete-form']
- response = submit_and_follow(app, form, name='delete',
+ response = submit_and_follow(self.app, form, name='delete',
extra_environ=self.user_env)
group = helpers.call_action('group_show',
id=self.group['id'])
assert_equal(group['state'], 'deleted')
def test_sysadmin_delete(self):
-
- app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='group', action='delete',
- id=self.group['id'])
-
sysadmin = factories.Sysadmin()
extra_environ = {'REMOTE_USER': sysadmin['name'].encode('ascii')}
- response = app.get(url=url,
- status=200,
- extra_environ=extra_environ)
+ response = self.app.get(url=url_for(controller='group',
+ action='delete',
+ id=self.group['id']),
+ status=200,
+ extra_environ=extra_environ)
form = response.forms['group-confirm-delete-form']
- response = submit_and_follow(app, form, name='delete',
+ response = submit_and_follow(self.app, form, name='delete',
extra_environ=self.user_env)
group = helpers.call_action('group_show',
id=self.group['id'])
assert_equal(group['state'], 'deleted')
def test_non_authorized_user_trying_to_delete_fails(self):
-
- app = self._get_test_app()
user = factories.User()
extra_environ = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='group', action='delete',
- id=self.group['id'])
-
- app.get(url=url,
- status=403,
- extra_environ=extra_environ)
+ self.app.get(url=url_for(controller='group',
+ action='delete',
+ id=self.group['id']),
+ status=403,
+ extra_environ=extra_environ)
group = helpers.call_action('group_show',
id=self.group['id'])
assert_equal(group['state'], 'active')
def test_anon_user_trying_to_delete_fails(self):
-
- app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='group', action='delete',
- id=self.group['id'])
-
- app.get(url=url,
- status=403)
+ self.app.get(url=url_for(controller='group',
+ action='delete',
+ id=self.group['id']),
+ status=403)
group = helpers.call_action('group_show',
id=self.group['id'])
@@ -317,11 +280,9 @@ def _create_group(self, owner_username, users=None):
def _get_group_add_member_page(self, app, user, group_name):
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='group',
- action='member_new',
- id=group_name)
+ url = url_for(controller='group',
+ action='member_new',
+ id=group_name)
response = app.get(url=url, extra_environ=env)
return env, response
@@ -337,14 +298,12 @@ def test_membership_list(self):
group = self._create_group(user_one['name'], other_users)
- with app.flask_app.test_request_context():
- member_list_url = url_for(controller='group', action='members',
- id=group['id'])
-
- env = {'REMOTE_USER': user_one['name'].encode('ascii')}
+ member_list_url = url_for(controller='group', action='members',
+ id=group['id'])
+ env = {'REMOTE_USER': user_one['name'].encode('ascii')}
- member_list_response = app.get(
- member_list_url, extra_environ=env)
+ member_list_response = app.get(
+ member_list_url, extra_environ=env)
assert_true('2 members' in member_list_response)
@@ -429,9 +388,8 @@ def test_remove_member(self):
group = self._create_group(user_one['name'], other_users)
- with app.flask_app.test_request_context():
- remove_url = url_for(controller='group', action='member_delete',
- user=user_two['id'], id=group['id'])
+ remove_url = url_for(controller='group', action='member_delete',
+ user=user_two['id'], id=group['id'])
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
remove_response = app.post(remove_url, extra_environ=env, status=302)
@@ -521,11 +479,9 @@ def test_group_follow(self):
group = factories.Group()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- follow_url = url_for(controller='group',
- action='follow',
- id=group['id'])
+ follow_url = url_for(controller='group',
+ action='follow',
+ id=group['id'])
response = app.post(follow_url, extra_environ=env, status=302)
response = response.follow()
assert_true('You are now following {0}'
@@ -539,11 +495,9 @@ def test_group_follow_not_exist(self):
user_one = factories.User()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- follow_url = url_for(controller='group',
- action='follow',
- id='not-here')
+ follow_url = url_for(controller='group',
+ action='follow',
+ id='not-here')
response = app.post(follow_url, extra_environ=env, status=404)
assert_true('Group not found' in response)
@@ -554,16 +508,13 @@ def test_group_unfollow(self):
group = factories.Group()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- follow_url = url_for(controller='group',
- action='follow',
- id=group['id'])
- unfollow_url = url_for(controller='group', action='unfollow',
- id=group['id'])
-
+ follow_url = url_for(controller='group',
+ action='follow',
+ id=group['id'])
app.post(follow_url, extra_environ=env, status=302)
+ unfollow_url = url_for(controller='group', action='unfollow',
+ id=group['id'])
unfollow_response = app.post(unfollow_url, extra_environ=env,
status=302)
unfollow_response = unfollow_response.follow()
@@ -580,10 +531,8 @@ def test_group_unfollow_not_following(self):
group = factories.Group()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- unfollow_url = url_for(controller='group', action='unfollow',
- id=group['id'])
+ unfollow_url = url_for(controller='group', action='unfollow',
+ id=group['id'])
unfollow_response = app.post(unfollow_url, extra_environ=env,
status=302)
unfollow_response = unfollow_response.follow()
@@ -598,10 +547,8 @@ def test_group_unfollow_not_exist(self):
user_one = factories.User()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- unfollow_url = url_for(controller='group', action='unfollow',
- id='not-here')
+ unfollow_url = url_for(controller='group', action='unfollow',
+ id='not-here')
unfollow_response = app.post(unfollow_url, extra_environ=env,
status=404)
assert_true('Group not found' in unfollow_response)
@@ -614,16 +561,14 @@ def test_group_follower_list(self):
group = factories.Group()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- follow_url = url_for(controller='group',
- action='follow',
- id=group['id'])
- followers_url = url_for(controller='group', action='followers',
- id=group['id'])
-
+ follow_url = url_for(controller='group',
+ action='follow',
+ id=group['id'])
app.post(follow_url, extra_environ=env, status=302)
+ followers_url = url_for(controller='group', action='followers',
+ id=group['id'])
+
# Only sysadmins can view the followers list pages
followers_response = app.get(followers_url, extra_environ=env,
status=200)
@@ -636,20 +581,17 @@ class TestGroupSearch(helpers.FunctionalTestBase):
def setup(self):
super(TestGroupSearch, self).setup()
+ self.app = self._get_test_app()
factories.Group(name='grp-one', title='AGrp One')
factories.Group(name='grp-two', title='AGrp Two')
factories.Group(name='grp-three', title='Grp Three')
-
- app = self._get_test_app()
- with app.flask_app.test_request_context():
- self.search_url = url_for(controller='group', action='index')
+ self.search_url = url_for(controller='group', action='index')
def test_group_search(self):
'''Requesting group search (index) returns list of groups and search
form.'''
- app = self._get_test_app()
- index_response = app.get(self.search_url)
+ index_response = self.app.get(self.search_url)
index_response_html = BeautifulSoup(index_response.body)
grp_names = index_response_html.select('ul.media-grid '
'li.media-item '
@@ -664,8 +606,7 @@ def test_group_search(self):
def test_group_search_results(self):
'''Searching via group search form returns list of expected groups.'''
- app = self._get_test_app()
- index_response = app.get(self.search_url)
+ index_response = self.app.get(self.search_url)
search_form = index_response.forms['group-search-form']
search_form['q'] = 'AGrp'
search_response = webtest_submit(search_form)
@@ -684,8 +625,7 @@ def test_group_search_results(self):
def test_group_search_no_results(self):
'''Searching with a term that doesn't apply returns no results.'''
- app = self._get_test_app()
- index_response = app.get(self.search_url)
+ index_response = self.app.get(self.search_url)
search_form = index_response.forms['group-search-form']
search_form['q'] = 'No Results Here'
search_response = webtest_submit(search_form)
@@ -717,9 +657,8 @@ def test_group_search_within_org(self):
factories.Dataset(name="ds-three", title="Dataset Three",
groups=[{'id': grp['id']}])
- with app.flask_app.test_request_context():
- grp_url = url_for(controller='group', action='read',
- id=grp['id'])
+ grp_url = url_for(controller='group', action='read',
+ id=grp['id'])
grp_response = app.get(grp_url)
grp_response_html = BeautifulSoup(grp_response.body)
@@ -746,9 +685,8 @@ def test_group_search_within_org_results(self):
factories.Dataset(name="ds-three", title="Dataset Three",
groups=[{'id': grp['id']}])
- with app.flask_app.test_request_context():
- grp_url = url_for(controller='group', action='read',
- id=grp['id'])
+ grp_url = url_for(controller='group', action='read',
+ id=grp['id'])
grp_response = app.get(grp_url)
search_form = grp_response.forms['group-datasets-search-form']
search_form['q'] = 'One'
@@ -780,9 +718,8 @@ def test_group_search_within_org_no_results(self):
factories.Dataset(name="ds-three", title="Dataset Three",
groups=[{'id': grp['id']}])
- with app.flask_app.test_request_context():
- grp_url = url_for(controller='group', action='read',
- id=grp['id'])
+ grp_url = url_for(controller='group', action='read',
+ id=grp['id'])
grp_response = app.get(grp_url)
search_form = grp_response.forms['group-datasets-search-form']
search_form['q'] = 'Nout'
@@ -811,9 +748,8 @@ def test_group_index(self):
name='test-group-{0}'.format(_i),
title='Test Group {0}'.format(_i))
- with app.flask_app.test_request_context():
- url = url_for(controller='group',
- action='index')
+ url = url_for(controller='group',
+ action='index')
response = app.get(url)
for i in xrange(1, 22):
@@ -822,10 +758,9 @@ def test_group_index(self):
assert 'Test Group 22' not in response
- with app.flask_app.test_request_context():
- url = url_for(controller='group',
- action='index',
- page=1)
+ url = url_for(controller='group',
+ action='index',
+ page=1)
response = app.get(url)
for i in xrange(1, 22):
@@ -834,10 +769,9 @@ def test_group_index(self):
assert 'Test Group 22' not in response
- with app.flask_app.test_request_context():
- url = url_for(controller='group',
- action='index',
- page=2)
+ url = url_for(controller='group',
+ action='index',
+ page=2)
response = app.get(url)
for i in xrange(22, 26):
diff --git a/ckan/tests/controllers/test_home.py b/ckan/tests/controllers/test_home.py
index 34c02739840..44731868635 100644
--- a/ckan/tests/controllers/test_home.py
+++ b/ckan/tests/controllers/test_home.py
@@ -12,11 +12,7 @@ class TestHome(helpers.FunctionalTestBase):
def test_home_renders(self):
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for('home')
-
- response = app.get(url)
+ response = app.get(url_for('home'))
assert 'Welcome to CKAN' in response.body
def test_template_head_end(self):
@@ -24,22 +20,14 @@ def test_template_head_end(self):
# test-core.ini sets ckan.template_head_end to this:
test_link = ' '
-
- with app.flask_app.test_request_context():
- url = url_for('home')
-
- response = app.get(url)
+ response = app.get(url_for('home'))
assert test_link in response.body
def test_template_footer_end(self):
app = self._get_test_app()
# test-core.ini sets ckan.template_footer_end to this:
test_html = 'TEST TEMPLATE_FOOTER_END TEST '
-
- with app.flask_app.test_request_context():
- url = url_for('home')
-
- response = app.get(url)
+ response = app.get(url_for('home'))
assert test_html in response.body
def test_email_address_nag(self):
@@ -53,14 +41,10 @@ def test_email_address_nag(self):
model.Session.commit()
env = {'REMOTE_USER': user.name.encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for('home')
- user_edit_url = url_for(controller='user', action='edit')
-
- response = app.get(url, extra_environ=env)
+ response = app.get(url=url_for('home'), extra_environ=env)
assert 'update your profile' in response.body
- assert user_edit_url in response.body
+ assert url_for(controller='user', action='edit') in response.body
assert ' and add your email address.' in response.body
def test_email_address_no_nag(self):
@@ -68,10 +52,7 @@ def test_email_address_no_nag(self):
user = factories.User(email='filled_in@nicely.com')
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for('home')
-
- response = app.get(url, extra_environ=env)
+ response = app.get(url=url_for('home'), extra_environ=env)
assert 'add your email address' not in response
@@ -80,11 +61,7 @@ class TestI18nURLs(helpers.FunctionalTestBase):
def test_right_urls_are_rendered_on_language_selector(self):
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for('home')
-
- response = app.get(url)
+ response = app.get(url_for('home'))
html = BeautifulSoup(response.body)
select = html.find(id='field-lang-select')
@@ -100,11 +77,7 @@ def test_right_urls_are_rendered_on_language_selector(self):
def test_default_english_option_is_selected_on_language_selector(self):
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for('home')
-
- response = app.get(url)
+ response = app.get(url_for('home'))
html = BeautifulSoup(response.body)
select = html.find(id='field-lang-select')
@@ -116,11 +89,7 @@ def test_default_english_option_is_selected_on_language_selector(self):
def test_right_option_is_selected_on_language_selector(self):
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for('home', locale='ca')
-
- response = app.get(url)
+ response = app.get(url_for('home', locale='ca'))
html = BeautifulSoup(response.body)
select = html.find(id='field-lang-select')
diff --git a/ckan/tests/controllers/test_organization.py b/ckan/tests/controllers/test_organization.py
index f5c82ec7a12..1b0b66d1e0a 100644
--- a/ckan/tests/controllers/test_organization.py
+++ b/ckan/tests/controllers/test_organization.py
@@ -1,8 +1,8 @@
# encoding: utf-8
from bs4 import BeautifulSoup
-from ckan.lib.helpers import url_for
from nose.tools import assert_equal, assert_true, assert_in
+from routes import url_for
from mock import patch
from ckan.tests import factories, helpers
@@ -12,29 +12,19 @@
class TestOrganizationNew(helpers.FunctionalTestBase):
def setup(self):
super(TestOrganizationNew, self).setup()
-
+ self.app = helpers._get_test_app()
self.user = factories.User()
self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')}
-
- app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- self.organization_new_url = url_for(controller='organization',
- action='new')
+ self.organization_new_url = url_for(controller='organization',
+ action='new')
def test_not_logged_in(self):
-
- app = helpers._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='group', action='new')
- app.get(url, status=403)
+ self.app.get(url=url_for(controller='group', action='new'),
+ status=403)
def test_name_required(self):
-
- app = helpers._get_test_app()
-
- response = app.get(url=self.organization_new_url,
- extra_environ=self.user_env)
+ response = self.app.get(url=self.organization_new_url,
+ extra_environ=self.user_env)
form = response.forms['organization-edit-form']
response = webtest_submit(form, name='save',
extra_environ=self.user_env)
@@ -43,16 +33,13 @@ def test_name_required(self):
assert_true('Name: Missing value' in response)
def test_saved(self):
-
- app = helpers._get_test_app()
-
- response = app.get(url=self.organization_new_url,
- extra_environ=self.user_env)
+ response = self.app.get(url=self.organization_new_url,
+ extra_environ=self.user_env)
form = response.forms['organization-edit-form']
form['name'] = u'saved'
- response = submit_and_follow(app, form, name='save',
+ response = submit_and_follow(self.app, form, name='save',
extra_environ=self.user_env)
group = helpers.call_action('organization_show', id='saved')
assert_equal(group['title'], u'')
@@ -60,7 +47,6 @@ def test_saved(self):
assert_equal(group['state'], 'active')
def test_all_fields_saved(self):
-
app = helpers._get_test_app()
response = app.get(url=self.organization_new_url,
extra_environ=self.user_env)
@@ -71,7 +57,7 @@ def test_all_fields_saved(self):
form['description'] = 'Sciencey datasets'
form['image_url'] = 'http://example.com/image.png'
- response = submit_and_follow(app, form, name='save',
+ response = submit_and_follow(self.app, form, name='save',
extra_environ=self.user_env)
group = helpers.call_action('organization_show', id='all-fields-saved')
assert_equal(group['title'], u'Science')
@@ -81,44 +67,33 @@ def test_all_fields_saved(self):
class TestOrganizationList(helpers.FunctionalTestBase):
def setup(self):
super(TestOrganizationList, self).setup()
-
+ self.app = helpers._get_test_app()
self.user = factories.User()
self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')}
- app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- self.organization_list_url = url_for(controller='organization',
- action='index')
-
- @patch('ckan.logic.auth.get.organization_list',
- return_value={'success': False})
- def test_error_message_shown_when_no_organization_list_permission(
- self, mock_check_access):
-
- app = helpers._get_test_app()
+ self.organization_list_url = url_for(controller='organization',
+ action='index')
- app.get(url=self.organization_list_url,
- extra_environ=self.user_env,
- status=403)
+ @patch('ckan.logic.auth.get.organization_list', return_value={'success': False})
+ def test_error_message_shown_when_no_organization_list_permission(self, mock_check_access):
+ response = self.app.get(url=self.organization_list_url,
+ extra_environ=self.user_env,
+ status=403)
class TestOrganizationRead(helpers.FunctionalTestBase):
def setup(self):
super(TestOrganizationRead, self).setup()
-
+ self.app = helpers._get_test_app()
self.user = factories.User()
self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')}
self.organization = factories.Organization(user=self.user)
def test_organization_read(self):
-
- app = helpers._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='organization', action='read',
- id=self.organization['id'])
- response = app.get(url=url,
- status=200,
- extra_environ=self.user_env)
+ response = self.app.get(url=url_for(controller='organization',
+ action='read',
+ id=self.organization['id']),
+ status=200,
+ extra_environ=self.user_env)
assert_in(self.organization['title'], response)
assert_in(self.organization['description'], response)
@@ -126,33 +101,24 @@ def test_organization_read(self):
class TestOrganizationEdit(helpers.FunctionalTestBase):
def setup(self):
super(TestOrganizationEdit, self).setup()
-
+ self.app = helpers._get_test_app()
self.user = factories.User()
self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')}
self.organization = factories.Organization(user=self.user)
- app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- self.organization_edit_url = url_for(controller='organization',
- action='edit',
- id=self.organization['id'])
+ self.organization_edit_url = url_for(controller='organization',
+ action='edit',
+ id=self.organization['id'])
def test_group_doesnt_exist(self):
-
- app = helpers._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='organization',
- action='edit',
- id='doesnt_exist')
- app.get(url=url, extra_environ=self.user_env,
- status=404)
+ url = url_for(controller='organization',
+ action='edit',
+ id='doesnt_exist')
+ self.app.get(url=url, extra_environ=self.user_env,
+ status=404)
def test_saved(self):
-
- app = helpers._get_test_app()
-
- response = app.get(url=self.organization_edit_url,
- extra_environ=self.user_env)
+ response = self.app.get(url=self.organization_edit_url,
+ extra_environ=self.user_env)
form = response.forms['organization-edit-form']
response = webtest_submit(form, name='save',
@@ -164,11 +130,8 @@ def test_saved(self):
assert_equal(group['state'], 'active')
def test_all_fields_saved(self):
-
- app = helpers._get_test_app()
-
- response = app.get(url=self.organization_edit_url,
- extra_environ=self.user_env)
+ response = self.app.get(url=self.organization_edit_url,
+ extra_environ=self.user_env)
form = response.forms['organization-edit-form']
form['name'] = u'all-fields-edited'
@@ -188,79 +151,59 @@ def test_all_fields_saved(self):
class TestOrganizationDelete(helpers.FunctionalTestBase):
def setup(self):
super(TestOrganizationDelete, self).setup()
-
+ self.app = helpers._get_test_app()
self.user = factories.User()
self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')}
self.organization = factories.Organization(user=self.user)
def test_owner_delete(self):
-
- app = helpers._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='organization',
- action='delete',
- id=self.organization['id'])
- response = app.get(url, status=200, extra_environ=self.user_env)
+ response = self.app.get(url=url_for(controller='organization',
+ action='delete',
+ id=self.organization['id']),
+ status=200,
+ extra_environ=self.user_env)
form = response.forms['organization-confirm-delete-form']
- response = submit_and_follow(app, form, name='delete',
+ response = submit_and_follow(self.app, form, name='delete',
extra_environ=self.user_env)
organization = helpers.call_action('organization_show',
id=self.organization['id'])
assert_equal(organization['state'], 'deleted')
def test_sysadmin_delete(self):
-
- app = helpers._get_test_app()
-
sysadmin = factories.Sysadmin()
extra_environ = {'REMOTE_USER': sysadmin['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='organization',
- action='delete',
- id=self.organization['id'])
-
- response = app.get(url=url,
- status=200,
- extra_environ=extra_environ)
+ response = self.app.get(url=url_for(controller='organization',
+ action='delete',
+ id=self.organization['id']),
+ status=200,
+ extra_environ=extra_environ)
form = response.forms['organization-confirm-delete-form']
- response = submit_and_follow(app, form, name='delete',
+ response = submit_and_follow(self.app, form, name='delete',
extra_environ=self.user_env)
organization = helpers.call_action('organization_show',
id=self.organization['id'])
assert_equal(organization['state'], 'deleted')
def test_non_authorized_user_trying_to_delete_fails(self):
-
- app = helpers._get_test_app()
-
user = factories.User()
extra_environ = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='organization',
- action='delete',
- id=self.organization['id'])
-
- app.get(url=url,
- status=403,
- extra_environ=extra_environ)
+ self.app.get(url=url_for(controller='organization',
+ action='delete',
+ id=self.organization['id']),
+ status=403,
+ extra_environ=extra_environ)
organization = helpers.call_action('organization_show',
id=self.organization['id'])
assert_equal(organization['state'], 'active')
def test_anon_user_trying_to_delete_fails(self):
-
- app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='organization',
- action='delete',
- id=self.organization['id'])
-
- app.get(url=url,
- status=403)
+ self.app.get(url=url_for(controller='organization',
+ action='delete',
+ id=self.organization['id']),
+ status=403)
organization = helpers.call_action('organization_show',
id=self.organization['id'])
@@ -270,24 +213,19 @@ def test_anon_user_trying_to_delete_fails(self):
class TestOrganizationBulkProcess(helpers.FunctionalTestBase):
def setup(self):
super(TestOrganizationBulkProcess, self).setup()
-
+ self.app = helpers._get_test_app()
self.user = factories.User()
self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')}
self.organization = factories.Organization(user=self.user)
- app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- self.organization_bulk_url = url_for(controller='organization',
- action='bulk_process',
- id=self.organization['id'])
+ self.organization_bulk_url = url_for(controller='organization',
+ action='bulk_process',
+ id=self.organization['id'])
def test_make_private(self):
-
- app = helpers._get_test_app()
-
datasets = [factories.Dataset(owner_org=self.organization['id'])
for i in range(0, 5)]
- response = app.get(url=self.organization_bulk_url,
- extra_environ=self.user_env)
+ response = self.app.get(url=self.organization_bulk_url,
+ extra_environ=self.user_env)
form = response.forms[1]
for v in form.fields.values():
try:
@@ -303,14 +241,11 @@ def test_make_private(self):
assert_equal(d['private'], True)
def test_make_public(self):
-
- app = helpers._get_test_app()
-
datasets = [factories.Dataset(owner_org=self.organization['id'],
private=True)
for i in range(0, 5)]
- response = app.get(url=self.organization_bulk_url,
- extra_environ=self.user_env)
+ response = self.app.get(url=self.organization_bulk_url,
+ extra_environ=self.user_env)
form = response.forms[1]
for v in form.fields.values():
try:
@@ -326,14 +261,11 @@ def test_make_public(self):
assert_equal(d['private'], False)
def test_delete(self):
-
- app = helpers._get_test_app()
-
datasets = [factories.Dataset(owner_org=self.organization['id'],
private=True)
for i in range(0, 5)]
- response = app.get(url=self.organization_bulk_url,
- extra_environ=self.user_env)
+ response = self.app.get(url=self.organization_bulk_url,
+ extra_environ=self.user_env)
form = response.forms[1]
for v in form.fields.values():
try:
@@ -355,23 +287,17 @@ class TestOrganizationSearch(helpers.FunctionalTestBase):
def setup(self):
super(TestOrganizationSearch, self).setup()
+ self.app = self._get_test_app()
factories.Organization(name='org-one', title='AOrg One')
factories.Organization(name='org-two', title='AOrg Two')
factories.Organization(name='org-three', title='Org Three')
-
- app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- self.search_url = url_for(controller='organization',
- action='index')
+ self.search_url = url_for(controller='organization', action='index')
def test_organization_search(self):
-
- app = helpers._get_test_app()
-
'''Requesting organization search (index) returns list of
organizations and search form.'''
- index_response = app.get(self.search_url)
+ index_response = self.app.get(self.search_url)
index_response_html = BeautifulSoup(index_response.body)
org_names = index_response_html.select('ul.media-grid '
'li.media-item '
@@ -384,13 +310,10 @@ def test_organization_search(self):
assert_true('Org Three' in org_names)
def test_organization_search_results(self):
-
- app = helpers._get_test_app()
-
'''Searching via organization search form returns list of expected
organizations.'''
- index_response = app.get(self.search_url)
+ index_response = self.app.get(self.search_url)
search_form = index_response.forms['organization-search-form']
search_form['q'] = 'AOrg'
search_response = webtest_submit(search_form)
@@ -407,12 +330,9 @@ def test_organization_search_results(self):
assert_true('Org Three' not in org_names)
def test_organization_search_no_results(self):
-
- app = helpers._get_test_app()
-
'''Searching with a term that doesn't apply returns no results.'''
- index_response = app.get(self.search_url)
+ index_response = self.app.get(self.search_url)
search_form = index_response.forms['organization-search-form']
search_form['q'] = 'No Results Here'
search_response = webtest_submit(search_form)
@@ -445,9 +365,8 @@ def test_organization_search_within_org(self):
factories.Dataset(name="ds-three", title="Dataset Three",
owner_org=org['id'])
- with app.flask_app.test_request_context():
- org_url = url_for(controller='organization', action='read',
- id=org['id'])
+ org_url = url_for(controller='organization', action='read',
+ id=org['id'])
org_response = app.get(org_url)
org_response_html = BeautifulSoup(org_response.body)
@@ -475,9 +394,8 @@ def test_organization_search_within_org_results(self):
factories.Dataset(name="ds-three", title="Dataset Three",
owner_org=org['id'])
- with app.flask_app.test_request_context():
- org_url = url_for(controller='organization', action='read',
- id=org['id'])
+ org_url = url_for(controller='organization', action='read',
+ id=org['id'])
org_response = app.get(org_url)
search_form = org_response.forms['organization-datasets-search-form']
search_form['q'] = 'One'
@@ -509,9 +427,8 @@ def test_organization_search_within_org_no_results(self):
factories.Dataset(name="ds-three", title="Dataset Three",
owner_org=org['id'])
- with app.flask_app.test_request_context():
- org_url = url_for(controller='organization', action='read',
- id=org['id'])
+ org_url = url_for(controller='organization', action='read',
+ id=org['id'])
org_response = app.get(org_url)
search_form = org_response.forms['organization-datasets-search-form']
search_form['q'] = 'Nout'
diff --git a/ckan/tests/controllers/test_package.py b/ckan/tests/controllers/test_package.py
index 5421c605d70..b527ba7c34a 100644
--- a/ckan/tests/controllers/test_package.py
+++ b/ckan/tests/controllers/test_package.py
@@ -9,10 +9,12 @@
assert_in
)
-from ckan.lib.helpers import url_for
+from mock import patch, MagicMock
+from routes import url_for
import ckan.model as model
import ckan.plugins as p
+from ckan.lib import search
import ckan.tests.helpers as helpers
import ckan.tests.factories as factories
@@ -25,10 +27,8 @@
def _get_package_new_page(app):
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='new')
response = app.get(
- url=url,
+ url=url_for(controller='package', action='new'),
extra_environ=env,
)
return env, response
@@ -50,18 +50,12 @@ def test_needs_organization_but_no_organizations_has_button(self):
sysadmin = factories.Sysadmin()
env = {'REMOTE_USER': sysadmin['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='new')
-
response = app.get(
- url=url,
+ url=url_for(controller='package', action='new'),
extra_environ=env
)
assert 'dataset-edit' not in response.forms
-
- with app.flask_app.test_request_context():
- assert url_for(controller='organization', action='new') in response
+ assert url_for(controller='organization', action='new') in response
@helpers.mock_auth('ckan.logic.auth.create.package_create')
@helpers.change_config('ckan.auth.create_unowned_dataset', 'false')
@@ -79,19 +73,13 @@ def test_needs_organization_but_no_organizations_no_button(self,
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='new')
-
response = app.get(
- url=url,
+ url=url_for(controller='package', action='new'),
extra_environ=env
)
assert 'dataset-edit' not in response.forms
-
- with app.flask_app.test_request_context():
- assert url_for(controller='organization', action='new') not in response
+ assert url_for(controller='organization', action='new') not in response
assert 'Ask a system administrator' in response
def test_name_required(self):
@@ -252,11 +240,8 @@ def test_dataset_edit_org_dropdown_visible_to_normal_user_with_orgs_available(se
app = self._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='new')
response = app.get(
- url=url,
+ url=url_for(controller='package', action='new'),
extra_environ=env,
)
@@ -277,11 +262,9 @@ def test_dataset_edit_org_dropdown_visible_to_normal_user_with_orgs_available(se
assert_equal(pkg.state, 'active')
# edit package page response
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='edit',
- id=pkg.id)
+ url = url_for(controller='package',
+ action='edit',
+ id=pkg.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']
@@ -302,10 +285,8 @@ def test_dataset_edit_org_dropdown_normal_user_can_remove_org(self):
app = self._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='new')
response = app.get(
- url=url,
+ url=url_for(controller='package', action='new'),
extra_environ=env,
)
@@ -325,10 +306,9 @@ def test_dataset_edit_org_dropdown_normal_user_can_remove_org(self):
assert_not_equal(pkg.owner_org, None)
# edit package page response
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='edit',
- id=pkg.id)
+ url = url_for(controller='package',
+ action='edit',
+ id=pkg.id)
pkg_edit_response = app.get(url=url, extra_environ=env)
# edit dataset
@@ -351,11 +331,8 @@ def test_dataset_edit_org_dropdown_not_visible_to_normal_user_with_no_orgs_avail
app = self._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='new')
response = app.get(
- url=url,
+ url=url_for(controller='package', action='new'),
extra_environ=env,
)
@@ -375,10 +352,9 @@ def test_dataset_edit_org_dropdown_not_visible_to_normal_user_with_no_orgs_avail
assert_equal(pkg.state, 'active')
# edit package response
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='edit',
- id=model.Package.by_name(u'my-dataset').id)
+ 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']
@@ -400,10 +376,8 @@ def test_dataset_edit_org_dropdown_visible_to_sysadmin_with_no_orgs_available(se
app = self._get_test_app()
# user in env is sysadmin
env = {'REMOTE_USER': sysadmin['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='new')
response = app.get(
- url=url,
+ url=url_for(controller='package', action='new'),
extra_environ=env,
)
@@ -424,10 +398,9 @@ def test_dataset_edit_org_dropdown_visible_to_sysadmin_with_no_orgs_available(se
assert_equal(pkg.state, 'active')
# edit package page response
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='edit',
- id=pkg.id)
+ url = url_for(controller='package',
+ action='edit',
+ id=pkg.id)
pkg_edit_response = app.get(url=url, extra_environ=env)
# A field with the correct id is in the response
assert 'id="field-organizations"' in pkg_edit_response
@@ -437,14 +410,11 @@ def test_dataset_edit_org_dropdown_visible_to_sysadmin_with_no_orgs_available(se
def test_unauthed_user_creating_dataset(self):
app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='new')
# provide REMOTE_ADDR to idenfity as remote user, see
# ckan.views.identify_user() for details
-
- app.post(url=url,
- extra_environ={'REMOTE_ADDR': '127.0.0.1'},
- status=403)
+ response = app.post(url=url_for(controller='package', action='new'),
+ extra_environ={'REMOTE_ADDR': '127.0.0.1'},
+ status=403)
class TestPackageEdit(helpers.FunctionalTestBase):
@@ -456,13 +426,10 @@ def test_organization_admin_can_edit(self):
dataset = factories.Dataset(owner_org=organization['id'])
app = helpers._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='edit',
- id=dataset['name'])
response = app.get(
- url,
+ url_for(controller='package',
+ action='edit',
+ id=dataset['name']),
extra_environ=env,
)
form = response.forms['dataset-edit']
@@ -480,12 +447,10 @@ def test_organization_editor_can_edit(self):
dataset = factories.Dataset(owner_org=organization['id'])
app = helpers._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='edit',
- id=dataset['name'])
response = app.get(
- url,
+ url_for(controller='package',
+ action='edit',
+ id=dataset['name']),
extra_environ=env,
)
form = response.forms['dataset-edit']
@@ -503,12 +468,10 @@ def test_organization_member_cannot_edit(self):
dataset = factories.Dataset(owner_org=organization['id'])
app = helpers._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='edit',
- id=dataset['name'])
- app.get(
- url,
+ response = app.get(
+ url_for(controller='package',
+ action='edit',
+ id=dataset['name']),
extra_environ=env,
status=403,
)
@@ -519,20 +482,19 @@ def test_user_not_in_organization_cannot_edit(self):
dataset = factories.Dataset(owner_org=organization['id'])
app = helpers._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='edit',
- id=dataset['name'])
- app.get(
- url,
+ response = app.get(
+ url_for(controller='package',
+ action='edit',
+ id=dataset['name']),
extra_environ=env,
status=403,
)
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- app.post(
- url,
+ response = app.post(
+ url_for(controller='package',
+ action='edit',
+ id=dataset['name']),
{'notes': 'edited description'},
extra_environ=env,
status=403,
@@ -542,17 +504,17 @@ def test_anonymous_user_cannot_edit(self):
organization = factories.Organization()
dataset = factories.Dataset(owner_org=organization['id'])
app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='edit',
- id=dataset['name'])
- app.get(
- url,
+ response = app.get(
+ url_for(controller='package',
+ action='edit',
+ id=dataset['name']),
status=403,
)
- app.post(
- url,
+ response = app.post(
+ url_for(controller='package',
+ action='edit',
+ id=dataset['name']),
{'notes': 'edited description'},
status=403,
)
@@ -566,13 +528,10 @@ def test_validation_errors_for_dataset_name_appear(self):
dataset = factories.Dataset(owner_org=organization['id'])
app = helpers._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='edit',
- id=dataset['name'])
-
response = app.get(
- url,
+ url_for(controller='package',
+ action='edit',
+ id=dataset['name']),
extra_environ=env,
)
form = response.forms['dataset-edit']
@@ -587,13 +546,10 @@ def test_edit_a_dataset_that_does_not_exist_404s(self):
user = factories.User()
app = helpers._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='edit',
- id='does-not-exist')
-
response = app.get(
- url,
+ url_for(controller='package',
+ action='edit',
+ id='does-not-exist'),
extra_environ=env,
expect_errors=True
)
@@ -604,11 +560,8 @@ class TestPackageRead(helpers.FunctionalTestBase):
def test_read(self):
dataset = factories.Dataset()
app = helpers._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='read',
- id=dataset['name'])
- response = app.get(url)
+ response = app.get(url_for(controller='package', action='read',
+ id=dataset['name']))
response.mustcontain('Test Dataset')
response.mustcontain('Just another test dataset')
@@ -632,13 +585,13 @@ def test_organization_members_can_read_private_datasets(self):
)
app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='read',
- id=dataset['name'])
for user, user_dict in members.items():
response = app.get(
- url,
+ url_for(
+ controller='package',
+ action='read',
+ id=dataset['name']
+ ),
extra_environ={
'REMOTE_USER': user_dict['name'].encode('ascii'),
},
@@ -653,13 +606,8 @@ def test_anonymous_users_cannot_read_private_datasets(self):
private=True,
)
app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='read',
- id=dataset['name'])
-
response = app.get(
- url,
+ url_for(controller='package', action='read', id=dataset['name']),
status=404
)
assert_equal(404, response.status_int)
@@ -672,13 +620,8 @@ def test_user_not_in_organization_cannot_read_private_datasets(self):
private=True,
)
app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='read',
- id=dataset['name'])
-
response = app.get(
- url,
+ url_for(controller='package', action='read', id=dataset['name']),
extra_environ={'REMOTE_USER': user['name'].encode('ascii')},
status=404
)
@@ -688,20 +631,18 @@ def test_read_rdf(self):
''' The RDF outputs now live in ckanext-dcat'''
dataset1 = factories.Dataset()
+ offset = url_for(controller='package', action='read',
+ id=dataset1['name']) + ".rdf"
app = self._get_test_app()
- with app.flask_app.test_request_context():
- offset = url_for(controller='package', action='read',
- id=dataset1['name']) + ".rdf"
app.get(offset, status=404)
def test_read_n3(self):
''' The RDF outputs now live in ckanext-dcat'''
dataset1 = factories.Dataset()
+ offset = url_for(controller='package', action='read',
+ id=dataset1['name']) + ".n3"
app = self._get_test_app()
- with app.flask_app.test_request_context():
- offset = url_for(controller='package', action='read',
- id=dataset1['name']) + ".n3"
app.get(offset, status=404)
@@ -715,12 +656,8 @@ def test_owner_delete(self):
app = helpers._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='delete', id=dataset['name'])
-
response = app.post(
- url,
+ url_for(controller='package', action='delete', id=dataset['name']),
extra_environ=env,
)
response = response.follow()
@@ -731,13 +668,9 @@ def test_owner_delete(self):
def test_delete_on_non_existing_dataset(self):
app = helpers._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='delete',
- id='schrodingersdatset')
-
response = app.post(
- url,
+ url_for(controller='package', action='delete',
+ id='schrodingersdatset'),
expect_errors=True,
)
assert_equal(404, response.status_int)
@@ -750,11 +683,8 @@ def test_sysadmin_can_delete_any_dataset(self):
user = factories.Sysadmin()
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='delete', id=dataset['name'])
-
response = app.post(
- url,
+ url_for(controller='package', action='delete', id=dataset['name']),
extra_environ=env,
)
response = response.follow()
@@ -771,12 +701,8 @@ def test_anon_user_cannot_delete_owned_dataset(self):
dataset = factories.Dataset(owner_org=owner_org['id'])
app = helpers._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='delete', id=dataset['name'])
-
response = app.post(
- url,
+ url_for(controller='package', action='delete', id=dataset['name']),
status=403,
)
response.mustcontain('Unauthorized to delete package')
@@ -794,12 +720,8 @@ def test_logged_in_user_cannot_delete_owned_dataset(self):
app = helpers._get_test_app()
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='delete', id=dataset['name'])
-
response = app.post(
- url,
+ url_for(controller='package', action='delete', id=dataset['name']),
extra_environ=env,
expect_errors=True
)
@@ -819,12 +741,8 @@ def test_confirm_cancel_delete(self):
app = helpers._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='delete', id=dataset['name'])
-
response = app.get(
- url,
+ url_for(controller='package', action='delete', id=dataset['name']),
extra_environ=env,
)
assert_equal(200, response.status_int)
@@ -848,16 +766,12 @@ def test_manage_dataset_resource_listing_page(self):
resource = factories.Resource(package_id=dataset['id'])
app = helpers._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(
+ response = app.get(
+ url_for(
controller='package',
action='resources',
id=dataset['name'],
- )
-
- response = app.get(
- url,
+ ),
extra_environ=env
)
assert_in(resource['name'], response)
@@ -871,16 +785,12 @@ def test_unauth_user_cannot_view_manage_dataset_resource_listing_page(self):
resource = factories.Resource(package_id=dataset['id'])
app = helpers._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(
+ response = app.get(
+ url_for(
controller='package',
action='resources',
id=dataset['name'],
- )
-
- response = app.get(
- url,
+ ),
extra_environ=env
)
assert_in(resource['name'], response)
@@ -891,16 +801,12 @@ def test_404_on_manage_dataset_resource_listing_page_that_does_not_exist(self):
user = factories.User()
app = helpers._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(
+ response = app.get(
+ url_for(
controller='package',
action='resources',
id='does-not-exist'
- )
-
- response = app.get(
- url,
+ ),
extra_environ=env,
expect_errors=True
)
@@ -912,15 +818,12 @@ def test_add_new_resource_with_link_and_download(self):
env = {'REMOTE_USER': user['name'].encode('ascii')}
app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(
+ response = app.get(
+ url_for(
controller='package',
action='new_resource',
id=dataset['id'],
- )
-
- response = app.get(
- url,
+ ),
extra_environ=env
)
@@ -930,17 +833,13 @@ def test_add_new_resource_with_link_and_download(self):
'go-dataset-complete')
result = helpers.call_action('package_show', id=dataset['id'])
-
- with app.flask_app.test_request_context():
- url = url_for(
+ response = app.get(
+ url_for(
controller='package',
action='resource_download',
id=dataset['id'],
resource_id=result['resources'][0]['id']
- )
-
- response = app.get(
- url,
+ ),
extra_environ=env,
)
assert_equal(302, response.status_int)
@@ -956,15 +855,12 @@ def test_editor_can_add_new_resource(self):
env = {'REMOTE_USER': user['name'].encode('ascii')}
app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(
+ response = app.get(
+ url_for(
controller='package',
action='new_resource',
id=dataset['id'],
- )
-
- response = app.get(
- url,
+ ),
extra_environ=env
)
@@ -989,15 +885,12 @@ def test_admin_can_add_new_resource(self):
env = {'REMOTE_USER': user['name'].encode('ascii')}
app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(
+ response = app.get(
+ url_for(
controller='package',
action='new_resource',
id=dataset['id'],
- )
-
- response = app.get(
- url,
+ ),
extra_environ=env
)
@@ -1022,28 +915,22 @@ def test_member_cannot_add_new_resource(self):
env = {'REMOTE_USER': user['name'].encode('ascii')}
app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(
+ response = app.get(
+ url_for(
controller='package',
action='new_resource',
id=dataset['id'],
- )
-
- response = app.get(
- url,
+ ),
extra_environ=env,
status=403,
)
- with app.flask_app.test_request_context():
- url = url_for(
+ response = app.post(
+ url_for(
controller='package',
action='new_resource',
id=dataset['id'],
- )
-
- response = app.post(
- url,
+ ),
{'name': 'test', 'url': 'test', 'save': 'save', 'id': ''},
extra_environ=env,
status=403,
@@ -1059,28 +946,22 @@ def test_non_organization_users_cannot_add_new_resource(self):
env = {'REMOTE_USER': user['name'].encode('ascii')}
app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(
+ response = app.get(
+ url_for(
controller='package',
action='new_resource',
id=dataset['id'],
- )
-
- response = app.get(
- url,
+ ),
extra_environ=env,
status=403,
)
- with app.flask_app.test_request_context():
- url = url_for(
+ response = app.post(
+ url_for(
controller='package',
action='new_resource',
id=dataset['id'],
- )
-
- response = app.post(
- url,
+ ),
{'name': 'test', 'url': 'test', 'save': 'save', 'id': ''},
extra_environ=env,
status=403,
@@ -1093,27 +974,21 @@ def test_anonymous_users_cannot_add_new_resource(self):
)
app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(
+ response = app.get(
+ url_for(
controller='package',
action='new_resource',
id=dataset['id'],
- )
-
- response = app.get(
- url,
+ ),
status=403,
)
- with app.flask_app.test_request_context():
- url = url_for(
+ response = app.post(
+ url_for(
controller='package',
action='new_resource',
id=dataset['id'],
- )
-
- response = app.post(
- url,
+ ),
{'name': 'test', 'url': 'test', 'save': 'save', 'id': ''},
status=403,
)
@@ -1166,41 +1041,35 @@ def teardown_class(cls):
def test_existent_resource_view_page_returns_ok_code(self):
resource_view = factories.ResourceView()
- app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='resource_read',
- id=resource_view['package_id'],
- resource_id=resource_view['resource_id'],
- view_id=resource_view['id'])
+ url = url_for(controller='package',
+ action='resource_read',
+ id=resource_view['package_id'],
+ resource_id=resource_view['resource_id'],
+ view_id=resource_view['id'])
+ app = self._get_test_app()
app.get(url, status=200)
def test_inexistent_resource_view_page_returns_not_found_code(self):
resource_view = factories.ResourceView()
- app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='resource_read',
- id=resource_view['package_id'],
- resource_id=resource_view['resource_id'],
- view_id='inexistent-view-id')
+ url = url_for(controller='package',
+ action='resource_read',
+ id=resource_view['package_id'],
+ resource_id=resource_view['resource_id'],
+ view_id='inexistent-view-id')
+ app = self._get_test_app()
app.get(url, status=404)
def test_resource_view_description_is_rendered_as_markdown(self):
resource_view = factories.ResourceView(description="Some **Markdown**")
-
+ url = url_for(controller='package',
+ action='resource_read',
+ id=resource_view['package_id'],
+ resource_id=resource_view['resource_id'],
+ view_id=resource_view['id'])
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='resource_read',
- id=resource_view['package_id'],
- resource_id=resource_view['resource_id'],
- view_id=resource_view['id'])
response = app.get(url)
response.mustcontain('Some Markdown ')
@@ -1211,12 +1080,12 @@ def test_existing_resource_with_not_associated_dataset(self):
dataset = factories.Dataset()
resource = factories.Resource()
+ url = url_for(controller='package',
+ action='resource_read',
+ id=dataset['id'],
+ resource_id=resource['id'])
+
app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='resource_read',
- id=dataset['id'],
- resource_id=resource['id'])
app.get(url, status=404)
def test_resource_read_logged_in_user(self):
@@ -1228,12 +1097,12 @@ def test_resource_read_logged_in_user(self):
dataset = factories.Dataset()
resource = factories.Resource(package_id=dataset['id'])
+ url = url_for(controller='package',
+ action='resource_read',
+ id=dataset['id'],
+ resource_id=resource['id'])
+
app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='resource_read',
- id=dataset['id'],
- resource_id=resource['id'])
app.get(url, status=200, extra_environ=env)
def test_resource_read_anon_user(self):
@@ -1243,12 +1112,12 @@ def test_resource_read_anon_user(self):
dataset = factories.Dataset()
resource = factories.Resource(package_id=dataset['id'])
+ url = url_for(controller='package',
+ action='resource_read',
+ id=dataset['id'],
+ resource_id=resource['id'])
+
app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='resource_read',
- id=dataset['id'],
- resource_id=resource['id'])
app.get(url, status=200)
def test_resource_read_sysadmin(self):
@@ -1260,12 +1129,12 @@ def test_resource_read_sysadmin(self):
dataset = factories.Dataset()
resource = factories.Resource(package_id=dataset['id'])
+ url = url_for(controller='package',
+ action='resource_read',
+ id=dataset['id'],
+ resource_id=resource['id'])
+
app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='resource_read',
- id=dataset['id'],
- resource_id=resource['id'])
app.get(url, status=200, extra_environ=env)
def test_user_not_in_organization_cannot_read_private_dataset(self):
@@ -1278,12 +1147,12 @@ def test_user_not_in_organization_cannot_read_private_dataset(self):
)
resource = factories.Resource(package_id=dataset['id'])
+ url = url_for(controller='package',
+ action='resource_read',
+ id=dataset['id'],
+ resource_id=resource['id'])
+
app = self._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='resource_read',
- id=dataset['id'],
- resource_id=resource['id'])
response = app.get(url,
status=404,
extra_environ=env)
@@ -1310,16 +1179,14 @@ def test_organization_members_can_read_resources_in_private_datasets(self):
app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- url = url_for(
- controller='package',
- action='resource_read',
- id=dataset['name'],
- resource_id=resource['id'],
- )
for user, user_dict in members.items():
response = app.get(
- url,
+ url_for(
+ controller='package',
+ action='resource_read',
+ id=dataset['name'],
+ resource_id=resource['id'],
+ ),
extra_environ={
'REMOTE_USER': user_dict['name'].encode('ascii'),
},
@@ -1333,12 +1200,8 @@ def test_anonymous_users_cannot_read_private_datasets(self):
private=True,
)
app = helpers._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='read', id=dataset['name'])
-
response = app.get(
- url,
+ url_for(controller='package', action='read', id=dataset['name']),
status=404
)
assert_equal(404, response.status_int)
@@ -1354,13 +1217,9 @@ def test_dataset_owners_can_delete_resources(self):
resource = factories.Resource(package_id=dataset['id'])
app = helpers._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='resource_delete',
- id=dataset['name'], resource_id=resource['id'])
-
response = app.post(
- url,
+ url_for(controller='package', action='resource_delete',
+ id=dataset['name'], resource_id=resource['id']),
extra_environ=env,
)
response = response.follow()
@@ -1378,13 +1237,9 @@ def test_deleting_non_existing_resource_404s(self):
dataset = factories.Dataset(owner_org=owner_org['id'])
env = {'REMOTE_USER': user['name'].encode('ascii')}
app = helpers._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='resource_delete',
- id=dataset['name'], resource_id='doesnotexist')
-
response = app.post(
- url,
+ url_for(controller='package', action='resource_delete',
+ id=dataset['name'], resource_id='doesnotexist'),
extra_environ=env,
expect_errors=True
)
@@ -1399,13 +1254,9 @@ def test_anon_users_cannot_delete_owned_resources(self):
resource = factories.Resource(package_id=dataset['id'])
app = helpers._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='resource_delete',
- id=dataset['name'], resource_id=resource['id'])
-
response = app.post(
- url,
+ url_for(controller='package', action='resource_delete',
+ id=dataset['name'], resource_id=resource['id']),
status=403,
)
response.mustcontain('Unauthorized to delete package')
@@ -1423,13 +1274,9 @@ def test_logged_in_users_cannot_delete_resources_they_do_not_own(self):
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
app = helpers._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='resource_delete',
- id=dataset['name'], resource_id=resource['id'])
-
response = app.post(
- url,
+ url_for(controller='package', action='resource_delete',
+ id=dataset['name'], resource_id=resource['id']),
extra_environ=env,
expect_errors=True
)
@@ -1444,13 +1291,9 @@ def test_sysadmins_can_delete_any_resource(self):
sysadmin = factories.Sysadmin()
app = helpers._get_test_app()
env = {'REMOTE_USER': sysadmin['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='resource_delete',
- id=dataset['name'], resource_id=resource['id'])
-
response = app.post(
- url,
+ url_for(controller='package', action='resource_delete',
+ id=dataset['name'], resource_id=resource['id']),
extra_environ=env,
)
response = response.follow()
@@ -1473,13 +1316,9 @@ def test_confirm_and_cancel_deleting_a_resource(self):
resource = factories.Resource(package_id=dataset['id'])
app = helpers._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='package', action='resource_delete',
- id=dataset['name'], resource_id=resource['id'])
-
response = app.get(
- url,
+ url_for(controller='package', action='resource_delete',
+ id=dataset['name'], resource_id=resource['id']),
extra_environ=env,
)
assert_equal(200, response.status_int)
@@ -1497,9 +1336,8 @@ class TestSearch(helpers.FunctionalTestBase):
def test_search_basic(self):
dataset1 = factories.Dataset()
+ offset = url_for(controller='package', action='search')
app = self._get_test_app()
- with app.flask_app.test_request_context():
- offset = url_for(controller='package', action='search')
page = app.get(offset)
assert dataset1['name'] in page.body.decode('utf8')
@@ -1520,10 +1358,8 @@ def test_search_sort_by_blank(self):
factories.Dataset()
# ?sort has caused an exception in the past
-
+ offset = url_for(controller='package', action='search') + '?sort'
app = self._get_test_app()
- with app.flask_app.test_request_context():
- offset = url_for(controller='package', action='search') + '?sort'
app.get(offset)
def test_search_sort_by_bad(self):
@@ -1532,11 +1368,9 @@ def test_search_sort_by_bad(self):
# bad spiders try all sorts of invalid values for sort. They should get
# a 400 error with specific error message. No need to alert the
# administrator.
-
+ offset = url_for(controller='package', action='search') + \
+ '?sort=gvgyr_fgevat+nfp'
app = self._get_test_app()
- with app.flask_app.test_request_context():
- offset = url_for(controller='package', action='search') + \
- '?sort=gvgyr_fgevat+nfp'
response = app.get(offset, status=[200, 400])
if response.status == 200:
import sys
@@ -1552,10 +1386,9 @@ def test_search_solr_syntax_error(self):
# Whilst this could be due to a bad user input, it could also be
# because CKAN mangled things somehow and therefore we flag it up to
# the administrator and give a meaningless error, just in case
+ offset = url_for(controller='package', action='search') + \
+ '?q=--included'
app = self._get_test_app()
- with app.flask_app.test_request_context():
- offset = url_for(controller='package', action='search') + \
- '?q=--included'
search_response = app.get(offset)
search_response_html = BeautifulSoup(search_response.body)
@@ -1566,9 +1399,8 @@ def test_search_solr_syntax_error(self):
def test_search_plugin_hooks(self):
with p.use_plugin('test_package_controller_plugin') as plugin:
+ offset = url_for(controller='package', action='search')
app = self._get_test_app()
- with app.flask_app.test_request_context():
- offset = url_for(controller='package', action='search')
app.get(offset)
# get redirected ...
@@ -1582,8 +1414,7 @@ def test_search_page_request(self):
factories.Dataset(name="dataset-two", title='Dataset Two')
factories.Dataset(name="dataset-three", title='Dataset Three')
- with app.flask_app.test_request_context():
- search_url = url_for(controller='package', action='search')
+ search_url = url_for(controller='package', action='search')
search_response = app.get(search_url)
assert_true('3 datasets found' in search_response)
@@ -1606,8 +1437,7 @@ def test_search_page_results(self):
factories.Dataset(name="dataset-two", title='Dataset Two')
factories.Dataset(name="dataset-three", title='Dataset Three')
- with app.flask_app.test_request_context():
- search_url = url_for(controller='package', action='search')
+ search_url = url_for(controller='package', action='search')
search_response = app.get(search_url)
search_form = search_response.forms['dataset-search-form']
@@ -1632,8 +1462,7 @@ def test_search_page_no_results(self):
factories.Dataset(name="dataset-two", title='Dataset Two')
factories.Dataset(name="dataset-three", title='Dataset Three')
- with app.flask_app.test_request_context():
- search_url = url_for(controller='package', action='search')
+ search_url = url_for(controller='package', action='search')
search_response = app.get(search_url)
search_form = search_response.forms['dataset-search-form']
@@ -1658,8 +1487,7 @@ def test_search_page_results_tag(self):
factories.Dataset(name="dataset-two", title='Dataset Two')
factories.Dataset(name="dataset-three", title='Dataset Three')
- with app.flask_app.test_request_context():
- search_url = url_for(controller='package', action='search')
+ search_url = url_for(controller='package', action='search')
search_response = app.get(search_url)
assert_true('/dataset?tags=my-tag' in search_response)
@@ -1687,8 +1515,7 @@ def test_search_page_results_private(self):
factories.Dataset(name="dataset-two", title='Dataset Two')
factories.Dataset(name="dataset-three", title='Dataset Three')
- with app.flask_app.test_request_context():
- search_url = url_for(controller='package', action='search')
+ search_url = url_for(controller='package', action='search')
search_response = app.get(search_url)
search_response_html = BeautifulSoup(search_response.body)
@@ -1711,9 +1538,7 @@ def test_user_not_in_organization_cannot_search_private_datasets(self):
private=True,
)
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- search_url = url_for(controller='package', action='search')
+ search_url = url_for(controller='package', action='search')
search_response = app.get(search_url, extra_environ=env)
search_response_html = BeautifulSoup(search_response.body)
@@ -1733,9 +1558,7 @@ def test_user_in_organization_can_search_private_datasets(self):
private=True,
)
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- search_url = url_for(controller='package', action='search')
+ search_url = url_for(controller='package', action='search')
search_response = app.get(search_url, extra_environ=env)
search_response_html = BeautifulSoup(search_response.body)
@@ -1756,9 +1579,7 @@ def test_user_in_different_organization_cannot_search_private_datasets(self):
private=True,
)
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- search_url = url_for(controller='package', action='search')
+ search_url = url_for(controller='package', action='search')
search_response = app.get(search_url, extra_environ=env)
search_response_html = BeautifulSoup(search_response.body)
@@ -1778,9 +1599,7 @@ def test_search_default_include_private_false(self):
private=True,
)
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- search_url = url_for(controller='package', action='search')
+ search_url = url_for(controller='package', action='search')
search_response = app.get(search_url, extra_environ=env)
search_response_html = BeautifulSoup(search_response.body)
@@ -1799,9 +1618,7 @@ def test_sysadmin_can_search_private_datasets(self):
private=True,
)
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- search_url = url_for(controller='package', action='search')
+ search_url = url_for(controller='package', action='search')
search_response = app.get(search_url, extra_environ=env)
search_response_html = BeautifulSoup(search_response.body)
@@ -1820,11 +1637,9 @@ def test_package_follow(self):
package = factories.Dataset()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- follow_url = url_for(controller='package',
- action='follow',
- id=package['id'])
+ follow_url = url_for(controller='package',
+ action='follow',
+ id=package['id'])
response = app.post(follow_url, extra_environ=env, status=302)
response = response.follow()
assert_true('You are now following {0}'
@@ -1838,11 +1653,9 @@ def test_package_follow_not_exist(self):
user_one = factories.User()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- follow_url = url_for(controller='package',
- action='follow',
- id='not-here')
+ follow_url = url_for(controller='package',
+ action='follow',
+ id='not-here')
response = app.post(follow_url, extra_environ=env, status=302)
response = response.follow(status=404)
assert_true('Dataset not found' in response)
@@ -1854,16 +1667,13 @@ def test_package_unfollow(self):
package = factories.Dataset()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- follow_url = url_for(controller='package',
- action='follow',
- id=package['id'])
+ follow_url = url_for(controller='package',
+ action='follow',
+ id=package['id'])
app.post(follow_url, extra_environ=env, status=302)
- with app.flask_app.test_request_context():
- unfollow_url = url_for(controller='package', action='unfollow',
- id=package['id'])
+ unfollow_url = url_for(controller='package', action='unfollow',
+ id=package['id'])
unfollow_response = app.post(unfollow_url, extra_environ=env,
status=302)
unfollow_response = unfollow_response.follow()
@@ -1880,10 +1690,8 @@ def test_package_unfollow_not_following(self):
package = factories.Dataset()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- unfollow_url = url_for(controller='package', action='unfollow',
- id=package['id'])
+ unfollow_url = url_for(controller='package', action='unfollow',
+ id=package['id'])
unfollow_response = app.post(unfollow_url, extra_environ=env,
status=302)
unfollow_response = unfollow_response.follow()
@@ -1898,10 +1706,8 @@ def test_package_unfollow_not_exist(self):
user_one = factories.User()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- unfollow_url = url_for(controller='package', action='unfollow',
- id='not-here')
+ unfollow_url = url_for(controller='package', action='unfollow',
+ id='not-here')
unfollow_response = app.post(unfollow_url, extra_environ=env,
status=302)
unfollow_response = unfollow_response.follow(status=404)
@@ -1915,17 +1721,14 @@ def test_package_follower_list(self):
package = factories.Dataset()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- follow_url = url_for(controller='package',
- action='follow',
- id=package['id'])
-
- followers_url = url_for(controller='package', action='followers',
- id=package['id'])
-
+ follow_url = url_for(controller='package',
+ action='follow',
+ id=package['id'])
app.post(follow_url, extra_environ=env, status=302)
+ followers_url = url_for(controller='package', action='followers',
+ id=package['id'])
+
# Only sysadmins can view the followers list pages
followers_response = app.get(followers_url, extra_environ=env,
status=200)
@@ -1939,9 +1742,8 @@ def test_dataset_read(self):
dataset = factories.Dataset()
- with app.flask_app.test_request_context():
- url = url_for(controller='package',
- action='read',
- id=dataset['id'])
+ url = url_for(controller='package',
+ action='read',
+ id=dataset['id'])
response = app.get(url)
assert_in(dataset['title'], response)
diff --git a/ckan/tests/controllers/test_tags.py b/ckan/tests/controllers/test_tags.py
index 3c34445e911..46b4b255b6f 100644
--- a/ckan/tests/controllers/test_tags.py
+++ b/ckan/tests/controllers/test_tags.py
@@ -6,7 +6,7 @@
from nose.tools import assert_equal, assert_true, assert_false, assert_in
from bs4 import BeautifulSoup
-from ckan.lib.helpers import url_for
+from routes import url_for
import ckan.tests.helpers as helpers
from ckan.tests import factories
@@ -32,8 +32,7 @@ def test_tags_listed_under_50(self):
expected_tags = _make_tag_list(49)
factories.Dataset(tags=expected_tags)
- with app.flask_app.test_request_context():
- tag_index_url = url_for(controller='tag', action='index')
+ tag_index_url = url_for(controller='tag', action='index')
tag_response = app.get(tag_index_url)
tag_response_html = BeautifulSoup(tag_response.body)
@@ -54,8 +53,7 @@ def test_tags_listed_over_50(self):
expected_tags = _make_tag_list(51)
factories.Dataset(tags=expected_tags)
- with app.flask_app.test_request_context():
- tag_index_url = url_for(controller='tag', action='index')
+ tag_index_url = url_for(controller='tag', action='index')
tag_response = app.get(tag_index_url)
tag_response_html = BeautifulSoup(tag_response.body)
@@ -78,8 +76,7 @@ def test_tag_search(self):
expected_tags.append({'name': 'find-me'})
factories.Dataset(tags=expected_tags)
- with app.flask_app.test_request_context():
- tag_index_url = url_for(controller='tag', action='index')
+ tag_index_url = url_for(controller='tag', action='index')
tag_response = app.get(tag_index_url)
search_form = tag_response.forms[1]
@@ -101,8 +98,7 @@ def test_tag_search_no_results(self):
expected_tags = _make_tag_list(50)
factories.Dataset(tags=expected_tags)
- with app.flask_app.test_request_context():
- tag_index_url = url_for(controller='tag', action='index')
+ tag_index_url = url_for(controller='tag', action='index')
tag_response = app.get(tag_index_url)
search_form = tag_response.forms[1]
@@ -125,8 +121,7 @@ def test_tag_read_redirects_to_dataset_search(self):
app = self._get_test_app()
factories.Dataset(title='My Other Dataset', tags=[{'name': 'find-me'}])
- with app.flask_app.test_request_context():
- tag_url = url_for(controller='tag', action='read', id='find-me')
+ tag_url = url_for(controller='tag', action='read', id='find-me')
tag_response = app.get(tag_url, status=302)
assert_equal(tag_response.headers['Location'],
'http://test.ckan.net/dataset?tags=find-me')
@@ -136,6 +131,5 @@ def test_tag_read_not_found(self):
app = self._get_test_app()
factories.Dataset(title='My Other Dataset', tags=[{'name': 'find-me'}])
- with app.flask_app.test_request_context():
- tag_url = url_for(controller='tag', action='read', id='not-here')
+ tag_url = url_for(controller='tag', action='read', id='not-here')
app.get(tag_url, status=404)
diff --git a/ckan/tests/controllers/test_user.py b/ckan/tests/controllers/test_user.py
index d3dbd12426c..3dc24df73b3 100644
--- a/ckan/tests/controllers/test_user.py
+++ b/ckan/tests/controllers/test_user.py
@@ -1,12 +1,14 @@
# encoding: utf-8
+from bs4 import BeautifulSoup
+from nose.tools import assert_true, assert_false, assert_equal, assert_in
-from ckan.lib.helpers import url_for
+import ckan.tests.helpers as helpers
import ckan.tests.factories as factories
import ckan.tests.helpers as helpers
-from bs4 import BeautifulSoup
+
+from ckan.lib.helpers import url_for
from ckan import model
from ckan.lib.mailer import create_reset_key
-from nose.tools import assert_true, assert_false, assert_equal, assert_in
webtest_submit = helpers.webtest_submit
submit_and_follow = helpers.submit_and_follow
@@ -15,11 +17,8 @@
def _get_user_edit_page(app):
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- url = url_for(controller='user', action='edit')
-
response = app.get(
- url=url,
+ url=url_for(controller='user', action='edit'),
extra_environ=env,
)
return env, response, user
@@ -28,11 +27,7 @@ def _get_user_edit_page(app):
class TestRegisterUser(helpers.FunctionalTestBase):
def test_register_a_user(self):
app = helpers._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='user', action='register')
-
- response = app.get(url)
+ response = app.get(url=url_for(controller='user', action='register'))
form = response.forms['user-register-form']
form['name'] = 'newuser'
@@ -51,11 +46,7 @@ def test_register_a_user(self):
def test_register_user_bad_password(self):
app = helpers._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='user', action='register')
-
- response = app.get(url)
+ response = app.get(url=url_for(controller='user', action='register'))
form = response.forms['user-register-form']
form['name'] = 'newuser'
@@ -85,10 +76,9 @@ def test_create_user_as_sysadmin(self):
# submit it
login_form.submit('save')
- with app.flask_app.test_request_context():
- url = url_for(controller='user', action='register')
- response = app.get(url)
-
+ response = app.get(
+ url=url_for(controller='user', action='register'),
+ )
assert "user-register-form" in response.forms
form = response.forms['user-register-form']
form['name'] = 'newestuser'
@@ -175,8 +165,7 @@ def test_user_logout_url_redirect(self):
'''
app = self._get_test_app()
- with app.flask_app.test_request_context():
- logout_url = url_for(controller='user', action='logout')
+ logout_url = url_for(controller='user', action='logout')
logout_response = app.get(logout_url, status=302)
final_response = helpers.webtest_maybe_follow(logout_response)
@@ -193,8 +182,7 @@ def test_non_root_user_logout_url_redirect(self):
'''
app = self._get_test_app()
- with app.flask_app.test_request_context():
- logout_url = url_for(controller='user', action='logout')
+ logout_url = url_for(controller='user', action='logout')
# Remove the prefix otherwise the test app won't find the correct route
logout_url = logout_url.replace('/my/prefix', '')
logout_response = app.get(logout_url, status=302)
@@ -213,12 +201,8 @@ def test_own_datasets_show_up_on_user_dashboard(self):
app = self._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='user', action='dashboard_datasets')
-
response = app.get(
- url,
+ url=url_for(controller='user', action='dashboard_datasets'),
extra_environ=env,
)
@@ -234,12 +218,8 @@ def test_other_datasets_dont_show_up_on_user_dashboard(self):
app = self._get_test_app()
env = {'REMOTE_USER': user2['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='user', action='dashboard_datasets')
-
response = app.get(
- url,
+ url=url_for(controller='user', action='dashboard_datasets'),
extra_environ=env,
)
@@ -250,12 +230,8 @@ class TestUserEdit(helpers.FunctionalTestBase):
def test_user_edit_no_user(self):
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='user', action='edit', id=None)
-
response = app.get(
- url,
+ url_for(controller='user', action='edit', id=None),
status=400
)
assert_true('No user specified' in response)
@@ -264,12 +240,8 @@ def test_user_edit_unknown_user(self):
'''Attempt to read edit user for an unknown user redirects to login
page.'''
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='user', action='edit', id='unknown_person')
-
response = app.get(
- url,
+ url_for(controller='user', action='edit', id='unknown_person'),
status=403
)
@@ -279,12 +251,8 @@ def test_user_edit_not_logged_in(self):
app = self._get_test_app()
user = factories.User()
username = user['name']
-
- with app.flask_app.test_request_context():
- url = url_for(controller='user', action='edit', id=username)
-
response = app.get(
- url,
+ url_for(controller='user', action='edit', id=username),
status=403
)
@@ -292,12 +260,8 @@ def test_edit_user(self):
user = factories.User(password='TestPassword1')
app = self._get_test_app()
env = {'REMOTE_USER': user['name'].encode('ascii')}
-
- with app.flask_app.test_request_context():
- url = url_for(controller='user', action='edit')
-
response = app.get(
- url,
+ url=url_for(controller='user', action='edit'),
extra_environ=env,
)
# existing values in the form
@@ -377,12 +341,8 @@ def test_edit_user_logged_in_username_change(self):
login_form.submit()
# Now the cookie is set, run the test
-
- with app.flask_app.test_request_context():
- url = url_for(controller='user', action='edit')
-
response = app.get(
- url,
+ url=url_for(controller='user', action='edit'),
)
# existing values in the form
form = response.forms['user-edit-form']
@@ -410,12 +370,8 @@ def test_edit_user_logged_in_username_change_by_name(self):
login_form.submit()
# Now the cookie is set, run the test
-
- with app.flask_app.test_request_context():
- url = url_for(controller='user', action='edit', id=user['name'])
-
response = app.get(
- url,
+ url=url_for(controller='user', action='edit', id=user['name']),
)
# existing values in the form
form = response.forms['user-edit-form']
@@ -443,12 +399,8 @@ def test_edit_user_logged_in_username_change_by_id(self):
login_form.submit()
# Now the cookie is set, run the test
-
- with app.flask_app.test_request_context():
- url = url_for(controller='user', action='edit', id=user['id'])
-
response = app.get(
- url,
+ url=url_for(controller='user', action='edit', id=user['id']),
)
# existing values in the form
form = response.forms['user-edit-form']
@@ -468,12 +420,10 @@ def test_perform_reset_for_key_change(self):
key = user_obj.reset_key
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- offset = url_for(controller='user',
- action='perform_reset',
- id=user_obj.id,
- key=user_obj.reset_key)
+ offset = url_for(controller='user',
+ action='perform_reset',
+ id=user_obj.id,
+ key=user_obj.reset_key)
response = app.post(offset, params=params, status=302)
user_obj = helpers.model.User.by_name(user['name']) # Update user_obj
@@ -524,10 +474,9 @@ def test_user_follow(self):
user_two = factories.User()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- follow_url = url_for(controller='user',
- action='follow',
- id=user_two['id'])
+ follow_url = url_for(controller='user',
+ action='follow',
+ id=user_two['id'])
response = app.post(follow_url, extra_environ=env, status=302)
response = response.follow()
assert_true('You are now following {0}'
@@ -541,10 +490,9 @@ def test_user_follow_not_exist(self):
user_one = factories.User()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- follow_url = url_for(controller='user',
- action='follow',
- id='not-here')
+ follow_url = url_for(controller='user',
+ action='follow',
+ id='not-here')
response = app.post(follow_url, extra_environ=env, status=302)
response = response.follow(status=302)
assert_in('user/login', response.headers['location'])
@@ -556,15 +504,13 @@ def test_user_unfollow(self):
user_two = factories.User()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- follow_url = url_for(controller='user',
- action='follow',
- id=user_two['id'])
+ follow_url = url_for(controller='user',
+ action='follow',
+ id=user_two['id'])
app.post(follow_url, extra_environ=env, status=302)
- with app.flask_app.test_request_context():
- unfollow_url = url_for(controller='user', action='unfollow',
- id=user_two['id'])
+ unfollow_url = url_for(controller='user', action='unfollow',
+ id=user_two['id'])
unfollow_response = app.post(unfollow_url, extra_environ=env,
status=302)
unfollow_response = unfollow_response.follow()
@@ -581,9 +527,8 @@ def test_user_unfollow_not_following(self):
user_two = factories.User()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- unfollow_url = url_for(controller='user', action='unfollow',
- id=user_two['id'])
+ unfollow_url = url_for(controller='user', action='unfollow',
+ id=user_two['id'])
unfollow_response = app.post(unfollow_url, extra_environ=env,
status=302)
unfollow_response = unfollow_response.follow()
@@ -598,9 +543,8 @@ def test_user_unfollow_not_exist(self):
user_one = factories.User()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- unfollow_url = url_for(controller='user', action='unfollow',
- id='not-here')
+ unfollow_url = url_for(controller='user', action='unfollow',
+ id='not-here')
unfollow_response = app.post(unfollow_url, extra_environ=env,
status=302)
unfollow_response = unfollow_response.follow(status=302)
@@ -614,15 +558,13 @@ def test_user_follower_list(self):
user_two = factories.User()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- follow_url = url_for(controller='user',
- action='follow',
- id=user_two['id'])
+ follow_url = url_for(controller='user',
+ action='follow',
+ id=user_two['id'])
app.post(follow_url, extra_environ=env, status=302)
- with app.flask_app.test_request_context():
- followers_url = url_for(controller='user', action='followers',
- id=user_two['id'])
+ followers_url = url_for(controller='user', action='followers',
+ id=user_two['id'])
# Only sysadmins can view the followers list pages
followers_response = app.get(followers_url, extra_environ=env,
@@ -636,8 +578,7 @@ def test_user_page_anon_access(self):
'''Anon users can access the user list page'''
app = self._get_test_app()
- with app.flask_app.test_request_context():
- user_url = url_for(controller='user', action='index')
+ user_url = url_for(controller='user', action='index')
user_response = app.get(user_url, status=200)
assert_true('All Users - CKAN '
in user_response)
@@ -649,8 +590,7 @@ def test_user_page_lists_users(self):
factories.User(fullname='User Two')
factories.User(fullname='User Three')
- with app.flask_app.test_request_context():
- user_url = url_for(controller='user', action='index')
+ user_url = url_for(controller='user', action='index')
user_response = app.get(user_url, status=200)
user_response_html = BeautifulSoup(user_response.body)
@@ -669,8 +609,7 @@ def test_user_page_doesnot_list_deleted_users(self):
factories.User(fullname='User Two')
factories.User(fullname='User Three')
- with app.flask_app.test_request_context():
- user_url = url_for(controller='user', action='index')
+ user_url = url_for(controller='user', action='index')
user_response = app.get(user_url, status=200)
user_response_html = BeautifulSoup(user_response.body)
@@ -689,8 +628,7 @@ def test_user_page_anon_search(self):
factories.User(fullname='Person Two')
factories.User(fullname='Person Three')
- with app.flask_app.test_request_context():
- user_url = url_for(controller='user', action='index')
+ user_url = url_for(controller='user', action='index')
user_response = app.get(user_url, status=200)
search_form = user_response.forms['user-search-form']
search_form['q'] = 'Person'
@@ -712,8 +650,7 @@ def test_user_page_anon_search_not_by_email(self):
factories.User(fullname='Person Two')
factories.User(fullname='Person Three')
- with app.flask_app.test_request_context():
- user_url = url_for(controller='user', action='index')
+ user_url = url_for(controller='user', action='index')
user_response = app.get(user_url, status=200)
search_form = user_response.forms['user-search-form']
search_form['q'] = 'useroneemail@example.com'
@@ -733,8 +670,7 @@ def test_user_page_sysadmin_user(self):
factories.User(fullname='Person Three')
env = {'REMOTE_USER': sysadmin['name'].encode('ascii')}
- with app.flask_app.test_request_context():
- user_url = url_for(controller='user', action='index')
+ user_url = url_for(controller='user', action='index')
user_response = app.get(user_url, status=200, extra_environ=env)
search_form = user_response.forms['user-search-form']
search_form['q'] = 'useroneemail@example.com'
diff --git a/ckan/tests/controllers/test_util.py b/ckan/tests/controllers/test_util.py
index 45616b60d67..db76196e677 100644
--- a/ckan/tests/controllers/test_util.py
+++ b/ckan/tests/controllers/test_util.py
@@ -1,8 +1,10 @@
# encoding: utf-8
from nose.tools import assert_equal
+from pylons.test import pylonsapp
+import paste.fixture
-from ckan.lib.helpers import url_for
+from routes import url_for as url_for
import ckan.tests.helpers as helpers
@@ -10,12 +12,8 @@
class TestUtil(helpers.FunctionalTestBase):
def test_redirect_ok(self):
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='util', action='redirect')
-
response = app.get(
- url,
+ url=url_for(controller='util', action='redirect'),
params={'url': '/dataset'},
status=302,
)
@@ -24,36 +22,24 @@ def test_redirect_ok(self):
def test_redirect_external(self):
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='util', action='redirect')
-
response = app.get(
- url,
+ url=url_for(controller='util', action='redirect'),
params={'url': 'http://nastysite.com'},
status=403,
)
def test_redirect_no_params(self):
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='util', action='redirect')
-
response = app.get(
- url,
+ url=url_for(controller='util', action='redirect'),
params={},
status=400,
)
def test_redirect_no_params_2(self):
app = self._get_test_app()
-
- with app.flask_app.test_request_context():
- url = url_for(controller='util', action='redirect')
-
response = app.get(
- url,
+ url=url_for(controller='util', action='redirect'),
params={'url': ''},
status=400,
)
diff --git a/ckan/tests/legacy/__init__.py b/ckan/tests/legacy/__init__.py
index a647c147899..3a056855f95 100644
--- a/ckan/tests/legacy/__init__.py
+++ b/ckan/tests/legacy/__init__.py
@@ -35,7 +35,6 @@
import ckan.model as model
from ckan import ckan_nose_plugin
from ckan.common import json
-from ckan.tests import helpers
# evil hack as url_for is passed out
url_for = h.url_for
@@ -238,8 +237,7 @@ class WsgiAppCase(BaseCase):
# Either that, or this file got imported somehow before the tests started
# running, meaning the pylonsapp wasn't setup yet (which is done in
# pylons.test.py:begin())
- #app = paste.fixture.TestApp(wsgiapp)
- app = helpers._get_test_app()
+ app = paste.fixture.TestApp(wsgiapp)
def config_abspath(file_path):
@@ -417,7 +415,6 @@ def call_action_api(app, action, apikey=None, status=200, **kwargs):
params = json.dumps(kwargs)
response = app.post('/api/action/{0}'.format(action), params=params,
extra_environ={'Authorization': str(apikey)}, status=status)
-
assert '/api/3/action/help_show?name={0}'.format(action) \
in response.json['help']
diff --git a/ckan/tests/legacy/functional/api/base.py b/ckan/tests/legacy/functional/api/base.py
index 6278e95077a..98c704ee979 100644
--- a/ckan/tests/legacy/functional/api/base.py
+++ b/ckan/tests/legacy/functional/api/base.py
@@ -190,7 +190,7 @@ def loads(self, chars):
raise Exception, "Couldn't loads string '%s': %s" % (chars, inst)
def assert_json_response(self, res, expected_in_body=None):
- content_type = res.headers['Content-Type']
+ content_type = res.header_dict['Content-Type']
assert 'application/json' in content_type, content_type
res_json = self.loads(res.body)
if expected_in_body:
diff --git a/ckan/tests/legacy/functional/api/model/test_package.py b/ckan/tests/legacy/functional/api/model/test_package.py
index 593f49d7612..22472d7dac3 100644
--- a/ckan/tests/legacy/functional/api/model/test_package.py
+++ b/ckan/tests/legacy/functional/api/model/test_package.py
@@ -13,7 +13,6 @@
from ckan.tests.legacy.functional.api.base import Api2TestCase as Version2TestCase
import ckan.tests.legacy as tests
-from ckan.tests import helpers
# Todo: Remove this ckan.model stuff.
import ckan.model as model
@@ -66,7 +65,7 @@ def test_register_post_ok(self):
assert_equal(pkg['extras'], self.package_fixture_data['extras'])
# Check the value of the Location header.
- location = res.headers['Location']
+ location = res.header('Location')
assert offset in location
res = self.app.get(location, status=self.STATUS_200_OK)
@@ -134,7 +133,7 @@ def test_register_post_with_group(self):
package_fixture_data = self.package_fixture_data
package_fixture_data['groups'] = groups
data = self.dumps(package_fixture_data)
- res = self.app.post(offset, data, status=self.STATUS_201_CREATED,
+ res = self.post_json(offset, data, status=self.STATUS_201_CREATED,
extra_environ={'Authorization':str(user.apikey)})
# Check the database record.
@@ -160,7 +159,7 @@ def test_register_post_with_group_not_authorized(self):
package_fixture_data = self.package_fixture_data
package_fixture_data['groups'] = groups
data = self.dumps(package_fixture_data)
- res = self.app.post(offset, data, status=self.STATUS_403_ACCESS_DENIED,
+ res = self.post_json(offset, data, status=self.STATUS_403_ACCESS_DENIED,
extra_environ=self.extra_environ)
del package_fixture_data['groups']
@@ -174,7 +173,7 @@ def test_register_post_with_group_not_found(self):
package_fixture_data = self.package_fixture_data
package_fixture_data['groups'] = groups
data = self.dumps(package_fixture_data)
- res = self.app.post(offset, data, status=self.STATUS_404_NOT_FOUND,
+ res = self.post_json(offset, data, status=self.STATUS_404_NOT_FOUND,
extra_environ=self.extra_environ)
del package_fixture_data['groups']
@@ -188,7 +187,7 @@ def test_register_post_with_group_sysadmin(self):
package_fixture_data = self.package_fixture_data
package_fixture_data['groups'] = groups
data = self.dumps(package_fixture_data)
- res = self.app.post(offset, data, status=self.STATUS_201_CREATED,
+ res = self.post_json(offset, data, status=self.STATUS_201_CREATED,
extra_environ={'Authorization':str(user.apikey)})
# Check the database record.
model.Session.remove()
@@ -208,7 +207,7 @@ def test_register_post_json(self):
assert not self.get_package_by_name(self.package_fixture_data['name'])
offset = self.package_offset()
data = self.dumps(self.package_fixture_data)
- res = self.app.post(offset, data, status=self.STATUS_201_CREATED,
+ res = self.post_json(offset, data, status=self.STATUS_201_CREATED,
extra_environ=self.admin_extra_environ)
# Check the database record.
model.Session.remove()
@@ -220,7 +219,7 @@ def test_register_post_bad_content_type(self):
assert not self.get_package_by_name(self.package_fixture_data['name'])
offset = self.package_offset()
data = self.dumps(self.package_fixture_data)
- res = self.app.post(offset, data,
+ res = self.http_request(offset, data,
content_type='something/unheard_of',
status=[self.STATUS_400_BAD_REQUEST,
self.STATUS_201_CREATED],
@@ -263,7 +262,7 @@ def test_register_post_indexerror(self):
offset = self.package_offset()
data = self.dumps(self.package_fixture_data)
- self.app.post(offset, data, status=500, extra_environ=self.admin_extra_environ)
+ self.post_json(offset, data, status=500, extra_environ=self.admin_extra_environ)
model.Session.remove()
finally:
SolrSettings.init(original_settings)
@@ -274,7 +273,7 @@ def test_register_post_tag_too_long(self):
assert not self.get_package_by_name(pkg['name'])
offset = self.package_offset()
data = self.dumps(pkg)
- res = self.app.post(offset, data, status=self.STATUS_409_CONFLICT,
+ res = self.post_json(offset, data, status=self.STATUS_409_CONFLICT,
extra_environ=self.admin_extra_environ)
assert 'length is more than maximum 100' in res.body, res.body
assert 'tagok' not in res.body
@@ -715,8 +714,8 @@ def test_entity_delete_ok_without_request_headers(self):
assert self.get_package_by_name(self.package_fixture_data['name'])
# delete it
offset = self.package_offset(self.package_fixture_data['name'])
- res = self.app.delete(offset, status=self.STATUS_200_OK,
- extra_environ=self.admin_extra_environ)
+ res = self.delete_request(offset, status=self.STATUS_200_OK,
+ extra_environ=self.admin_extra_environ)
package = self.get_package_by_name(self.package_fixture_data['name'])
self.assert_equal(package.state, 'deleted')
model.Session.remove()
diff --git a/ckan/tests/legacy/functional/api/model/test_relationships.py b/ckan/tests/legacy/functional/api/model/test_relationships.py
index 069776d663d..0bdc30c84e5 100644
--- a/ckan/tests/legacy/functional/api/model/test_relationships.py
+++ b/ckan/tests/legacy/functional/api/model/test_relationships.py
@@ -1,14 +1,14 @@
# encoding: utf-8
-from nose.tools import assert_equal
+from nose.tools import assert_equal
from nose.plugins.skip import SkipTest
from ckan import model
from ckan.lib.create_test_data import CreateTestData
from ckan.tests.legacy.functional.api.base import BaseModelApiTestCase
-from ckan.tests.legacy.functional.api.base import Api1TestCase as Version1TestCase
-from ckan.tests.legacy.functional.api.base import Api2TestCase as Version2TestCase
+from ckan.tests.legacy.functional.api.base import Api1TestCase as Version1TestCase
+from ckan.tests.legacy.functional.api.base import Api2TestCase as Version2TestCase
class RelationshipsTestCase(BaseModelApiTestCase):
@@ -115,7 +115,7 @@ def test_01_create_and_read_relationship(self):
assert len(rels) == 1
self.check_relationship_dict(rels[0],
'annakarenina', 'parent_of', 'warandpeace', self.comment)
-
+
def test_02_create_relationship_way_2(self):
# Create a relationship using 2nd way
self.create_annakarenina_parent_of_war_and_peace(way=2)
@@ -189,7 +189,7 @@ def test_create_relationship_unknown(self):
def create_annakarenina_parent_of_war_and_peace(self, way=1):
# Create package relationship.
# More than one 'way' to create a package.
- # Todo: Redesign this in a RESTful style, so that a relationship is
+ # Todo: Redesign this in a RESTful style, so that a relationship is
# created by posting a relationship to a relationship **register**.
assert way in (1, 2, 3, 4)
if way == 1:
@@ -220,7 +220,7 @@ def create_annakarenina_parent_of_war_and_peace(self, way=1):
assert_equal(rel['type'], 'child_of')
assert_equal(rel['subject'], self.ref_package(self.war))
assert_equal(rel['object'], self.ref_package(self.anna))
-
+
# Check the model, directly.
rels = self.anna.get_relationships()
assert len(rels) == 1, rels
@@ -271,7 +271,7 @@ def get_relationships(self, package1_name=u'annakarenina', type='relationships',
if type:
allowable_statuses.append(404)
res = self.app.get(offset, status=allowable_statuses)
- if res.status_int == 200:
+ if res.status == 200:
res_dict = self.data_from_res(res) if res.body else []
return res_dict
else:
@@ -300,7 +300,7 @@ def check_relationships_rest(self, pkg1_name, pkg2_name=None,
expected_relationships=[]):
rels = self.get_relationships(package1_name=pkg1_name,
package2_name=pkg2_name)
- self.assert_len_relationships(rels, expected_relationships)
+ self.assert_len_relationships(rels, expected_relationships)
for rel in rels:
the_expected_rel = None
for expected_rel in expected_relationships:
diff --git a/ckan/tests/legacy/functional/api/model/test_vocabulary.py b/ckan/tests/legacy/functional/api/model/test_vocabulary.py
index ab6bb25f6f2..feb03af3717 100644
--- a/ckan/tests/legacy/functional/api/model/test_vocabulary.py
+++ b/ckan/tests/legacy/functional/api/model/test_vocabulary.py
@@ -1,17 +1,17 @@
# encoding: utf-8
import ckan
+import pylons.test
+import paste.fixture
import ckan.lib.helpers as helpers
import ckan.lib.dictization.model_dictize as model_dictize
-from ckan.tests import helpers as test_helpers
-
class TestVocabulary(object):
@classmethod
def setup_class(self):
- self.app = test_helpers._get_test_app()
+ self.app = paste.fixture.TestApp(pylons.test.pylonsapp)
@classmethod
def teardown_class(self):
diff --git a/ckan/tests/legacy/functional/api/test_activity.py b/ckan/tests/legacy/functional/api/test_activity.py
index 571c8ae4701..ffb9834de08 100644
--- a/ckan/tests/legacy/functional/api/test_activity.py
+++ b/ckan/tests/legacy/functional/api/test_activity.py
@@ -23,8 +23,6 @@
import ckan.tests.legacy as tests
from ckan.tests.helpers import call_action
-from ckan.tests import helpers
-
##def package_update(context, data_dict):
## # These tests call package_update directly which is really bad
@@ -208,7 +206,7 @@ def setup_class(self):
'id': annakarenina.id,
}
self.users = [self.sysadmin_user, self.normal_user]
- self.app = helpers._get_test_app()
+ self.app = paste.fixture.TestApp(pylons.test.pylonsapp)
@classmethod
def teardown_class(self):
diff --git a/ckan/tests/legacy/functional/api/test_dashboard.py b/ckan/tests/legacy/functional/api/test_dashboard.py
index e3a7c4b521e..1dec5c9cbd2 100644
--- a/ckan/tests/legacy/functional/api/test_dashboard.py
+++ b/ckan/tests/legacy/functional/api/test_dashboard.py
@@ -12,8 +12,6 @@
import paste
import pylons.test
from ckan.tests.legacy import CreateTestData
-from ckan.tests import helpers
-
class TestDashboard(object):
'''Tests for the logic action functions related to the user's dashboard.'''
@@ -37,7 +35,7 @@ def setup_class(cls):
ckan.model.repo.rebuild_db()
ckan.lib.search.clear_all()
CreateTestData.create()
- cls.app = helpers._get_test_app()
+ cls.app = paste.fixture.TestApp(pylons.test.pylonsapp)
joeadmin = ckan.model.User.get('joeadmin')
cls.joeadmin = {
'id': joeadmin.id,
diff --git a/ckan/tests/legacy/functional/api/test_email_notifications.py b/ckan/tests/legacy/functional/api/test_email_notifications.py
index 8ecee84a549..cfc5d417395 100644
--- a/ckan/tests/legacy/functional/api/test_email_notifications.py
+++ b/ckan/tests/legacy/functional/api/test_email_notifications.py
@@ -10,12 +10,13 @@
import ckan.tests.legacy.pylons_controller as pylons_controller
import ckan.config.middleware
-from ckan.tests import helpers
+import paste
+import paste.deploy
+import pylons.test
from ckan.common import config
-
class TestEmailNotifications(mock_mail_server.SmtpServerHarness,
pylons_controller.PylonsTestCase):
@@ -24,7 +25,7 @@ def setup_class(cls):
mock_mail_server.SmtpServerHarness.setup_class()
pylons_controller.PylonsTestCase.setup_class()
tests.CreateTestData.create()
- cls.app = helpers._get_test_app()
+ cls.app = paste.fixture.TestApp(pylons.test.pylonsapp)
joeadmin = model.User.get('joeadmin')
cls.joeadmin = {'id': joeadmin.id,
'apikey': joeadmin.apikey,
@@ -197,7 +198,7 @@ def setup_class(cls):
mock_mail_server.SmtpServerHarness.setup_class()
pylons_controller.PylonsTestCase.setup_class()
tests.CreateTestData.create()
- cls.app = helpers._get_test_app()
+ cls.app = paste.fixture.TestApp(pylons.test.pylonsapp)
joeadmin = model.User.get('joeadmin')
cls.joeadmin = {'id': joeadmin.id,
'apikey': joeadmin.apikey,
@@ -337,7 +338,7 @@ def setup_class(cls):
wsgiapp = ckan.config.middleware.make_app(config['global_conf'],
**config)
- cls.app = helpers._get_test_app()
+ cls.app = paste.fixture.TestApp(wsgiapp)
mock_mail_server.SmtpServerHarness.setup_class()
pylons_controller.PylonsTestCase.setup_class()
@@ -421,7 +422,7 @@ def setup_class(cls):
wsgiapp = ckan.config.middleware.make_app(config['global_conf'],
**config)
- cls.app = helpers._get_test_app()
+ cls.app = paste.fixture.TestApp(wsgiapp)
mock_mail_server.SmtpServerHarness.setup_class()
pylons_controller.PylonsTestCase.setup_class()
diff --git a/ckan/tests/legacy/functional/api/test_follow.py b/ckan/tests/legacy/functional/api/test_follow.py
index 98f4bd23073..909b2e77606 100644
--- a/ckan/tests/legacy/functional/api/test_follow.py
+++ b/ckan/tests/legacy/functional/api/test_follow.py
@@ -18,8 +18,6 @@
import ckan
from ckan.tests.legacy import are_foreign_keys_supported, SkipTest, CreateTestData, call_action_api
-from ckan.tests import helpers
-
def datetime_from_string(s):
'''Return a standard datetime.datetime object initialised from a string in
@@ -297,7 +295,7 @@ def setup_class(self):
'id': ckan.model.Group.get('david').id,
'name': ckan.model.Group.get('david').name,
}
- self.app = helpers._get_test_app()
+ self.app = paste.fixture.TestApp(pylons.test.pylonsapp)
@classmethod
def teardown_class(self):
@@ -806,7 +804,7 @@ def setup_class(self):
'id': ckan.model.Group.get('david').id,
'name': ckan.model.Group.get('david').name,
}
- self.app = helpers._get_test_app()
+ self.app = paste.fixture.TestApp(pylons.test.pylonsapp)
follow_user(self.app, self.testsysadmin['id'],
self.testsysadmin['apikey'], self.joeadmin['id'],
self.joeadmin['id'], self.testsysadmin['apikey'])
@@ -1156,7 +1154,7 @@ def setup_class(self):
'id': ckan.model.Group.get('david').id,
'name': ckan.model.Group.get('david').name,
}
- self.app = helpers._get_test_app()
+ self.app = paste.fixture.TestApp(pylons.test.pylonsapp)
follow_user(self.app, self.joeadmin['id'], self.joeadmin['apikey'],
self.testsysadmin['id'], self.testsysadmin['id'],
diff --git a/ckan/tests/legacy/functional/api/test_resource.py b/ckan/tests/legacy/functional/api/test_resource.py
index 28c3a6bed15..e8c53593bcd 100644
--- a/ckan/tests/legacy/functional/api/test_resource.py
+++ b/ckan/tests/legacy/functional/api/test_resource.py
@@ -45,7 +45,7 @@ def teardown_class(self):
def test_good_input(self):
offset = self.base_url + '/format_autocomplete?incomplete=cs'
result = self.app.get(offset, status=200)
- content_type = result.headers['Content-Type']
+ content_type = result.header_dict['Content-Type']
assert 'application/json' in content_type, content_type
res_json = self.loads(result.body)
assert 'ResultSet' in res_json, res_json
@@ -58,7 +58,7 @@ def test_good_input(self):
def test_missing_format(self):
offset = self.base_url + '/format_autocomplete?incomplete=incorrectformat'
result = self.app.get(offset, status=200)
- content_type = result.headers['Content-Type']
+ content_type = result.header_dict['Content-Type']
assert 'application/json' in content_type, content_type
res_json = self.loads(result.body)
assert 'ResultSet' in res_json, res_json
diff --git a/ckan/tests/legacy/functional/api/test_user.py b/ckan/tests/legacy/functional/api/test_user.py
index 3096a9f9cff..c2718fc1bdc 100644
--- a/ckan/tests/legacy/functional/api/test_user.py
+++ b/ckan/tests/legacy/functional/api/test_user.py
@@ -13,7 +13,6 @@
from ckan.tests.legacy import url_for
import ckan.config.middleware
from ckan.common import json
-from ckan.tests import helpers
class TestUserApi(ControllerTestCase):
@@ -26,12 +25,8 @@ def teardown_class(cls):
model.repo.rebuild_db()
def test_autocomplete(self):
-
- with self.app.flask_app.test_request_context():
- url = url_for(controller='api', action='user_autocomplete', ver=2)
-
response = self.app.get(
- url,
+ url=url_for(controller='api', action='user_autocomplete', ver=2),
params={
'q': u'sysadmin',
},
@@ -40,15 +35,11 @@ def test_autocomplete(self):
print response.json
assert set(response.json[0].keys()) == set(['id', 'name', 'fullname'])
assert_equal(response.json[0]['name'], u'testsysadmin')
- assert_equal(response.headers['Content-Type'], 'application/json;charset=utf-8')
+ assert_equal(response.header('Content-Type'), 'application/json;charset=utf-8')
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,
+ url=url_for(controller='api', action='user_autocomplete', ver=2),
params={
'q': u'tes',
},
@@ -58,12 +49,8 @@ def test_autocomplete_multiple(self):
assert_equal(len(response.json), 2)
def test_autocomplete_limit(self):
-
- with self.app.flask_app.test_request_context():
- url = url_for(controller='api', action='user_autocomplete', ver=2)
-
response = self.app.get(
- url,
+ url=url_for(controller='api', action='user_autocomplete', ver=2),
params={
'q': u'tes',
'limit': 1
@@ -85,7 +72,7 @@ def setup_class(cls):
cls._original_config = config.copy()
wsgiapp = ckan.config.middleware.make_app(
config['global_conf'], **config)
- cls.app = helpers._get_test_app()
+ cls.app = paste.fixture.TestApp(wsgiapp)
cls.sysadmin_user = model.User.get('testsysadmin')
PylonsTestCase.setup_class()
@@ -135,7 +122,7 @@ def setup_class(cls):
config['ckan.auth.create_user_via_api'] = True
wsgiapp = ckan.config.middleware.make_app(
config['global_conf'], **config)
- cls.app = helpers._get_test_app()
+ cls.app = paste.fixture.TestApp(wsgiapp)
PylonsTestCase.setup_class()
cls.sysadmin_user = model.User.get('testsysadmin')
@@ -183,7 +170,7 @@ def setup_class(cls):
config['ckan.auth.create_user_via_web'] = False
wsgiapp = ckan.config.middleware.make_app(
config['global_conf'], **config)
- cls.app = helpers._get_test_app()
+ cls.app = paste.fixture.TestApp(wsgiapp)
cls.sysadmin_user = model.User.get('testsysadmin')
PylonsTestCase.setup_class()
@@ -219,7 +206,7 @@ def setup_class(cls):
config['ckan.auth.create_user_via_web'] = True
wsgiapp = ckan.config.middleware.make_app(
config['global_conf'], **config)
- cls.app = helpers._get_test_app()
+ cls.app = paste.fixture.TestApp(wsgiapp)
cls.sysadmin_user = model.User.get('testsysadmin')
PylonsTestCase.setup_class()
diff --git a/ckan/tests/legacy/functional/api/test_util.py b/ckan/tests/legacy/functional/api/test_util.py
index 9065d00e7da..9ba2adb9afe 100644
--- a/ckan/tests/legacy/functional/api/test_util.py
+++ b/ckan/tests/legacy/functional/api/test_util.py
@@ -18,12 +18,8 @@ def teardown_class(cls):
model.repo.rebuild_db()
def test_package_slug_invalid(self):
-
- with self.app.flask_app.test_request_context():
- url = url_for(controller='api', action='is_slug_valid', ver=2)
-
response = self.app.get(
- url,
+ url=url_for(controller='api', action='is_slug_valid', ver=2),
params={
'type': u'package',
'slug': u'edit',
@@ -31,14 +27,10 @@ def test_package_slug_invalid(self):
status=200,
)
assert_equal(response.body, '{"valid": false}')
- assert_equal(response.headers['Content-Type'], 'application/json;charset=utf-8')
-
-
- with self.app.flask_app.test_request_context():
- url = url_for(controller='api', action='is_slug_valid', ver=2)
+ assert_equal(response.header('Content-Type'), 'application/json;charset=utf-8')
response = self.app.get(
- url,
+ url=url_for(controller='api', action='is_slug_valid', ver=2),
params={
'type': u'package',
'slug': u'new',
@@ -46,15 +38,11 @@ def test_package_slug_invalid(self):
status=200,
)
assert_equal(response.body, '{"valid": false}')
- assert_equal(response.headers['Content-Type'], 'application/json;charset=utf-8')
+ assert_equal(response.header('Content-Type'), 'application/json;charset=utf-8')
def test_package_slug_valid(self):
-
- with self.app.flask_app.test_request_context():
- url = url_for(controller='api', action='is_slug_valid', ver=2)
-
response = self.app.get(
- url,
+ url=url_for(controller='api', action='is_slug_valid', ver=2),
params={
'type': u'package',
'slug': u'A New Title * With & Funny CHARacters',
@@ -62,14 +50,10 @@ def test_package_slug_valid(self):
status=200,
)
assert_equal(response.body, '{"valid": true}')
- assert_equal(response.headers['Content-Type'], 'application/json;charset=utf-8')
-
-
- with self.app.flask_app.test_request_context():
- url = url_for(controller='api', action='is_slug_valid', ver=2)
+ assert_equal(response.header('Content-Type'), 'application/json;charset=utf-8')
response = self.app.get(
- url,
+ url=url_for(controller='api', action='is_slug_valid', ver=2),
params={
'type': u'package',
'slug': u'warandpeace',
@@ -77,64 +61,44 @@ def test_package_slug_valid(self):
status=200,
)
assert_equal(response.body, '{"valid": false}')
- assert_equal(response.headers['Content-Type'], 'application/json;charset=utf-8')
+ assert_equal(response.header('Content-Type'), 'application/json;charset=utf-8')
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,
+ url=url_for(controller='api', action='markdown', ver=2),
params={'q': markdown},
status=200,
)
assert_equal(response.body, '"Title "')
def test_munge_package_name(self):
-
- with self.app.flask_app.test_request_context():
- url = url_for(controller='api', action='munge_package_name', ver=2)
-
response = self.app.get(
- url,
+ url=url_for(controller='api', action='munge_package_name', ver=2),
params={'name': 'test name'},
status=200,
)
assert_equal(response.body, '"test-name"')
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,
+ 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_tag(self):
-
- with self.app.flask_app.test_request_context():
- url = url_for(controller='api', action='munge_tag', ver=2)
-
response = self.app.get(
- url,
+ url=url_for(controller='api', action='munge_tag', ver=2),
params={'name': 'Test subject'},
status=200,
)
assert_equal(response.body, '"test-subject"')
def test_status(self):
-
- with self.app.flask_app.test_request_context():
- url = url_for(controller='api', action='status', ver=2)
-
response = self.app.get(
- url,
+ url=url_for(controller='api', action='status', ver=2),
params={},
status=200,
)
diff --git a/ckan/tests/legacy/functional/test_activity.py b/ckan/tests/legacy/functional/test_activity.py
index 49e228a7d87..e6aa85fc843 100644
--- a/ckan/tests/legacy/functional/test_activity.py
+++ b/ckan/tests/legacy/functional/test_activity.py
@@ -14,10 +14,9 @@
from ckan.logic.action.update import user_update, group_update
from ckan.logic.action.delete import package_delete
from ckan.tests.legacy.html_check import HtmlCheckMethods
-from ckan.tests.legacy import CreateTestData, WsgiAppCase
+from ckan.tests.legacy import CreateTestData
-
-class TestActivity(WsgiAppCase, HtmlCheckMethods):
+class TestActivity(HtmlCheckMethods):
"""Test the rendering of activity streams into HTML pages.
Activity streams are tested in detail elsewhere, this class just briefly
@@ -30,6 +29,7 @@ def setup(cls):
raise SkipTest('Activity streams not enabled')
CreateTestData.create()
cls.sysadmin_user = ckan.model.User.get('testsysadmin')
+ cls.app = paste.fixture.TestApp(pylonsapp)
@classmethod
def teardown(cls):
@@ -52,9 +52,7 @@ def test_user_activity(self):
'allow_partial_update': True,
}
user = user_create(context, user_dict)
-
- with self.app.flask_app.test_request_context():
- offset = url_for(controller='user', action='activity', id=user['id'])
+ offset = url_for(controller='user', action='activity', id=user['id'])
result = self.app.get(offset, status=200)
stripped = self.strip_tags(result)
assert '%s signed up' % user['fullname'] in stripped, stripped
@@ -241,9 +239,7 @@ def test_user_activity(self):
# The user's dashboard page should load successfully and have the
# latest 15 activities on it.
-
- with self.app.flask_app.test_request_context():
- offset = url_for(controller='user', action='dashboard')
+ offset = url_for(controller='user', action='dashboard')
extra_environ = {'Authorization':
str(ckan.model.User.get('billybeane').apikey)}
result = self.app.post(offset, extra_environ=extra_environ,
diff --git a/ckan/tests/legacy/functional/test_admin.py b/ckan/tests/legacy/functional/test_admin.py
index 41ba685fc38..7737f036650 100644
--- a/ckan/tests/legacy/functional/test_admin.py
+++ b/ckan/tests/legacy/functional/test_admin.py
@@ -15,8 +15,7 @@ def teardown_class(self):
#test that only sysadmins can access the /ckan-admin page
def test_index(self):
- with self.app.flask_app.test_request_context():
- url = url_for('ckanadmin', action='index')
+ url = url_for('ckanadmin', action='index')
response = self.app.get(url, status=[403])
# random username
response = self.app.get(url, status=[403],
diff --git a/ckan/tests/legacy/functional/test_group.py b/ckan/tests/legacy/functional/test_group.py
index 1e952879ad1..0223375f232 100644
--- a/ckan/tests/legacy/functional/test_group.py
+++ b/ckan/tests/legacy/functional/test_group.py
@@ -95,9 +95,7 @@ def test_sorting(self):
def test_read_non_existent(self):
name = u'group_does_not_exist'
-
- with self.app.flask_app.test_request_context():
- offset = url_for(controller='group', action='read', id=name)
+ offset = url_for(controller='group', action='read', id=name)
res = self.app.get(offset, status=404)
@@ -133,10 +131,8 @@ def teardown_class(self):
model.repo.rebuild_db()
def test_2_atom_feed(self):
-
- with self.app.flask_app.test_request_context():
- offset = url_for(controller='group', action='history',
- id=self.grp.name)
+ offset = url_for(controller='group', action='history',
+ id=self.grp.name)
offset = "%s?format=atom" % offset
res = self.app.get(offset)
assert ']')
# edit the package
-
- with self.app.flask_app.test_request_context():
- self.offset = url_for(controller='package', action='edit', id=self.editpkg_name)
+ self.offset = url_for(controller='package', action='edit', id=self.editpkg_name)
self.res = self.app.get(self.offset, extra_environ=self.extra_environ_admin)
fv = self.res.forms['dataset-edit']
fv['title'] = u'New Title'
@@ -564,8 +547,7 @@ def test_delete(self):
plugins.load('test_package_controller_plugin')
plugin = plugins.get_plugin('test_package_controller_plugin')
- with self.app.flask_app.test_request_context():
- offset = url_for(controller='package', action='delete',
+ offset = url_for(controller='package', action='delete',
id='warandpeace')
# Since organizations, any owned dataset can be edited/deleted by any
# user
@@ -600,9 +582,7 @@ def teardown_class(self):
def test_new_plugin_hook(self):
plugins.load('test_package_controller_plugin')
plugin = plugins.get_plugin('test_package_controller_plugin')
-
- with self.app.flask_app.test_request_context():
- offset = url_for(controller='package', action='new')
+ offset = url_for(controller='package', action='new')
res = self.app.get(offset, extra_environ=self.extra_environ_tester)
new_name = u'plugged'
fv = res.forms['dataset-edit']
@@ -617,9 +597,7 @@ def test_new_plugin_hook(self):
def test_after_create_plugin_hook(self):
plugins.load('test_package_controller_plugin')
plugin = plugins.get_plugin('test_package_controller_plugin')
-
- with self.app.flask_app.test_request_context():
- offset = url_for(controller='package', action='new')
+ offset = url_for(controller='package', action='new')
res = self.app.get(offset, extra_environ=self.extra_environ_tester)
new_name = u'plugged2'
fv = res.forms['dataset-edit']
@@ -639,8 +617,8 @@ def test_new_indexerror(self):
try:
SolrSettings.init(bad_solr_url)
new_package_name = u'new-package-missing-solr'
- with self.app.flask_app.test_request_context():
- offset = url_for(controller='package', action='new')
+
+ offset = url_for(controller='package', action='new')
res = self.app.get(offset, extra_environ=self.extra_environ_tester)
fv = res.forms['dataset-edit']
fv['name'] = new_package_name
@@ -655,9 +633,7 @@ def test_new_indexerror(self):
SolrSettings.init(solr_url)
def test_change_locale(self):
-
- with self.app.flask_app.test_request_context():
- offset = url_for(controller='package', action='new')
+ offset = url_for(controller='package', action='new')
res = self.app.get(offset, extra_environ=self.extra_environ_tester)
res = self.app.get('/de/dataset/new', extra_environ=self.extra_environ_tester)
@@ -707,16 +683,12 @@ def teardown_class(self):
model.repo.rebuild_db()
def test_read(self):
-
- with self.app.flask_app.test_request_context():
- offset = url_for(controller='package', action='read', id=self.non_active_name)
+ offset = url_for(controller='package', action='read', id=self.non_active_name)
res = self.app.get(offset, status=[404])
def test_read_as_admin(self):
-
- with self.app.flask_app.test_request_context():
- offset = url_for(controller='package', action='read', id=self.non_active_name)
+ offset = url_for(controller='package', action='read', id=self.non_active_name)
res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER':'testsysadmin'})
@@ -746,9 +718,7 @@ def setup_class(cls):
cls.revision_ids = [rev[0].id for rev in cls.pkg1.all_related_revisions]
# revision ids are newest first
cls.revision_timestamps = [rev[0].timestamp for rev in cls.pkg1.all_related_revisions]
-
- with cls.app.flask_app.test_request_context():
- cls.offset = url_for(controller='package', action='history', id=cls.pkg1.name)
+ cls.offset = url_for(controller='package', action='history', id=cls.pkg1.name)
@classmethod
def teardown_class(cls):
diff --git a/ckan/tests/legacy/functional/test_pagination.py b/ckan/tests/legacy/functional/test_pagination.py
index 3a4fc7912c7..596bc5fa453 100644
--- a/ckan/tests/legacy/functional/test_pagination.py
+++ b/ckan/tests/legacy/functional/test_pagination.py
@@ -59,41 +59,25 @@ def teardown_class(self):
model.repo.rebuild_db()
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)
+ 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
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)
def test_package_search_p2(self):
-
- with self.app.flask_app.test_request_context():
- url = url_for(controller='package', action='search', q='groups:group_00', page=2)
-
- res = self.app.get(url)
+ 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
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)
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)
+ 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):
-
- 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)
+ 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)
@@ -117,20 +101,12 @@ def teardown_class(self):
model.repo.rebuild_db()
def test_group_index(self):
-
- with self.app.flask_app.test_request_context():
- url = url_for(controller='group', action='index')
-
- res = self.app.get(url)
+ 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)
-
- with self.app.flask_app.test_request_context():
- url = url_for(controller='group', action='index', page=2)
-
- res = self.app.get(url)
+ 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)
@@ -154,19 +130,12 @@ def teardown_class(self):
model.repo.rebuild_db()
def test_users_index(self):
-
- with self.app.flask_app.test_request_context():
- url = url_for(controller='user', action='index')
-
- res = self.app.get(url)
+ 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)
- with self.app.flask_app.test_request_context():
- url = url_for(controller='user', action='index', page=2)
-
- res = self.app.get(url)
+ 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)
diff --git a/ckan/tests/legacy/functional/test_preview_interface.py b/ckan/tests/legacy/functional/test_preview_interface.py
index 747834446e3..deb7aa6f965 100644
--- a/ckan/tests/legacy/functional/test_preview_interface.py
+++ b/ckan/tests/legacy/functional/test_preview_interface.py
@@ -19,16 +19,14 @@ def setup_class(cls):
cls.package = model.Package.get('annakarenina')
cls.resource = cls.package.resources[0]
-
- with cls.app.flask_app.test_request_context():
- cls.url = h.url_for(controller='package',
- action='resource_read',
- id=cls.package.name,
- resource_id=cls.resource.id)
- cls.preview_url = h.url_for(controller='package',
- action='resource_datapreview',
- id=cls.package.id,
- resource_id=cls.resource.id)
+ cls.url = h.url_for(controller='package',
+ action='resource_read',
+ id=cls.package.name,
+ resource_id=cls.resource.id)
+ cls.preview_url = h.url_for(controller='package',
+ action='resource_datapreview',
+ id=cls.package.id,
+ resource_id=cls.resource.id)
@classmethod
def teardown_class(cls):
@@ -37,9 +35,7 @@ def teardown_class(cls):
def test_hook(self):
testpackage = self.package
-
- with self.app.flask_app.test_request_context():
- resource_dict = model_dictize.resource_dictize(self.resource, {'model': model})
+ resource_dict = model_dictize.resource_dictize(self.resource, {'model': model})
context = {
'model': model,
@@ -74,12 +70,10 @@ def test_hook(self):
assert self.plugin.calls['preview_templates'] == 1, self.plugin.calls
# test whether the json preview is used
-
- with self.app.flask_app.test_request_context():
- preview_url = h.url_for(controller='package',
- action='resource_datapreview',
- id=testpackage.id,
- resource_id=testpackage.resources[1].id)
+ preview_url = h.url_for(controller='package',
+ action='resource_datapreview',
+ id=testpackage.id,
+ resource_id=testpackage.resources[1].id)
result = self.app.get(preview_url, status=200)
assert 'mock-json-preview' in result.body
diff --git a/ckan/tests/legacy/functional/test_revision.py b/ckan/tests/legacy/functional/test_revision.py
index 5312aad7c29..af0f653fa1c 100644
--- a/ckan/tests/legacy/functional/test_revision.py
+++ b/ckan/tests/legacy/functional/test_revision.py
@@ -90,9 +90,7 @@ def get_package(self, name):
def test_read(self):
anna = model.Package.by_name(u'annakarenina')
rev_id = anna.revision.id
-
- with self.app.flask_app.test_request_context():
- offset = url_for(controller='revision', action='read', id='%s' % rev_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
@@ -134,18 +132,14 @@ def test_list_format_atom(self):
# Todo: Test for first revision on last page.
# Todo: Test for last revision minus 50 on second page.
# Page 1. (Implied id=1)
-
- with self.app.flask_app.test_request_context():
- offset = url_for(controller='revision', action='list', format='atom')
+ offset = url_for(controller='revision', action='list', format='atom')
res = self.app.get(offset)
assert '' in res, res
# Todo: Better test for 'days' request param.
# - fake some older revisions and check they aren't included.
-
- with self.app.flask_app.test_request_context():
- offset = url_for(controller='revision', action='list', format='atom',
+ offset = url_for(controller='revision', action='list', format='atom',
days=30)
res = self.app.get(offset)
assert ' section'
the_html = self._get_html_from_res(html)
@@ -34,13 +33,13 @@ def strip_tags(self, res):
'''Call strip_tags on a TestResponse object to strip any and all HTML and normalise whitespace.'''
if not isinstance(res, basestring):
res = res.body.decode('utf-8')
- return Stripper().strip(res)
+ return Stripper().strip(res)
def check_named_element(self, html, tag_name, *html_to_find):
'''Searches in the html and returns True if it can find a particular
tag and all its subtags & data which contains all the of the
html_to_find'''
- named_element_re = re.compile('(<(%(tag)s\w*).*?(>.*?%(tag)s)?>)' % {'tag':tag_name})
+ named_element_re = re.compile('(<(%(tag)s\w*).*?(>.*?%(tag)s)?>)' % {'tag':tag_name})
html_str = self._get_html_from_res(html)
self._check_html(named_element_re, html_str.replace('\n', ''), html_to_find)
@@ -61,14 +60,13 @@ def check_tag(self, html, *html_to_find):
self._check_html(self.tag_re, html, html_to_find)
def _get_html_from_res(self, html):
- if isinstance(html, (paste.fixture.TestResponse, webtest.app.TestResponse)):
+ if isinstance(html, paste.fixture.TestResponse):
html_str = html.body.decode('utf8')
elif isinstance(html, unicode):
html_str = html
elif isinstance(html, str):
html_str = html.decode('utf8')
else:
- import ipdb; ipdb.set_trace()
raise TypeError
return html_str # always unicode
diff --git a/ckan/tests/legacy/lib/test_alphabet_pagination.py b/ckan/tests/legacy/lib/test_alphabet_pagination.py
index c3f4cdaa428..97d2773c634 100644
--- a/ckan/tests/legacy/lib/test_alphabet_pagination.py
+++ b/ckan/tests/legacy/lib/test_alphabet_pagination.py
@@ -8,7 +8,6 @@
from ckan.tests.legacy import regex_related
from ckan.lib.create_test_data import CreateTestData
from ckan import model
-from ckan.tests import helpers
other = 'Other'
@@ -55,9 +54,7 @@ def test_01_package_page(self):
page='A',
other_text=other,
)
- app = helpers._get_test_app()
- with app.flask_app.test_request_context():
- pager = page.pager()
+ pager = page.pager()
assert_true(
pager.startswith(
'