forked from jarus/flask-fillin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
145 lines (109 loc) · 4.96 KB
/
tests.py
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
# -*- coding: utf-8 -*-
"""
flask-fillin-tests
~~~~~~~~~~~~~~~~~~
:copyright: (c) 2012 by Christoph Heer.
:license: BSD, see LICENSE for more details.
"""
import unittest
from test_app import app
from flask.testing import FlaskClient
from flask.ext.fillin import FormWrapper
class fillinTest(unittest.TestCase):
def setUp(self):
self.app = app
self.app.testing = True
self.client = FlaskClient(self.app, response_wrapper=FormWrapper)
def tearDown(self):
pass
def test_login_form(self):
response = self.client.get('/login-form')
response.form.fields['username'] = "test"
assert 'Missing password' in response.form.submit(self.client).data
response = self.client.get('/login-form')
response.form.fields['password'] = "secret"
assert 'Missing username' in response.form.submit(self.client).data
response = self.client.get('/login-form')
response.form.fields['username'] = "test"
response.form.fields['password'] = "secret"
assert 'Welcome test' in response.form.submit(self.client).data
def test_data_injection(self):
response = self.client.get('/login-form')
response.form.fields['password'] = "secret"
inject_data = {'username': 'test'}
response = response.form.submit(self.client, data=inject_data)
assert 'Welcome test' in response.data
def test_hidden_field_form(self):
response = self.client.get('/hidden-field-form')
response = response.form.submit(self.client)
assert 'Hidden field received' in response.data
response = self.client.post('/hidden-field-form')
assert 'Missing the hidden field' in response.data
def test_checkbox_form(self):
response = self.client.get('/checkbox-field-form')
response = response.form.submit(self.client)
assert "Checkbox did not check" in response.data
response.form.fields['checkbox_field'] = True
response = response.form.submit(self.client)
assert "Checkbox checked" in response.data
def test_select_form(self):
response = self.client.get('/select-field-form')
response = response.form.submit(self.client)
assert "select1" in response.data
response.form.fields['select_field'] = "select2"
response = response.form.submit(self.client)
assert "select2" in response.data
def test_radio_form(self):
response = self.client.get('/radio-field-form')
response = response.form.submit(self.client)
assert "No Radio Value Selected" in response.data
response.form.fields['radio_field'] = "1"
response = response.form.submit(self.client)
assert "Selected 1" in response.data
response.form.fields['radio_field'] = "0"
response = response.form.submit(self.client)
assert "Selected 0" in response.data
def test_empty_field_form(self):
response = self.client.get('/empty-field-form')
response.form.fields['text1'] = 'hi'
# response.form.fields['text2'] = ''
response = response.form.submit(self.client)
assert "No None" in response.data
response = self.client.get('/empty-field-form')
response = response.form.submit(self.client)
assert "No None" in response.data
def test_links_get(self):
response = self.client.get('/link')
self.assertEquals(2, len(response.links()), '2 links are parsed from the source')
self.assertEquals('link1', response.link('#link1').text)
self.assertEquals('link2', response.link('.link').text)
def test_link_click(self):
response = self.client.get('/link')
response = response.link("#link1").click(self.client)
self.assertEquals(200, response.status_code)
def test_file_form(self):
response = self.client.get('file-form')
response.form.fields['text'] = 'text'
with open("README.rst") as fh:
response.form.files['file'] = fh
response = response.form.submit(self.client)
assert "File submitted" in response.data
def test_no_file_form(self):
response = self.client.get('file-form')
response.form.fields['text'] = 'text'
response = response.form.submit(self.client)
assert "File not submitted" in response.data
def test_bad_file_form(self):
response = self.client.get('file-form')
response.form.fields['text'] = 'text'
response.form.files['file'] = 'not a file type'
with self.assertRaises(TypeError):
self.response = response.form.submit(self.client)
def test_bad_input_form(self):
response = self.client.get('file-form')
response.form.fields['text'] = 'text'
with open("README.rst") as fh, self.assertRaises(ValueError):
response.form.files['not_file_name'] = fh
response = response.form.submit(self.client)
if __name__ == '__main__':
unittest.main()