]> WPIA git - motion.git/commitdiff
some test cleanup
authorFelix Dörre <felix@dogcraft.de>
Tue, 2 Jun 2020 10:54:07 +0000 (12:54 +0200)
committerFelix Dörre <felix@dogcraft.de>
Tue, 2 Jun 2020 10:54:07 +0000 (12:54 +0200)
motion.py
tests/test_motion.py

index f6ddf1344004ee30bd5bbb40d096eadcd33cea90..ef98529fb19150a29245b13b522a1c68ff843ef0 100644 (file)
--- a/motion.py
+++ b/motion.py
@@ -27,11 +27,20 @@ md = Markdown(app, extensions=[EscapeHtml()])
 # Load config
 app.config.from_pyfile('config.py')
 
-prefix=app.config.get("GROUP_PREFIX")
 
-times=app.config.get("DURATION")
+class ConfigProxy:
+    def __init__(self, name):
+        self.name = name
+    @property
+    def per_host(self):
+        dict = app.config.get(self.name)
+        if dict is None:
+            return None
+        return dict.get(request.host)
 
-debuguser=app.config.get("DEBUGUSER")
+prefix = ConfigProxy("GROUP_PREFIX")
+times = ConfigProxy("DURATION")
+debuguser = ConfigProxy("DEBUGUSER")
 
 @app.before_request
 def lookup_user():
@@ -39,8 +48,9 @@ def lookup_user():
 
     env = request.environ
     user = None
-    if debuguser is not None:
-        parts =debuguser[request.host].split("/", 1)
+    my_debuguser = debuguser.per_host
+    if my_debuguser is not None:
+        parts = my_debuguser.split("/", 1)
         user = parts[0]
         roles = parts[1]
 
@@ -78,7 +88,7 @@ def lookup_user():
             if a[0] not in g.roles:
                 g.roles[a[0]] = []
             if val == "*":
-                g.roles[a[0]] = [group for group in prefix[request.host]]
+                g.roles[a[0]] = [group for group in prefix.per_host]
             else:
                 g.roles[a[0]].append(val)
     return None
@@ -161,7 +171,7 @@ def main():
             prev = rs[9][0]
         else:
             prev = -1
-    return render_template('index.html', motions=rv[:10], more=rv[10]["id"] if len(rv) == 11 else None, times=times[request.host], prev=prev,
+    return render_template('index.html', motions=rv[:10], more=rv[10]["id"] if len(rv) == 11 else None, times=times.per_host, prev=prev,
                            categories=get_allowed_cats("create"))
 
 def rel_redirect(loc):
@@ -175,7 +185,7 @@ def put_motion():
     if cat not in get_allowed_cats("create"):
         return "Forbidden", 403
     time = int(request.form.get("days", "3"));
-    if time not in times[request.host]:
+    if time not in times.per_host:
         return "Error, invalid length", 500
     db = get_db()
     with db.xact():
@@ -184,9 +194,9 @@ def put_motion():
         sr = s(cat, request.host)
         ident=""
         if len(sr) == 0 or sr[0][0] is None:
-            ident=prefix[request.host][cat]+"."+t.strftime("%Y%m%d")+".001"
+            ident=prefix.per_host[cat]+"."+t.strftime("%Y%m%d")+".001"
         else:
-            ident=prefix[request.host][cat]+"."+t.strftime("%Y%m%d")+"."+("%03d" % (int(sr[0][0].split(".")[2])+1))
+            ident=prefix.per_host[cat]+"."+t.strftime("%Y%m%d")+"."+("%03d" % (int(sr[0][0].split(".")[2])+1))
         p = db.prepare("INSERT INTO motion(\"name\", \"content\", \"deadline\", \"posed_by\", \"type\", \"identifier\", \"host\") VALUES($1, $2, CURRENT_TIMESTAMP + $3 * interval '1 days', $4, $5, $6, $7)")
         p(request.form.get("title", ""), request.form.get("content",""), time, g.voter, cat, ident, request.host)
     return rel_redirect("/")
index fe853852d292ff3e1f7c9c0a60b297fd626f58aa..6ec2e1bef57e068585ada4f9d0c89005bc1c96a6 100644 (file)
@@ -5,6 +5,12 @@ 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]}
+)
+
 class BasicTest(TestCase):
 
     # functions to manipulate motions
@@ -37,14 +43,14 @@ class BasicTest(TestCase):
 
     # functions to clear database
     def db_clear(self):
-        db = postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD"))
-        with app.open_resource('sql/schema.sql', mode='r') as f:
-            db.execute(f.read())
+        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):
-        db = postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD"))
-        with app.open_resource('sql/sample_data.sql', mode='r') as f:
-            db.execute(f.read())
+        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