From d198fca95919cc78275d3d9fa8f1b0a8acfdbab3 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Tue, 28 Mar 2023 18:53:15 +0100 Subject: Switch back to centralised opening of the database Create Store instances when needed This will make more sense with following commits --- tests/middleware/test_authenticate.py | 23 +++++++++++++++++------ tests/middleware/test_open_store.py | 20 ++++++++++---------- tests/test_application.py | 6 +++--- 3 files changed, 30 insertions(+), 19 deletions(-) (limited to 'tests') diff --git a/tests/middleware/test_authenticate.py b/tests/middleware/test_authenticate.py index 2acfe92..9fccb32 100644 --- a/tests/middleware/test_authenticate.py +++ b/tests/middleware/test_authenticate.py @@ -34,7 +34,7 @@ def test_unauthenticated_request(app, method): def test_unauthenticated_request_with_key(app, method): environ = { "REQUEST_METHOD": method, - "paste.store": None, + "paste.db_conn": None, "HTTP_AUTHORIZATION": "ApiKey AAAA", } response = call_app(app, environ) @@ -64,13 +64,22 @@ def test_authenticate_malformed_key(app, method, key): assert ("WWW-Authenticate", "APIKey") in response.headers -class MockDB: +class MockConnection: def __init__(self, check_token): self.check_token = check_token +class MockStore: + 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): +def test_authenticate_check_token_fail(app, method, monkeypatch): check_token_called = False token = b"test" @@ -82,10 +91,11 @@ def test_authenticate_check_token_fail(app, method): environ = { "REQUEST_METHOD": method, - "paste.store": MockDB(check_token), + "paste.db_conn": MockConnection(check_token), "HTTP_AUTHORIZATION": f"APIKey {b64encode(token).decode()}", } + monkeypatch.setattr("paste.Store", MockStore) response = call_app(app, environ) assert check_token_called assert response.data == b"401 Unauthorized\n" @@ -95,7 +105,7 @@ def test_authenticate_check_token_fail(app, method): @pytest.mark.parametrize("method", ["POST", "PUT", "DELETE"]) -def test_authenticate_check_token_success(app, method): +def test_authenticate_check_token_success(app, method, monkeypatch): check_token_called = False token = b"test" @@ -107,10 +117,11 @@ def test_authenticate_check_token_success(app, method): environ = { "REQUEST_METHOD": method, - "paste.store": MockDB(check_token), + "paste.db_conn": MockConnection(check_token), "HTTP_AUTHORIZATION": f"APIKey {b64encode(token).decode()}", } + monkeypatch.setattr("paste.Store", MockStore) response = call_app(app, environ) assert check_token_called assert response.data == b"Hello, world!" diff --git a/tests/middleware/test_open_store.py b/tests/middleware/test_open_store.py index dd430e9..3e844a9 100644 --- a/tests/middleware/test_open_store.py +++ b/tests/middleware/test_open_store.py @@ -1,30 +1,30 @@ from contextlib import contextmanager from wsgiref.validate import validator -from paste import open_store +from paste import open_database from ..common_wsgi import call_app -def test_open_store(monkeypatch): +def test_open_database(monkeypatch): db_path = "test_db_path" - store = object() + conn = object() @contextmanager - def store_open(path): - assert path == db_path - yield store + def connect(uri): + assert uri == db_path + yield conn @validator - @open_store + @open_database @validator def app(environ, start_response): - assert "paste.store" in environ - assert environ["paste.store"] == store + assert "paste.db_conn" in environ + assert environ["paste.db_conn"] == conn start_response("200 OK", [("Content-Type", "text/plain")]) return [b"Hello, World!"] - monkeypatch.setattr("paste.store.open", store_open) + monkeypatch.setattr("paste.db.connect", connect) response = call_app(app, environ={"PASTE_DB": db_path}) assert response.status == "200 OK" assert response.data == b"Hello, World!" diff --git a/tests/test_application.py b/tests/test_application.py index e86d937..20cbeac 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -4,15 +4,15 @@ import pytest from webtest import TestApp import paste.db -import paste.store from paste import __main__, application +from paste.store import Store DB = "file::memory:?cache=shared" @pytest.fixture def db(): - with paste.store.open(DB) as d: + with paste.db.connect(DB) as d: yield d @@ -25,7 +25,7 @@ def app(db): @pytest.fixture def token(db): - return b64encode(db.generate_token()).decode() + return b64encode(Store(db).generate_token()).decode() @pytest.mark.parametrize("method", ["put", "post", "delete"]) -- cgit v1.2.3-54-g00ecf