|
| 1 | +import json |
1 | 2 | import os |
2 | 3 | from unittest import mock |
3 | 4 |
|
|
11 | 12 | from django.core.management import call_command |
12 | 13 | from django.test import RequestFactory, TestCase |
13 | 14 | from django.test.utils import override_settings |
| 15 | +from django.urls import reverse |
14 | 16 | from django.utils.encoding import force_str |
| 17 | +from django.utils.translation import get_language |
15 | 18 | from faker import Faker |
16 | 19 | from faker.providers import geo |
17 | 20 | from freezegun import freeze_time |
@@ -666,3 +669,72 @@ def test_filter_view_creates_full_generic_filter(self): |
666 | 669 | response = self.client.get(City.get_filter_url()) |
667 | 670 | self.assertContains(response, '<input type="text" name="name"') |
668 | 671 | self.assertContains(response, '<input type="hidden" name="bbox"') |
| 672 | + |
| 673 | + |
| 674 | +class MapScreenshotTest(BaseTest): |
| 675 | + context = { |
| 676 | + "mapview": {"lat": 42.771211138625894, "lng": 1.336212158203125, "zoom": 9}, |
| 677 | + "maplayers": ["OSM", "Cadastre", "Signalétiques", "POI", "▣ Tronçons"], |
| 678 | + "filter": "", |
| 679 | + "sortcolumns": {}, |
| 680 | + "fullurl": "https://test.fr/path/list/", |
| 681 | + "url": "/path/list/", |
| 682 | + "selector": "#map", |
| 683 | + "viewport": {"width": 1854, "height": 481}, |
| 684 | + "timestamp": 1762525783907, |
| 685 | + } |
| 686 | + |
| 687 | + @mock.patch("mapentity.helpers.requests.get") |
| 688 | + def test_map_screenshot_success(self, mock_capture): |
| 689 | + self.login() |
| 690 | + |
| 691 | + mock_capture.return_value.content = b"fake_png_data" |
| 692 | + mock_capture.return_value.status_code = 200 |
| 693 | + |
| 694 | + data = {"printcontext": json.dumps(self.context)} |
| 695 | + |
| 696 | + response = self.client.post(reverse("mapentity:map_screenshot"), data) |
| 697 | + |
| 698 | + self.assertEqual(response.status_code, 200) |
| 699 | + self.assertEqual(response["Content-Type"], "image/png") |
| 700 | + self.assertIn("attachment", response["Content-Disposition"]) |
| 701 | + self.assertEqual(response.content, b"fake_png_data") |
| 702 | + |
| 703 | + args, kwargs = mock_capture.call_args |
| 704 | + called_url = args[0] |
| 705 | + |
| 706 | + # check mapentity url |
| 707 | + self.assertTrue(called_url.startswith("http")) |
| 708 | + self.assertIn(f"lang%3D{get_language()}", called_url) |
| 709 | + self.assertIn("auth_token", called_url) |
| 710 | + self.assertIn("context", called_url) |
| 711 | + |
| 712 | + # check screamshotter url |
| 713 | + self.assertIn("width=1854", called_url) |
| 714 | + self.assertIn("height=481", called_url) |
| 715 | + self.assertIn("selector=%23map", called_url) |
| 716 | + |
| 717 | + def test_map_screenshot_invalid_json_context(self): |
| 718 | + self.login() |
| 719 | + |
| 720 | + data = {"printcontext": "json_error"} |
| 721 | + |
| 722 | + with self.assertLogs(level="INFO") as cm: |
| 723 | + response = self.client.post(reverse("mapentity:map_screenshot"), data) |
| 724 | + self.assertEqual(response.status_code, 400) |
| 725 | + self.assertIn( |
| 726 | + "ERROR:mapentity.views.base:Expecting value: line 1 column 1 (char 0)", |
| 727 | + cm.output[0], |
| 728 | + ) |
| 729 | + |
| 730 | + def test_map_screenshot_invalid_length_context(self): |
| 731 | + self.login() |
| 732 | + |
| 733 | + data = {"printcontext": "{" + "test" * 1000 + "}"} |
| 734 | + |
| 735 | + with self.assertLogs(level="INFO") as cm: |
| 736 | + response = self.client.post(reverse("mapentity:map_screenshot"), data) |
| 737 | + self.assertEqual(response.status_code, 400) |
| 738 | + self.assertIn( |
| 739 | + "ERROR:mapentity.views.base:Print context is way too big", cm.output[0] |
| 740 | + ) |
0 commit comments