]> WPIA git - motion.git/blob - tests/test_basics.py
9385095a03d447c9d6cbf2578798380aff22c254
[motion.git] / tests / test_basics.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     MAX_PROXY=2
14 )
15
16 app.config['TESTING'] = True
17 app.config['DEBUG'] = False
18
19
20 class BasicTest(TestCase):
21
22     def init_test(self):
23         self.app = app.test_client()
24         self.assertEqual(app.debug, False)
25
26         # reset database
27         self.db_clear()
28
29     # functions to manipulate motions
30     def createVote(self, user, motion, vote, voter):
31         return self.app.post(
32             '/motion/' + motion + '/vote/' + str(voter),
33             environ_base={'USER_ROLES': user},
34             data=dict(vote=vote)
35         )
36         
37
38     def createMotion(self, user, motiontitle, motioncontent, days, category):
39         return self.app.post(
40             '/motion',
41             environ_base={'USER_ROLES': user},
42             data=dict(title=motiontitle, content=motioncontent, days=days, category=category)
43         )
44
45     def cancelMotion(self, user, motion, reason):
46         return self.app.post(
47             '/motion/' + motion +'/cancel',
48             environ_base={'USER_ROLES': user},
49             data=dict(reason=reason)
50         )
51
52     def finishMotion(self, user, motion):
53         return self.app.post(
54             '/motion/' + motion +'/finish',
55             environ_base={'USER_ROLES': user}
56         )
57
58     def addProxy(self, user, voter, proxy):
59         return self.app.post(
60             '/proxy/add',
61             environ_base={'USER_ROLES': user},
62             data=dict(voter=voter, proxy=proxy)
63         )
64
65     def revokeProxy(self, user, id):
66         return self.app.post(
67             '/proxy/revoke',
68             environ_base={'USER_ROLES': user},
69             data=dict(id=id)
70         )
71
72     def buildResultText(self, motiontext, yes, no, abstain):
73         return '<p>'+motiontext+'</p></p>\n    <p>\nYes <span class=\"badge badge-pill badge-secondary\">'+str(yes)+'</span><br>'\
74             + '\nNo <span class=\"badge badge-pill badge-secondary\">'+str(no)+'</span><br>'\
75             + '\nAbstain <span class=\"badge badge-pill badge-secondary\">'+str(abstain)+'</span>'
76
77     # functions to clear database
78     def db_clear(self):
79         with postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD")) as db:
80             with app.open_resource('sql/schema.sql', mode='r') as f:
81                 db.execute(f.read())
82
83     def db_sampledata(self):
84         with postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD")) as db:
85             with app.open_resource('sql/sample_data.sql', mode='r') as f:
86                 db.execute(f.read())