Skip to content

Commit

Permalink
Simplify non-controller tests, using a context on setup methods
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Jun 6, 2016
1 parent fdd7cdb commit 36f5a32
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 81 deletions.
120 changes: 59 additions & 61 deletions ckan/tests/lib/test_helpers.py
Expand Up @@ -19,180 +19,178 @@

class BaseUrlFor(object):

def __init__(self):
@classmethod
def setup_class(cls):

self.app = helpers._get_test_app()
cls.app = helpers._get_test_app()

def url_for(self, *args, **kw):
with self.app.flask_app.test_request_context():
return h.url_for(*args, **kw)
def setup(self):

def url_for_static(self, *args, **kw):
with self.app.flask_app.test_request_context():
return h.url_for_static(*args, **kw)
self.app_context = self.app.flask_app.app_context()
self.app_context.push()

def url_for_static_or_external(self, *args, **kw):
with self.app.flask_app.test_request_context():
return h.url_for_static_or_external(*args, **kw)
def teardown(self):

self.app_context.pop()


class TestHelpersUrlForStatic(BaseUrlFor):

def test_url_for_static(self):
url = '/assets/ckan.jpg'
eq_(self.url_for_static(url), url)
eq_(h.url_for_static(url), url)

def test_url_for_static_adds_starting_slash_if_url_doesnt_have_it(self):
slashless_url = 'ckan.jpg'
url = '/' + slashless_url
eq_(self.url_for_static(slashless_url), url)
eq_(h.url_for_static(slashless_url), url)

def test_url_for_static_converts_unicode_strings_to_regular_strings(self):
url = u'/ckan.jpg'
assert isinstance(self.url_for_static(url), str)
assert isinstance(h.url_for_static(url), str)

def test_url_for_static_raises_when_called_with_external_urls(self):
url = 'http://assets.ckan.org/ckan.jpg'
nose.tools.assert_raises(CkanUrlException, self.url_for_static, url)
nose.tools.assert_raises(CkanUrlException, h.url_for_static, url)

def test_url_for_static_raises_when_called_with_protocol_relative(self):
url = '//assets.ckan.org/ckan.jpg'
nose.tools.assert_raises(CkanUrlException, self.url_for_static, url)
nose.tools.assert_raises(CkanUrlException, h.url_for_static, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo')
def test_url_for_static_with_root_path(self):
url = '/my/custom/path/foo/my-asset/file.txt'
generated_url = self.url_for_static('/my-asset/file.txt')
generated_url = h.url_for_static('/my-asset/file.txt')
eq_(generated_url, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo')
def test_url_for_static_qualified_with_root_path(self):
url = 'http://example.com/my/custom/path/foo/my-asset/file.txt'
generated_url = self.url_for_static('/my-asset/file.txt',
qualified=True)
generated_url = h.url_for_static('/my-asset/file.txt',
qualified=True)
eq_(generated_url, url)

@helpers.set_extra_environ('SCRIPT_NAME', '/my/custom/path')
@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo')
def test_url_for_static_with_root_path_and_script_name_env(self):
url = 'http://example.com/my/custom/path/foo/my-asset/file.txt'
generated_url = self.url_for_static('/my-asset/file.txt',
qualified=True)
generated_url = h.url_for_static('/my-asset/file.txt',
qualified=True)
eq_(generated_url, url)


class TestHelpersUrlForStaticOrExternal(BaseUrlFor):

def test_url_for_static_or_external(self):
url = '/assets/ckan.jpg'
eq_(self.url_for_static_or_external(url), url)
eq_(h.url_for_static_or_external(url), url)

def test_url_for_static_or_external_works_with_external_urls(self):
url = 'http://assets.ckan.org/ckan.jpg'
eq_(self.url_for_static_or_external(url), url)
eq_(h.url_for_static_or_external(url), url)

def test_url_for_static_or_external_converts_unicode_to_strings(self):
url = u'/ckan.jpg'
assert isinstance(self.url_for_static_or_external(url), str)
assert isinstance(h.url_for_static_or_external(url), str)

def test_url_for_static_or_external_adds_starting_slash_if_needed(self):
slashless_url = 'ckan.jpg'
url = '/' + slashless_url
eq_(self.url_for_static_or_external(slashless_url), url)
eq_(h.url_for_static_or_external(slashless_url), url)

def test_url_for_static_or_external_works_with_protocol_relative_url(self):
url = '//assets.ckan.org/ckan.jpg'
eq_(self.url_for_static_or_external(url), url)
eq_(h.url_for_static_or_external(url), url)


class TestHelpersUrlFor(BaseUrlFor):

@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_default(self):
url = '/dataset/my_dataset'
generated_url = self.url_for(controller='package', action='read',
id='my_dataset')
generated_url = h.url_for(controller='package', action='read',
id='my_dataset')
eq_(generated_url, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_with_locale(self):
url = '/de/dataset/my_dataset'
generated_url = self.url_for(controller='package',
action='read',
id='my_dataset',
locale='de')
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
locale='de')
eq_(generated_url, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/foo/{{LANG}}')
def test_url_for_with_locale_object(self):
url = '/foo/de/dataset/my_dataset'
generated_url = self.url_for('/dataset/my_dataset',
locale=Locale('de'))
generated_url = h.url_for('/dataset/my_dataset',
locale=Locale('de'))
eq_(generated_url, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_not_qualified(self):
url = '/dataset/my_dataset'
generated_url = self.url_for(controller='package',
action='read',
id='my_dataset',
qualified=False)
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=False)
eq_(generated_url, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_qualified(self):
url = 'http://example.com/dataset/my_dataset'
generated_url = self.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True)
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True)
eq_(generated_url, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/prefix')
def test_url_for_qualified_with_root_path(self):
url = 'http://example.com/my/prefix/dataset/my_dataset'
generated_url = self.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True)
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True)
eq_(generated_url, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_qualified_with_locale(self):
url = 'http://example.com/de/dataset/my_dataset'
generated_url = self.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True,
locale='de')
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True,
locale='de')
eq_(generated_url, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo')
def test_url_for_qualified_with_root_path_and_locale(self):
url = 'http://example.com/my/custom/path/de/foo/dataset/my_dataset'
generated_url = self.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True,
locale='de')
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True,
locale='de')
eq_(generated_url, url)

@helpers.set_extra_environ('SCRIPT_NAME', '/my/custom/path')
@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo')
def test_url_for_qualified_with_root_path_locale_and_script_name_env(self):
url = 'http://example.com/my/custom/path/de/foo/dataset/my_dataset'
generated_url = self.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True,
locale='de')
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True,
locale='de')
eq_(generated_url, url)


Expand Down
36 changes: 16 additions & 20 deletions ckan/tests/lib/test_mailer.py
Expand Up @@ -42,7 +42,6 @@ def setup_class(cls):
# Used to provide a context for url_for
cls.app = helpers._get_test_app()


@classmethod
def teardown_class(cls):
SmtpServerHarness.teardown_class()
Expand All @@ -51,6 +50,13 @@ def teardown_class(cls):
def setup(self):
self.clear_smtp_messages()

self.app_context = self.app.flask_app.app_context()
self.app_context.push()

def teardown(self):

self.app_context.pop()

def mime_encode(self, msg, recipient_name):
text = MIMEText(msg.encode('utf-8'), 'plain', 'utf-8')
encoded_body = text.get_payload().strip()
Expand Down Expand Up @@ -160,9 +166,7 @@ def test_send_reset_email(self):
user = factories.User()
user_obj = model.User.by_name(user['name'])

# We need to provide a context as url_for is used internally
with self.app.flask_app.test_request_context():
mailer.send_reset_link(user_obj)
mailer.send_reset_link(user_obj)

# check it went to the mock smtp server
msgs = self.get_smtp_messages()
Expand All @@ -171,9 +175,7 @@ def test_send_reset_email(self):
assert_equal(msg[1], config['smtp.mail_from'])
assert_equal(msg[2], [user['email']])
assert 'Reset' in msg[3], msg[3]
# We need to provide a context as url_for is used internally
with self.app.flask_app.test_request_context():
test_msg = mailer.get_reset_link_body(user_obj)
test_msg = mailer.get_reset_link_body(user_obj)
expected_body = self.mime_encode(test_msg,
user['name'])

Expand All @@ -184,12 +186,10 @@ def test_send_invite_email(self):
user_obj = model.User.by_name(user['name'])
assert user_obj.reset_key is None, user_obj

# We need to provide a context as url_for is used internally
with self.app.flask_app.test_request_context():
# send email
mailer.send_invite(user_obj)
# send email
mailer.send_invite(user_obj)

test_msg = mailer.get_invite_body(user_obj)
test_msg = mailer.get_invite_body(user_obj)

# check it went to the mock smtp server
msgs = self.get_smtp_messages()
Expand All @@ -210,10 +210,8 @@ def test_send_invite_email_with_group(self):
group = factories.Group()
role = 'member'

# We need to provide a context as url_for is used internally
with self.app.flask_app.test_request_context():
# send email
mailer.send_invite(user_obj, group_dict=group, role=role)
# send email
mailer.send_invite(user_obj, group_dict=group, role=role)

# check it went to the mock smtp server
msgs = self.get_smtp_messages()
Expand All @@ -229,10 +227,8 @@ def test_send_invite_email_with_org(self):
org = factories.Organization()
role = 'admin'

# We need to provide a context as url_for is used internally
with self.app.flask_app.test_request_context():
# send email
mailer.send_invite(user_obj, group_dict=org, role=role)
# send email
mailer.send_invite(user_obj, group_dict=org, role=role)

# check it went to the mock smtp server
msgs = self.get_smtp_messages()
Expand Down

0 comments on commit 36f5a32

Please sign in to comment.