From ea8eda35a7bd831964c38b38cc9a5b19bcb44d6a Mon Sep 17 00:00:00 2001
From: 0livd <0livd@users.noreply.github.com>
Date: Thu, 18 May 2017 10:48:09 +0100
Subject: Public project creation and admin permissions (#210)
* Add a @requires_admin decorator
It can be used to protect specific endpoints with ADMIN_PASSWORD
(a password that is stored unencrypted in the settings)
The decorator has no effect if ADMIN_PASSWORD is an empty string (default value)
* Require admin permissions to access create project endpoint
When ADMIN_PASSWORD is not empty, project creation form on the
home page will be replaced by a link to the create project endpoint
so one is able to enter the admin password before filling the form
---
budget/tests/tests.py | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
(limited to 'budget/tests')
diff --git a/budget/tests/tests.py b/budget/tests/tests.py
index e18e9c3..a1cedfa 100644
--- a/budget/tests/tests.py
+++ b/budget/tests/tests.py
@@ -44,6 +44,8 @@ class TestCase(unittest.TestCase):
# clean after testing
models.db.session.remove()
models.db.drop_all()
+ # reconfigure app with default settings
+ run.configure()
def login(self, project, password=None, test_client=None):
password = password or project
@@ -373,6 +375,25 @@ class BudgetTestCase(TestCase):
c.get("/exit")
self.assertNotIn('raclette', session)
+ def test_admin_authentication(self):
+ run.app.config['ADMIN_PASSWORD'] = "pass"
+
+ # test the redirection to the authentication page when trying to access admin endpoints
+ resp = self.app.get("/create")
+ self.assertIn('', resp.data.decode('utf-8'))
+
+ # test right password
+ resp = self.app.post("/admin?goto=%2Fcreate", data={'admin_password': 'pass'})
+ self.assertIn('/create', resp.data.decode('utf-8'))
+
+ # test wrong password
+ resp = self.app.post("/admin?goto=%2Fcreate", data={'admin_password': 'wrong'})
+ self.assertNotIn('/create', resp.data.decode('utf-8'))
+
+ # test empty password
+ resp = self.app.post("/admin?goto=%2Fcreate", data={'admin_password': ''})
+ self.assertNotIn('/create', resp.data.decode('utf-8'))
+
def test_manage_bills(self):
self.post_project("raclette")
--
cgit v1.1