From f354b92d573d37db92e6f82af3e50d08eaf10547 Mon Sep 17 00:00:00 2001 From: Christian Boettcher <c.boettcher@fz-juelich.de> Date: Mon, 6 Dec 2021 10:32:53 +0100 Subject: [PATCH] bearer auth with basic test --- datacat_integration/auth.py | 11 +++++++++++ tests/__init__.py | 0 tests/dummy_tests/test_dummy.py | 7 ------- tests/test_auth.py | 20 ++++++++++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 datacat_integration/auth.py create mode 100644 tests/__init__.py delete mode 100644 tests/dummy_tests/test_dummy.py create mode 100644 tests/test_auth.py diff --git a/datacat_integration/auth.py b/datacat_integration/auth.py new file mode 100644 index 0000000..4cf6c31 --- /dev/null +++ b/datacat_integration/auth.py @@ -0,0 +1,11 @@ +import requests +import requests.auth + +class BearerAuth(requests.auth.AuthBase): + def __init__(self, token: str) -> None: + super().__init__() + self.token = token + + def __call__(self, r: requests.Request) -> requests.Request: + r.headers['Authorization'] = 'Bearer {}'.format(self.token) + return r \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/dummy_tests/test_dummy.py b/tests/dummy_tests/test_dummy.py deleted file mode 100644 index e1e838b..0000000 --- a/tests/dummy_tests/test_dummy.py +++ /dev/null @@ -1,7 +0,0 @@ -from unittest import TestCase - -class DummyTest(TestCase): - - def test_dummy(self): - print("Test Print") - self.assertEqual(4, 2*2) \ No newline at end of file diff --git a/tests/test_auth.py b/tests/test_auth.py new file mode 100644 index 0000000..db3f1ca --- /dev/null +++ b/tests/test_auth.py @@ -0,0 +1,20 @@ +from unittest import TestCase + +from requests import Request + +from datacat_integration.auth import BearerAuth + +class AuthTest(TestCase): + def test_auth_creation(self): + token = "dummy_token_text" + header_key = "Authorization" + header_value = f"Bearer {token}" + auth: BearerAuth = BearerAuth(token) + r: Request = Request(auth=auth) + + auth.__call__(r) + + headers = r.headers + + self.assertTrue(headers[header_key], header_value) + -- GitLab