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 buildResultText(self, motiontext, yes, no, abstain):
52 return '<p>'+motiontext+'</p></p>\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">'+str(yes)+'</span><br>'\
53 + '\nNo <span class=\"badge badge-pill badge-secondary\">'+str(no)+'</span><br>'\
54 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">'+str(abstain)+'</span>'
56 # functions to clear database
58 with postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD")) as db:
59 with app.open_resource('sql/schema.sql', mode='r') as f:
62 def db_sampledata(self):
63 with postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD")) as db:
64 with app.open_resource('sql/sample_data.sql', mode='r') as f:
68 # no specific rights required
69 class GeneralTests(BasicTest):
80 def test_main_page(self):
81 response = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
82 self.assertEqual(response.status_code, 200)
84 def test_basic_results_data(self):
85 result = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
86 testtext= '<div class="motion card" id="motion-3">\n <div class="motion-title card-heading alert-warning">'\
87 + '\n <span class=\"title-text\">Motion C</span> (Canceled)\n <span class=\"motion-type\">group1</span>'\
88 + '\n <div># <a href=\"/motion/g1.20200402.003\" class=\"anchor\">g1.20200402.003</a></div>'\
89 + '\n <div class=\"date\">\n <div>Proposed: 2020-04-02 21:47:24 (UTC) by User A</div>'\
90 + '\n <div>Canceled: 2020-04-03 21:48:24 (UTC) by User A</div></div>\n </div>'\
91 + '\n <div class=\"card-body\">\n <p><p>A third motion</p></p>'\
92 + '\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">1</span><br>'\
93 + '\nNo <span class=\"badge badge-pill badge-secondary\">0</span><br>'\
94 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">0</span><br>\n </p>'\
95 + '\n <p>Cancelation reason: Entered with wrong text</p>\n </div>\n</div>'
96 self.assertIn(str.encode(testtext), result.data)
97 testtext= '<div class="motion card" id="motion-2">\n <div class="motion-title card-heading alert-danger">'\
98 + '\n <span class=\"title-text\">Motion B</span> (Finished)\n <span class=\"motion-type\">group1</span>'\
99 + '\n <div># <a href=\"/motion/g1.20200402.002\" class=\"anchor\">g1.20200402.002</a></div>'\
100 + '\n <div class=\"date\">\n <div>Proposed: 2020-04-02 21:41:26 (UTC) by User A</div>'\
101 + '\n <div>Votes until: 2020-04-04 21:41:26 (UTC)</div></div>\n </div>'\
102 + '\n <div class=\"card-body\">\n <p><p>A second motion</p></p>'\
103 + '\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">1</span><br>'\
104 + '\nNo <span class=\"badge badge-pill badge-secondary\">2</span><br>'\
105 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">0</span><br>\n </p>\n </div>\n</div>\n'
106 self.assertIn(str.encode(testtext), result.data)
107 testtext= '<div class=\"motion card\" id=\"motion-1\">\n <div class=\"motion-title card-heading alert-success\">'\
108 + '\n <span class=\"title-text\">Motion A</span> (Finished)\n <span class=\"motion-type\">group1</span>'\
109 + '\n <div># <a href=\"/motion/g1.20200402.001\" class=\"anchor\">g1.20200402.001</a></div>'\
110 + '\n <div class=\"date">\n <div>Proposed: 2020-04-02 21:40:33 (UTC) by User A</div>'\
111 + '\n <div>Votes until: 2020-04-02 21:40:33 (UTC)</div></div>\n </div>'\
112 + '\n <div class=\"card-body\">\n <p><p>My special motion</p></p>'\
113 + '\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">2</span><br>'\
114 + '\nNo <span class=\"badge badge-pill badge-secondary\">1</span><br>'\
115 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">0</span><br>\n </p>\n </div>\n</div>\n</div>'
116 self.assertIn(str.encode(testtext), result.data)
118 # start with second motion
119 result = self.app.get('/', environ_base={'USER_ROLES': user}, query_string=dict(start=2))
120 testtext= 'id=\"motion-3\">'
121 self.assertNotIn(str.encode(testtext), result.data)
122 testtext= 'id=\"motion-2">'
123 self.assertIn(str.encode(testtext), result.data)
124 testtext= 'id=\"motion-1\">'
125 self.assertIn(str.encode(testtext), result.data)
127 def test_basic_results_data_details(self):
128 motion='g1.20200402.002'
129 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
130 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>'
131 self.assertIn(str.encode(testtext), result.data)
134 motion='g1.20200402.004'
135 response = self.createVote(user, motion, 'yes')
136 self.assertEqual(response.status_code, 403)
137 self.assertIn(str.encode('Forbidden'), response.data)
139 def test_no_user(self):
140 result = self.app.get('/', follow_redirects=True)
141 self.assertEqual(result.status_code, 500)
142 self.assertIn(str.encode('Server misconfigured'), result.data)
144 def test_user_invalid(self):
145 result = self.app.get('/', environ_base={'USER_ROLES': '<invalid>/'}, follow_redirects=True)
146 self.assertEqual(result.status_code, 403)
147 self.assertIn(str.encode('Access denied'), result.data)
149 def test_basic_env(self):
150 result = self.app.get('/', environ_base={'USER': 'testuser', 'ROLES':''}, follow_redirects=True)
151 testtext= 'id=\"motion-3\">'
152 self.assertIn(str.encode(testtext), result.data)
154 def test_basic_results_data_details_not_given(self):
155 motion='g1.30190402.001'
156 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
157 self.assertEqual(result.status_code, 404)
158 self.assertIn(str.encode('Error, Not found'), result.data)
161 class VoterTests(BasicTest):
166 user='testuser/vote:*'
172 def test_main_page(self):
173 response = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
174 self.assertEqual(response.status_code, 200)
176 def test_home_data(self):
177 result = self.app.get('/', environ_base={'USER_ROLES': user})
178 self.assertNotIn("<select class=\"float form-control\" name=\"category\">", str(result.data) )
180 def test_vote_yes(self):
181 motion='g1.20200402.004'
182 response = self.createVote(user, motion, 'yes')
183 self.assertEqual(response.status_code, 302)
184 result = self.app.get('/', environ_base={'USER_ROLES': user})
185 resulttext=self.buildResultText('A fourth motion', 1, 0, 0)
186 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
187 testtext= 'class=\"btn btn-success\" name=\"vote\" value="yes" id="vote-yes">yes</button>'
188 self.assertIn(str.encode(testtext), result.data)
189 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"no\" id=\"vote-no\">no</button>'
190 self.assertIn(str.encode(testtext), result.data)
191 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">abstain</button>'
192 self.assertIn(str.encode(testtext), result.data)
194 def test_vote_no(self):
195 motion='g1.20200402.004'
196 response = self.createVote(user, motion, 'no')
197 self.assertEqual(response.status_code, 302)
198 result = self.app.get('/', environ_base={'USER_ROLES': user})
199 resulttext=self.buildResultText('A fourth motion', 0, 1, 0)
200 self.assertIn(str.encode(resulttext), result.data)
201 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
202 testtext= 'class="btn btn-primary" name="vote\" value=\"yes\" id=\"vote-yes\">yes</button>'
203 self.assertIn(str.encode(testtext), result.data)
204 testtext= 'class=\"btn btn-success\" name=\"vote\" value=\"no\" id=\"vote-no\">no</button>'
205 self.assertIn(str.encode(testtext), result.data)
206 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">abstain</button>'
207 self.assertIn(str.encode(testtext), result.data)
209 def test_vote_abstain(self):
210 motion='g1.20200402.004'
211 response = self.createVote(user, motion, 'abstain')
212 self.assertEqual(response.status_code, 302)
213 result = self.app.get('/', environ_base={'USER_ROLES': user})
214 resulttext=self.buildResultText('A fourth motion', 0, 0, 1)
215 self.assertIn(str.encode(resulttext), result.data)
216 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
217 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"yes\" id=\"vote-yes\">yes</button>'
218 self.assertIn(str.encode(testtext), result.data)
219 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"no\" id=\"vote-no\">no</button>'
220 self.assertIn(str.encode(testtext), result.data)
221 testtext= 'class=\"btn btn-success\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">abstain</button>'
222 self.assertIn(str.encode(testtext), result.data)
224 def test_vote_change(self):
225 motion='g1.20200402.004'
226 response = self.createVote(user, motion, 'yes')
227 self.assertEqual(response.status_code, 302)
228 result = self.app.get('/', environ_base={'USER_ROLES': user})
229 resulttext=self.buildResultText('A fourth motion', 1, 0, 0)
230 self.assertIn(str.encode(resulttext), result.data)
231 response = self.createVote(user, motion, 'no')
232 self.assertEqual(response.status_code, 302)
233 result = self.app.get('/', environ_base={'USER_ROLES': user})
234 resulttext=self.buildResultText('A fourth motion', 0, 1, 0)
235 self.assertIn(str.encode(resulttext), result.data)
236 response = self.createVote(user, motion, 'abstain')
237 self.assertEqual(response.status_code, 302)
238 result = self.app.get('/', environ_base={'USER_ROLES': user})
239 resulttext=self.buildResultText('A fourth motion', 0, 0, 1)
240 self.assertIn(str.encode(resulttext), result.data)
242 def test_vote_group(self):
243 motion='g1.20200402.004'
244 response = self.createVote(user, motion, 'yes')
245 self.assertEqual(response.status_code, 302)
247 motion='g1.20200402.004'
248 user1='testuser/vote:group1'
249 response = self.createVote(user1, motion, 'yes')
250 self.assertEqual(response.status_code, 302)
252 motion='g1.20200402.004'
253 user1='testuser/vote:group1 vote:group2'
254 response = self.createVote(user1, motion, 'yes')
255 self.assertEqual(response.status_code, 302)
257 def test_vote_wrong_group(self):
258 motion='g1.20200402.004'
259 user1='testuser/vote:group2'
260 response = self.createVote(user1, motion, 'yes')
261 self.assertEqual(response.status_code, 403)
262 self.assertIn(str.encode('Forbidden'), response.data)
264 def test_vote_closed(self):
265 motion='g1.20200402.002'
266 response = self.createVote(user, motion, 'abstain')
267 self.assertEqual(response.status_code, 500)
268 self.assertIn(str.encode('Error, motion deadline has passed'), response.data)
270 def test_vote_canceled(self):
271 motion='g1.20200402.003'
272 response = self.createVote(user, motion, 'abstain')
273 self.assertEqual(response.status_code, 500)
274 self.assertIn(str.encode('Error, motion deadline has passed'), response.data)
276 def test_vote_not_given(self):
277 motion='g1.30190402.001'
278 response = self.createVote(user, motion, 'abstain')
279 self.assertEqual(response.status_code, 404)
280 self.assertIn(str.encode('Error, Not found'), response.data)
282 def test_cancelMotion(self):
283 motion='g1.20200402.004'
285 response = self.cancelMotion(user, motion, reason)
286 self.assertEqual(response.status_code, 403)
287 self.assertIn(str.encode('Forbidden'), response.data)
289 def test_see_old_vote(self):
290 motion='g1.20200402.002'
291 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
292 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>'\
293 + '\n </div>\n <div class="card-body">\n <p><p>A second motion</p></p>\n </div>\n</div>'\
294 + '\n<a href="/?start=2#motion-2" class="btn btn-primary">Back</a>'
295 self.assertIn(str.encode(testtext), result.data)
297 def test_createMotion(self):
300 response = self.createMotion(user, title, content, '3', 'group1')
301 self.assertEqual(response.status_code, 403)
302 self.assertIn(str.encode('Forbidden'), response.data)
305 class CreateMotionTests(BasicTest):
310 user='testuser/vote:* create:* cancel:*'
316 def test_main_page(self):
317 response = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
318 self.assertEqual(response.status_code, 200)
320 def test_home_data(self):
321 result = self.app.get('/', environ_base={'USER_ROLES': user})
323 # assert the response data
324 self.assertIn(b'User: testuser', result.data)
325 self.assertIn("<select class=\"float form-control\" name=\"category\">", str(result.data) )
327 def test_createMotion(self):
330 response = self.createMotion(user, title, content, '3', 'group1')
331 self.assertEqual(response.status_code, 302)
332 result = self.app.get('/', environ_base={'USER_ROLES': user})
333 self.assertIn(str.encode(title), result.data)
334 self.assertIn(str.encode(content), result.data)
335 self.assertIn(str.encode('g1.'+datetime.today().strftime('%Y%m%d')+'.001'), result.data)
339 response = self.createMotion(user, title, content, '3', 'group1')
340 self.assertEqual(response.status_code, 302)
341 result = self.app.get('/', environ_base={'USER_ROLES': user})
342 self.assertIn(str.encode(title), result.data)
343 self.assertIn(str.encode(content), result.data)
344 self.assertIn(str.encode('g1.'+datetime.today().strftime('%Y%m%d')+'.002'), result.data)
348 response = self.createMotion(user, title, content, '3', 'group2')
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('g2.'+datetime.today().strftime('%Y%m%d')+'.001'), result.data)
357 user1='testuser/vote:* create:group1 cancel:*'
358 response = self.createMotion(user1, title, content, '3', 'group1')
359 self.assertEqual(response.status_code, 302)
363 user1='testuser/vote:* create:group1 create:group2 cancel:*'
364 response = self.createMotion(user1, title, content, '3', 'group1')
365 self.assertEqual(response.status_code, 302)
368 def test_createMotionMarkdown(self):
369 title='Markdown Test'
370 content= 'MyMotionBody MD [text](https//domain.tld/link)'
371 response = self.createMotion(user, title, content, '3', 'group1')
372 self.assertEqual(response.status_code, 302)
373 result = self.app.get('/', environ_base={'USER_ROLES': user})
374 self.assertIn(str.encode(title), result.data)
375 self.assertIn(b'MyMotionBody MD <a href=\"https//domain.tld/link\">text</a>', result.data)
377 def test_createMotionMarkdownDirectLink(self):
378 title='Markdown Test Link'
379 content='MyMotionBody MD <a href=\"https//domain.tld/link\">direct</a'
380 response = self.createMotion(user, title, content, '3', 'group1')
381 self.assertEqual(response.status_code, 302)
382 result = self.app.get('/', environ_base={'USER_ROLES': user})
383 self.assertIn(str.encode(title), result.data)
384 self.assertIn(b'MyMotionBody MD <a href="https//domain.tld/link">direct</a', result.data)
386 def test_createMotionMarkdownCombined(self):
387 title='Markdown Test Link'
388 content='Body [combined](https//domain.tld/link) <a href=\"https//domain.tld/link\">combined1</a'
389 response = self.createMotion(user, title, content, '3', 'group1')
390 self.assertEqual(response.status_code, 302)
391 result = self.app.get('/', environ_base={'USER_ROLES': user})
392 self.assertIn(str.encode(title), result.data)
393 self.assertIn(b'Body <a href=\"https//domain.tld/link\">combined</a> <a href="https//domain.tld/link">combined1</a', result.data)
395 def test_createMotionWrongDayLength(self):
398 response = self.createMotion(user, title, content, '21', 'group1')
399 self.assertEqual(response.status_code, 500)
400 self.assertIn(str.encode('Error, invalid length'), response.data)
402 def test_createMotionWrongGroup(self):
405 response = self.createMotion(user, title, content, '3', 'test1')
406 self.assertEqual(response.status_code, 403)
407 self.assertIn(str.encode('Forbidden'), response.data)
409 user1='testuser/vote:* create:group1 cancel:*'
410 response = self.createMotion(user1, title, content, '3', 'group2')
411 self.assertEqual(response.status_code, 403)
412 self.assertIn(str.encode('Forbidden'), response.data)
414 def test_cancelMotion(self):
417 motion='g1.20200402.004'
419 response = self.cancelMotion(user, motion, reason)
420 self.assertEqual(response.status_code, 500)
421 self.assertIn(str.encode('Error, form requires reason'), response.data)
424 response = self.cancelMotion(user, motion, reason)
425 self.assertEqual(response.status_code, 302)
426 result = self.app.get('/', environ_base={'USER_ROLES': user})
427 self.assertIn(b'Cancelation reason: ' + str.encode(reason), result.data)
429 motion='g1.30190402.001'
431 response = self.cancelMotion(user, motion, reason)
432 self.assertEqual(response.status_code, 404)
433 self.assertIn(str.encode('Error, Not found'), response.data)
436 class AuditMotionTests(BasicTest):
441 user='testuser/audit:*'
447 def test_see_old_vote(self):
448 motion='g1.20200402.002'
449 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
450 testtext= '<div class="motion card" id="votes">\n <div class="card-heading text-white bg-info">\n Motion Votes\n </div>'\
451 + '\n <div class="card-body">\n <div>User A: yes</div>\n <div>User B: no</div>'\
452 + '\n <div>User C: no</div>\n </div>\n</div>\n<a href="/?start=2#motion-2" class="btn btn-primary">Back</a>'
453 self.assertIn(str.encode(testtext), result.data)
456 if __name__ == "__main__":