Files
replypier/backend/tests/services/test_image_security.py
T
WangShen007 54b949b392 chore(brand): rename the product to ReplyPier
The upstream project name is gone from code, assets, docs and build
output: brand strings, domains (replypier.site and its app/api/docs/help
subdomains), env prefixes, image and network names, the widget's public
JS API (window.ReplyPier, replypierId/BaseUrl/Config/Token), CSS tokens
(--rp-*), file and directory names, and the WordPress plugin package.

- widget loader is now webclient/replypier.min.js; both bundles rebuilt
  from source (backend/assets/widget.{js,css}, frontend/public/webclient)
- NOTICE, llms.txt, CONTRIBUTING, HELP_CENTER_INFRA and config.dev.js
  rewritten rather than string-swapped; the demo config no longer ships
  the previous vendor's Firebase/Razorpay/Google keys
- new migration replypier_ai_model_001 adds REPLYPIER to the managed
  AIModelType and rewrites pre-rename rows
- local compose project, database, role and volume renamed; LOCAL_DEPLOY
  documents carrying the old local data over
2026-10-26 00:51:03 +08:00

95 lines
2.9 KiB
Python

"""
Copyright 2024-2026 ReplyPier
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import io
import pytest
from fastapi import HTTPException
from PIL import Image
from app.services.image_security import sanitize_image
RASTER = {"image/png", "image/jpeg", "image/webp"}
def _img_bytes(fmt: str, size=(64, 48), color=(200, 30, 30)) -> bytes:
buf = io.BytesIO()
Image.new("RGB", size, color).save(buf, format=fmt)
return buf.getvalue()
def test_png_reencoded_to_png():
out, ctype, ext = sanitize_image(
_img_bytes("PNG"), allowed_content_types=RASTER, max_bytes=2_000_000, max_dim=4000
)
assert ctype == "image/png" and ext == ".png"
assert Image.open(io.BytesIO(out)).format == "PNG"
def test_jpeg_stays_jpeg():
out, ctype, ext = sanitize_image(
_img_bytes("JPEG"), allowed_content_types=RASTER, max_bytes=2_000_000, max_dim=4000
)
assert ctype == "image/jpeg" and ext == ".jpg"
assert Image.open(io.BytesIO(out)).format == "JPEG"
def test_fit_downscales_longest_side():
out, _c, _e = sanitize_image(
_img_bytes("PNG", size=(1200, 600)),
allowed_content_types=RASTER,
max_bytes=2_000_000,
max_dim=4000,
fit=512,
)
w, h = Image.open(io.BytesIO(out)).size
assert max(w, h) == 512 and (w, h) == (512, 256)
def test_rejects_non_image():
with pytest.raises(HTTPException) as e:
sanitize_image(b"not an image", allowed_content_types=RASTER, max_bytes=2_000_000, max_dim=4000)
assert e.value.status_code == 400
def test_rejects_oversized_bytes():
with pytest.raises(HTTPException) as e:
sanitize_image(_img_bytes("PNG"), allowed_content_types=RASTER, max_bytes=10, max_dim=4000)
assert e.value.status_code == 400
def test_rejects_dimensions_over_cap():
with pytest.raises(HTTPException) as e:
sanitize_image(
_img_bytes("PNG", size=(300, 300)),
allowed_content_types=RASTER,
max_bytes=2_000_000,
max_dim=100,
)
assert e.value.status_code == 400
def test_rejects_disallowed_format():
# GIF is a valid image but not in the allowlist here → rejected.
with pytest.raises(HTTPException) as e:
sanitize_image(_img_bytes("GIF"), allowed_content_types=RASTER, max_bytes=2_000_000, max_dim=4000)
assert e.value.status_code == 400
def test_empty_rejected():
with pytest.raises(HTTPException):
sanitize_image(b"", allowed_content_types=RASTER, max_bytes=2_000_000, max_dim=4000)