4 from unittest import TestCase
6 from datetime import datetime
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',
17 app.config['TESTING'] = True
18 app.config['DEBUG'] = False
20 class BasicTest(TestCase):
23 self.app = app.test_client()
24 self.assertEqual(app.debug, False)
29 # functions to manipulate motions
30 def createVote(self, user, motion, vote, voter):
32 '/motion/' + motion + '/vote/' + str(voter),
33 environ_base={'USER_ROLES': user},
37 def createMotion(self, user, motiontitle, motioncontent, days, category):
40 environ_base={'USER_ROLES': user},
41 data=dict(title=motiontitle, content=motioncontent, days=days, category=category)
44 def cancelMotion(self, user, motion, reason):
46 '/motion/' + motion +'/cancel',
47 environ_base={'USER_ROLES': user},
48 data=dict(reason=reason)
51 def finishMotion(self, user, motion):
53 '/motion/' + motion +'/finish',
54 environ_base={'USER_ROLES': user}
57 def addProxy(self, user, voter, proxy):
60 environ_base={'USER_ROLES': user},
61 data=dict(voter=voter, proxy=proxy)
64 def revokeProxy(self, user, id):
67 environ_base={'USER_ROLES': user},
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>'
76 # functions handling or using database
78 return postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD"))
80 def db_select(self, sql, parameter):
81 with self.open_DB() as db:
82 rv = db.prepare(sql)(parameter)
85 def db_select2(self, sql, parameter, parameter2):
86 with self.open_DB() as db:
87 rv = db.prepare(sql)(parameter, parameter2)
90 def recordCountLog(self, parameter):
91 return self.recordCount("SELECT * FROM adminlog WHERE action=$1", parameter)
93 def recordCount(self, sql, parameter):
94 rv = self.db_select(sql, parameter)
97 def logRecordDetailsTest(self, parameter, recordno, voterid, comment, actionuserid):
98 rv = self.db_select("SELECT * FROM adminlog WHERE action=$1 ORDER BY id", parameter)
99 self.assertEqual(voterid, rv[recordno].get("user_id"))
101 self.assertEqual(comment, rv[recordno].get("comment"))
103 self.assertEqual('', rv[recordno].get("comment"))
104 self.assertEqual(actionuserid, rv[recordno].get("action_user_id"))
106 # functions to clear database
108 with self.open_DB() as db:
109 with app.open_resource('sql/schema.sql', mode='r') as f:
112 def db_sampledata(self):
113 with self.open_DB() as db:
114 with app.open_resource('sql/sample_data.sql', mode='r') as f: