From 3f1d9361497c4e0803f3dc3ea9cee9d3a4087171 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Tue, 28 Mar 2023 18:57:40 +0100 Subject: Split Store into Store and Auth This separates the concerns --- paste/__init__.py | 4 ++-- paste/store.py | 5 +++++ tests/middleware/test_authenticate.py | 6 +++--- tests/test_application.py | 4 ++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/paste/__init__.py b/paste/__init__.py index 155e927..ddd1054 100644 --- a/paste/__init__.py +++ b/paste/__init__.py @@ -9,7 +9,7 @@ from typing import Optional from wsgiref.util import application_uri, request_uri from . import db -from .store import Store +from .store import Auth, Store from .types import ( App, Closable, @@ -155,7 +155,7 @@ def authenticate(app: App, environ: Env, start_response: StartResponse) -> Respo value = b64decode(value.encode(), validate=True) except (binascii.Error, UnicodeEncodeError): return False - return Store(environ["paste.db_conn"]).check_token(value) + return Auth(environ["paste.db_conn"]).check_token(value) if environ["REQUEST_METHOD"] in {"GET", "HEAD"} or check_auth(): return app(environ, start_response) diff --git a/paste/store.py b/paste/store.py index f69c03d..427345b 100644 --- a/paste/store.py +++ b/paste/store.py @@ -89,6 +89,11 @@ class Store: ) return cur.rowcount == 1 + +class Auth: + def __init__(self, conn: Connection): + self.conn = conn + def generate_token(self): token = token_bytes(TOKEN_BYTES) with self.conn: diff --git a/tests/middleware/test_authenticate.py b/tests/middleware/test_authenticate.py index 9fccb32..73b7ded 100644 --- a/tests/middleware/test_authenticate.py +++ b/tests/middleware/test_authenticate.py @@ -69,7 +69,7 @@ class MockConnection: self.check_token = check_token -class MockStore: +class MockAuth: def __init__(self, c): assert isinstance(c, MockConnection) self.conn = c @@ -95,7 +95,7 @@ def test_authenticate_check_token_fail(app, method, monkeypatch): "HTTP_AUTHORIZATION": f"APIKey {b64encode(token).decode()}", } - monkeypatch.setattr("paste.Store", MockStore) + monkeypatch.setattr("paste.Auth", MockAuth) response = call_app(app, environ) assert check_token_called assert response.data == b"401 Unauthorized\n" @@ -121,7 +121,7 @@ def test_authenticate_check_token_success(app, method, monkeypatch): "HTTP_AUTHORIZATION": f"APIKey {b64encode(token).decode()}", } - monkeypatch.setattr("paste.Store", MockStore) + monkeypatch.setattr("paste.Auth", MockAuth) response = call_app(app, environ) assert check_token_called assert response.data == b"Hello, world!" diff --git a/tests/test_application.py b/tests/test_application.py index 20cbeac..c250909 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -5,7 +5,7 @@ from webtest import TestApp import paste.db from paste import __main__, application -from paste.store import Store +from paste.store import Auth DB = "file::memory:?cache=shared" @@ -25,7 +25,7 @@ def app(db): @pytest.fixture def token(db): - return b64encode(Store(db).generate_token()).decode() + return b64encode(Auth(db).generate_token()).decode() @pytest.mark.parametrize("method", ["put", "post", "delete"]) -- cgit v1.2.3-54-g00ecf