]> WPIA git - motion.git/blob - tests/test_basics.py
b6f111cb6660c49a4dae7d136ac6911574b501b2
[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     DEFAULT_HOST = '127.0.0.1:5000',
14     MAX_PROXY=2
15 )
16
17 app.config['TESTING'] = True
18 app.config['DEBUG'] = False
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     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 finishMotion(self, user, motion):
52         return self.app.post(
53             '/motion/' + motion +'/finish',
54             environ_base={'USER_ROLES': user}
55         )
56
57     def addProxy(self, user, voter, proxy):
58         return self.app.post(
59             '/proxy/add',
60             environ_base={'USER_ROLES': user},
61             data=dict(voter=voter, proxy=proxy)
62         )
63
64     def revokeProxy(self, user, id):
65         return self.app.post(
66             '/proxy/revoke',
67             environ_base={'USER_ROLES': user},
68             data=dict(id=id)
69         )
70
71     def buildResultText(self, motiontext, yes, no, abstain):
72         return '<p>'+motiontext+'</p></p>\n    <p>\nYes <span class=\"badge badge-pill badge-secondary\">'+str(yes)+'</span><br>'\
73             + '\nNo <span class=\"badge badge-pill badge-secondary\">'+str(no)+'</span><br>'\
74             + '\nAbstain <span class=\"badge badge-pill badge-secondary\">'+str(abstain)+'</span>'
75
76
77     def open_DB(self):
78         return postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD"))
79
80     # functions to clear database
81     def db_clear(self):
82         with self.open_DB() as db:
83             with app.open_resource('sql/schema.sql', mode='r') as f:
84                 db.execute(f.read())
85
86     def db_sampledata(self):
87         with self.open_DB() as db:
88             with app.open_resource('sql/sample_data.sql', mode='r') as f:
89                 db.execute(f.read())