X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;ds=inline;f=tests%2Ftest_motion.py;h=7ef5ceac11c309943e8fdb0e910d999c8e051bef;hb=068793bc0abe796473cb252ed7481b56b91f47bc;hp=e500639bc3a006d52fec1fbcb001ce3c2dc6b80e;hpb=2efb3a16046ae08c91b6096a42e8825337612356;p=motion.git diff --git a/tests/test_motion.py b/tests/test_motion.py index e500639..7ef5cea 100644 --- a/tests/test_motion.py +++ b/tests/test_motion.py @@ -1,90 +1,6 @@ -import motion -import unittest -import postgresql -from unittest import TestCase -from motion import app from datetime import datetime - -app.config.update( - DEBUGUSER = {}, - GROUP_PREFIX = {'127.0.0.1:5000': {'group1': 'g1', 'group2': 'g2'}}, - DURATION = {'127.0.0.1:5000':[3, 7, 14]}, - SERVER_NAME = '127.0.0.1:5000', - MAX_PROXY=2 -) - -app.config['TESTING'] = True -app.config['DEBUG'] = False - - -class BasicTest(TestCase): - - def init_test(self): - self.app = app.test_client() - self.assertEqual(app.debug, False) - - # reset database - self.db_clear() - - # functions to manipulate motions - def createVote(self, user, motion, vote, voter): - return self.app.post( - '/motion/' + motion + '/vote/' + str(voter), - environ_base={'USER_ROLES': user}, - data=dict(vote=vote) - ) - - - def createMotion(self, user, motiontitle, motioncontent, days, category): - return self.app.post( - '/motion', - environ_base={'USER_ROLES': user}, - data=dict(title=motiontitle, content=motioncontent, days=days, category=category) - ) - - def cancelMotion(self, user, motion, reason): - return self.app.post( - '/motion/' + motion +'/cancel', - environ_base={'USER_ROLES': user}, - data=dict(reason=reason) - ) - - def finishMotion(self, user, motion): - return self.app.post( - '/motion/' + motion +'/finish', - environ_base={'USER_ROLES': user} - ) - - def addProxy(self, user, voter, proxy): - return self.app.post( - '/proxy/add', - environ_base={'USER_ROLES': user}, - data=dict(voter=voter, proxy=proxy) - ) - - def revokeProxy(self, user, id): - return self.app.post( - '/proxy/revoke', - environ_base={'USER_ROLES': user}, - data=dict(id=id) - ) - - def buildResultText(self, motiontext, yes, no, abstain): - return '

'+motiontext+'

\n

\nYes '+str(yes)+'
'\ - + '\nNo '+str(no)+'
'\ - + '\nAbstain '+str(abstain)+'' - - # functions to clear database - def db_clear(self): - with postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD")) as db: - with app.open_resource('sql/schema.sql', mode='r') as f: - db.execute(f.read()) - - def db_sampledata(self): - with postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD")) as db: - with app.open_resource('sql/sample_data.sql', mode='r') as f: - db.execute(f.read()) - +from tests.test_basics import BasicTest +from motion import app # no specific rights required class GeneralTests(BasicTest): @@ -234,11 +150,11 @@ class VoterTests(BasicTest): result = self.app.get('/', environ_base={'USER_ROLES': user}) resulttext=self.buildResultText('A fourth motion', 1, 0, 0) result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True) - testtext= 'class=\"btn btn-success\" name=\"vote\" value="yes" id="vote-yes">yes' + testtext= 'class=\"btn btn-success\" name=\"vote\" value="yes" id="vote-yes">Yes' self.assertIn(str.encode(testtext), result.data) - testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"no\" id=\"vote-no\">no' + testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"no\" id=\"vote-no\">No' self.assertIn(str.encode(testtext), result.data) - testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">abstain' + testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">Abstain' self.assertIn(str.encode(testtext), result.data) def test_vote_no(self): @@ -249,11 +165,11 @@ class VoterTests(BasicTest): resulttext=self.buildResultText('A fourth motion', 0, 1, 0) self.assertIn(str.encode(resulttext), result.data) result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True) - testtext= 'class="btn btn-primary" name="vote\" value=\"yes\" id=\"vote-yes\">yes' + testtext= 'class="btn btn-primary" name="vote\" value=\"yes\" id=\"vote-yes\">Yes' self.assertIn(str.encode(testtext), result.data) - testtext= 'class=\"btn btn-success\" name=\"vote\" value=\"no\" id=\"vote-no\">no' + testtext= 'class=\"btn btn-success\" name=\"vote\" value=\"no\" id=\"vote-no\">No' self.assertIn(str.encode(testtext), result.data) - testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">abstain' + testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">Abstain' self.assertIn(str.encode(testtext), result.data) def test_vote_abstain(self): @@ -264,11 +180,11 @@ class VoterTests(BasicTest): resulttext=self.buildResultText('A fourth motion', 0, 0, 1) self.assertIn(str.encode(resulttext), result.data) result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True) - testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"yes\" id=\"vote-yes\">yes' + testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"yes\" id=\"vote-yes\">Yes' self.assertIn(str.encode(testtext), result.data) - testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"no\" id=\"vote-no\">no' + testtext= 'class=\"btn btn-primary\" name=\"vote\" value=\"no\" id=\"vote-no\">No' self.assertIn(str.encode(testtext), result.data) - testtext= 'class=\"btn btn-success\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">abstain' + testtext= 'class=\"btn btn-success\" name=\"vote\" value=\"abstain\" id=\"vote-abstain\">Abstain' self.assertIn(str.encode(testtext), result.data) def test_vote_change(self): @@ -485,6 +401,19 @@ class CreateMotionTests(BasicTest): self.assertEqual(response.status_code, 403) self.assertIn(str.encode('Forbidden'), response.data) + def test_SeeCancelMotion(self): + self.db_sampledata() + + motion='g1.20200402.004' + result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True) + testtext= '' + self.assertIn(str.encode(testtext), result.data) + + motion='g1.20200402.004' + result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': 'testuser/vote:*'}, follow_redirects=True) + testtext= '' + self.assertNotIn(str.encode(testtext), result.data) + def test_cancelMotion(self): self.db_sampledata() @@ -517,6 +446,19 @@ class CreateMotionTests(BasicTest): self.assertEqual(response.status_code, 403) self.assertIn(str.encode('Error, motion was canceled'), response.data) + def test_SeeFinishMotion(self): + self.db_sampledata() + + motion='g1.20200402.004' + result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True) + testtext= '' + self.assertIn(str.encode(testtext), result.data) + + motion='g1.20200402.004' + result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': 'testuser/vote:*'}, follow_redirects=True) + testtext= '' + self.assertNotIn(str.encode(testtext), result.data) + def test_finishMotion(self): self.db_sampledata() @@ -536,6 +478,24 @@ class CreateMotionTests(BasicTest): self.assertEqual(response.status_code, 403) self.assertIn(str.encode('Error, out of time'), response.data) + def test_createMotionWait(self): + # test no limit given + self.db_sampledata() + title='My Motion' + content='My body' + response = self.createMotion(user, title, content, '3', 'group1') + self.assertEqual(response.status_code, 302) + + # test different host + app.config.update(MOTION_WAIT_MINUTES={'127.0.0.1:5001':1}) + response = self.createMotion(user, title, content, '3', 'group1') + self.assertEqual(response.status_code, 302) + + # test 3 minutes + app.config.update(MOTION_WAIT_MINUTES={'127.0.0.1:5000':3}) + response = self.createMotion(user, title, content, '3', 'group1') + self.assertIn(str.encode('Error, time between last motion to short. The current setting is 3 minute(s).'), response.data) + class AuditMotionTests(BasicTest): def setUp(self): @@ -561,6 +521,8 @@ class ProxyManagementTests(BasicTest): self.init_test() global user user='testuser/proxyadmin:*' + global userid + userid=4 self.db_sampledata() def tearDown(self): @@ -623,6 +585,12 @@ class ProxyManagementTests(BasicTest): response = self.addProxy(user, voter, proxy) self.assertEqual(response.status_code, 400) self.assertIn(str.encode('Error, voter equals proxy.'), response.data) + + voter='User A' + proxy='User Z' + response = self.addProxy(user, voter, proxy) + self.assertEqual(response.status_code, 400) + self.assertIn(str.encode('Error, proxy not found.'), response.data) voter='User A' proxy='User B' @@ -761,9 +729,48 @@ class ProxyManagementTests(BasicTest): result = self.app.get('proxy', environ_base={'USER_ROLES': user}, follow_redirects=True) testtext= '\n '\ + '\n '\ - + '\n \n \n \n '\ + + '\n \n \n \n'\ + '
VoterProxy
VoterProxy
\n' + self.assertNotIn(str.encode(testtext), result.data) + + proxytest="proxytest" + with self.open_DB() as db: + db.prepare("INSERT INTO voter(\"email\", \"host\") VALUES($1, $2)")(proxytest, '127.0.0.1:5001') + result = self.app.get('proxy', environ_base={'USER_ROLES': user}, follow_redirects=True) + + response = self.addProxy(user, proxytest, 'testuser') + self.assertEqual(response.status_code, 400) + self.assertIn(str.encode('Error, voter not found.'), response.data) + + response = self.addProxy(user, 'testuser', proxytest) + self.assertEqual(response.status_code, 400) + self.assertIn(str.encode('Error, proxy not found.'), response.data) + + def test_see_proxy_host_only(self): + proxytest="proxytest" + with self.open_DB() as db: + db.prepare("INSERT INTO voter(\"email\", \"host\") VALUES($1, $2)")(proxytest, '127.0.0.1:5001') + result = self.app.get('proxy', environ_base={'USER_ROLES': user}, follow_redirects=True) + testtext= 'div class="container">\n

' + self.assertIn(str.encode(testtext), result.data) + testtext= 'proxy granted to:' + self.assertNotIn(str.encode(testtext), result.data) + testtext= 'holds proxy of:' + self.assertNotIn(str.encode(testtext), result.data) + testtext= '\n' + self.assertIn(str.encode(testtext), result.data) + testtext= '\n' self.assertIn(str.encode(testtext), result.data) + self.assertNotIn(str.encode(proxytest), result.data) class ProxyVoteTests(BasicTest): @@ -793,15 +800,15 @@ class ProxyVoteTests(BasicTest): result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True) # own vote without change testtext= '\n'\ - + '\n'\ - + '\n'\ - + '\n
' + + '\n'\ + + '\n'\ + + '\n' self.assertIn(str.encode(testtext), result.data) # proxy vote with change testtext= '
\n'\ - + '\n'\ - + '\n'\ - + '\n
\n' + + '\n'\ + + '\n'\ + + '\n\n' self.assertIn(str.encode(testtext), result.data) # User B view @@ -809,9 +816,9 @@ class ProxyVoteTests(BasicTest): # own vote without change testtext= '

My vote

\nGiven by testuser\n'\ + '
\n'\ - + '\n'\ - + '\n'\ - + '\n
' + + '\n'\ + + '\n'\ + + '\n' self.assertIn(str.encode(testtext), result.data) # change vote @@ -820,9 +827,9 @@ class ProxyVoteTests(BasicTest): result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True) testtext= '
\n'\ - + '\n'\ - + '\n'\ - + '\n
\n' + + '\n'\ + + '\n'\ + + '\n\n' self.assertIn(str.encode(testtext), result.data) def test_proxy_vote_no_proxy(self):