From 5236bca3c86d25bd4345dbefdf3f1da6fb327c97 Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Thu, 19 Dec 2019 14:54:13 +0000
Subject: [PATCH] BUG: anonymous user in python requires prior login

---
 CHANGELOG.md                                      | 2 ++
 src/caosdb/connection/authentication/interface.py | 2 ++
 src/caosdb/connection/mockup.py                   | 9 ++++++++-
 tox.ini                                           | 2 +-
 unittests/test_administraction.py                 | 3 ++-
 unittests/test_connection.py                      | 8 ++++----
 6 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0130d91e..c14fab2f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ### Fixed
 - #1 - Problems with pass as a credentials provider
+- #3 - Python client does login before the first request to circumvent problems
+  with anonymous role.
 
 
 
diff --git a/src/caosdb/connection/authentication/interface.py b/src/caosdb/connection/authentication/interface.py
index c0e580a3..f156345a 100644
--- a/src/caosdb/connection/authentication/interface.py
+++ b/src/caosdb/connection/authentication/interface.py
@@ -148,6 +148,8 @@ class AbstractAuthenticator(ABC):
         Returns
         -------
         """
+        if self.auth_token is None:
+            self.login()
         if self.auth_token is not None:
             headers['Cookie'] = auth_token_to_cookie(self.auth_token)
 
diff --git a/src/caosdb/connection/mockup.py b/src/caosdb/connection/mockup.py
index 6d1bb1f3..692ec2b1 100644
--- a/src/caosdb/connection/mockup.py
+++ b/src/caosdb/connection/mockup.py
@@ -75,7 +75,14 @@ class MockUpServerConnection(CaosDBServerConnection):
     just returns predefined responses which mimic the caosdb server."""
 
     def __init__(self):
-        self.resources = []
+        self.resources = [self._login]
+
+    def _login(self, method, path, headers, body):
+        if method == "POST" and path == "login":
+            return MockUpResponse(200,
+                                  headers={"AuthToken":
+                                           "mockup-auth-token"},
+                                  body="")
 
     def configure(self, **kwargs):
         """This configure method does nothing."""
diff --git a/tox.ini b/tox.ini
index dcd29575..0ad658af 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
 [tox]
-envlist= py27, py34, py35, py36, py37
+envlist=py34, py35, py36, py37
 skip_missing_interpreters = true
 [testenv]
 deps=nose
diff --git a/unittests/test_administraction.py b/unittests/test_administraction.py
index dc05be2a..25a7d0de 100644
--- a/unittests/test_administraction.py
+++ b/unittests/test_administraction.py
@@ -29,8 +29,9 @@ from caosdb import administration, configure_connection, get_connection
 from caosdb.connection.mockup import MockUpServerConnection, MockUpResponse
 
 
-def setup():
+def setup_module():
     configure_connection(url="unittests", username="testuser",
+                         password_method="plain",
                          password="testpassword", timeout=200,
                          implementation=MockUpServerConnection)
 
diff --git a/unittests/test_connection.py b/unittests/test_connection.py
index 26e3388d..c1f62088 100644
--- a/unittests/test_connection.py
+++ b/unittests/test_connection.py
@@ -125,9 +125,9 @@ def test_connection_interface():
 
 
 def test_use_mockup_implementation():
-    configure_connection(implementation=MockUpServerConnection)
     with raiz(RuntimeError) as rerr:
         execute_query("FIND Something")
+    print(rerr.exception.args[0])
     eq(rerr.exception.args[0],
        "No response for this request - GET: Entity?query=FIND%20Something")
 
@@ -180,10 +180,10 @@ def test_init_connection():
 
 def test_resources_list():
     connection = test_init_connection()
-    tru(hasattr(connection, "resources"))
-    falz(connection.resources)
+    assert hasattr(connection, "resources")
+    assert len(connection.resources) == 1
     connection.resources.append(lambda **kwargs: test_init_response())
-    tru(connection.resources)
+    assert len(connection.resources) == 2
     return connection
 
 
-- 
GitLab