From 65b87a399944affd024dee773204e2ca8108bdbb Mon Sep 17 00:00:00 2001 From: Timm Fitschen <t.fitschen@indiscale.com> Date: Thu, 19 Dec 2019 14:53:21 +0000 Subject: [PATCH] TST: fix for anonymous user in pylib --- tests/test_authentication.py | 78 ++++++++++++++++++++++++++++-------- tests/test_deletion.py | 6 +-- 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/tests/test_authentication.py b/tests/test_authentication.py index 1fbf834..37173cc 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -27,14 +27,28 @@ """ import os +from sys import hexversion +from urllib.parse import urlparse +from http.client import HTTPSConnection +import ssl from subprocess import call, check_output +from lxml import etree from pytest import skip from caosdb.exceptions import LoginFailedException import caosdb as h -from nose.tools import assert_false, assert_true, assert_is_none, assert_raises, assert_equal, assert_is_not_none, nottest # @UnresolvedImport +from nose.tools import (assert_false, assert_true, assert_is_none, + assert_raises, assert_equal, assert_is_not_none, + nottest, with_setup) from caosdb.connection.connection import _Connection +def setup(): + try: + h.execute_query("FIND Test*").delete() + except Exception as e: + print(e) + + def test_pass(): if not h.get_config().has_option("Connection", "password_method") or not h.get_config().get("Connection", "password_method") == "pass": skip() @@ -43,22 +57,10 @@ def test_pass(): def test_https_support(): - from sys import hexversion - if hexversion < 0x02070900: - raise Exception("version " + str(hex(hexversion))) - elif 0x02999999 < hexversion < 0x03020000: + if 0x02999999 < hexversion < 0x03020000: raise Exception("version " + str(hex(hexversion))) - try: - # python2 - from httplib import HTTPSConnection - from urlparse import urlparse - except: - # python 3 - from urllib.parse import urlparse - from http.client import HTTPSConnection - import ssl - - context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) # @UndefinedVariable + + context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) context.verify_mode = ssl.CERT_REQUIRED if hasattr(context, "check_hostname"): context.check_hostname = True @@ -82,3 +84,47 @@ def test_login_via_post_form_data_failure(): "username": h.get_config().get("Connection", "username"), "password": "wrongpassphrase" }) + + +def test_anonymous_setter(): + """ this test verifies that the "test_login_while_anonymous_is_active" is + effective.""" + + # activate anonymous user + h.administration.set_server_property("AUTH_OPTIONAL", "TRUE") + + # connect without auth-token + context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) + context.verify_mode = ssl.CERT_REQUIRED + context.load_verify_locations(h.get_config().get("Connection", "cacert")) + + url = h.get_config().get("Connection", "url") + fullurl = urlparse(url) + + http_con = HTTPSConnection( + str(fullurl.netloc), timeout=200, context=context) + + http_con.request(method="GET", headers={}, url=str(fullurl.path) + "Info") + body = http_con.getresponse().read() + + xml = etree.fromstring(body) + # verify unauthenticated + assert xml.xpath("/Response/UserInfo/Roles/Role")[0].text == "anonymous" + + +@with_setup(setup, setup) +def test_login_while_anonymous_is_active(): + # activate anonymous user + h.administration.set_server_property("AUTH_OPTIONAL", "TRUE") + + # logout + h.get_connection()._logout() + + body = h.get_connection().retrieve( + entity_uri_segments=["Entity"], + reconnect=True).read() + xml = etree.fromstring(body) + + # pylib did the login even though the anonymous user is active + assert xml.xpath( + "/Response/UserInfo/Roles/Role")[0].text == "administration" diff --git a/tests/test_deletion.py b/tests/test_deletion.py index 73c5283..fd2236e 100755 --- a/tests/test_deletion.py +++ b/tests/test_deletion.py @@ -151,11 +151,7 @@ def test_deletion(): assert_is_not_none(cr2.id) c.extend([cr1, sr, d]) - try: - assert_raises(h.TransactionError, c.delete) - except: - import time - time.sleep(120) + assert_raises(h.TransactionError, c.delete) assert_true(c.has_errors()) assert_equal(int(c.get_errors()[0].code), 12) -- GitLab