]> WPIA git - motion.git/blob - tests/test_motion.py
fe853852d292ff3e1f7c9c0a60b297fd626f58aa
[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 class BasicTest(TestCase):
9
10     # functions to manipulate motions
11     def createVote(self, user, motion, vote):
12         return self.app.post(
13             '/motion/' + motion +'/vote',
14             environ_base={'USER_ROLES': user},
15             data=dict(vote=vote)
16         )
17         
18
19     def createMotion(self, user, motiontitle, motioncontent, days, category):
20         return self.app.post(
21             '/motion',
22             environ_base={'USER_ROLES': user},
23             data=dict(title=motiontitle, content=motioncontent, days=days, category=category)
24         )
25
26     def cancelMotion(self, user, motion, reason):
27         return self.app.post(
28             '/motion/' + motion +'/cancel',
29             environ_base={'USER_ROLES': user},
30             data=dict(reason=reason)
31         )
32
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>'
37
38     # functions to clear database
39     def db_clear(self):
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:
42             db.execute(f.read())
43
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:
47             db.execute(f.read())
48
49
50 # no specific rights required
51 class GeneralTests(BasicTest):
52     
53     def setUp(self):
54         app.config['TESTING'] = True
55         app.config['DEBUG'] = False
56         app.config.update(SERVER_NAME="127.0.0.1:5000")
57         global user
58         user = 'testuser/'
59         # reset database
60         self.db_clear()
61         self.db_sampledata()
62
63         self.app = app.test_client()
64         self.assertEqual(app.debug, False)
65
66     def tearDown(self):
67         pass
68
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)
72         
73     def test_basic_results_data(self):
74         result = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True)
75
76         #self.assertIn(str.encode('User: testuser'), result.data)
77
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)
109
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)
118         
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)
124
125     def test_vote(self):
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)
130
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)
135
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)
140
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)
145
146 class VoterTests(BasicTest):
147     def setUp(self):
148         app.config['TESTING'] = True
149         app.config['DEBUG'] = False
150         app.config.update(SERVER_NAME="127.0.0.1:5000")
151         global user
152         user='testuser/vote:*'
153         # reset database
154         self.db_clear()
155         self.db_sampledata()
156
157         self.app = app.test_client()
158         self.assertEqual(app.debug, False)
159
160     def tearDown(self):
161         pass
162
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)
166         
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) )
170
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)
184
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)
199
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)
214
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)
232         
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)
237
238         motion='g1.20200402.004'
239         user1='testuser/vote:group1'
240         response = self.createVote(user1, motion, 'yes')
241         self.assertEqual(response.status_code, 302)
242
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)
247
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)
254
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)
260
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)
266         
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)
272
273     def test_cancelMotion(self):
274         motion='g1.20200402.004'
275         reason="none"
276         response = self.cancelMotion(user, motion, reason)
277         self.assertEqual(response.status_code, 403)
278         self.assertIn(str.encode('Forbidden'), response.data)
279
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)
287
288     def test_createMotion(self):
289         title='My Motion'
290         content='My body'
291         response = self.createMotion(user, title, content, '3', 'group1')
292         self.assertEqual(response.status_code, 403)
293         self.assertIn(str.encode('Forbidden'), response.data)
294
295 class CreateMotionTests(BasicTest):
296     def setUp(self):
297         app.config['TESTING'] = True
298         app.config['DEBUG'] = False
299         app.config.update(SERVER_NAME="127.0.0.1:5000")
300         global user
301         user='testuser/vote:* create:* cancel:*'
302         # reset database
303         self.db_clear()
304
305         self.app = app.test_client()
306         self.assertEqual(app.debug, False)
307  
308     # executed after each test
309     def tearDown(self):
310         pass
311
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)
315
316     def test_home_data(self):
317         result = self.app.get('/', environ_base={'USER_ROLES': user})
318
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) )
322
323     def test_createMotion(self):
324         title='My Motion'
325         content='My body'
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)
332
333         title='My Motion1'
334         content='My body1'
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)
341         
342         title='My Motion2'
343         content='My body2'
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)
350
351         title='My Motion3'
352         content='My body3'
353         user1='testuser/vote:* create:group1 cancel:*'
354         response = self.createMotion(user1, title, content, '3', 'group1')
355         self.assertEqual(response.status_code, 302)
356
357         title='My Motion4'
358         content='My body4'
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)
362
363
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)
372
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 &lt;a href="https//domain.tld/link"&gt;direct&lt;/a', result.data)
381
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> &lt;a href="https//domain.tld/link"&gt;combined1&lt;/a', result.data)
390
391     def test_createMotionWrongDayLength(self):
392         title='My Motion'
393         content='My body'
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)
397
398     def test_createMotionWrongGroup(self):
399         title='My Motion'
400         content='My body'
401         response = self.createMotion(user, title, content, '3', 'test1')
402         self.assertEqual(response.status_code, 403)
403         self.assertIn(str.encode('Forbidden'), response.data)
404
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)
409
410     def test_cancelMotion(self):
411         self.db_sampledata()
412
413         motion='g1.20200402.004'
414         reason="none"
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)
418
419         reason='cancel test'
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)
424
425         motion='g1.30190402.001'
426         reason="none"
427         response = self.cancelMotion(user, motion, reason)
428         self.assertEqual(response.status_code, 404)
429         self.assertIn(str.encode('Error, Not found'), response.data)
430
431 class AuditMotionTests(BasicTest):
432     def setUp(self):
433         app.config['TESTING'] = True
434         app.config['DEBUG'] = False
435         app.config.update(SERVER_NAME="127.0.0.1:5000")
436         global user
437         user='testuser/audit:*'
438         # reset database
439         self.db_clear()
440         self.db_sampledata()
441
442         self.app = app.test_client()
443         self.assertEqual(app.debug, False)
444
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)
452
453     # executed after each test
454     def tearDown(self):
455         pass
456
457
458 if __name__ == "__main__":
459     unittest.main()