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># g1.20200402.003'\
89 + '\n <a class="btn btn-primary" href="/motion/g1.20200402.003" role="button">Result</a>'\
91 + '\n <div class=\"date\">\n <div>Proposed: 2020-04-02 21:47:24 (UTC) by User A</div>'\
92 + '\n <div>Canceled: 2020-04-03 21:48:24 (UTC) by User A</div></div>\n </div>'\
93 + '\n <div class=\"card-body\">\n <p><p>A third motion</p></p>'\
94 + '\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">1</span><br>'\
95 + '\nNo <span class=\"badge badge-pill badge-secondary\">0</span><br>'\
96 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">0</span><br>\n </p>'\
97 + '\n <p>Cancelation reason: Entered with wrong text</p>\n </div>\n</div>'
98 self.assertIn(str.encode(testtext), result.data)
99 testtext= '<div class="motion card" id="motion-2">\n <div class="motion-title card-heading alert-danger">'\
100 + '\n <span class=\"title-text\">Motion B</span> (Finished)\n <span class=\"motion-type\">group1</span>'\
101 + '\n <div># g1.20200402.002'\
102 + '\n <a class="btn btn-primary" href="/motion/g1.20200402.002" role="button">Result</a>'\
104 + '\n <div class=\"date\">\n <div>Proposed: 2020-04-02 21:41:26 (UTC) by User A</div>'\
105 + '\n <div>Votes until: 2020-04-04 21:41:26 (UTC)</div></div>\n </div>'\
106 + '\n <div class=\"card-body\">\n <p><p>A second motion</p></p>'\
107 + '\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">1</span><br>'\
108 + '\nNo <span class=\"badge badge-pill badge-secondary\">2</span><br>'\
109 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">0</span><br>\n </p>\n </div>\n</div>\n'
110 self.assertIn(str.encode(testtext), result.data)
111 testtext= '<div class=\"motion card\" id=\"motion-1\">\n <div class=\"motion-title card-heading alert-success\">'\
112 + '\n <span class=\"title-text\">Motion A</span> (Finished)\n <span class=\"motion-type\">group1</span>'\
113 + '\n <div># g1.20200402.001'\
114 + '\n <a class="btn btn-primary" href="/motion/g1.20200402.001" role="button">Result</a>'\
116 + '\n <div class=\"date">\n <div>Proposed: 2020-04-02 21:40:33 (UTC) by User A</div>'\
117 + '\n <div>Votes until: 2020-04-02 21:40:33 (UTC)</div></div>\n </div>'\
118 + '\n <div class=\"card-body\">\n <p><p>My special motion</p></p>'\
119 + '\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">2</span><br>'\
120 + '\nNo <span class=\"badge badge-pill badge-secondary\">1</span><br>'\
121 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">0</span><br>\n </p>\n </div>\n</div>\n</div>'
122 self.assertIn(str.encode(testtext), result.data)
124 # start with second motion
125 result = self.app.get('/', environ_base={'USER_ROLES': user}, query_string=dict(start=2))
126 testtext= 'id=\"motion-3\">'
127 self.assertNotIn(str.encode(testtext), result.data)
128 testtext= 'id=\"motion-2">'
129 self.assertIn(str.encode(testtext), result.data)
130 testtext= 'id=\"motion-1\">'
131 self.assertIn(str.encode(testtext), result.data)
133 def test_basic_results_data_details(self):
134 motion='g1.20200402.002'
135 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
136 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>'
137 self.assertIn(str.encode(testtext), result.data)
140 motion='g1.20200402.004'
141 response = self.createVote(user, motion, 'yes')
142 self.assertEqual(response.status_code, 403)
143 self.assertIn(str.encode('Forbidden'), response.data)
145 def test_no_user(self):
146 result = self.app.get('/', follow_redirects=True)
147 self.assertEqual(result.status_code, 500)
148 self.assertIn(str.encode('Server misconfigured'), result.data)
150 def test_user_invalid(self):
151 result = self.app.get('/', environ_base={'USER_ROLES': '<invalid>/'}, follow_redirects=True)
152 self.assertEqual(result.status_code, 403)
153 self.assertIn(str.encode('Access denied'), result.data)
155 def test_basic_env(self):
156 result = self.app.get('/', environ_base={'USER': 'testuser', 'ROLES':''}, follow_redirects=True)
157 testtext= 'id=\"motion-3\">'
158 self.assertIn(str.encode(testtext), result.data)
160 def test_basic_results_data_details_not_given(self):
161 motion='g1.30190402.001'
162 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
163 self.assertEqual(result.status_code, 404)
164 self.assertIn(str.encode('Error, Not found'), result.data)
167 class VoterTests(BasicTest):
172 user='testuser/vote:*'
178 def test_main_page(self):
179 response = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
180 self.assertEqual(response.status_code, 200)
182 def test_home_data(self):
183 result = self.app.get('/', environ_base={'USER_ROLES': user})
184 self.assertNotIn("<select class=\"float form-control\" name=\"category\">", str(result.data) )
186 def test_vote_yes(self):
187 motion='g1.20200402.004'
188 response = self.createVote(user, motion, 'yes')
189 self.assertEqual(response.status_code, 302)
190 result = self.app.get('/', environ_base={'USER_ROLES': user})
191 resulttext=self.buildResultText('A fourth motion', 1, 0, 0)
192 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
193 testtext= 'class=\"btn btn-success\" name=\"vote\" value="yes" id="vote-yes">yes</button>'
194 self.assertIn(str.encode(testtext), result.data)
195 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"no\" id=\"vote-no\">no</button>'
196 self.assertIn(str.encode(testtext), result.data)
197 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">abstain</button>'
198 self.assertIn(str.encode(testtext), result.data)
200 def test_vote_no(self):
201 motion='g1.20200402.004'
202 response = self.createVote(user, motion, 'no')
203 self.assertEqual(response.status_code, 302)
204 result = self.app.get('/', environ_base={'USER_ROLES': user})
205 resulttext=self.buildResultText('A fourth motion', 0, 1, 0)
206 self.assertIn(str.encode(resulttext), result.data)
207 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
208 testtext= 'class="btn btn-primary" name="vote\" value=\"yes\" id=\"vote-yes\">yes</button>'
209 self.assertIn(str.encode(testtext), result.data)
210 testtext= 'class=\"btn btn-success\" name=\"vote\" value=\"no\" id=\"vote-no\">no</button>'
211 self.assertIn(str.encode(testtext), result.data)
212 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">abstain</button>'
213 self.assertIn(str.encode(testtext), result.data)
215 def test_vote_abstain(self):
216 motion='g1.20200402.004'
217 response = self.createVote(user, motion, 'abstain')
218 self.assertEqual(response.status_code, 302)
219 result = self.app.get('/', environ_base={'USER_ROLES': user})
220 resulttext=self.buildResultText('A fourth motion', 0, 0, 1)
221 self.assertIn(str.encode(resulttext), result.data)
222 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
223 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"yes\" id=\"vote-yes\">yes</button>'
224 self.assertIn(str.encode(testtext), result.data)
225 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"no\" id=\"vote-no\">no</button>'
226 self.assertIn(str.encode(testtext), result.data)
227 testtext= 'class=\"btn btn-success\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">abstain</button>'
228 self.assertIn(str.encode(testtext), result.data)
230 def test_vote_change(self):
231 motion='g1.20200402.004'
232 response = self.createVote(user, motion, 'yes')
233 self.assertEqual(response.status_code, 302)
234 result = self.app.get('/', environ_base={'USER_ROLES': user})
235 resulttext=self.buildResultText('A fourth motion', 1, 0, 0)
236 self.assertIn(str.encode(resulttext), result.data)
237 response = self.createVote(user, motion, 'no')
238 self.assertEqual(response.status_code, 302)
239 result = self.app.get('/', environ_base={'USER_ROLES': user})
240 resulttext=self.buildResultText('A fourth motion', 0, 1, 0)
241 self.assertIn(str.encode(resulttext), result.data)
242 response = self.createVote(user, motion, 'abstain')
243 self.assertEqual(response.status_code, 302)
244 result = self.app.get('/', environ_base={'USER_ROLES': user})
245 resulttext=self.buildResultText('A fourth motion', 0, 0, 1)
246 self.assertIn(str.encode(resulttext), result.data)
248 def test_vote_group(self):
249 motion='g1.20200402.004'
250 response = self.createVote(user, motion, 'yes')
251 self.assertEqual(response.status_code, 302)
253 motion='g1.20200402.004'
254 user1='testuser/vote:group1'
255 response = self.createVote(user1, motion, 'yes')
256 self.assertEqual(response.status_code, 302)
258 motion='g1.20200402.004'
259 user1='testuser/vote:group1 vote:group2'
260 response = self.createVote(user1, motion, 'yes')
261 self.assertEqual(response.status_code, 302)
263 def test_vote_wrong_group(self):
264 motion='g1.20200402.004'
265 user1='testuser/vote:group2'
266 response = self.createVote(user1, motion, 'yes')
267 self.assertEqual(response.status_code, 403)
268 self.assertIn(str.encode('Forbidden'), response.data)
270 def test_vote_closed(self):
271 motion='g1.20200402.002'
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_canceled(self):
277 motion='g1.20200402.003'
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_not_given(self):
283 motion='g1.30190402.001'
284 response = self.createVote(user, motion, 'abstain')
285 self.assertEqual(response.status_code, 404)
286 self.assertIn(str.encode('Error, Not found'), response.data)
288 def test_cancelMotion(self):
289 motion='g1.20200402.004'
291 response = self.cancelMotion(user, motion, reason)
292 self.assertEqual(response.status_code, 403)
293 self.assertIn(str.encode('Forbidden'), response.data)
295 def test_see_old_vote(self):
296 motion='g1.20200402.002'
297 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
298 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>'\
299 + '\n </div>\n <div class="card-body">\n <p><p>A second motion</p></p>\n </div>\n</div>'\
300 + '\n<a href="/?start=2#motion-2" class="btn btn-primary">Back</a>'
301 self.assertIn(str.encode(testtext), result.data)
303 def test_createMotion(self):
306 response = self.createMotion(user, title, content, '3', 'group1')
307 self.assertEqual(response.status_code, 403)
308 self.assertIn(str.encode('Forbidden'), response.data)
311 class CreateMotionTests(BasicTest):
316 user='testuser/vote:* create:* cancel:*'
322 def test_main_page(self):
323 response = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
324 self.assertEqual(response.status_code, 200)
326 def test_home_data(self):
327 result = self.app.get('/', environ_base={'USER_ROLES': user})
329 # assert the response data
330 self.assertIn(b'User: testuser', result.data)
331 self.assertIn("<select class=\"float form-control\" name=\"category\">", str(result.data) )
333 def test_createMotion(self):
336 response = self.createMotion(user, title, content, '3', 'group1')
337 self.assertEqual(response.status_code, 302)
338 result = self.app.get('/', environ_base={'USER_ROLES': user})
339 self.assertIn(str.encode(title), result.data)
340 self.assertIn(str.encode(content), result.data)
341 self.assertIn(str.encode('g1.'+datetime.today().strftime('%Y%m%d')+'.001'), result.data)
342 testtext='<a class=\"btn btn-primary" href=\"/motion/g1.'+datetime.today().strftime('%Y%m%d')+'.001\" role=\"button\">Vote</a>'
343 self.assertIn(str.encode(testtext), result.data)
347 response = self.createMotion(user, title, content, '3', 'group1')
348 self.assertEqual(response.status_code, 302)
349 result = self.app.get('/', environ_base={'USER_ROLES': user})
350 self.assertIn(str.encode(title), result.data)
351 self.assertIn(str.encode(content), result.data)
352 self.assertIn(str.encode('g1.'+datetime.today().strftime('%Y%m%d')+'.002'), result.data)
356 response = self.createMotion(user, title, content, '3', 'group2')
357 self.assertEqual(response.status_code, 302)
358 result = self.app.get('/', environ_base={'USER_ROLES': user})
359 self.assertIn(str.encode(title), result.data)
360 self.assertIn(str.encode(content), result.data)
361 self.assertIn(str.encode('g2.'+datetime.today().strftime('%Y%m%d')+'.001'), result.data)
365 user1='testuser/vote:* create:group1 cancel:*'
366 response = self.createMotion(user1, title, content, '3', 'group1')
367 self.assertEqual(response.status_code, 302)
371 user1='testuser/vote:* create:group1 create:group2 cancel:*'
372 response = self.createMotion(user1, title, content, '3', 'group1')
373 self.assertEqual(response.status_code, 302)
376 def test_createMotionMarkdown(self):
377 title='Markdown Test'
378 content= 'MyMotionBody MD [text](https//domain.tld/link)'
379 response = self.createMotion(user, title, content, '3', 'group1')
380 self.assertEqual(response.status_code, 302)
381 result = self.app.get('/', environ_base={'USER_ROLES': user})
382 self.assertIn(str.encode(title), result.data)
383 self.assertIn(b'MyMotionBody MD <a href=\"https//domain.tld/link\">text</a>', result.data)
385 def test_createMotionMarkdownDirectLink(self):
386 title='Markdown Test Link'
387 content='MyMotionBody MD <a href=\"https//domain.tld/link\">direct</a'
388 response = self.createMotion(user, title, content, '3', 'group1')
389 self.assertEqual(response.status_code, 302)
390 result = self.app.get('/', environ_base={'USER_ROLES': user})
391 self.assertIn(str.encode(title), result.data)
392 self.assertIn(b'MyMotionBody MD <a href="https//domain.tld/link">direct</a', result.data)
394 def test_createMotionMarkdownCombined(self):
395 title='Markdown Test Link'
396 content='Body [combined](https//domain.tld/link) <a href=\"https//domain.tld/link\">combined1</a'
397 response = self.createMotion(user, title, content, '3', 'group1')
398 self.assertEqual(response.status_code, 302)
399 result = self.app.get('/', environ_base={'USER_ROLES': user})
400 self.assertIn(str.encode(title), result.data)
401 self.assertIn(b'Body <a href=\"https//domain.tld/link\">combined</a> <a href="https//domain.tld/link">combined1</a', result.data)
403 def test_createMotionWrongDayLength(self):
406 response = self.createMotion(user, title, content, '21', 'group1')
407 self.assertEqual(response.status_code, 500)
408 self.assertIn(str.encode('Error, invalid length'), response.data)
410 def test_createMotionWrongGroup(self):
413 response = self.createMotion(user, title, content, '3', 'test1')
414 self.assertEqual(response.status_code, 403)
415 self.assertIn(str.encode('Forbidden'), response.data)
417 user1='testuser/vote:* create:group1 cancel:*'
418 response = self.createMotion(user1, title, content, '3', 'group2')
419 self.assertEqual(response.status_code, 403)
420 self.assertIn(str.encode('Forbidden'), response.data)
422 def test_cancelMotion(self):
425 motion='g1.20200402.004'
427 response = self.cancelMotion(user, motion, reason)
428 self.assertEqual(response.status_code, 500)
429 self.assertIn(str.encode('Error, form requires reason'), response.data)
432 response = self.cancelMotion(user, motion, reason)
433 self.assertEqual(response.status_code, 302)
434 result = self.app.get('/', environ_base={'USER_ROLES': user})
435 self.assertIn(b'Cancelation reason: ' + str.encode(reason), result.data)
437 motion='g1.30190402.001'
439 response = self.cancelMotion(user, motion, reason)
440 self.assertEqual(response.status_code, 404)
441 self.assertIn(str.encode('Error, Not found'), response.data)
444 class AuditMotionTests(BasicTest):
449 user='testuser/audit:*'
455 def test_see_old_vote(self):
456 motion='g1.20200402.002'
457 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
458 testtext= '<div class="motion card" id="votes">\n <div class="card-heading text-white bg-info">\n Motion Votes\n </div>'\
459 + '\n <div class="card-body">\n <div>User A: yes</div>\n <div>User B: no</div>'\
460 + '\n <div>User C: no</div>\n </div>\n</div>\n<a href="/?start=2#motion-2" class="btn btn-primary">Back</a>'
461 self.assertIn(str.encode(testtext), result.data)
464 if __name__ == "__main__":