diff --git a/CHANGELOG.md b/CHANGELOG.md index 0130d91ef3e6df7ac5e9f8a31c6cabca684cc85e..c14fab2f449ffc9f4cec24f21a273d0d1c99cd14 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 c0e580a3cc9bbfa2e5a313f712f9e6cd5a9f8924..f156345afcb9d77c118daa7bf53b57b8499d87cc 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 6d1bb1f389823c2824bbaa3f3b1b41462e284692..692ec2b13f16556a9acfab2a377a04aaf27d650c 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 dcd29575bd46a46327944c06d986202883a327dc..0ad658afb4bf82d39a066437d5f0955915845ef6 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 dc05be2ac7c19ae066b9c8829677626796cea5fa..25a7d0de7d2a591135ddf21530f23ad532101c53 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 26e3388dfa4dd7d360b8a373d631f5a362990f66..c1f62088c425b2ef587a6b0ca0028f38180ba2ee 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