4 from unittest import TestCase
6 from datetime import datetime
10 GROUP_PREFIX = {'127.0.0.1:5000': {'group1': 'g1', 'group2': 'g2'}},
11 DURATION = {'127.0.0.1:5000':[3, 7, 14]},
12 SERVER_NAME = '127.0.0.1:5000'
15 app.config['TESTING'] = True
16 app.config['DEBUG'] = False
19 class BasicTest(TestCase):
22 self.app = app.test_client()
23 self.assertEqual(app.debug, False)
28 # functions to manipulate motions
29 def createVote(self, user, motion, vote):
31 '/motion/' + motion +'/vote',
32 environ_base={'USER_ROLES': user},
37 def createMotion(self, user, motiontitle, motioncontent, days, category):
40 environ_base={'USER_ROLES': user},
41 data=dict(title=motiontitle, content=motioncontent, days=days, category=category)
44 def cancelMotion(self, user, motion, reason):
46 '/motion/' + motion +'/cancel',
47 environ_base={'USER_ROLES': user},
48 data=dict(reason=reason)
51 def finishMotion(self, user, motion):
53 '/motion/' + motion +'/finish',
54 environ_base={'USER_ROLES': user}
57 def buildResultText(self, motiontext, yes, no, abstain):
58 return '<p>'+motiontext+'</p></p>\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">'+str(yes)+'</span><br>'\
59 + '\nNo <span class=\"badge badge-pill badge-secondary\">'+str(no)+'</span><br>'\
60 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">'+str(abstain)+'</span>'
62 # functions to clear database
64 with postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD")) as db:
65 with app.open_resource('sql/schema.sql', mode='r') as f:
68 def db_sampledata(self):
69 with postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD")) as db:
70 with app.open_resource('sql/sample_data.sql', mode='r') as f:
74 # no specific rights required
75 class GeneralTests(BasicTest):
86 def test_main_page(self):
87 response = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
88 self.assertEqual(response.status_code, 200)
90 def test_basic_results_data(self):
91 result = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
92 testtext= '<div class="motion card" id="motion-3">\n <div class="motion-title card-heading alert-warning">'\
93 + '\n <span class=\"title-text\">Motion C</span> (Canceled)\n <span class=\"motion-type\">group1</span>'\
94 + '\n <div># g1.20200402.003'\
95 + '\n <a class="btn btn-primary" href="/motion/g1.20200402.003" role="button">Result</a>'\
97 + '\n <div class=\"date\">\n <div>Proposed: 2020-04-02 21:47:24 (UTC) by User A</div>'\
98 + '\n <div>Canceled: 2020-04-03 21:48:24 (UTC) by User A</div></div>\n </div>'\
99 + '\n <div class=\"card-body\">\n <p><p>A third motion</p></p>'\
100 + '\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">1</span><br>'\
101 + '\nNo <span class=\"badge badge-pill badge-secondary\">0</span><br>'\
102 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">0</span><br>\n </p>'\
103 + '\n <p>Cancelation reason: Entered with wrong text</p>\n </div>\n</div>'
104 self.assertIn(str.encode(testtext), result.data)
105 testtext= '<div class="motion card" id="motion-2">\n <div class="motion-title card-heading alert-danger">'\
106 + '\n <span class=\"title-text\">Motion B</span> (Finished)\n <span class=\"motion-type\">group1</span>'\
107 + '\n <div># g1.20200402.002'\
108 + '\n <a class="btn btn-primary" href="/motion/g1.20200402.002" role="button">Result</a>'\
110 + '\n <div class=\"date\">\n <div>Proposed: 2020-04-02 21:41:26 (UTC) by User A</div>'\
111 + '\n <div>Votes until: 2020-04-04 21:41:26 (UTC)</div></div>\n </div>'\
112 + '\n <div class=\"card-body\">\n <p><p>A second motion</p></p>'\
113 + '\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">1</span><br>'\
114 + '\nNo <span class=\"badge badge-pill badge-secondary\">2</span><br>'\
115 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">0</span><br>\n </p>\n </div>\n</div>\n'
116 self.assertIn(str.encode(testtext), result.data)
117 testtext= '<div class=\"motion card\" id=\"motion-1\">\n <div class=\"motion-title card-heading alert-success\">'\
118 + '\n <span class=\"title-text\">Motion A</span> (Finished)\n <span class=\"motion-type\">group1</span>'\
119 + '\n <div># g1.20200402.001'\
120 + '\n <a class="btn btn-primary" href="/motion/g1.20200402.001" role="button">Result</a>'\
122 + '\n <div class=\"date">\n <div>Proposed: 2020-04-02 21:40:33 (UTC) by User A</div>'\
123 + '\n <div>Votes until: 2020-04-02 21:40:33 (UTC)</div></div>\n </div>'\
124 + '\n <div class=\"card-body\">\n <p><p>My special motion</p></p>'\
125 + '\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">2</span><br>'\
126 + '\nNo <span class=\"badge badge-pill badge-secondary\">1</span><br>'\
127 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">0</span><br>\n </p>\n </div>\n</div>\n</div>'
128 self.assertIn(str.encode(testtext), result.data)
130 # start with second motion
131 result = self.app.get('/', environ_base={'USER_ROLES': user}, query_string=dict(start=2))
132 testtext= 'id=\"motion-3\">'
133 self.assertNotIn(str.encode(testtext), result.data)
134 testtext= 'id=\"motion-2">'
135 self.assertIn(str.encode(testtext), result.data)
136 testtext= 'id=\"motion-1\">'
137 self.assertIn(str.encode(testtext), result.data)
139 def test_basic_results_data_details(self):
140 motion='g1.20200402.002'
141 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
142 testtext= '<p>A second motion</p></p>\n </div>\n</div>\n<a href=\"/?start=2#motion-2\" class=\"btn btn-primary\">Back</a>\n</body>'
143 self.assertIn(str.encode(testtext), result.data)
146 motion='g1.20200402.004'
147 response = self.createVote(user, motion, 'yes')
148 self.assertEqual(response.status_code, 403)
149 self.assertIn(str.encode('Forbidden'), response.data)
151 def test_no_user(self):
152 result = self.app.get('/', follow_redirects=True)
153 self.assertEqual(result.status_code, 500)
154 self.assertIn(str.encode('Server misconfigured'), result.data)
156 def test_user_invalid(self):
157 result = self.app.get('/', environ_base={'USER_ROLES': '<invalid>/'}, follow_redirects=True)
158 self.assertEqual(result.status_code, 403)
159 self.assertIn(str.encode('Access denied'), result.data)
161 def test_basic_env(self):
162 result = self.app.get('/', environ_base={'USER': 'testuser', 'ROLES':''}, follow_redirects=True)
163 testtext= 'id=\"motion-3\">'
164 self.assertIn(str.encode(testtext), result.data)
166 def test_basic_results_data_details_not_given(self):
167 motion='g1.30190402.001'
168 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
169 self.assertEqual(result.status_code, 404)
170 self.assertIn(str.encode('Error, Not found'), result.data)
173 class VoterTests(BasicTest):
178 user='testuser/vote:*'
184 def test_main_page(self):
185 response = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
186 self.assertEqual(response.status_code, 200)
188 def test_home_data(self):
189 result = self.app.get('/', environ_base={'USER_ROLES': user})
190 self.assertNotIn("<select class=\"float form-control\" name=\"category\">", str(result.data) )
192 def test_vote_yes(self):
193 motion='g1.20200402.004'
194 response = self.createVote(user, motion, 'yes')
195 self.assertEqual(response.status_code, 302)
196 result = self.app.get('/', environ_base={'USER_ROLES': user})
197 resulttext=self.buildResultText('A fourth motion', 1, 0, 0)
198 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
199 testtext= 'class=\"btn btn-success\" name=\"vote\" value="yes" id="vote-yes">yes</button>'
200 self.assertIn(str.encode(testtext), result.data)
201 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"no\" id=\"vote-no\">no</button>'
202 self.assertIn(str.encode(testtext), result.data)
203 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">abstain</button>'
204 self.assertIn(str.encode(testtext), result.data)
206 def test_vote_no(self):
207 motion='g1.20200402.004'
208 response = self.createVote(user, motion, 'no')
209 self.assertEqual(response.status_code, 302)
210 result = self.app.get('/', environ_base={'USER_ROLES': user})
211 resulttext=self.buildResultText('A fourth motion', 0, 1, 0)
212 self.assertIn(str.encode(resulttext), result.data)
213 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
214 testtext= 'class="btn btn-primary" name="vote\" value=\"yes\" id=\"vote-yes\">yes</button>'
215 self.assertIn(str.encode(testtext), result.data)
216 testtext= 'class=\"btn btn-success\" name=\"vote\" value=\"no\" id=\"vote-no\">no</button>'
217 self.assertIn(str.encode(testtext), result.data)
218 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">abstain</button>'
219 self.assertIn(str.encode(testtext), result.data)
221 def test_vote_abstain(self):
222 motion='g1.20200402.004'
223 response = self.createVote(user, motion, 'abstain')
224 self.assertEqual(response.status_code, 302)
225 result = self.app.get('/', environ_base={'USER_ROLES': user})
226 resulttext=self.buildResultText('A fourth motion', 0, 0, 1)
227 self.assertIn(str.encode(resulttext), result.data)
228 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
229 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"yes\" id=\"vote-yes\">yes</button>'
230 self.assertIn(str.encode(testtext), result.data)
231 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"no\" id=\"vote-no\">no</button>'
232 self.assertIn(str.encode(testtext), result.data)
233 testtext= 'class=\"btn btn-success\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">abstain</button>'
234 self.assertIn(str.encode(testtext), result.data)
236 def test_vote_change(self):
237 motion='g1.20200402.004'
238 response = self.createVote(user, motion, 'yes')
239 self.assertEqual(response.status_code, 302)
240 result = self.app.get('/', environ_base={'USER_ROLES': user})
241 resulttext=self.buildResultText('A fourth motion', 1, 0, 0)
242 self.assertIn(str.encode(resulttext), result.data)
243 response = self.createVote(user, motion, 'no')
244 self.assertEqual(response.status_code, 302)
245 result = self.app.get('/', environ_base={'USER_ROLES': user})
246 resulttext=self.buildResultText('A fourth motion', 0, 1, 0)
247 self.assertIn(str.encode(resulttext), result.data)
248 response = self.createVote(user, motion, 'abstain')
249 self.assertEqual(response.status_code, 302)
250 result = self.app.get('/', environ_base={'USER_ROLES': user})
251 resulttext=self.buildResultText('A fourth motion', 0, 0, 1)
252 self.assertIn(str.encode(resulttext), result.data)
254 def test_vote_group(self):
255 motion='g1.20200402.004'
256 response = self.createVote(user, motion, 'yes')
257 self.assertEqual(response.status_code, 302)
259 motion='g1.20200402.004'
260 user1='testuser/vote:group1'
261 response = self.createVote(user1, motion, 'yes')
262 self.assertEqual(response.status_code, 302)
264 motion='g1.20200402.004'
265 user1='testuser/vote:group1 vote:group2'
266 response = self.createVote(user1, motion, 'yes')
267 self.assertEqual(response.status_code, 302)
269 def test_vote_wrong_group(self):
270 motion='g1.20200402.004'
271 user1='testuser/vote:group2'
272 response = self.createVote(user1, motion, 'yes')
273 self.assertEqual(response.status_code, 403)
274 self.assertIn(str.encode('Forbidden'), response.data)
276 def test_vote_closed(self):
277 motion='g1.20200402.002'
278 response = self.createVote(user, motion, 'abstain')
279 self.assertEqual(response.status_code, 500)
280 self.assertIn(str.encode('Error, motion deadline has passed'), response.data)
282 def test_vote_canceled(self):
283 motion='g1.20200402.003'
284 response = self.createVote(user, motion, 'abstain')
285 self.assertEqual(response.status_code, 500)
286 self.assertIn(str.encode('Error, motion deadline has passed'), response.data)
288 def test_vote_not_given(self):
289 motion='g1.30190402.001'
290 response = self.createVote(user, motion, 'abstain')
291 self.assertEqual(response.status_code, 404)
292 self.assertIn(str.encode('Error, Not found'), response.data)
294 def test_cancelMotion(self):
295 motion='g1.20200402.004'
297 response = self.cancelMotion(user, motion, reason)
298 self.assertEqual(response.status_code, 403)
299 self.assertIn(str.encode('Forbidden'), response.data)
301 def test_finishMotion(self):
302 motion='g1.20200402.004'
303 response = self.finishMotion(user, motion)
304 self.assertEqual(response.status_code, 403)
305 self.assertIn(str.encode('Forbidden'), response.data)
307 def test_see_old_vote(self):
308 motion='g1.20200402.002'
309 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
310 testtext= '<div>Proposed: 2020-04-02 21:41:26 (UTC) by User A</div>\n <div>Votes until: 2020-04-04 21:41:26 (UTC)</div></div>'\
311 + '\n </div>\n <div class="card-body">\n <p><p>A second motion</p></p>\n </div>\n</div>'\
312 + '\n<a href="/?start=2#motion-2" class="btn btn-primary">Back</a>'
313 self.assertIn(str.encode(testtext), result.data)
315 def test_createMotion(self):
318 response = self.createMotion(user, title, content, '3', 'group1')
319 self.assertEqual(response.status_code, 403)
320 self.assertIn(str.encode('Forbidden'), response.data)
323 class CreateMotionTests(BasicTest):
328 user='testuser/vote:* create:* cancel:* finish:*'
334 def test_main_page(self):
335 response = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
336 self.assertEqual(response.status_code, 200)
338 def test_home_data(self):
339 result = self.app.get('/', environ_base={'USER_ROLES': user})
341 # assert the response data
342 self.assertIn(b'User: testuser', result.data)
343 self.assertIn("<select class=\"float form-control\" name=\"category\">", str(result.data) )
345 def test_createMotion(self):
348 response = self.createMotion(user, title, content, '3', 'group1')
349 self.assertEqual(response.status_code, 302)
350 result = self.app.get('/', environ_base={'USER_ROLES': user})
351 self.assertIn(str.encode(title), result.data)
352 self.assertIn(str.encode(content), result.data)
353 self.assertIn(str.encode('g1.'+datetime.today().strftime('%Y%m%d')+'.001'), result.data)
354 testtext='<a class=\"btn btn-primary" href=\"/motion/g1.'+datetime.today().strftime('%Y%m%d')+'.001\" role=\"button\">Vote</a>'
355 self.assertIn(str.encode(testtext), result.data)
359 response = self.createMotion(user, title, content, '3', 'group1')
360 self.assertEqual(response.status_code, 302)
361 result = self.app.get('/', environ_base={'USER_ROLES': user})
362 self.assertIn(str.encode(title), result.data)
363 self.assertIn(str.encode(content), result.data)
364 self.assertIn(str.encode('g1.'+datetime.today().strftime('%Y%m%d')+'.002'), result.data)
368 response = self.createMotion(user, title, content, '3', 'group2')
369 self.assertEqual(response.status_code, 302)
370 result = self.app.get('/', environ_base={'USER_ROLES': user})
371 self.assertIn(str.encode(title), result.data)
372 self.assertIn(str.encode(content), result.data)
373 self.assertIn(str.encode('g2.'+datetime.today().strftime('%Y%m%d')+'.001'), result.data)
377 user1='testuser/vote:* create:group1 cancel:*'
378 response = self.createMotion(user1, title, content, '3', 'group1')
379 self.assertEqual(response.status_code, 302)
383 user1='testuser/vote:* create:group1 create:group2 cancel:*'
384 response = self.createMotion(user1, title, content, '3', 'group1')
385 self.assertEqual(response.status_code, 302)
388 def test_createMotionMarkdown(self):
389 title='Markdown Test'
390 content= 'MyMotionBody MD [text](https//domain.tld/link)'
391 response = self.createMotion(user, title, content, '3', 'group1')
392 self.assertEqual(response.status_code, 302)
393 result = self.app.get('/', environ_base={'USER_ROLES': user})
394 self.assertIn(str.encode(title), result.data)
395 self.assertIn(b'MyMotionBody MD <a href=\"https//domain.tld/link\">text</a>', result.data)
397 def test_createMotionMarkdownDirectLink(self):
398 title='Markdown Test Link'
399 content='MyMotionBody MD <a href=\"https//domain.tld/link\">direct</a'
400 response = self.createMotion(user, title, content, '3', 'group1')
401 self.assertEqual(response.status_code, 302)
402 result = self.app.get('/', environ_base={'USER_ROLES': user})
403 self.assertIn(str.encode(title), result.data)
404 self.assertIn(b'MyMotionBody MD <a href="https//domain.tld/link">direct</a', result.data)
406 def test_createMotionMarkdownCombined(self):
407 title='Markdown Test Link'
408 content='Body [combined](https//domain.tld/link) <a href=\"https//domain.tld/link\">combined1</a'
409 response = self.createMotion(user, title, content, '3', 'group1')
410 self.assertEqual(response.status_code, 302)
411 result = self.app.get('/', environ_base={'USER_ROLES': user})
412 self.assertIn(str.encode(title), result.data)
413 self.assertIn(b'Body <a href=\"https//domain.tld/link\">combined</a> <a href="https//domain.tld/link">combined1</a', result.data)
415 def test_createMotionWrongDayLength(self):
418 response = self.createMotion(user, title, content, '21', 'group1')
419 self.assertEqual(response.status_code, 500)
420 self.assertIn(str.encode('Error, invalid length'), response.data)
422 def test_createMotionWrongGroup(self):
425 response = self.createMotion(user, title, content, '3', 'test1')
426 self.assertEqual(response.status_code, 403)
427 self.assertIn(str.encode('Forbidden'), response.data)
429 user1='testuser/vote:* create:group1 cancel:*'
430 response = self.createMotion(user1, title, content, '3', 'group2')
431 self.assertEqual(response.status_code, 403)
432 self.assertIn(str.encode('Forbidden'), response.data)
434 def test_cancelMotion(self):
437 motion='g1.20200402.004'
439 response = self.cancelMotion(user, motion, reason)
440 self.assertEqual(response.status_code, 500)
441 self.assertIn(str.encode('Error, form requires reason'), response.data)
444 response = self.cancelMotion(user, motion, reason)
445 self.assertEqual(response.status_code, 302)
446 result = self.app.get('/', environ_base={'USER_ROLES': user})
447 self.assertIn(b'Cancelation reason: ' + str.encode(reason), result.data)
449 motion='g1.20190402.001'
451 response = self.cancelMotion(user, motion, reason)
452 self.assertEqual(response.status_code, 404)
453 self.assertIn(str.encode('Error, Not found'), response.data)
455 motion='g1.30200402.001'
457 response = self.cancelMotion(user, motion, reason)
458 self.assertEqual(response.status_code, 404)
459 self.assertIn(str.encode('Error, Not found'), response.data)
461 motion='g1.20200402.004'
462 response = self.cancelMotion(user, motion, reason)
463 self.assertEqual(response.status_code, 403)
464 self.assertIn(str.encode('Error, out of time'), response.data)
466 def test_finishMotion(self):
469 motion='g1.20200402.004'
470 response = self.finishMotion(user, motion)
471 self.assertEqual(response.status_code, 302)
472 result = self.app.get('/', environ_base={'USER_ROLES': user})
473 self.assertIn(b'Motion D</span> (Finished)', result.data)
475 motion='g1.30190402.001'
476 response = self.finishMotion(user, motion)
477 self.assertEqual(response.status_code, 404)
478 self.assertIn(str.encode('Error, Not found'), response.data)
480 motion='g1.20200402.001'
481 response = self.finishMotion(user, motion)
482 self.assertEqual(response.status_code, 403)
483 self.assertIn(str.encode('Error, out of time'), response.data)
485 class AuditMotionTests(BasicTest):
490 user='testuser/audit:*'
496 def test_see_old_vote(self):
497 motion='g1.20200402.002'
498 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
499 testtext= '<div class="motion card" id="votes">\n <div class="card-heading text-white bg-info">\n Motion Votes\n </div>'\
500 + '\n <div class="card-body">\n <div>User A: yes</div>\n <div>User B: no</div>'\
501 + '\n <div>User C: no</div>\n </div>\n</div>\n<a href="/?start=2#motion-2" class="btn btn-primary">Back</a>'
502 self.assertIn(str.encode(testtext), result.data)
505 if __name__ == "__main__":