|
| 1 | +import sys |
| 2 | +import unittest |
| 3 | +from io import BytesIO |
| 4 | +from pathlib import Path |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +import httpx |
| 8 | +from PIL import Image |
| 9 | + |
| 10 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
| 11 | + |
| 12 | +from src.application.validate_certificate_background import ValidateCertificateBackground |
| 13 | +from src.domain.exception.invalid_certificate_background import InvalidCertificateBackground |
| 14 | + |
| 15 | + |
| 16 | +def _image_bytes(size, image_format="PNG") -> bytes: |
| 17 | + buffer = BytesIO() |
| 18 | + Image.new("RGB", size, (255, 255, 255)).save(buffer, format=image_format) |
| 19 | + return buffer.getvalue() |
| 20 | + |
| 21 | + |
| 22 | +def _response(url: str, content: bytes, status_code: int = 200) -> httpx.Response: |
| 23 | + return httpx.Response(status_code, content=content, request=httpx.Request("GET", url)) |
| 24 | + |
| 25 | + |
| 26 | +class ValidateCertificateBackgroundTestCase(unittest.TestCase): |
| 27 | + URL = "https://tech.floripa.br/wp-content/uploads/fundo.png" |
| 28 | + |
| 29 | + def setUp(self): |
| 30 | + self.validator = ValidateCertificateBackground() |
| 31 | + patcher = patch("src.application.validate_certificate_background.httpx.Client") |
| 32 | + self.client_class = patcher.start() |
| 33 | + self.addCleanup(patcher.stop) |
| 34 | + self.client = MagicMock() |
| 35 | + self.client_class.return_value.__enter__.return_value = self.client |
| 36 | + |
| 37 | + def _serve(self, content: bytes, status_code: int = 200): |
| 38 | + self.client.get.side_effect = lambda url: _response(url, content, status_code) |
| 39 | + |
| 40 | + def test_accepts_png_with_standard_size(self): |
| 41 | + self._serve(_image_bytes((1200, 627))) |
| 42 | + self.validator.execute([self.URL]) |
| 43 | + |
| 44 | + def test_accepts_jpeg_with_standard_size(self): |
| 45 | + self._serve(_image_bytes((1200, 627), "JPEG")) |
| 46 | + self.validator.execute([self.URL]) |
| 47 | + |
| 48 | + def test_rejects_off_standard_size_with_actual_dimensions(self): |
| 49 | + self._serve(_image_bytes((2000, 1414))) |
| 50 | + with self.assertRaises(InvalidCertificateBackground) as ctx: |
| 51 | + self.validator.execute([self.URL]) |
| 52 | + self.assertIn("2000×1414", ctx.exception.message) |
| 53 | + self.assertIn("1200×627", ctx.exception.message) |
| 54 | + self.assertEqual(ctx.exception.url, self.URL) |
| 55 | + |
| 56 | + def test_rejects_other_formats(self): |
| 57 | + self._serve(_image_bytes((1200, 627), "GIF")) |
| 58 | + with self.assertRaises(InvalidCertificateBackground) as ctx: |
| 59 | + self.validator.execute([self.URL]) |
| 60 | + self.assertIn("GIF", ctx.exception.message) |
| 61 | + |
| 62 | + def test_rejects_empty_background(self): |
| 63 | + for value in ("", " "): |
| 64 | + with self.assertRaises(InvalidCertificateBackground) as ctx: |
| 65 | + self.validator.execute([value]) |
| 66 | + self.assertIn("não informado", ctx.exception.message) |
| 67 | + self.client.get.assert_not_called() |
| 68 | + |
| 69 | + def test_rejects_unreachable_url(self): |
| 70 | + self._serve(b"not found", status_code=404) |
| 71 | + with self.assertRaises(InvalidCertificateBackground) as ctx: |
| 72 | + self.validator.execute([self.URL]) |
| 73 | + self.assertIn("Não foi possível baixar", ctx.exception.message) |
| 74 | + |
| 75 | + def test_rejects_content_that_is_not_an_image(self): |
| 76 | + self._serve(b"<html>login</html>") |
| 77 | + with self.assertRaises(InvalidCertificateBackground): |
| 78 | + self.validator.execute([self.URL]) |
| 79 | + |
| 80 | + def test_downloads_each_distinct_url_once(self): |
| 81 | + self._serve(_image_bytes((1200, 627))) |
| 82 | + self.validator.execute([self.URL, self.URL, self.URL]) |
| 83 | + self.assertEqual(self.client.get.call_count, 1) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + unittest.main() |
0 commit comments