]> WPIA git - motion.git/blob - tests/test_basics.py
add: adjust user handling per host
[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
21 class BasicTest(TestCase):
22
23     def init_test(self):
24         self.app = app.test_client()
25         self.assertEqual(app.debug, False)
26
27         # reset database
28         self.db_clear()
29
30     # functions to manipulate motions
31     def createVote(self, user, motion, vote, voter):
32         return self.app.post(
33             '/motion/' + motion + '/vote/' + str(voter),
34             environ_base={'USER_ROLES': user},
35             data=dict(vote=vote)
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
78     def open_DB(self):
79         return postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD"))
80
81     # functions to clear database
82     def db_clear(self):
83         with self.open_DB() as db:
84             with app.open_resource('sql/schema.sql', mode='r') as f:
85                 db.execute(f.read())
86
87     def db_sampledata(self):
88         with self.open_DB() as db:
89             with app.open_resource('sql/sample_data.sql', mode='r') as f:
90                 db.execute(f.read())