From 9da33f786ea4c3d30ed464cfeb425b06590d672a Mon Sep 17 00:00:00 2001 From: Username Date: Sat, 20 Dec 2025 17:20:27 +0100 Subject: [PATCH] fix lint issues across codebase --- app/__init__.py | 23 ++++++++++++++--------- tests/test_abuse_prevention.py | 20 +++++++------------- tests/test_api.py | 4 +--- tests/test_database.py | 5 ++--- tests/test_pow.py | 2 +- tests/test_security.py | 7 ++++--- 6 files changed, 29 insertions(+), 32 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 78e2c39..88508e9 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -13,11 +13,13 @@ from app.config import VERSION, config def setup_logging(app: Flask) -> None: """Configure structured logging.""" log_level = logging.DEBUG if app.debug else logging.INFO - log_format = ( - "%(asctime)s %(levelname)s [%(name)s] %(message)s" - if app.debug - else '{"time":"%(asctime)s","level":"%(levelname)s","logger":"%(name)s","message":"%(message)s"}' - ) + if app.debug: + log_format = "%(asctime)s %(levelname)s [%(name)s] %(message)s" + else: + log_format = ( + '{"time":"%(asctime)s","level":"%(levelname)s",' + '"logger":"%(name)s","message":"%(message)s"}' + ) logging.basicConfig( level=log_level, @@ -112,7 +114,9 @@ def setup_error_handlers(app: Flask) -> None: def rate_limit_exceeded(error): app.logger.warning( "Rate limit exceeded: %s from %s [rid=%s]", - request.path, request.remote_addr, getattr(g, "request_id", "-") + request.path, + request.remote_addr, + getattr(g, "request_id", "-"), ) return Response( json.dumps({"error": "Rate limit exceeded", "retry_after": error.description}), @@ -124,7 +128,9 @@ def setup_error_handlers(app: Flask) -> None: def internal_error(error): app.logger.error( "Internal error: %s - %s [rid=%s]", - request.path, str(error), getattr(g, "request_id", "-") + request.path, + str(error), + getattr(g, "request_id", "-"), ) return Response( json.dumps({"error": "Internal server error"}), @@ -135,8 +141,7 @@ def setup_error_handlers(app: Flask) -> None: @app.errorhandler(Exception) def handle_exception(error): app.logger.exception( - "Unhandled exception: %s [rid=%s]", - str(error), getattr(g, "request_id", "-") + "Unhandled exception: %s [rid=%s]", str(error), getattr(g, "request_id", "-") ) return Response( json.dumps({"error": "Internal server error"}), diff --git a/tests/test_abuse_prevention.py b/tests/test_abuse_prevention.py index 7a0a7b7..8cd8ae9 100644 --- a/tests/test_abuse_prevention.py +++ b/tests/test_abuse_prevention.py @@ -37,14 +37,14 @@ class TestContentDedup: # First 3 submissions should succeed for i in range(3): response = strict_client.post("/", data=content) - assert response.status_code == 201, f"Submission {i+1} failed" + assert response.status_code == 201, f"Submission {i + 1} failed" def test_duplicate_exceeds_threshold_rejected(self, strict_client): """Fourth duplicate within window should be rejected.""" content = b"unique content 3" # First 3 succeed - for i in range(3): + for _ in range(3): response = strict_client.post("/", data=content) assert response.status_code == 201 @@ -144,8 +144,7 @@ class TestContentHashDatabase: # Query database directly db = get_db() row = db.execute( - "SELECT hash, count FROM content_hashes WHERE hash = ?", - (content_hash,) + "SELECT hash, count FROM content_hashes WHERE hash = ?", (content_hash,) ).fetchone() assert row is not None @@ -179,10 +178,7 @@ class TestContentHashCleanup: # Verify removed db = get_db() - row = db.execute( - "SELECT * FROM content_hashes WHERE hash = ?", - (content_hash,) - ).fetchone() + row = db.execute("SELECT * FROM content_hashes WHERE hash = ?", (content_hash,)).fetchone() assert row is None def test_cleanup_keeps_recent(self, app_context): @@ -193,14 +189,11 @@ class TestContentHashCleanup: check_content_hash(content_hash) # Cleanup should not remove it - deleted = cleanup_expired_hashes() + cleanup_expired_hashes() # Verify still present db = get_db() - row = db.execute( - "SELECT * FROM content_hashes WHERE hash = ?", - (content_hash,) - ).fetchone() + row = db.execute("SELECT * FROM content_hashes WHERE hash = ?", (content_hash,)).fetchone() assert row is not None @@ -268,6 +261,7 @@ class TestEntropyEnforcement: def test_random_data_accepted(self, entropy_client): """Random/encrypted data should pass entropy check.""" import os + random_data = os.urandom(512) # High entropy random bytes response = entropy_client.post( diff --git a/tests/test_api.py b/tests/test_api.py index 30c2179..de17865 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -254,9 +254,7 @@ class TestDeletePaste: data = json.loads(response.data) assert "error" in data - def test_delete_paste_wrong_owner( - self, client, sample_text, auth_header, other_auth_header - ): + def test_delete_paste_wrong_owner(self, client, sample_text, auth_header, other_auth_header): """Delete paste owned by another user fails.""" create = client.post( "/", diff --git a/tests/test_database.py b/tests/test_database.py index 31486ef..2ca7af4 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -67,9 +67,8 @@ class TestCleanupExpiredPastes: # Mock time to simulate expiry (paste expiry + 1 second) future_time = time.time() + app.config["PASTE_EXPIRY_SECONDS"] + 1 - with patch("time.time", return_value=future_time): - with app.app_context(): - deleted = cleanup_expired_pastes() + with patch("time.time", return_value=future_time), app.app_context(): + deleted = cleanup_expired_pastes() assert deleted >= 1 diff --git a/tests/test_pow.py b/tests/test_pow.py index a51f426..9241146 100644 --- a/tests/test_pow.py +++ b/tests/test_pow.py @@ -166,7 +166,7 @@ def solve_pow(nonce, difficulty): if byte == 0: zero_bits += 8 else: - zero_bits += (8 - byte.bit_length()) + zero_bits += 8 - byte.bit_length() break if zero_bits >= difficulty: diff --git a/tests/test_security.py b/tests/test_security.py index aaf9f02..5ff5b98 100644 --- a/tests/test_security.py +++ b/tests/test_security.py @@ -104,6 +104,7 @@ class TestProxyTrustValidation: ) assert response.status_code == 201 import json + data = json.loads(response.data) assert "owner" in data @@ -124,6 +125,7 @@ class TestProxyTrustValidation: ) assert response.status_code == 201 import json + data = json.loads(response.data) # Owner should NOT be set because proxy wasn't trusted assert "owner" not in data @@ -148,6 +150,7 @@ class TestProxyTrustValidation: ) assert response.status_code == 201 import json + data = json.loads(response.data) assert "owner" in data finally: @@ -259,9 +262,7 @@ class TestSizeLimits: class TestOwnershipEnforcement: """Tests for paste ownership and access control.""" - def test_cannot_delete_others_paste( - self, client, sample_text, auth_header, other_auth_header - ): + def test_cannot_delete_others_paste(self, client, sample_text, auth_header, other_auth_header): """Users cannot delete pastes they don't own.""" create = client.post( "/",