forked from freifunk-berlin/ca.berlin.freifunk.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
125 lines (101 loc) · 4.41 KB
/
test.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
import os
import unittest
import tempfile
from flask_mail import Mail
from ca import app, db
from ca.models import Request
class FlaskrTestCase(unittest.TestCase):
def setUp(self):
self.db_fd, self.temp_filepath = tempfile.mkstemp()
database_path = 'sqlite:///{}'.format(self.temp_filepath)
app.config['SQLALCHEMY_DATABASE_URI'] = database_path
app.config['TESTING'] = True
self.mail = Mail(app)
self.app = app.test_client()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
os.close(self.db_fd)
os.remove(self.temp_filepath)
def test_empty_db(self):
entries = db.session.query(Request).all()
assert len(entries) == 0
def test_get_index(self):
response = self.app.get("/")
assert response.status_code == 200
def test_make_request(self):
with self.mail.record_messages() as outbox:
example_data = dict(id='foobar',
email='email@provider.com',
email_confirm='email@provider.com',
captcha='Berlin'
)
response = self.app.post('/',
data=example_data,
follow_redirects=True)
assert response.status_code == 200
entries = db.session.query(Request).all()
assert len(entries) == 1
assert len(outbox) == 1
assert outbox[0].subject == "Deine Anfrage für das Freifunk VPN ist eingegangen!"
assert "Hallo Freifunka!" in outbox[0].body
def test_duplicate_id(self):
with self.mail.record_messages() as outbox:
example_data = dict(id='foobar',
email='email@provider.com',
email_confirm='email@provider.com',
captcha='Berlin'
)
response = self.app.post('/',
data=example_data,
follow_redirects=True)
assert response.status_code == 200
entries = db.session.query(Request).all()
assert len(entries) == 1
assert len(outbox) == 1
assert outbox[0].subject == "Deine Anfrage für das Freifunk VPN ist eingegangen!"
assert "Hallo Freifunka!" in outbox[0].body
response = self.app.post('/',
data=example_data,
follow_redirects=True)
assert 'Id already exists.' in str(response.data)
# The new entry should not have been added so we should
# still only have on entry
entries = db.session.query(Request).all()
assert len(entries) == 1
def test_invalid_request_too_short(self):
example_data = dict(id='a',
email='email@provider.com',
email_confirm='email@provider.com',
captcha='Berlin'
)
response = self.app.post('/',
data=example_data,
follow_redirects=True)
entries = db.session.query(Request).all()
assert len(entries) == 0
def test_invalid_request_underscore(self):
example_data = dict(id='underscores_not_allowed',
email='email@provider.com',
email_confirm='email@provider.com',
captcha='Berlin'
)
response = self.app.post('/',
data=example_data,
follow_redirects=True)
entries = db.session.query(Request).all()
assert len(entries) == 0
def test_invalid_request_mail_to_long(self):
example_data = dict(id='validusername',
email='a'*40+'@foo.bar',
email_confirm='a'*40+'@foo.bar',
captcha='Berlin'
)
response = self.app.post('/',
data=example_data,
follow_redirects=True)
entries = db.session.query(Request).all()
assert len(entries) == 0
if __name__ == '__main__':
unittest.main()