forked from codeforamerica/brigade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
242 lines (190 loc) · 10.1 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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://www.codeforamerica.org/api/projects":
return response(200, '''{
"objects": [
{
"code_url": "https://github.com/jmcelroy5/sf-in-progress",
"description": "Engaging San Francisco Citizens in the housing development process through data and technology.",
"link_url": "http://107.170.214.244/",
"last_updated": "Mon, 10 Aug 2015 23:22:40 GMT",
"name": "SF in Progress",
"github_details" : {},
"organization": {
"id": "Code-for-San-Francisco",
"name": "Code for San Francisco"
},
"organization_name": "Code for San Francisco",
"status": "Alpha",
"tags": "housing, ndoch, active"
}
],
"total" : 1,
"pages": {}
} ''')
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 '''
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" : "[email protected]",
"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" : "[email protected]",
"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)
def test_existing(self):
''' Test that these org ids exist '''
from app import is_existing_organization
self.assertTrue(is_existing_organization("Code-for-America"))
self.assertFalse(is_existing_organization("TEST-TEST"))
def test_projects_page(self):
''' Test that the prkject page loads and looks like what we want '''
with HTTMock(self.response_content):
response = self.app.get("/brigade/projects")
self.assertTrue('<p>Status: <a href="?=Alpha" class="Alpha button-s">Alpha</a></p>' in response.data)
def test_project_monitor(self):
''' Test the project monitor page works as expected '''
with HTTMock(self.response_content):
response = self.app.get("/brigade/projects/monitor")
self.assertTrue('"travis_url": "https://api.travis-ci.org/repositories/jmcelroy5/sf-in-progress/builds"' in response.data)
if __name__ == '__main__':
unittest.main()