X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=tests%2Ftest_motion.py;h=f6992968ff128e05375539cc51ba1d4f19e9ff54;hb=11e82f491ae15d3b7d9f1f88db2ab79d1fe131bf;hp=b6622a2c006cf6aca5d7b7401568499b3abb9439;hpb=aa66d52ad5b00a2ee8da2accbcd58bde428d502b;p=motion.git diff --git a/tests/test_motion.py b/tests/test_motion.py index b6622a2..f699296 100644 --- a/tests/test_motion.py +++ b/tests/test_motion.py @@ -1,75 +1,7 @@ -import motion -import unittest +from datetime import datetime +from tests.test_basics import BasicTest 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' -) - -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): - return self.app.post( - '/motion/' + motion +'/vote', - 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 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()) - # no specific rights required class GeneralTests(BasicTest): @@ -78,6 +10,8 @@ class GeneralTests(BasicTest): self.init_test() global user user = 'testuser/' + global userid + userid = 4 self.db_sampledata() def tearDown(self): @@ -90,42 +24,42 @@ class GeneralTests(BasicTest): def test_basic_results_data(self): result = self.app.get('/', environ_base={'USER_ROLES': user}, follow_redirects=True) testtext= '

\n
'\ - + '\n Motion C (Canceled)\n group1'\ + + '\n Motion C (Canceled)\n group1'\ + '\n
# g1.20200402.003'\ + '\n Result'\ - + '\n
'\ - + '\n
\n
Proposed: 2020-04-02 21:47:24 (UTC) by User A
'\ - + '\n
Canceled: 2020-04-03 21:48:24 (UTC) by User A
\n
'\ - + '\n
\n

A third motion

'\ - + '\n

\nYes 1
'\ - + '\nNo 0
'\ - + '\nAbstain 0
\n

'\ - + '\n

Cancelation reason: Entered with wrong text

\n
\n
' + + '\n \n
'\ + + '\n
Proposed: 2020-04-02 21:47:24 (UTC) by User A
'\ + + '\n
Canceled: 2020-04-03 21:48:24 (UTC) by User A
\n
\n '\ + + '\n
\n

A third motion

'\ + + '\n

\nYes 1
'\ + + '\nNo 0
'\ + + '\nAbstain 0
\n

'\ + + '\n

Cancelation reason: Entered with wrong text

\n
\n\n' self.assertIn(str.encode(testtext), result.data) testtext= '
\n
'\ - + '\n Motion B (Finished)\n group1'\ + + '\n Motion B (Finished)\n group1'\ + '\n
# g1.20200402.002'\ + '\n Result'\ - + '\n
'\ - + '\n
\n
Proposed: 2020-04-02 21:41:26 (UTC) by User A
'\ - + '\n
Votes until: 2020-04-04 21:41:26 (UTC)
\n
'\ - + '\n
\n

A second motion

'\ - + '\n

\nYes 1
'\ - + '\nNo 2
'\ - + '\nAbstain 0
\n

\n
\n
\n' + + '\n \n
\n
Proposed: 2020-04-02 21:41:26 (UTC) by User A
'\ + + '\n
Votes until: 2020-04-04 21:41:26 (UTC)
\n
\n '\ + + '\n
\n

A second motion

\n

'\ + + '\nYes 1
'\ + + '\nNo 2
'\ + + '\nAbstain 0
\n

\n
\n\n' self.assertIn(str.encode(testtext), result.data) - testtext= '
\n
'\ - + '\n Motion A (Finished)\n group1'\ + testtext= '
\n
'\ + + '\n Motion A (Finished)\n group1'\ + '\n
# g1.20200402.001'\ + '\n Result'\ - + '\n
'\ - + '\n
\n
Proposed: 2020-04-02 21:40:33 (UTC) by User A
'\ - + '\n
Votes until: 2020-04-02 21:40:33 (UTC)
\n
'\ - + '\n
\n

My special motion

'\ - + '\n

\nYes 2
'\ - + '\nNo 1
'\ - + '\nAbstain 0
\n

\n
\n
\n
' + + '\n
\n
\n
Proposed: 2020-04-02 21:40:33 (UTC) by User A
'\ + + '\n
Votes until: 2020-04-02 21:40:33 (UTC)
\n
\n '\ + + '\n
\n

My special motion

\n

'\ + + '\nYes 2
'\ + + '\nNo 1
'\ + + '\nAbstain 0
\n

\n
\n\n' self.assertIn(str.encode(testtext), result.data) + testtext= 'Proxy management' + self.assertNotIn(str.encode(testtext), result.data) # start with second motion result = self.app.get('/', environ_base={'USER_ROLES': user}, query_string=dict(start=2)) @@ -139,12 +73,12 @@ class GeneralTests(BasicTest): def test_basic_results_data_details(self): motion='g1.20200402.002' result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True) - testtext= '

A second motion

\n \n\nBack\n' + testtext= '

A second motion

\n \n\nBack' self.assertIn(str.encode(testtext), result.data) def test_vote(self): motion='g1.20200402.004' - response = self.createVote(user, motion, 'yes') + response = self.createVote(user, motion, 'yes', userid) self.assertEqual(response.status_code, 403) self.assertIn(str.encode('Forbidden'), response.data) @@ -169,13 +103,34 @@ class GeneralTests(BasicTest): self.assertEqual(result.status_code, 404) self.assertIn(str.encode('Error, Not found'), result.data) + def test_no_proxy(self): + result = self.app.get('proxy', environ_base={'USER_ROLES': user}, follow_redirects=True) + self.assertEqual(result.status_code, 403) + self.assertIn(str.encode('Forbidden'), result.data) + + def test_no_proxy_add(self): + result = self.app.post('proxy/add', environ_base={'USER_ROLES': user}, follow_redirects=True) + self.assertEqual(result.status_code, 403) + self.assertIn(str.encode('Forbidden'), result.data) + def test_no_proxy_revoke(self): + result = self.app.post('proxy/revoke', environ_base={'USER_ROLES': user}, follow_redirects=True) + self.assertEqual(result.status_code, 403) + self.assertIn(str.encode('Forbidden'), result.data) + + def test_no_proxy_revokeAll(self): + result = self.app.post('proxy/revokeall', environ_base={'USER_ROLES': user}, follow_redirects=True) + self.assertEqual(result.status_code, 403) + self.assertIn(str.encode('Forbidden'), result.data) + class VoterTests(BasicTest): def setUp(self): self.init_test() global user user='testuser/vote:*' + global userid + userid = 4 self.db_sampledata() def tearDown(self): @@ -191,61 +146,61 @@ class VoterTests(BasicTest): def test_vote_yes(self): motion='g1.20200402.004' - response = self.createVote(user, motion, 'yes') + response = self.createVote(user, motion, 'yes', userid) self.assertEqual(response.status_code, 302) 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): motion='g1.20200402.004' - response = self.createVote(user, motion, 'no') + response = self.createVote(user, motion, 'no', userid) self.assertEqual(response.status_code, 302) result = self.app.get('/', environ_base={'USER_ROLES': user}) 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): motion='g1.20200402.004' - response = self.createVote(user, motion, 'abstain') + response = self.createVote(user, motion, 'abstain', userid) self.assertEqual(response.status_code, 302) result = self.app.get('/', environ_base={'USER_ROLES': user}) 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): motion='g1.20200402.004' - response = self.createVote(user, motion, 'yes') + response = self.createVote(user, motion, 'yes', userid) self.assertEqual(response.status_code, 302) result = self.app.get('/', environ_base={'USER_ROLES': user}) resulttext=self.buildResultText('A fourth motion', 1, 0, 0) self.assertIn(str.encode(resulttext), result.data) - response = self.createVote(user, motion, 'no') + response = self.createVote(user, motion, 'no', userid) self.assertEqual(response.status_code, 302) result = self.app.get('/', environ_base={'USER_ROLES': user}) resulttext=self.buildResultText('A fourth motion', 0, 1, 0) self.assertIn(str.encode(resulttext), result.data) - response = self.createVote(user, motion, 'abstain') + response = self.createVote(user, motion, 'abstain', userid) self.assertEqual(response.status_code, 302) result = self.app.get('/', environ_base={'USER_ROLES': user}) resulttext=self.buildResultText('A fourth motion', 0, 0, 1) @@ -253,41 +208,41 @@ class VoterTests(BasicTest): def test_vote_group(self): motion='g1.20200402.004' - response = self.createVote(user, motion, 'yes') + response = self.createVote(user, motion, 'yes', userid) self.assertEqual(response.status_code, 302) motion='g1.20200402.004' user1='testuser/vote:group1' - response = self.createVote(user1, motion, 'yes') + response = self.createVote(user1, motion, 'yes', userid) self.assertEqual(response.status_code, 302) motion='g1.20200402.004' user1='testuser/vote:group1 vote:group2' - response = self.createVote(user1, motion, 'yes') + response = self.createVote(user1, motion, 'yes', userid) self.assertEqual(response.status_code, 302) def test_vote_wrong_group(self): motion='g1.20200402.004' user1='testuser/vote:group2' - response = self.createVote(user1, motion, 'yes') + response = self.createVote(user1, motion, 'yes', userid) self.assertEqual(response.status_code, 403) self.assertIn(str.encode('Forbidden'), response.data) def test_vote_closed(self): motion='g1.20200402.002' - response = self.createVote(user, motion, 'abstain') + response = self.createVote(user, motion, 'abstain', userid) self.assertEqual(response.status_code, 403) self.assertIn(str.encode('Error, out of time'), response.data) def test_vote_canceled(self): motion='g1.20200402.003' - response = self.createVote(user, motion, 'abstain') + response = self.createVote(user, motion, 'abstain', userid) self.assertEqual(response.status_code, 403) self.assertIn(str.encode('Error, motion was canceled'), response.data) def test_vote_not_given(self): motion='g1.30190402.001' - response = self.createVote(user, motion, 'abstain') + response = self.createVote(user, motion, 'abstain', userid) self.assertEqual(response.status_code, 404) self.assertIn(str.encode('Error, Not found'), response.data) @@ -307,8 +262,8 @@ class VoterTests(BasicTest): def test_see_old_vote(self): motion='g1.20200402.002' result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True) - testtext= '
Proposed: 2020-04-02 21:41:26 (UTC) by User A
\n
Votes until: 2020-04-04 21:41:26 (UTC)
'\ - + '\n \n
\n

A second motion

\n
\n'\ + testtext= '
Proposed: 2020-04-02 21:41:26 (UTC) by User A
\n
Votes until: 2020-04-04 21:41:26 (UTC)
'\ + + '\n \n \n
\n

A second motion

\n
\n'\ + '\nBack' self.assertIn(str.encode(testtext), result.data) @@ -416,9 +371,25 @@ class CreateMotionTests(BasicTest): title='My Motion' content='My body' response = self.createMotion(user, title, content, '21', 'group1') - self.assertEqual(response.status_code, 500) + self.assertEqual(response.status_code, 400) self.assertIn(str.encode('Error, invalid length'), response.data) + def test_createMotionMissingData(self): + title='' + content='' + response = self.createMotion(user, title, content, '3', 'group1') + self.assertEqual(response.status_code, 400) + self.assertIn(str.encode('Error, missing title'), response.data) + title='New Motion' + response = self.createMotion(user, title, content, '3', 'group1') + self.assertEqual(response.status_code, 400) + self.assertIn(str.encode('Error, missing content'), response.data) + title='' + content='New Content' + response = self.createMotion(user, title, content, '3', 'group1') + self.assertEqual(response.status_code, 400) + self.assertIn(str.encode('Error, missing title'), response.data) + def test_createMotionWrongGroup(self): title='My Motion' content='My body' @@ -431,6 +402,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() @@ -463,6 +447,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()