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
21 class BasicTest(TestCase):
24 self.app = app.test_client()
25 self.assertEqual(app.debug, False)
30 # functions to manipulate motions
31 def createVote(self, user, motion, vote, voter):
33 '/motion/' + motion + '/vote/' + str(voter),
34 environ_base={'USER_ROLES': user},
38 def createMotion(self, user, motiontitle, motioncontent, days, category):
41 environ_base={'USER_ROLES': user},
42 data=dict(title=motiontitle, content=motioncontent, days=days, category=category)
45 def cancelMotion(self, user, motion, reason):
47 '/motion/' + motion +'/cancel',
48 environ_base={'USER_ROLES': user},
49 data=dict(reason=reason)
52 def finishMotion(self, user, motion):
54 '/motion/' + motion +'/finish',
55 environ_base={'USER_ROLES': user}
58 def addProxy(self, user, voter, proxy):
61 environ_base={'USER_ROLES': user},
62 data=dict(voter=voter, proxy=proxy)
65 def revokeProxy(self, user, id):
68 environ_base={'USER_ROLES': user},
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>'
77 # functions handling or using database
79 return postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD"))
81 def db_select(self, sql, parameter):
82 with self.open_DB() as db:
83 rv = db.prepare(sql)(parameter)
86 def db_select2(self, sql, parameter, parameter2):
87 with self.open_DB() as db:
88 rv = db.prepare(sql)(parameter, parameter2)
91 def recordCountLog(self, parameter):
92 return self.recordCount("SELECT * FROM adminlog WHERE action=$1", parameter)
94 def recordCount(self, sql, parameter):
95 rv = self.db_select(sql, parameter)
98 def logRecordDetailsTest(self, parameter, recordno, voterid, comment, actionuserid):
99 rv = self.db_select("SELECT * FROM adminlog WHERE action=$1 ORDER BY id", parameter)
100 self.assertEqual(voterid, rv[recordno].get("user_id"))
102 self.assertEqual(comment, rv[recordno].get("comment"))
104 self.assertEqual('', rv[recordno].get("comment"))
105 self.assertEqual(actionuserid, rv[recordno].get("action_user_id"))
107 # functions to clear database
109 with self.open_DB() as db:
110 with app.open_resource('sql/schema.sql', mode='r') as f:
113 def db_sampledata(self):
114 with self.open_DB() as db:
115 with app.open_resource('sql/sample_data.sql', mode='r') as f: