diff --git a/src/main.py b/src/main.py index 272a44ee82117fd21d7cb8a9a29256c62761055e..44b82f28899bb445573bac1d65e038b559aa24a6 100644 --- a/src/main.py +++ b/src/main.py @@ -1,9 +1,37 @@ from urllib.parse import urlparse +import getpass +import sys -def parseurl(url): +def parse_credentials(url): result = urlparse(url) - return result.netloc + if result.username is None: + username = getpass.getuser() + else: + username = result.username + if result.port is None: + hostname = result.hostname + else: + hostname = result.hostname + ':' + str(result.port) + return 'MDDB (' + hostname + ')', username + + +def get_user_full_name(): + if sys.platform == 'win32': + import ctypes + GetUserNameEx = ctypes.windll.secur32.GetUserNameExW + NameDisplay = 3 + + size = ctypes.pointer(ctypes.c_ulong(0)) + GetUserNameEx(NameDisplay, None, size) + + nameBuffer = ctypes.create_unicode_buffer(size.contents.value) + GetUserNameEx(NameDisplay, nameBuffer, size) + return nameBuffer.value + else: + import pwd, os + pwd_data = pwd.getpwuid(os.getuid()) + return pwd_data.pw_gecos # Press the green button in the gutter to run the script. diff --git a/tests/test_keyring.py b/tests/test_keyring.py index 7279f5cabc47efa6eb85140c92a692339ff0adaa..bd3844e2f1346a00976b1e438153375d007c9622 100644 --- a/tests/test_keyring.py +++ b/tests/test_keyring.py @@ -1,13 +1,58 @@ -from pytest import fixture -from main import parseurl import keyring +from pytest import fixture, mark +from main import parse_credentials +import sys, os @fixture -def url(): - return "https://kashuba@metadata.institut2c.physik.rwth-aachen.de:8123/api/" +def ex_whoami(ex_user): + if ex_user is None: + if sys.platform == 'darwin': + return os.getenv('USER') + else: + return os.getlogin() + else: + return ex_user -def test_urlparse(url): - print() - print(parseurl(url)) \ No newline at end of file +@fixture +def ex_service(ex_host): + return 'MDDB (' + ex_host + ')' + + +@mark.parametrize("uri,ex_user,ex_host", [( + "https://kashuba@metadata.institut2c.physik.rwth-aachen.de:8123/api/", + 'kashuba', 'metadata.institut2c.physik.rwth-aachen.de:8123'),( + "https://o.kashuba@metadata.institut2c.physik.rwth-aachen.de:8123", + 'o.kashuba', 'metadata.institut2c.physik.rwth-aachen.de:8123'),( + "https://metadata.institut2c.physik.rwth-aachen.de:8123", + None, 'metadata.institut2c.physik.rwth-aachen.de:8123'),( + "https://metadata.institut2c.physik.rwth-aachen.de", + None, 'metadata.institut2c.physik.rwth-aachen.de')]) +def test_parse_credentials(uri, ex_whoami, ex_service): + service, username = parse_credentials(uri) + assert service == ex_service + assert username == ex_whoami + + +@mark.parametrize("ex_pass", ['TESTpass3£$%^$%', '', 'any']) +def test_set_get_pass(ex_pass): + service, user = parse_credentials("https://metadata.institut2c.physik.rwth-aachen.de") + keyring.set_password(service, user, ex_pass) + password = keyring.get_password(service, user) + assert password == ex_pass + + +# @mark.parametrize("uri,ex_user,ex_host", [( +# "https://kashuba@metadata.institut2c.physik.rwth-aachen.de:8123/api/", +# 'kashuba', 'metadata.institut2c.physik.rwth-aachen.de:8123'),( +# "https://o.kashuba@metadata.institut2c.physik.rwth-aachen.de:8123", +# 'o.kashuba', 'metadata.institut2c.physik.rwth-aachen.de:8123'),( +# "https://metadata.institut2c.physik.rwth-aachen.de:8123", +# None, 'metadata.institut2c.physik.rwth-aachen.de:8123'),( +# "https://metadata.institut2c.physik.rwth-aachen.de", +# None, 'metadata.institut2c.physik.rwth-aachen.de')]) +# def test_set_pass(): +# print() +# user, service = parse_credentials("https://metadata.institut2c.physik.rwth-aachen.de") +# keyring.set_password(service, user, 'TESTpass3465£$%^$%111') \ No newline at end of file