]> WPIA git - motion.git/commitdiff
add: command line interface to add user
authorINOPIAE <m.maengel@inopiae.de>
Tue, 1 Dec 2020 05:57:50 +0000 (06:57 +0100)
committerINOPIAE <m.maengel@inopiae.de>
Wed, 9 Dec 2020 03:52:58 +0000 (04:52 +0100)
Change-Id: I9ff58c516760f89eb80cb524fc7d0d71049fa3ac

README.md
motion.py
tests/test_basics.py [new file with mode: 0644]
tests/test_motion.py
tests/test_user_api.py [new file with mode: 0644]

index 18df67fdae1672541b8e4705a7810fa2e90677bc..5470c43401c205db452a92c6448d8374a6d08e06 100644 (file)
--- a/README.md
+++ b/README.md
@@ -180,3 +180,24 @@ Add Publish JUnit test result report - test report XMLs
 ```
 python_tests_xml/*
 ```
+
+## Add user via command line
+
+For linux start with
+```
+FLASK_APP=motion.py
+```
+
+For windows start with
+```
+set FLASK_APP=motion.py
+
+```
+
+To add a user use this command
+```
+flask create-user "email address"
+
+```
+
+The application will return a message for the success.
index 25c863c06d12ac24636e901f3e519a04818c3ae6..5291469f8eaa35a5e8fe310bb6743f1f5d6ce1f2 100644 (file)
--- a/motion.py
+++ b/motion.py
@@ -11,6 +11,7 @@ from markdown.extensions import Extension
 from datetime import date, time, datetime
 from flask_language import Language, current_language
 import gettext
+import click
 
 def get_db():
     db = getattr(g, '_database', None)
@@ -444,3 +445,15 @@ def revoke_proxy_all():
 def set_language(language):
     lang.change_language(language)
     return rel_redirect("/")
+
+@app.cli.command("create-user")
+@click.argument("email")
+def create_user(email):
+    db = get_db()
+    with db.xact():
+        rv = db.prepare("SELECT id FROM voter WHERE lower(email)=lower($1)")(email)
+        messagetext="User '%s' already exists." % (email)
+        if len(rv) == 0:
+            db.prepare("INSERT INTO voter(\"email\") VALUES($1)")(email)
+            messagetext="User '%s' inserted." % (email)
+    click.echo(messagetext)
diff --git a/tests/test_basics.py b/tests/test_basics.py
new file mode 100644 (file)
index 0000000..9385095
--- /dev/null
@@ -0,0 +1,86 @@
+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 '<p>'+motiontext+'</p></p>\n    <p>\nYes <span class=\"badge badge-pill badge-secondary\">'+str(yes)+'</span><br>'\
+            + '\nNo <span class=\"badge badge-pill badge-secondary\">'+str(no)+'</span><br>'\
+            + '\nAbstain <span class=\"badge badge-pill badge-secondary\">'+str(abstain)+'</span>'
+
+    # 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())
index 0399df04fc955922eae2510351774e556135cfb6..8634d2ca2b9c2fb92f598daaa411d5f383b791dd 100644 (file)
@@ -1,90 +1,5 @@
-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 '<p>'+motiontext+'</p></p>\n    <p>\nYes <span class=\"badge badge-pill badge-secondary\">'+str(yes)+'</span><br>'\
-            + '\nNo <span class=\"badge badge-pill badge-secondary\">'+str(no)+'</span><br>'\
-            + '\nAbstain <span class=\"badge badge-pill badge-secondary\">'+str(abstain)+'</span>'
-
-    # 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 .test_basics import BasicTest
 
 # no specific rights required
 class GeneralTests(BasicTest):
diff --git a/tests/test_user_api.py b/tests/test_user_api.py
new file mode 100644 (file)
index 0000000..2051bc2
--- /dev/null
@@ -0,0 +1,37 @@
+from .test_basics import BasicTest
+import click
+import motion
+import postgresql
+
+from click.testing import CliRunner
+from motion import create_user
+from motion import app
+
+def db_select(self, sql, parameter):
+    with postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD")) as db:
+        rv = db.prepare(sql)(parameter)
+        return rv
+
+class GeneralTests(BasicTest):
+
+    def setUp(self):
+        self.init_test()
+
+    def tearDown(self):
+        pass
+    
+
+
+    def test_create_user(self):
+        user = 'John Doe'
+        runner = CliRunner()
+        result = runner.invoke(create_user, [user])
+        assert result.exit_code == 0
+        self.assertIn("User 'John Doe' inserted.", result.output)
+
+        rv = db_select(self,"SELECT email FROM voter WHERE lower(email)=lower($1)", user)
+        self.assertIn(user, rv[0].get("email"))
+
+        result = runner.invoke(create_user, [user])
+        assert result.exit_code == 0
+        self.assertIn("User 'John Doe' already exists.", result.output)