-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommon.py
More file actions
115 lines (89 loc) · 3.38 KB
/
common.py
File metadata and controls
115 lines (89 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from flask import url_for
from flask.ext.testing import JsonResponseMixin, TestCase as FlaskTestCase, \
Twill as BaseTwill
from twill import commands
from webtest import TestApp
from learnpython.app import app, mail
if not hasattr(FlaskTestCase, 'assertIn'):
from unittest2 import TestCase
BaseTestCase = type('BaseTestCase', (FlaskTestCase, TestCase), {})
else:
BaseTestCase = FlaskTestCase
__all__ = ('TEST_COMMENTS', 'TEST_EMAIL', 'TEST_MESSAGE', 'TEST_NAME',
'TEST_PHONE', 'TEST_SUBJECT', 'TestCase', 'Twill')
TEST_COMMENTS = 'Test additional comments.'
TEST_MESSAGE = 'Test message.'
TEST_NAME = 'Igor Davydenko'
TEST_PHONE = '+380 12 345-67-89'
TEST_SKYPE = 'igor.davydenko'
TEST_SUBJECT = 'Test subject'
class TestCase(BaseTestCase):
"""
Improve base test class from ``Flask-Testing`` with adding ``url`` method
and ``udata`` property to each test client response.
"""
ALLOW_SUBSCRIBERS = True
BABEL_DEFAULT_LOCALE = 'en'
CSRF_ENABLED = False
MAIL_SUPRESS_SEND = True
TESTING = True
def setUp(self):
self.about_url = self.url('page', name='about')
self.archive_url = self.url('page', name='archive')
self.contacts_url = self.url('contacts')
self.flows_url = self.url('flows')
self.index_url = self.url('index')
self.status_url = self.url('status')
self.subscribe_url = self.url('subscribe')
def tearDown(self):
for attr in dir(self):
if not attr.startswith('original_'):
continue
key = attr.replace('original_', '')
self.app.config[key] = getattr(self, attr)
def check_message(self, message, subject, *args):
assert len(args) > 2
name, email = args[:2]
self.assertEqual(message.subject, '[Learn Python] {0}'.format(subject))
self.assertEqual(
)
self.assertEqual(message.sender, '{0} <{1}>'.format(name, email))
self.assertIn('Learn Python', message.body)
for arg in args:
self.assertIn(arg, message.body)
def config(self, key, value):
setattr(self, 'original_{0}'.format(key), self.app.config.get(key))
self.app.config[key] = value
def create_app(self):
for attr in dir(self):
if attr.isupper():
app.config[attr] = getattr(self, attr)
return app
def url(self, *args, **kwargs):
return url_for(*args, **kwargs)
def _pre_setup(self):
super(TestCase, self)._pre_setup()
self.assertTrue(self.app.testing)
self.app.response_class = \
make_response_class(self._orig_response_class)
self.client = self.app.test_client()
mail.suppress = True
self.mail = mail
self.webtest = TestApp(self.app)
class Twill(BaseTwill):
"""
Update ``Twill`` mixin class from ``Flask-Testing``. Return tuple contains
current instance and all available twill commands on entering to context
with ``__enter__`` method.
"""
def __enter__(self):
super(Twill, self).__enter__()
return self, commands
def make_response_class(response_class):
class TestResponse(response_class, JsonResponseMixin):
@property
def udata(self):
return self.data.decode('utf-8')
return TestResponse