forked from codeforamerica/brigade
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
199 lines (154 loc) · 8.22 KB
/
tests.py
File metadata and controls
199 lines (154 loc) · 8.22 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
190
191
192
193
194
195
196
197
198
199
from urlparse import parse_qsl
from base64 import b64decode
import unittest
import json
import os
import flask
from httmock import response, HTTMock
os.environ['BRIGADE_SIGNUP_SECRET'] = 'muy bueno'
from app import app
class BrigadeTests(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
def tearDown(self):
pass
def response_content(self, url, request):
if "list-manage.com/subscribe/post" in url.geturl():
return response(200, '{ "status_code" : 200, "msg" : "Almost finished... We need to confirm your email address. To complete the subscription process, please click the link in the email we just sent you."}')
if url.geturl() == 'http://www.codeforamerica.org/fragments/email-signup.html' \
or url.geturl() == 'http://www.codeforamerica.org/fragments/global-footer.html':
return response(200, '''<html>bunch of HTML</html>''')
if url.geturl() == 'https://www.codeforamerica.org/api/organizations/404':
return response(404, '{"status": "Resource Not Found"}')
if url.geturl() == 'https://www.codeforamerica.org/api/organizations/Code-for-San-Francisco':
return response(200, '{"city": "San Francisco, CA"}')
if url.geturl() == "https://www.codeforamerica.org/api/organizations.geojson":
return response(200, '{"features" : [{ "properties" : { "id" : "TEST-ORG", "type" : "Brigade" } } ] }')
if url.geturl() == "https://www.codeforamerica.org/api/attendance":
return response(200, '{"total": 100, "weekly" : {"1999" : "100"}}')
if url.geturl() == 'https://people.codeforamerica.org/brigade/signup':
if request.method == 'POST':
form = dict(parse_qsl(request.body))
username, password = None, None
if 'Authorization' in request.headers:
method, encoded = request.headers['Authorization'].split(' ', 1)
if method == 'Basic':
username, password = b64decode(encoded).split(':', 1)
if (username, password) == (os.environ['BRIGADE_SIGNUP_SECRET'], 'x-brigade-signup'):
return response(200, 'Added to the peopledb')
if form.get('BRIGADE_SIGNUP_SECRET') == os.environ['BRIGADE_SIGNUP_SECRET']:
return response(200, 'Added to the peopledb')
return response(401, 'Go away')
if url.geturl() == 'https://people.codeforamerica.org/checkin':
if request.method == 'POST':
form = dict(parse_qsl(request.body))
username, password = None, None
if 'Authorization' in request.headers:
method, encoded = request.headers['Authorization'].split(' ', 1)
if method == 'Basic':
username, password = b64decode(encoded).split(':', 1)
if (username, password) == (os.environ['BRIGADE_SIGNUP_SECRET'], 'x-brigade-signup'):
return response(200, 'Added checkin')
return response(401, 'Go away')
raise NotImplementedError()
raise ValueError('Bad {} to "{}"'.format(request.method, url.geturl()))
def test_signup(self):
''' Test that main page signups work '''
signup = {
"FNAME" : "FIRST NAME",
"LNAME" : "LAST NAME",
"EMAIL" : "EMAIL",
"mailchimp_url" : None,
"brigade_id" : "FAKE-BRIGADE-ID"
}
# Test that our data is going through
with app.test_request_context("/brigade/signup/", method="POST", data=signup):
self.assertEqual(flask.request.form.get("FNAME"), "FIRST NAME")
self.assertEqual(flask.request.form.get("LNAME"), "LAST NAME")
self.assertEqual(flask.request.form.get("EMAIL"), "EMAIL")
# Test that our responses are being packaged up the way we expect
with HTTMock(self.response_content):
response = self.app.post('/brigade/signup/', data=signup)
response = json.loads(response.data)
self.assertEqual(response['msg'], "Added to the peopledb")
def test_old_brigade_links(self):
''' Test that the old brigade links are being redirected '''
with HTTMock(self.response_content):
response = self.app.get("/brigade/index/Code-for-San-Francisco/")
self.assertTrue(response.status_code == 301)
def test_good_links(self):
''' Test that normal Brigade links are working '''
with HTTMock(self.response_content):
response = self.app.get("/brigade/Code-for-San-Francisco/")
self.assertTrue(response.status_code == 200)
def test_404(self):
''' Test for 404 links '''
with HTTMock(self.response_content):
response = self.app.get("/brigade/404/")
self.assertTrue(response.status_code == 404)
def test_attendance(self):
''' Test attendance endpoints '''
with HTTMock(self.response_content):
response = self.app.get("/brigade/attendance")
self.assertEqual(response.status_code, 200)
self.assertTrue('<p class="h1">100</p>' in response.data)
def test_checkin(self):
''' Test checkin '''
checkin = {
"name" : "TEST NAME",
"email" : "test@testing.com",
"event" : "TEST EVENT",
"cfapi_url" : "https://www.codeforamerica.org/api/organizations/TEST-ORG",
"question" : "TEST QUESTION",
"answer" : "TEST ANSWER"
}
with HTTMock(self.response_content):
response = self.app.post("/brigade/checkin/", data=checkin, follow_redirects=True)
self.assertTrue(response.status_code == 200)
checkin["question"] = None
checkin["answer"] = None
with HTTMock(self.response_content):
response = self.app.post("/brigade/checkin/", data=checkin, follow_redirects=True)
self.assertTrue(response.status_code == 200)
# test nonexistant Brigade
checkin["cfapi_url"] = "http://www.codeforamerica.org/api/organizations/BLAH-BLAH"
response = self.app.post("/brigade/checkin/", data=checkin)
self.assertTrue(response.status_code == 422)
# test http
checkin["cfapi_url"] = "http://www.codeforamerica.org/api/organizations/Code-for-San-Francisco"
response = self.app.post("/brigade/checkin/", data=checkin)
self.assertTrue(response.status_code == 422)
# test missing cfapi_url
checkin["cfapi_url"] = None
response = self.app.post("/brigade/checkin/", data=checkin)
self.assertTrue(response.status_code == 422)
def test_test_checkin(self):
''' Test the test-checkin route '''
checkin = {
"name" : "TEST NAME",
"email" : "test@testing.com",
"event" : "TEST EVENT",
"cfapi_url" : "https://www.codeforamerica.org/api/organizations/Code-for-San-Francisco",
"question" : "TEST QUESTION",
"answer" : "TEST ANSWER"
}
response = self.app.post("/brigade/test-checkin/", data=checkin)
self.assertTrue(response.status_code == 200)
# test nonexistant Brigade
checkin["cfapi_url"] = "http://www.codeforamerica.org/api/organizations/BLAH-BLAH"
response = self.app.post("/brigade/test-checkin/", data=checkin)
self.assertTrue(response.status_code == 422)
# test http
checkin["cfapi_url"] = "http://www.codeforamerica.org/api/organizations/Code-for-San-Francisco"
response = self.app.post("/brigade/test-checkin/", data=checkin)
self.assertTrue(response.status_code == 422)
# test bad url
checkin["cfapi_url"] = "https://codeforamerica.org/api/organizations/Code-for-San-Francisco"
response = self.app.post("/brigade/test-checkin/", data=checkin)
self.assertTrue(response.status_code == 422)
# test missing cfapi_url
checkin["cfapi_url"] = None
response = self.app.post("/brigade/test-checkin/", data=checkin)
self.assertTrue(response.status_code == 422)
if __name__ == '__main__':
unittest.main()