]> WPIA git - motion.git/blob - tests/test_motion.py
Merge branch 'unit-test' 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># <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)
117
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)
126
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)
132
133     def test_vote(self):
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)
138
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)
143
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)
148
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)
153
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)
159
160
161 class VoterTests(BasicTest):
162
163     def setUp(self):
164         self.init_test()
165         global user
166         user='testuser/vote:*'
167         self.db_sampledata()
168
169     def tearDown(self):
170         pass
171
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)
175
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) )
179
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)
193
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)
208
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)
223
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)
241
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)
246
247         motion='g1.20200402.004'
248         user1='testuser/vote:group1'
249         response = self.createVote(user1, motion, 'yes')
250         self.assertEqual(response.status_code, 302)
251
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)
256
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)
263
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)
269
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)
275
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)
281
282     def test_cancelMotion(self):
283         motion='g1.20200402.004'
284         reason="none"
285         response = self.cancelMotion(user, motion, reason)
286         self.assertEqual(response.status_code, 403)
287         self.assertIn(str.encode('Forbidden'), response.data)
288
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)
296
297     def test_createMotion(self):
298         title='My Motion'
299         content='My body'
300         response = self.createMotion(user, title, content, '3', 'group1')
301         self.assertEqual(response.status_code, 403)
302         self.assertIn(str.encode('Forbidden'), response.data)
303
304
305 class CreateMotionTests(BasicTest):
306
307     def setUp(self):
308         self.init_test()
309         global user
310         user='testuser/vote:* create:* cancel:*'
311         self.db_clear()
312
313     def tearDown(self):
314         pass
315
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)
319
320     def test_home_data(self):
321         result = self.app.get('/', environ_base={'USER_ROLES': user})
322
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) )
326
327     def test_createMotion(self):
328         title='My Motion'
329         content='My body'
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)
336
337         title='My Motion1'
338         content='My body1'
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)
345
346         title='My Motion2'
347         content='My body2'
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)
354
355         title='My Motion3'
356         content='My body3'
357         user1='testuser/vote:* create:group1 cancel:*'
358         response = self.createMotion(user1, title, content, '3', 'group1')
359         self.assertEqual(response.status_code, 302)
360
361         title='My Motion4'
362         content='My body4'
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)
366
367
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)
376
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 &lt;a href="https//domain.tld/link"&gt;direct&lt;/a', result.data)
385
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> &lt;a href="https//domain.tld/link"&gt;combined1&lt;/a', result.data)
394
395     def test_createMotionWrongDayLength(self):
396         title='My Motion'
397         content='My body'
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)
401
402     def test_createMotionWrongGroup(self):
403         title='My Motion'
404         content='My body'
405         response = self.createMotion(user, title, content, '3', 'test1')
406         self.assertEqual(response.status_code, 403)
407         self.assertIn(str.encode('Forbidden'), response.data)
408
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)
413
414     def test_cancelMotion(self):
415         self.db_sampledata()
416
417         motion='g1.20200402.004'
418         reason="none"
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)
422
423         reason='cancel test'
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)
428
429         motion='g1.30190402.001'
430         reason="none"
431         response = self.cancelMotion(user, motion, reason)
432         self.assertEqual(response.status_code, 404)
433         self.assertIn(str.encode('Error, Not found'), response.data)
434
435
436 class AuditMotionTests(BasicTest):
437
438     def setUp(self):
439         self.init_test()
440         global user
441         user='testuser/audit:*'
442         self.db_sampledata()
443
444     def tearDown(self):
445         pass
446
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)
454
455
456 if __name__ == "__main__":
457     unittest.main()