diff options
author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2023-03-27 22:46:44 +0100 |
---|---|---|
committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2023-03-27 22:46:44 +0100 |
commit | 77889ec30410280ef1ec1671b6226a8d0c0444f7 (patch) | |
tree | b6f371ee6838b179681dc101c8c32b0ed088fe41 /tests | |
parent | 3a9629e49b4c7e1d10c89bbffa04d18e96948116 (diff) | |
download | paste-77889ec30410280ef1ec1671b6226a8d0c0444f7.tar.gz paste-77889ec30410280ef1ec1671b6226a8d0c0444f7.tar.xz paste-77889ec30410280ef1ec1671b6226a8d0c0444f7.zip |
Throw all data manipulation code in one place
This means that everything now goes through a Store object, which should
make testing a little bit easier.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/middleware/test_authenticate.py | 53 | ||||
-rw-r--r-- | tests/middleware/test_open_database.py | 7 | ||||
-rw-r--r-- | tests/test_application.py | 6 |
3 files changed, 32 insertions, 34 deletions
diff --git a/tests/middleware/test_authenticate.py b/tests/middleware/test_authenticate.py index 2395316..2acfe92 100644 --- a/tests/middleware/test_authenticate.py +++ b/tests/middleware/test_authenticate.py @@ -22,8 +22,7 @@ def app(): @pytest.mark.parametrize("method", ["GET", "HEAD"]) -def test_unauthenticated_request(app, method, monkeypatch): - monkeypatch.delattr(paste, "check_token") +def test_unauthenticated_request(app, method): environ = {"REQUEST_METHOD": method} response = call_app(app, environ) assert response.data == b"Hello, world!" @@ -32,11 +31,10 @@ def test_unauthenticated_request(app, method, monkeypatch): @pytest.mark.parametrize("method", ["GET", "HEAD"]) -def test_unauthenticated_request_with_key(app, method, monkeypatch): - monkeypatch.delattr(paste, "check_token") +def test_unauthenticated_request_with_key(app, method): environ = { "REQUEST_METHOD": method, - "paste.db_conn": None, + "paste.store": None, "HTTP_AUTHORIZATION": "ApiKey AAAA", } response = call_app(app, environ) @@ -46,8 +44,7 @@ def test_unauthenticated_request_with_key(app, method, monkeypatch): @pytest.mark.parametrize("method", ["POST", "PUT", "DELETE"]) -def test_authenticate_no_header(app, method, monkeypatch): - monkeypatch.delattr(paste, "check_token") +def test_authenticate_no_header(app, method): environ = {"REQUEST_METHOD": method} response = call_app(app, environ) assert response.data == b"401 Unauthorized\n" @@ -58,8 +55,7 @@ def test_authenticate_no_header(app, method, monkeypatch): @pytest.mark.parametrize("method", ["POST", "PUT", "DELETE"]) @pytest.mark.parametrize("key", ["ApiKey AAAA", "APIKey AAA", "APIKey AAAA", "AAAA"]) -def test_authenticate_malformed_key(app, method, key, monkeypatch): - monkeypatch.delattr(paste, "check_token") +def test_authenticate_malformed_key(app, method, key): environ = {"REQUEST_METHOD": method, "HTTP_AUTHORIZATION": key} response = call_app(app, environ) assert response.data == b"401 Unauthorized\n" @@ -68,24 +64,28 @@ def test_authenticate_malformed_key(app, method, key, monkeypatch): assert ("WWW-Authenticate", "APIKey") in response.headers +class MockDB: + def __init__(self, check_token): + self.check_token = check_token + + @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" - environ = { - "REQUEST_METHOD": method, - "paste.db_conn": object(), - "HTTP_AUTHORIZATION": f"APIKey {b64encode(token).decode()}", - } - def check_token(conn, tok): + def check_token(tok): nonlocal check_token_called - assert conn == environ["paste.db_conn"] assert tok == token check_token_called = True return False - monkeypatch.setattr(paste, "check_token", check_token) + environ = { + "REQUEST_METHOD": method, + "paste.store": MockDB(check_token), + "HTTP_AUTHORIZATION": f"APIKey {b64encode(token).decode()}", + } + response = call_app(app, environ) assert check_token_called assert response.data == b"401 Unauthorized\n" @@ -95,23 +95,22 @@ 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" - environ = { - "REQUEST_METHOD": method, - "paste.db_conn": object(), - "HTTP_AUTHORIZATION": f"APIKey {b64encode(token).decode()}", - } - def check_token(conn, tok): + def check_token(tok): nonlocal check_token_called - assert conn == environ["paste.db_conn"] assert tok == token check_token_called = True return True - monkeypatch.setattr(paste, "check_token", check_token) + environ = { + "REQUEST_METHOD": method, + "paste.store": MockDB(check_token), + "HTTP_AUTHORIZATION": f"APIKey {b64encode(token).decode()}", + } + response = call_app(app, environ) assert check_token_called assert response.data == b"Hello, world!" diff --git a/tests/middleware/test_open_database.py b/tests/middleware/test_open_database.py index 43ebb07..1584ac0 100644 --- a/tests/middleware/test_open_database.py +++ b/tests/middleware/test_open_database.py @@ -15,10 +15,9 @@ def app(): @open_database @validator def app(environ, start_response): - assert "paste.db_conn" in environ - conn = environ["paste.db_conn"] - (ver,) = conn.execute("PRAGMA user_version").fetchone() - assert ver > 0 + assert "paste.store" in environ + db = environ["paste.store"] + assert db.version > 0 start_response("200 OK", [("Content-Type", "text/plain")]) return [b"Hello, World!"] diff --git a/tests/test_application.py b/tests/test_application.py index 5a7847e..e86d937 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -4,6 +4,7 @@ import pytest from webtest import TestApp import paste.db +import paste.store from paste import __main__, application DB = "file::memory:?cache=shared" @@ -11,7 +12,7 @@ DB = "file::memory:?cache=shared" @pytest.fixture def db(): - with paste.db.connect(DB) as d: + with paste.store.open(DB) as d: yield d @@ -24,7 +25,7 @@ def app(db): @pytest.fixture def token(db): - return b64encode(__main__.generate_token(db)).decode() + return b64encode(db.generate_token()).decode() @pytest.mark.parametrize("method", ["put", "post", "delete"]) @@ -229,7 +230,6 @@ def test_delete(app, token): ) assert res.status == "201 Created" assert res.headers["Location"] == res.request.url - etag = res.headers["ETag"] res = app.delete("/test_key", expect_errors=True) assert res.status == "401 Unauthorized" res = app.delete("/test_key", headers={"Authorization": f"APIKey {token}"}) |