Skip to content

Commit

Permalink
Merge branch '3196-common-url_for-tests' into 3196-common-url_for
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Sep 1, 2016
2 parents 9b9313f + d3a4403 commit 23f9c53
Show file tree
Hide file tree
Showing 75 changed files with 2,057 additions and 1,125 deletions.
2 changes: 0 additions & 2 deletions ckan/config/deployment.ini_tmpl
Expand Up @@ -78,8 +78,6 @@ ckan.auth.roles_that_cascade_to_sub_groups = admin
ckan.site_id = default
#solr_url = http://127.0.0.1:8983/solr

#ckan.simple_search = 1


## CORS Settings

Expand Down
6 changes: 6 additions & 0 deletions ckan/config/middleware/flask_app.py
Expand Up @@ -193,6 +193,12 @@ def hello_world_post():
}
app = Fanstatic(app, **fanstatic_config)

# Update the main CKAN config object with the Flask specific keys
# that were set here or autogenerated
flask_config_keys = set(flask_app.config.keys()) - set(config.keys())
for key in flask_config_keys:
config[key] = flask_app.config[key]

# Add a reference to the actual Flask app so it's easier to access
app._wsgi_app = flask_app

Expand Down
2 changes: 1 addition & 1 deletion ckan/config/routing.py
Expand Up @@ -9,10 +9,10 @@
"""
import re

from pylons import config
from routes.mapper import SubMapper, Mapper as _Mapper

import ckan.plugins as p
from ckan.common import config

named_routes = {}

Expand Down
21 changes: 14 additions & 7 deletions ckan/controllers/feed.py
Expand Up @@ -170,12 +170,17 @@ def _alternate_url(self, params, **kwargs):
def _group_or_organization(self, obj_dict, is_org):

data_dict, params = self._parse_url_params()
key = 'owner_org' if is_org else 'groups'
data_dict['fq'] = '%s:"%s"' % (key, obj_dict['id'],)
group_type = 'organization'
if not is_org:
if is_org:
key = 'owner_org'
value = obj_dict['id']
group_type = 'organization'
else:
key = 'groups'
value = obj_dict['name']
group_type = 'group'

data_dict['fq'] = '{0}:"{1}"'.format(key, value)

item_count, results = _package_search(data_dict)

navigation_urls = self._navigation_urls(params,
Expand Down Expand Up @@ -259,12 +264,14 @@ def tag(self, id):

alternate_url = self._alternate_url(params, tags=id)

site_title = config.get('ckan.site_title', 'CKAN')

return self.output_feed(results,
feed_title=u'%s - Tag: "%s"' %
(g.site_title, id),
(site_title, id),
feed_description=u'Recently created or '
'updated datasets on %s by tag: "%s"' %
(g.site_title, id),
(site_title, id),
feed_link=alternate_url,
feed_guid=_create_atom_id
(u'/feeds/tag/%s.atom' % id),
Expand Down Expand Up @@ -346,7 +353,7 @@ def custom(self):
feed_title=u'%s - Custom query' % site_title,
feed_description=u'Recently created or updated'
' datasets on %s. Custom query: \'%s\'' %
(g.site_title, q),
(site_title, q),
feed_link=alternate_url,
feed_guid=_create_atom_id(atom_url),
feed_url=feed_url,
Expand Down
2 changes: 1 addition & 1 deletion ckan/lib/alphabet_paginate.py
Expand Up @@ -21,7 +21,7 @@
from sqlalchemy import __version__ as sqav
from sqlalchemy.orm.query import Query
from webhelpers.html.builder import HTML
from routes import url_for
from ckan.lib.helpers import url_for


class AlphaPage(object):
Expand Down
2 changes: 1 addition & 1 deletion ckan/lib/render.py
Expand Up @@ -4,7 +4,7 @@
import re
import logging

from pylons import config
from ckan.common import config

log = logging.getLogger(__name__)

Expand Down
21 changes: 4 additions & 17 deletions ckan/lib/search/__init__.py
Expand Up @@ -7,7 +7,6 @@
import xml.dom.minidom
import urllib2

from ckan.common import config
from paste.deploy.converters import asbool

import ckan.model as model
Expand All @@ -23,7 +22,6 @@
log = logging.getLogger(__name__)



def text_traceback():
with warnings.catch_warnings():
warnings.simplefilter("ignore")
Expand All @@ -32,7 +30,6 @@ def text_traceback():
).strip()
return res

SIMPLE_SEARCH = asbool(config.get('ckan.simple_search', False))

SUPPORTED_SCHEMA_VERSIONS = ['2.3']

Expand Down Expand Up @@ -60,11 +57,6 @@ def text_traceback():

SOLR_SCHEMA_FILE_OFFSET = '/admin/file/?file=schema.xml'

if SIMPLE_SEARCH:
import sql as sql
_INDICES['package'] = NoopSearchIndex
_QUERIES['package'] = sql.PackageSearchQuery


def _normalize_type(_type):
if isinstance(_type, model.domain_object.DomainObject):
Expand Down Expand Up @@ -219,6 +211,7 @@ def commit():
package_index.commit()
log.info('Commited pending changes on the search index')


def check():
package_query = query_for(model.Package)

Expand Down Expand Up @@ -249,10 +242,9 @@ def clear(package_reference):


def clear_all():
if not SIMPLE_SEARCH:
package_index = index_for(model.Package)
log.debug("Clearing search index...")
package_index.clear()
package_index = index_for(model.Package)
log.debug("Clearing search index...")
package_index.clear()


def check_solr_schema_version(schema_file=None):
Expand All @@ -276,11 +268,6 @@ def check_solr_schema_version(schema_file=None):
be only used for testing purposes (Default is None)
'''


if SIMPLE_SEARCH:
# Not using the SOLR search backend
return False

if not is_available():
# Something is wrong with the SOLR server
log.warn('Problems were found while connecting to the SOLR server')
Expand Down
42 changes: 0 additions & 42 deletions ckan/lib/search/sql.py

This file was deleted.

5 changes: 3 additions & 2 deletions ckan/tests/config/test_middleware.py
Expand Up @@ -3,7 +3,6 @@
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

Expand All @@ -30,7 +29,9 @@ 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()
response = app.get(url=url_for(controller='home', action='index'))
with app.flask_app.test_request_context():
response = app.get(
url=h.url_for(controller='home', action='index'))

assert_equals(200, response.status_int)
# make sure we haven't overwritten the response too early.
Expand Down
4 changes: 2 additions & 2 deletions ckan/tests/config/test_sessions.py
Expand Up @@ -5,7 +5,6 @@
from flask import Blueprint
from flask import render_template
from flask import redirect as flask_redirect
from flask import url_for
from ckan.lib.base import render as pylons_render

import ckan.plugins as p
Expand Down Expand Up @@ -73,7 +72,8 @@ def flash_message_view(self):
def add_flash_message_view_redirect_to_flask(self):
u'''Add flash message, then redirect to Flask view to render it.'''
h.flash_success(u'This is a success message populated by Flask')
return flask_redirect(url_for(u'test_flash_plugin.flash_message_view'))
return flask_redirect(
h.url_for(u'test_flash_plugin.flash_message_view'))

def add_flash_message_view_redirect_to_pylons(self):
u'''Add flash message, then redirect to view that renders it'''
Expand Down
36 changes: 24 additions & 12 deletions ckan/tests/controllers/test_admin.py
Expand Up @@ -3,7 +3,7 @@
from nose.tools import assert_true, assert_equal

from bs4 import BeautifulSoup
from routes import url_for
from ckan.lib.helpers import url_for
from ckan.common import config

import ckan.model as model
Expand All @@ -19,8 +19,10 @@
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_for(controller='admin', action='config'),
url=url,
extra_environ=env,
)
return env, response
Expand All @@ -30,8 +32,10 @@ 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_for(controller='admin', action='reset_config'),
url=url,
extra_environ=env,
)

Expand Down Expand Up @@ -262,17 +266,19 @@ 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()

trash_url = url_for(controller='admin', action='trash')
trash_response = app.get(trash_url, status=403)
with app.flask_app.test_request_context():
trash_url = url_for(controller='admin', action='trash')
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.'''
user = factories.User()
app = self._get_test_app()

env = {'REMOTE_USER': user['name'].encode('ascii')}
trash_url = url_for(controller='admin', action='trash')

with app.flask_app.test_request_context():
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)
Expand All @@ -283,7 +289,9 @@ def test_trash_view_sysadmin(self):
app = self._get_test_app()

env = {'REMOTE_USER': user['name'].encode('ascii')}
trash_url = url_for(controller='admin', action='trash')

with app.flask_app.test_request_context():
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)
Expand All @@ -296,7 +304,8 @@ def test_trash_no_datasets(self):
app = self._get_test_app()

env = {'REMOTE_USER': user['name'].encode('ascii')}
trash_url = url_for(controller='admin', action='trash')
with app.flask_app.test_request_context():
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)
Expand All @@ -315,7 +324,8 @@ def test_trash_with_deleted_datasets(self):
app = self._get_test_app()

env = {'REMOTE_USER': user['name'].encode('ascii')}
trash_url = url_for(controller='admin', action='trash')
with app.flask_app.test_request_context():
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)
Expand All @@ -338,7 +348,8 @@ def test_trash_purge_deleted_datasets(self):
assert_equal(pkgs_before_purge, 3)

env = {'REMOTE_USER': user['name'].encode('ascii')}
trash_url = url_for(controller='admin', action='trash')
with app.flask_app.test_request_context():
trash_url = url_for(controller='admin', action='trash')
trash_response = app.get(trash_url, extra_environ=env, status=200)

# submit the purge form
Expand All @@ -364,7 +375,8 @@ def _update_config_option(self):
sysadmin = factories.Sysadmin()
env = {'REMOTE_USER': sysadmin['name'].encode('ascii')}
app = self._get_test_app()
url = url_for(controller='admin', action='config')
with app.flask_app.test_request_context():
url = url_for(controller='admin', action='config')

response = app.get(url=url, extra_environ=env)
form = response.forms[1]
Expand Down

0 comments on commit 23f9c53

Please sign in to comment.