]> WPIA git - motion.git/blob - tests/test_motion.py
Merge branch 'vote-button' into 'master'
[motion.git] / tests / test_motion.py
1 import motion
2 import unittest
3 import postgresql
4 from unittest import TestCase
5 from motion import app
6 from datetime import datetime
7
8 app.config.update(
9     DEBUGUSER = {},
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'
13 )
14
15 app.config['TESTING'] = True
16 app.config['DEBUG'] = False
17
18
19 class BasicTest(TestCase):
20
21     def init_test(self):
22         self.app = app.test_client()
23         self.assertEqual(app.debug, False)
24
25         # reset database
26         self.db_clear()
27
28     # functions to manipulate motions
29     def createVote(self, user, motion, vote):
30         return self.app.post(
31             '/motion/' + motion +'/vote',
32             environ_base={'USER_ROLES': user},
33             data=dict(vote=vote)
34         )
35         
36
37     def createMotion(self, user, motiontitle, motioncontent, days, category):
38         return self.app.post(
39             '/motion',
40             environ_base={'USER_ROLES': user},
41             data=dict(title=motiontitle, content=motioncontent, days=days, category=category)
42         )
43
44     def cancelMotion(self, user, motion, reason):
45         return self.app.post(
46             '/motion/' + motion +'/cancel',
47             environ_base={'USER_ROLES': user},
48             data=dict(reason=reason)
49         )
50
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>'
55
56     # functions to clear database
57     def db_clear(self):
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:
60                 db.execute(f.read())
61
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:
65                 db.execute(f.read())
66
67
68 # no specific rights required
69 class GeneralTests(BasicTest):
70
71     def setUp(self):
72         self.init_test()
73         global user
74         user = 'testuser/'
75         self.db_sampledata()
76
77     def tearDown(self):
78         pass
79
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)
83
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>'\
90             + '\n    </div>'\
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>'\
103             + '\n    </div>'\
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>'\
115             + '\n    </div>'\
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)
123
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)
132
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)
138
139     def test_vote(self):
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)
144
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)
149
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)
154
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)
159
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)
165
166
167 class VoterTests(BasicTest):
168
169     def setUp(self):
170         self.init_test()
171         global user
172         user='testuser/vote:*'
173         self.db_sampledata()
174
175     def tearDown(self):
176         pass
177
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)
181
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) )
185
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)
199
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)
214
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)
229
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)
247
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)
252
253         motion='g1.20200402.004'
254         user1='testuser/vote:group1'
255         response = self.createVote(user1, motion, 'yes')
256         self.assertEqual(response.status_code, 302)
257
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)
262
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)
269
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)
275
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)
281
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)
287
288     def test_cancelMotion(self):
289         motion='g1.20200402.004'
290         reason="none"
291         response = self.cancelMotion(user, motion, reason)
292         self.assertEqual(response.status_code, 403)
293         self.assertIn(str.encode('Forbidden'), response.data)
294
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)
302
303     def test_createMotion(self):
304         title='My Motion'
305         content='My body'
306         response = self.createMotion(user, title, content, '3', 'group1')
307         self.assertEqual(response.status_code, 403)
308         self.assertIn(str.encode('Forbidden'), response.data)
309
310
311 class CreateMotionTests(BasicTest):
312
313     def setUp(self):
314         self.init_test()
315         global user
316         user='testuser/vote:* create:* cancel:*'
317         self.db_clear()
318
319     def tearDown(self):
320         pass
321
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)
325
326     def test_home_data(self):
327         result = self.app.get('/', environ_base={'USER_ROLES': user})
328
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) )
332
333     def test_createMotion(self):
334         title='My Motion'
335         content='My body'
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)
344
345         title='My Motion1'
346         content='My body1'
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)
353
354         title='My Motion2'
355         content='My body2'
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)
362
363         title='My Motion3'
364         content='My body3'
365         user1='testuser/vote:* create:group1 cancel:*'
366         response = self.createMotion(user1, title, content, '3', 'group1')
367         self.assertEqual(response.status_code, 302)
368
369         title='My Motion4'
370         content='My body4'
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)
374
375
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)
384
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 &lt;a href="https//domain.tld/link"&gt;direct&lt;/a', result.data)
393
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> &lt;a href="https//domain.tld/link"&gt;combined1&lt;/a', result.data)
402
403     def test_createMotionWrongDayLength(self):
404         title='My Motion'
405         content='My body'
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)
409
410     def test_createMotionWrongGroup(self):
411         title='My Motion'
412         content='My body'
413         response = self.createMotion(user, title, content, '3', 'test1')
414         self.assertEqual(response.status_code, 403)
415         self.assertIn(str.encode('Forbidden'), response.data)
416
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)
421
422     def test_cancelMotion(self):
423         self.db_sampledata()
424
425         motion='g1.20200402.004'
426         reason="none"
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)
430
431         reason='cancel test'
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)
436
437         motion='g1.30190402.001'
438         reason="none"
439         response = self.cancelMotion(user, motion, reason)
440         self.assertEqual(response.status_code, 404)
441         self.assertIn(str.encode('Error, Not found'), response.data)
442
443
444 class AuditMotionTests(BasicTest):
445
446     def setUp(self):
447         self.init_test()
448         global user
449         user='testuser/audit:*'
450         self.db_sampledata()
451
452     def tearDown(self):
453         pass
454
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)
462
463
464 if __name__ == "__main__":
465     unittest.main()