aboutsummaryrefslogtreecommitdiffstats
path: root/tests/middleware
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2023-03-28 19:10:41 +0100
committerTomasz Kramkowski <tomasz@kramkow.ski>2023-03-28 20:10:48 +0100
commite344527db7faae25ff1cb13ff70edc98cd811b4e (patch)
tree351c25486eda232270c16a4061fb7933a1dfe3e4 /tests/middleware
parentcba20d70c21cefaad3345a5779c88423edd0655b (diff)
downloadpaste-e344527db7faae25ff1cb13ff70edc98cd811b4e.tar.gz
paste-e344527db7faae25ff1cb13ff70edc98cd811b4e.tar.xz
paste-e344527db7faae25ff1cb13ff70edc98cd811b4e.zip
Make authenticate easier to test without monkeypatching
By having authenticate be a function taking a parameter to a getter which can get an Auth from an Env, it's now possible to test it without needing monkeypatching.
Diffstat (limited to 'tests/middleware')
-rw-r--r--tests/middleware/test_authenticate.py39
1 files changed, 17 insertions, 22 deletions
diff --git a/tests/middleware/test_authenticate.py b/tests/middleware/test_authenticate.py
index d3d7607..ec8734a 100644
--- a/tests/middleware/test_authenticate.py
+++ b/tests/middleware/test_authenticate.py
@@ -8,10 +8,22 @@ from paste import authenticate
from ..common_wsgi import call_app
+def get_auth(environ):
+ assert environ
+ assert "test.check_token" in environ
+
+ class MockAuth:
+ @staticmethod
+ def check_token(tok):
+ return environ["test.check_token"](tok)
+
+ return MockAuth()
+
+
@pytest.fixture
def app():
@validator
- @authenticate
+ @authenticate(get_auth) # type: ignore
@validator
def app(_, start_response):
start_response("200 OK", [("Content-Type", "text/plain")])
@@ -33,7 +45,6 @@ def test_unauthenticated_request(app, method):
def test_unauthenticated_request_with_key(app, method):
environ = {
"REQUEST_METHOD": method,
- "paste.db_conn": None,
"HTTP_AUTHORIZATION": "ApiKey AAAA",
}
response = call_app(app, environ)
@@ -63,22 +74,8 @@ def test_authenticate_malformed_key(app, method, key):
assert ("WWW-Authenticate", "APIKey") in response.headers
-class MockConnection:
- def __init__(self, check_token):
- self.check_token = check_token
-
-
-class MockAuth:
- def __init__(self, c):
- assert isinstance(c, MockConnection)
- self.conn = c
-
- def check_token(self, tok):
- return self.conn.check_token(tok)
-
-
@pytest.mark.parametrize("method", ["POST", "PUT", "DELETE"])
-def test_authenticate_check_token_fail(app, method, monkeypatch):
+def test_authenticate_check_token_fail(app, method):
check_token_called = False
token = b"test"
@@ -90,11 +87,10 @@ def test_authenticate_check_token_fail(app, method, monkeypatch):
environ = {
"REQUEST_METHOD": method,
- "paste.db_conn": MockConnection(check_token),
+ "test.check_token": check_token,
"HTTP_AUTHORIZATION": f"APIKey {b64encode(token).decode()}",
}
- monkeypatch.setattr("paste.Auth", MockAuth)
response = call_app(app, environ)
assert check_token_called
assert response.data == b"401 Unauthorized\n"
@@ -104,7 +100,7 @@ def test_authenticate_check_token_fail(app, method, monkeypatch):
@pytest.mark.parametrize("method", ["POST", "PUT", "DELETE"])
-def test_authenticate_check_token_success(app, method, monkeypatch):
+def test_authenticate_check_token_success(app, method):
check_token_called = False
token = b"test"
@@ -116,11 +112,10 @@ def test_authenticate_check_token_success(app, method, monkeypatch):
environ = {
"REQUEST_METHOD": method,
- "paste.db_conn": MockConnection(check_token),
+ "test.check_token": check_token,
"HTTP_AUTHORIZATION": f"APIKey {b64encode(token).decode()}",
}
- monkeypatch.setattr("paste.Auth", MockAuth)
response = call_app(app, environ)
assert check_token_called
assert response.data == b"Hello, world!"