forked from mtusi2026/mtusi2026lab4
34 lines
977 B
Python
34 lines
977 B
Python
"""Unit-level image checks; run in built api1 container."""
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from src.main import validate_image
|
|
from src.errors import ApiError
|
|
|
|
PNG = (Path(__file__).resolve().parents[1]/'tests/fixtures/sample.png').read_bytes()
|
|
|
|
|
|
class ImageValidationTest(unittest.TestCase):
|
|
def check(self, content, content_type):
|
|
with tempfile.SpooledTemporaryFile() as stream:
|
|
stream.write(content)
|
|
validate_image(stream, content_type)
|
|
|
|
def test_fixture(self):
|
|
self.check(PNG,'image/png')
|
|
|
|
def test_corrupt(self):
|
|
with self.assertRaises(ApiError):
|
|
self.check(PNG[:70], 'image/png')
|
|
|
|
def test_spoof_mime(self):
|
|
with self.assertRaises(ApiError):
|
|
self.check(PNG,'image/jpeg')
|
|
|
|
def test_random_bytes(self):
|
|
with self.assertRaises(ApiError):
|
|
self.check(b'unrelated bytes', 'image/png')
|
|
|
|
if __name__=='__main__':
|
|
unittest.main()
|