From e344527db7faae25ff1cb13ff70edc98cd811b4e Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Tue, 28 Mar 2023 19:10:41 +0100 Subject: 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. --- tests/middleware/test_authenticate.py | 39 +++++++++++++++-------------------- 1 file changed, 17 insertions(+), 22 deletions(-) (limited to 'tests/middleware/test_authenticate.py') 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!" -- cgit v1.2.3-54-g00ecf