Files
certificate-system/backend/tests/test_captcha.py
2026-08-13 15:46:50 +08:00

31 lines
877 B
Python

import base64
import io
from PIL import Image
from app.services.captcha import (
CAPTCHA_FONT_SIZE,
CAPTCHA_HEIGHT,
CAPTCHA_WIDTH,
load_captcha_font,
make_captcha,
verify_captcha,
)
def test_captcha_can_only_be_used_once():
captcha = make_captcha()
captcha_id = captcha["captcha_id"]
# The real code is intentionally not exposed. This test locks in one-time consumption behavior.
assert not verify_captcha(captcha_id, "bad")
assert not verify_captcha(captcha_id, "bad")
def test_captcha_uses_large_font_and_expected_image_size():
captcha = make_captcha()
encoded_image = captcha["image"].split(",", 1)[1]
image = Image.open(io.BytesIO(base64.b64decode(encoded_image)))
assert image.size == (CAPTCHA_WIDTH, CAPTCHA_HEIGHT)
assert getattr(load_captcha_font(), "size", CAPTCHA_FONT_SIZE) == CAPTCHA_FONT_SIZE