From 232fbe6cea8bd3c56c826321ea8f124bb58fe4fa Mon Sep 17 00:00:00 2001 From: Matthieu Petiteau Date: Tue, 12 Sep 2023 18:18:50 +0100 Subject: [PATCH] Add test for length validation --- shhh/api/schemas.py | 2 +- shhh/config.py | 1 + tests/test_api.py | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/shhh/api/schemas.py b/shhh/api/schemas.py index de1693c0..a6c4ca99 100644 --- a/shhh/api/schemas.py +++ b/shhh/api/schemas.py @@ -31,7 +31,7 @@ def _secret_validator(secret: str) -> None: max_length = app.config["SHHH_SECRET_MAX_LENGTH"] if len(secret) > max_length: raise ValidationError(f"The secret should not exceed {max_length} " - "characters") + "characters.") class WriteRequest(Schema): diff --git a/shhh/config.py b/shhh/config.py index 5fc89d48..e9c1708e 100644 --- a/shhh/config.py +++ b/shhh/config.py @@ -67,6 +67,7 @@ class TestConfig(DefaultConfig): SQLALCHEMY_DATABASE_URI = "sqlite://" SHHH_HOST = "http://test.test" + SHHH_SECRET_MAX_LENGTH = 20 SHHH_DB_LIVENESS_RETRY_COUNT = 1 SHHH_DB_LIVENESS_SLEEP_INTERVAL = 0.1 diff --git a/tests/test_api.py b/tests/test_api.py index 0609362e..569a7c1a 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -89,6 +89,17 @@ def test_api_post_weak_passphrase(app, post_payload, passphrase): "characters, with 1 number and 1 uppercase.") +def test_api_post_secret_too_long(app, post_payload): + post_payload["secret"] = "MoreThan20Characters!" + with app.test_request_context(), app.test_client() as test_client: + response = test_client.post(url_for("api.secret"), json=post_payload) + assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY + data = response.get_json() + assert data["response"]["status"] == Status.ERROR + assert data["response"]["details"] == ("The secret should not exceed " + "20 characters.") + + def test_api_get_wrong_passphrase(app, secret): with app.test_request_context(), app.test_client() as test_client: response = test_client.get(