-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_webtest.py
More file actions
189 lines (147 loc) · 6.66 KB
/
test_webtest.py
File metadata and controls
189 lines (147 loc) · 6.66 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# -*- coding: utf-8 -*-
from random import choice
from learnpython.app import pages
from learnpython.forms import FLOW_CHOICES
from .common import TEST_COMMENTS, TEST_EMAIL, TEST_NAME, TEST_MESSAGE, \
TEST_PHONE, TEST_SKYPE, TEST_SUBJECT, TestCase
class TestViewsWithWebTest(TestCase):
def assertRedirects(self, response, location):
location = ':80{0}'.format(location)
super(TestViewsWithWebTest, self).assertRedirects(response, location)
def check_form_errors(self, url, collection):
response = self.webtest.get(url)
for data in collection:
error = data.pop('error', 'This field is required.')
form = response.form
for key, value in data.items():
form[key] = value
response = form.submit()
self.assert200(response)
self.assertTrue(response.pyquery('form > p.error'))
self.assertTrue(response.pyquery('form > p.error-line'))
response.mustcontain(
'Cannot submit form! Please, fix errors below:'
)
response.mustcontain(error)
def check_form_success(self, url, collection, subject):
for data in collection:
response = self.webtest.get(url)
form = response.form
subject = data.get('subject', subject)
for key, value in data.items():
form[key] = value
with self.mail.record_messages() as outbox:
response = form.submit()
self.assertStatus(response, 302)
self.assertIn(self.status_url, response.headers['Location'])
args = [data.pop('name'), data.pop('email')]
data.pop('subject', None)
args.extend(data.values())
self.assertEqual(len(outbox), 1)
self.check_message(outbox[0], subject, *args)
def check_links(self, links, result):
self.assertEqual(len(links), len(result))
for i, link in enumerate(links):
href, text = result[i]
self.assertEqual(link.attrib['href'], href)
self.assertEqual(link.text, text)
def check_page(self, name, url):
page = pages.get(name)
self.assertIsNotNone(page)
response = self.webtest.get(url, status=200)
self.assertEqual(response.pyquery('article h2').text(),
page['title'])
def test_about(self):
self.check_page('about', self.about_url)
def test_archive(self):
self.check_page('archive', self.archive_url)
def test_contacts(self):
self.check_page('contacts', self.contacts_url)
collection = (
{},
{'name': '', 'email': TEST_EMAIL, 'message': TEST_MESSAGE},
{'name': TEST_NAME, 'email': '', 'message': TEST_MESSAGE},
{'name': TEST_NAME, 'email': TEST_EMAIL, 'message': ''},
{'name': TEST_NAME, 'email': TEST_NAME, 'message': TEST_MESSAGE,
'error': 'Invalid email address.'},
)
self.check_form_errors(self.contacts_url, collection)
collection = (
{'name': TEST_NAME, 'email': TEST_EMAIL, 'message': TEST_MESSAGE},
{'name': TEST_NAME, 'email': TEST_EMAIL, 'subject': TEST_SUBJECT,
'message': TEST_MESSAGE},
)
self.check_form_success(self.contacts_url, collection, 'Feedback')
def test_flows(self):
flows = filter(lambda item: item[0].startswith('flows/'),
pages._pages.items())
response = self.webtest.get(self.flows_url, status=200)
doc = response.pyquery
for fullname, flow in flows:
name = fullname.replace('flows/', '')
elements = doc('#{0}'.format(name))
self.assertTrue(elements)
self.assertEqual(len(elements), 1)
element = elements[0]
method = self.assertIn if flow['active'] else self.assertNotIn
method('active', element.attrib['class'])
def test_index(self):
self.check_page('index', self.index_url)
response = self.webtest.get(self.index_url, status=200)
doc = response.pyquery
self.assertEqual(len(doc('a.active')), 1)
self.assertEqual(len(doc('a[href="{0}"]'.format(self.about_url))), 2)
self.assertEqual(
len(doc('a[href="{0}"]'.format(self.contacts_url))), 3
)
self.assertEqual(len(doc('a[href="{0}"]'.format(self.flows_url))), 0)
self.assertEqual(len(doc('a[href="{0}"]'.format(self.index_url))), 1)
self.assertEqual(
len(doc('a[href="{0}"]'.format(self.subscribe_url))), 2
)
result = (
(self.about_url, 'About us'),
(self.archive_url, 'Flows archive'),
(self.contacts_url, 'Contacts')
)
self.check_links(doc('header .left-wrapper p a'), result)
result = (
(self.flows_url + '#async', 'Async flow'),
(self.flows_url + '#web', 'Web flow'),
(self.flows_url + '#optimization', 'Optimization flow'),
(self.subscribe_url, u'Subscribe →'),
)
self.check_links(doc('nav a'), result)
def test_nosubscribe(self):
self.config('ALLOW_SUBSCRIBERS', False)
response = self.webtest.get(self.subscribe_url, status=200)
doc = response.pyquery
self.assertEqual(len(doc('form')), 0)
self.assertEqual(len(doc('h2')), 1)
title = doc('h2')[0]
page = pages.get('nosubscribe')
self.assertEqual(title.text, page.meta['title'])
def test_static(self):
url = self.url('static', filename='css/screen.css')
response = self.webtest.get(url, status=200)
url = self.url('static', filename='does_not_exist.exe')
response = self.webtest.get(url, status=404)
def test_subscribe(self):
self.check_page('subscribe', self.subscribe_url)
flow = choice(FLOW_CHOICES)[0]
collection = (
{},
{'name': '', 'email': TEST_EMAIL, 'flow': flow},
{'name': TEST_NAME, 'email': '', 'flow': flow},
{'name': TEST_NAME, 'email': TEST_NAME, 'flow': flow,
'error': 'Invalid email address.'},
)
self.check_form_errors(self.subscribe_url, collection)
collection = (
{'name': TEST_NAME, 'email': TEST_EMAIL, 'flow': flow},
{'name': TEST_NAME, 'email': TEST_EMAIL, 'phone': TEST_PHONE,
'skype': TEST_SKYPE, 'flow': flow, 'comments': TEST_COMMENTS}
)
self.check_form_success(self.subscribe_url,
collection,
'Flow subscription: {0}'.format(flow))