Skip to content

Commit 9b4a413

Browse files
Massive update to tests, add ability to safe setup application config, check further deny subscribers feature.
1 parent 1a9964a commit 9b4a413

5 files changed

Lines changed: 46 additions & 3 deletions

File tree

learnpython/tests/common.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class TestCase(BaseTestCase):
3030
Improve base test class from ``Flask-Testing`` with adding ``url`` method
3131
and ``udata`` property to each test client response.
3232
"""
33+
ALLOW_SUBSCRIBERS = True
3334
BABEL_DEFAULT_LOCALE = 'en'
3435
CSRF_ENABLED = False
3536
TESTING = True
@@ -42,6 +43,14 @@ def setUp(self):
4243
self.status_url = self.url('status')
4344
self.subscribe_url = self.url('subscribe')
4445

46+
def tearDown(self):
47+
for attr in dir(self):
48+
if not attr.startswith('original_'):
49+
continue
50+
51+
key = attr.replace('original_', '')
52+
self.app.config[key] = getattr(self, attr)
53+
4554
def check_message(self, message, subject, *args):
4655
assert len(args) > 2
4756
name, email = args[:2]
@@ -56,6 +65,10 @@ def check_message(self, message, subject, *args):
5665
for arg in args:
5766
self.assertIn(arg, message.body)
5867

68+
def config(self, key, value):
69+
setattr(self, 'original_{0}'.format(key), self.app.config.get(key))
70+
self.app.config[key] = value
71+
5972
def create_app(self):
6073
for attr in dir(self):
6174
if attr.isupper():

learnpython/tests/test_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_flows(self):
3131

3232
def test_pages(self):
3333
data = filter(lambda key: not '/' in key, pages._pages.keys())
34-
self.assertEqual(len(data), 4)
34+
self.assertEqual(len(data), 5)
3535

3636
self.check_page('about')
3737
self.check_page('contacts')

learnpython/tests/test_flasktesting.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ def test_index(self):
9494
for url in urls:
9595
self.assertIn('href="{0}"'.format(url), response.data)
9696

97+
def test_nosubscribe(self):
98+
self.config('ALLOW_SUBSCRIBERS', False)
99+
100+
response = self.client.get(self.subscribe_url)
101+
self.assertEqual(response.status_code, 200)
102+
self.assertNotIn('<form action="" ', response.data)
103+
self.assertIn('<h2>', response.data)
104+
97105
def test_static(self):
98106
url = self.url('static', filename='css/screen.css')
99107
response = self.client.get(url)

learnpython/tests/test_twill.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ def test_index(self):
190190
c.code(200)
191191
c.url(t.url(self.index_url))
192192

193+
def test_nosubscribe(self):
194+
self.config('ALLOW_SUBSCRIBERS', False)
195+
196+
with Twill(self.app, port=self.port) as (t, c):
197+
c.go(t.url(self.subscribe_url))
198+
c.code(200)
199+
c.notfind('<form action="" ')
200+
c.find('<h2>')
201+
193202
def test_static(self):
194203
url200 = self.url('static', filename='css/screen.css')
195204
url404 = self.url('static', filename='does_not_exists.exe')

learnpython/tests/test_webtest.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def test_flows(self):
114114
def test_index(self):
115115
self.check_page('index', self.index_url)
116116

117-
response = self.webtest.get(self.index_url)
117+
response = self.webtest.get(self.index_url, status=200)
118118
doc = response.pyquery
119119

120120
self.assertEqual(len(doc('a.active')), 1)
@@ -125,7 +125,7 @@ def test_index(self):
125125
self.assertEqual(len(doc('a[href="{0}"]'.format(self.flows_url))), 0)
126126
self.assertEqual(len(doc('a[href="{0}"]'.format(self.index_url))), 1)
127127
self.assertEqual(
128-
len(doc('a[href="{0}"]'.format(self.subscribe_url))), 3
128+
len(doc('a[href="{0}"]'.format(self.subscribe_url))), 2
129129
)
130130

131131
result = (
@@ -141,6 +141,19 @@ def test_index(self):
141141
)
142142
self.check_links(doc('nav a'), result)
143143

144+
def test_nosubscribe(self):
145+
self.config('ALLOW_SUBSCRIBERS', False)
146+
147+
response = self.webtest.get(self.subscribe_url, status=200)
148+
doc = response.pyquery
149+
150+
self.assertEqual(len(doc('form')), 0)
151+
self.assertEqual(len(doc('h2')), 1)
152+
153+
title = doc('h2')[0]
154+
page = pages.get('nosubscribe')
155+
self.assertEqual(title.text, page.meta['title'])
156+
144157
def test_static(self):
145158
url = self.url('static', filename='css/screen.css')
146159
response = self.webtest.get(url, status=200)

0 commit comments

Comments
 (0)