]> WPIA git - motion.git/blob - tests/test_basics.py
Merge branch 'admin_cleanup' into 'master'
[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     # functions handling or using database
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     def db_select(self, sql, parameter):
81         with self.open_DB() as db:
82             rv = db.prepare(sql)(parameter)
83             return rv
84
85     def db_select2(self, sql, parameter, parameter2):
86         with self.open_DB() as db:
87             rv = db.prepare(sql)(parameter, parameter2)
88             return rv
89
90     def recordCountLog(self, parameter):
91         return self.recordCount("SELECT * FROM adminlog WHERE action=$1", parameter)
92
93     def recordCount(self, sql, parameter):
94         rv = self.db_select(sql, parameter)
95         return len(rv)
96
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"))
100         if comment:
101             self.assertEqual(comment, rv[recordno].get("comment"))
102         else:
103             self.assertEqual('', rv[recordno].get("comment"))
104         self.assertEqual(actionuserid, rv[recordno].get("action_user_id"))
105     
106     # functions to clear database
107     def db_clear(self):
108         with self.open_DB() as db:
109             with app.open_resource('sql/schema.sql', mode='r') as f:
110                 db.execute(f.read())
111
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:
115                 db.execute(f.read())