4 from unittest import TestCase
6 from datetime import datetime
8 class BasicTest(TestCase):
10 # functions to manipulate motions
11 def createVote(self, user, motion, vote):
13 '/motion/' + motion +'/vote',
14 environ_base={'USER_ROLES': user},
19 def createMotion(self, user, motiontitle, motioncontent, days, category):
22 environ_base={'USER_ROLES': user},
23 data=dict(title=motiontitle, content=motioncontent, days=days, category=category)
26 def cancelMotion(self, user, motion, reason):
28 '/motion/' + motion +'/cancel',
29 environ_base={'USER_ROLES': user},
30 data=dict(reason=reason)
33 def buildResultText(self, motiontext, yes, no, abstain):
34 return '<p>'+motiontext+'</p></p>\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">'+str(yes)+'</span><br>'\
35 + '\nNo <span class=\"badge badge-pill badge-secondary\">'+str(no)+'</span><br>'\
36 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">'+str(abstain)+'</span>'
38 # functions to clear database
40 db = postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD"))
41 with app.open_resource('sql/schema.sql', mode='r') as f:
44 def db_sampledata(self):
45 db = postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD"))
46 with app.open_resource('sql/sample_data.sql', mode='r') as f:
50 # no specific rights required
51 class GeneralTests(BasicTest):
54 app.config['TESTING'] = True
55 app.config['DEBUG'] = False
56 app.config.update(SERVER_NAME="127.0.0.1:5000")
63 self.app = app.test_client()
64 self.assertEqual(app.debug, False)
69 def test_main_page(self):
70 response = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
71 self.assertEqual(response.status_code, 200)
73 def test_basic_results_data(self):
74 result = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
76 #self.assertIn(str.encode('User: testuser'), result.data)
78 testtext= '<div class="motion card" id="motion-3">\n <div class="motion-title card-heading alert-warning">'\
79 + '\n <span class=\"title-text\">Motion C</span> (Canceled)\n <span class=\"motion-type\">group1</span>'\
80 + '\n <div># <a href=\"/motion/g1.20200402.003\" class=\"anchor\">g1.20200402.003</a></div>'\
81 + '\n <div class=\"date\">\n <div>Proposed: 2020-04-02 21:47:24 (UTC) by User A</div>'\
82 + '\n <div>Canceled: 2020-04-03 21:48:24 (UTC) by User A</div></div>\n </div>'\
83 + '\n <div class=\"card-body\">\n <p><p>A third motion</p></p>'\
84 + '\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">1</span><br>'\
85 + '\nNo <span class=\"badge badge-pill badge-secondary\">0</span><br>'\
86 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">0</span><br>\n </p>'\
87 + '\n <p>Cancelation reason: Entered with wrong text</p>\n </div>\n</div>'
88 self.assertIn(str.encode(testtext), result.data)
89 testtext= '<div class="motion card" id="motion-2">\n <div class="motion-title card-heading alert-danger">'\
90 + '\n <span class=\"title-text\">Motion B</span> (Finished)\n <span class=\"motion-type\">group1</span>'\
91 + '\n <div># <a href=\"/motion/g1.20200402.002\" class=\"anchor\">g1.20200402.002</a></div>'\
92 + '\n <div class=\"date\">\n <div>Proposed: 2020-04-02 21:41:26 (UTC) by User A</div>'\
93 + '\n <div>Votes until: 2020-04-04 21:41:26 (UTC)</div></div>\n </div>'\
94 + '\n <div class=\"card-body\">\n <p><p>A second motion</p></p>'\
95 + '\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">1</span><br>'\
96 + '\nNo <span class=\"badge badge-pill badge-secondary\">2</span><br>'\
97 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">0</span><br>\n </p>\n </div>\n</div>\n'
98 self.assertIn(str.encode(testtext), result.data)
99 testtext= '<div class=\"motion card\" id=\"motion-1\">\n <div class=\"motion-title card-heading alert-success\">'\
100 + '\n <span class=\"title-text\">Motion A</span> (Finished)\n <span class=\"motion-type\">group1</span>'\
101 + '\n <div># <a href=\"/motion/g1.20200402.001\" class=\"anchor\">g1.20200402.001</a></div>'\
102 + '\n <div class=\"date">\n <div>Proposed: 2020-04-02 21:40:33 (UTC) by User A</div>'\
103 + '\n <div>Votes until: 2020-04-02 21:40:33 (UTC)</div></div>\n </div>'\
104 + '\n <div class=\"card-body\">\n <p><p>My special motion</p></p>'\
105 + '\n <p>\nYes <span class=\"badge badge-pill badge-secondary\">2</span><br>'\
106 + '\nNo <span class=\"badge badge-pill badge-secondary\">1</span><br>'\
107 + '\nAbstain <span class=\"badge badge-pill badge-secondary\">0</span><br>\n </p>\n </div>\n</div>\n</div>'
108 self.assertIn(str.encode(testtext), result.data)
110 # start with second motion
111 result = self.app.get('/', environ_base={'USER_ROLES': user}, query_string=dict(start=2))
112 testtext= 'id=\"motion-3\">'
113 self.assertNotIn(str.encode(testtext), result.data)
114 testtext= 'id=\"motion-2">'
115 self.assertIn(str.encode(testtext), result.data)
116 testtext= 'id=\"motion-1\">'
117 self.assertIn(str.encode(testtext), result.data)
119 def test_basic_results_data_details(self):
120 motion='g1.20200402.002'
121 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
122 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>'
123 self.assertIn(str.encode(testtext), result.data)
126 motion='g1.20200402.004'
127 response = self.createVote(user, motion, 'yes')
128 self.assertEqual(response.status_code, 403)
129 self.assertIn(str.encode('Forbidden'), response.data)
131 def test_no_user(self):
132 result = self.app.get('/', follow_redirects=True)
133 self.assertEqual(result.status_code, 500)
134 self.assertIn(str.encode('Server misconfigured'), result.data)
136 def test_user_invalid(self):
137 result = self.app.get('/', environ_base={'USER_ROLES': '<invalid>/'}, follow_redirects=True)
138 self.assertEqual(result.status_code, 403)
139 self.assertIn(str.encode('Access denied'), result.data)
141 def test_basic_env(self):
142 result = self.app.get('/', environ_base={'USER': 'testuser', 'ROLES':''}, follow_redirects=True)
143 testtext= 'id=\"motion-3\">'
144 self.assertIn(str.encode(testtext), result.data)
146 class VoterTests(BasicTest):
148 app.config['TESTING'] = True
149 app.config['DEBUG'] = False
150 app.config.update(SERVER_NAME="127.0.0.1:5000")
152 user='testuser/vote:*'
157 self.app = app.test_client()
158 self.assertEqual(app.debug, False)
163 def test_main_page(self):
164 response = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
165 self.assertEqual(response.status_code, 200)
167 def test_home_data(self):
168 result = self.app.get('/', environ_base={'USER_ROLES': user})
169 self.assertNotIn("<select class=\"float form-control\" name=\"category\">", str(result.data) )
171 def test_vote_yes(self):
172 motion='g1.20200402.004'
173 response = self.createVote(user, motion, 'yes')
174 self.assertEqual(response.status_code, 302)
175 result = self.app.get('/', environ_base={'USER_ROLES': user})
176 resulttext=self.buildResultText('A fourth motion', 1, 0, 0)
177 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
178 testtext= 'class=\"btn btn-success\" name=\"vote\" value="yes" id="vote-yes">yes</button>'
179 self.assertIn(str.encode(testtext), result.data)
180 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"no\" id=\"vote-no\">no</button>'
181 self.assertIn(str.encode(testtext), result.data)
182 testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">abstain</button>'
183 self.assertIn(str.encode(testtext), result.data)
185 def test_vote_no(self):
186 motion='g1.20200402.004'
187 response = self.createVote(user, motion, 'no')
188 self.assertEqual(response.status_code, 302)
189 result = self.app.get('/', environ_base={'USER_ROLES': user})
190 resulttext=self.buildResultText('A fourth motion', 0, 1, 0)
191 self.assertIn(str.encode(resulttext), result.data)
192 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
193 testtext= 'class="btn btn-primary" name="vote\" value=\"yes\" id=\"vote-yes\">yes</button>'
194 self.assertIn(str.encode(testtext), result.data)
195 testtext= 'class=\"btn btn-success\" 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_abstain(self):
201 motion='g1.20200402.004'
202 response = self.createVote(user, motion, 'abstain')
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, 0, 1)
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-primary\" name=\"vote\" value=\"no\" id=\"vote-no\">no</button>'
211 self.assertIn(str.encode(testtext), result.data)
212 testtext= 'class=\"btn btn-success\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">abstain</button>'
213 self.assertIn(str.encode(testtext), result.data)
215 def test_vote_change(self):
216 motion='g1.20200402.004'
217 response = self.createVote(user, motion, 'yes')
218 self.assertEqual(response.status_code, 302)
219 result = self.app.get('/', environ_base={'USER_ROLES': user})
220 resulttext=self.buildResultText('A fourth motion', 1, 0, 0)
221 self.assertIn(str.encode(resulttext), result.data)
222 response = self.createVote(user, motion, 'no')
223 self.assertEqual(response.status_code, 302)
224 result = self.app.get('/', environ_base={'USER_ROLES': user})
225 resulttext=self.buildResultText('A fourth motion', 0, 1, 0)
226 self.assertIn(str.encode(resulttext), result.data)
227 response = self.createVote(user, motion, 'abstain')
228 self.assertEqual(response.status_code, 302)
229 result = self.app.get('/', environ_base={'USER_ROLES': user})
230 resulttext=self.buildResultText('A fourth motion', 0, 0, 1)
231 self.assertIn(str.encode(resulttext), result.data)
233 def test_vote_group(self):
234 motion='g1.20200402.004'
235 response = self.createVote(user, motion, 'yes')
236 self.assertEqual(response.status_code, 302)
238 motion='g1.20200402.004'
239 user1='testuser/vote:group1'
240 response = self.createVote(user1, motion, 'yes')
241 self.assertEqual(response.status_code, 302)
243 motion='g1.20200402.004'
244 user1='testuser/vote:group1 vote:group2'
245 response = self.createVote(user1, motion, 'yes')
246 self.assertEqual(response.status_code, 302)
248 def test_vote_wrong_group(self):
249 motion='g1.20200402.004'
250 user1='testuser/vote:group2'
251 response = self.createVote(user1, motion, 'yes')
252 self.assertEqual(response.status_code, 403)
253 self.assertIn(str.encode('Forbidden'), response.data)
255 def test_vote_closed(self):
256 motion='g1.20200402.002'
257 response = self.createVote(user, motion, 'abstain')
258 self.assertEqual(response.status_code, 500)
259 self.assertIn(str.encode('Error, motion deadline has passed'), response.data)
261 def test_vote_canceled(self):
262 motion='g1.20200402.003'
263 response = self.createVote(user, motion, 'abstain')
264 self.assertEqual(response.status_code, 500)
265 self.assertIn(str.encode('Error, motion deadline has passed'), response.data)
267 def test_vote_not_given(self):
268 motion='g1.30190402.001'
269 response = self.createVote(user, motion, 'abstain')
270 self.assertEqual(response.status_code, 404)
271 self.assertIn(str.encode('Error, Not found'), response.data)
273 def test_cancelMotion(self):
274 motion='g1.20200402.004'
276 response = self.cancelMotion(user, motion, reason)
277 self.assertEqual(response.status_code, 403)
278 self.assertIn(str.encode('Forbidden'), response.data)
280 def test_see_old_vote(self):
281 motion='g1.20200402.002'
282 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
283 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>'\
284 + '\n </div>\n <div class="card-body">\n <p><p>A second motion</p></p>\n </div>\n</div>'\
285 + '\n<a href="/?start=2#motion-2" class="btn btn-primary">Back</a>'
286 self.assertIn(str.encode(testtext), result.data)
288 def test_createMotion(self):
291 response = self.createMotion(user, title, content, '3', 'group1')
292 self.assertEqual(response.status_code, 403)
293 self.assertIn(str.encode('Forbidden'), response.data)
295 class CreateMotionTests(BasicTest):
297 app.config['TESTING'] = True
298 app.config['DEBUG'] = False
299 app.config.update(SERVER_NAME="127.0.0.1:5000")
301 user='testuser/vote:* create:* cancel:*'
305 self.app = app.test_client()
306 self.assertEqual(app.debug, False)
308 # executed after each test
312 def test_main_page(self):
313 response = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
314 self.assertEqual(response.status_code, 200)
316 def test_home_data(self):
317 result = self.app.get('/', environ_base={'USER_ROLES': user})
319 # assert the response data
320 self.assertIn(b'User: testuser', result.data)
321 self.assertIn("<select class=\"float form-control\" name=\"category\">", str(result.data) )
323 def test_createMotion(self):
326 response = self.createMotion(user, title, content, '3', 'group1')
327 self.assertEqual(response.status_code, 302)
328 result = self.app.get('/', environ_base={'USER_ROLES': user})
329 self.assertIn(str.encode(title), result.data)
330 self.assertIn(str.encode(content), result.data)
331 self.assertIn(str.encode('g1.'+datetime.today().strftime('%Y%m%d')+'.001'), result.data)
335 response = self.createMotion(user, title, content, '3', 'group1')
336 self.assertEqual(response.status_code, 302)
337 result = self.app.get('/', environ_base={'USER_ROLES': user})
338 self.assertIn(str.encode(title), result.data)
339 self.assertIn(str.encode(content), result.data)
340 self.assertIn(str.encode('g1.'+datetime.today().strftime('%Y%m%d')+'.002'), result.data)
344 response = self.createMotion(user, title, content, '3', 'group2')
345 self.assertEqual(response.status_code, 302)
346 result = self.app.get('/', environ_base={'USER_ROLES': user})
347 self.assertIn(str.encode(title), result.data)
348 self.assertIn(str.encode(content), result.data)
349 self.assertIn(str.encode('g2.'+datetime.today().strftime('%Y%m%d')+'.001'), result.data)
353 user1='testuser/vote:* create:group1 cancel:*'
354 response = self.createMotion(user1, title, content, '3', 'group1')
355 self.assertEqual(response.status_code, 302)
359 user1='testuser/vote:* create:group1 create:group2 cancel:*'
360 response = self.createMotion(user1, title, content, '3', 'group1')
361 self.assertEqual(response.status_code, 302)
364 def test_createMotionMarkdown(self):
365 title='Markdown Test'
366 content= 'MyMotionBody MD [text](https//domain.tld/link)'
367 response = self.createMotion(user, title, content, '3', 'group1')
368 self.assertEqual(response.status_code, 302)
369 result = self.app.get('/', environ_base={'USER_ROLES': user})
370 self.assertIn(str.encode(title), result.data)
371 self.assertIn(b'MyMotionBody MD <a href=\"https//domain.tld/link\">text</a>', result.data)
373 def test_createMotionMarkdownDirectLink(self):
374 title='Markdown Test Link'
375 content='MyMotionBody MD <a href=\"https//domain.tld/link\">direct</a'
376 response = self.createMotion(user, title, content, '3', 'group1')
377 self.assertEqual(response.status_code, 302)
378 result = self.app.get('/', environ_base={'USER_ROLES': user})
379 self.assertIn(str.encode(title), result.data)
380 self.assertIn(b'MyMotionBody MD <a href="https//domain.tld/link">direct</a', result.data)
382 def test_createMotionMarkdownCombined(self):
383 title='Markdown Test Link'
384 content='Body [combined](https//domain.tld/link) <a href=\"https//domain.tld/link\">combined1</a'
385 response = self.createMotion(user, title, content, '3', 'group1')
386 self.assertEqual(response.status_code, 302)
387 result = self.app.get('/', environ_base={'USER_ROLES': user})
388 self.assertIn(str.encode(title), result.data)
389 self.assertIn(b'Body <a href=\"https//domain.tld/link\">combined</a> <a href="https//domain.tld/link">combined1</a', result.data)
391 def test_createMotionWrongDayLength(self):
394 response = self.createMotion(user, title, content, '21', 'group1')
395 self.assertEqual(response.status_code, 500)
396 self.assertIn(str.encode('Error, invalid length'), response.data)
398 def test_createMotionWrongGroup(self):
401 response = self.createMotion(user, title, content, '3', 'test1')
402 self.assertEqual(response.status_code, 403)
403 self.assertIn(str.encode('Forbidden'), response.data)
405 user1='testuser/vote:* create:group1 cancel:*'
406 response = self.createMotion(user1, title, content, '3', 'group2')
407 self.assertEqual(response.status_code, 403)
408 self.assertIn(str.encode('Forbidden'), response.data)
410 def test_cancelMotion(self):
413 motion='g1.20200402.004'
415 response = self.cancelMotion(user, motion, reason)
416 self.assertEqual(response.status_code, 500)
417 self.assertIn(str.encode('Error, form requires reason'), response.data)
420 response = self.cancelMotion(user, motion, reason)
421 self.assertEqual(response.status_code, 302)
422 result = self.app.get('/', environ_base={'USER_ROLES': user})
423 self.assertIn(b'Cancelation reason: ' + str.encode(reason), result.data)
425 motion='g1.30190402.001'
427 response = self.cancelMotion(user, motion, reason)
428 self.assertEqual(response.status_code, 404)
429 self.assertIn(str.encode('Error, Not found'), response.data)
431 class AuditMotionTests(BasicTest):
433 app.config['TESTING'] = True
434 app.config['DEBUG'] = False
435 app.config.update(SERVER_NAME="127.0.0.1:5000")
437 user='testuser/audit:*'
442 self.app = app.test_client()
443 self.assertEqual(app.debug, False)
445 def test_see_old_vote(self):
446 motion='g1.20200402.002'
447 result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
448 testtext= '<div class="motion card" id="votes">\n <div class="card-heading text-white bg-info">\n Motion Votes\n </div>'\
449 + '\n <div class="card-body">\n <div>User A: yes</div>\n <div>User B: no</div>'\
450 + '\n <div>User C: no</div>\n </div>\n</div>\n<a href="/?start=2#motion-2" class="btn btn-primary">Back</a>'
451 self.assertIn(str.encode(testtext), result.data)
453 # executed after each test
458 if __name__ == "__main__":