|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +import requests |
| 4 | + |
| 5 | +from odoo.tests import tagged |
| 6 | +from odoo.tests.common import HttpCase |
| 7 | + |
| 8 | + |
| 9 | +@tagged("post_install", "-at_install") |
| 10 | +class TestIconifyProxyController(HttpCase): |
| 11 | + def test_get_svg_success(self): |
| 12 | + response = self.url_open("/web_iconify_proxy/mdi/home.svg") |
| 13 | + self.assertEqual(response.status_code, 200) |
| 14 | + self.assertEqual(response.headers["Content-Type"], "image/svg+xml") |
| 15 | + # Add basic check for SVG content (can be improved) |
| 16 | + self.assertTrue(b"<svg" in response.content) |
| 17 | + |
| 18 | + def test_get_css_success(self): |
| 19 | + response = self.url_open("/web_iconify_proxy/mdi.css?icons=home,account") |
| 20 | + self.assertEqual(response.status_code, 200) |
| 21 | + self.assertEqual(response.headers["Content-Type"], "text/css") |
| 22 | + # Add basic check for CSS content |
| 23 | + self.assertTrue(b".iconify" in response.content) |
| 24 | + |
| 25 | + def test_get_json_success(self): |
| 26 | + response = self.url_open("/web_iconify_proxy/mdi.json?icons=home,account") |
| 27 | + self.assertEqual(response.status_code, 200) |
| 28 | + self.assertEqual(response.headers["Content-Type"], "application/json") |
| 29 | + # Add basic check for JSON content (can be improved) |
| 30 | + self.assertTrue(b'{"prefix":' in response.content) |
| 31 | + |
| 32 | + def test_get_svg_invalid_prefix(self): |
| 33 | + response = self.url_open("/web_iconify_proxy/invalid!prefix/home.svg") |
| 34 | + self.assertEqual(response.status_code, 404) |
| 35 | + |
| 36 | + def test_get_svg_invalid_icon(self): |
| 37 | + response = self.url_open("/web_iconify_proxy/mdi/invalid!icon.svg") |
| 38 | + self.assertEqual(response.status_code, 404) |
| 39 | + |
| 40 | + def test_get_css_invalid_icons(self): |
| 41 | + response = self.url_open("/web_iconify_proxy/mdi.css?icons=home,invalid!icon") |
| 42 | + self.assertEqual(response.status_code, 404) |
| 43 | + |
| 44 | + def test_get_json_invalid_icons(self): |
| 45 | + response = self.url_open("/web_iconify_proxy/mdi.json?icons=home,invalid!icon") |
| 46 | + self.assertEqual(response.status_code, 404) |
| 47 | + |
| 48 | + def test_get_last_modified(self): |
| 49 | + response = self.url_open("/web_iconify_proxy/last-modified?prefixes=mdi") |
| 50 | + self.assertEqual(response.status_code, 200) |
| 51 | + self.assertEqual(response.headers["Content-Type"], "application/json") |
| 52 | + # Check if the response is a valid timestamp |
| 53 | + try: |
| 54 | + float(response.content) |
| 55 | + except ValueError: |
| 56 | + self.fail("last-modified did not return a valid timestamp") |
| 57 | + |
| 58 | + def test_get_last_modified_no_prefix(self): |
| 59 | + response = self.url_open("/web_iconify_proxy/last-modified") |
| 60 | + self.assertEqual(response.status_code, 404) |
| 61 | + |
| 62 | + def test_get_css_no_icons(self): |
| 63 | + response = self.url_open("/web_iconify_proxy/mdi.css") |
| 64 | + self.assertEqual(response.status_code, 404) |
| 65 | + |
| 66 | + def test_get_json_no_icons(self): |
| 67 | + response = self.url_open("/web_iconify_proxy/mdi.json") |
| 68 | + self.assertEqual(response.status_code, 404) |
| 69 | + |
| 70 | + @patch("odoo.addons.web_iconify_proxy.controllers.main.requests.get") |
| 71 | + def test_caching(self, mock_get): |
| 72 | + # Mock the requests.get method to return a dummy response |
| 73 | + mock_response = requests.Response() |
| 74 | + mock_response.status_code = 200 |
| 75 | + mock_response._content = b"<svg>dummy content</svg>" |
| 76 | + mock_response.headers["Content-Type"] = "image/svg+xml" |
| 77 | + mock_get.return_value = mock_response |
| 78 | + |
| 79 | + # First request, should fetch from API |
| 80 | + response1 = self.url_open("/web_iconify_proxy/mdi/home.svg") |
| 81 | + self.assertEqual(response1.status_code, 200) |
| 82 | + self.assertFalse("X-Cached-At" in response1.headers) # Check that is NOT cached |
| 83 | + |
| 84 | + # Second request, should be served from cache |
| 85 | + response2 = self.url_open("/web_iconify_proxy/mdi/home.svg") |
| 86 | + self.assertEqual(response2.status_code, 200) |
| 87 | + self.assertTrue("X-Cached-At" in response2.headers) # Check that IS cached |
| 88 | + |
| 89 | + # Check that content is the same |
| 90 | + self.assertEqual(response1.content, response2.content) |
| 91 | + |
| 92 | + @patch("odoo.addons.web_iconify_proxy.controllers.main.requests.get") |
| 93 | + def test_api_error(self, mock_get): |
| 94 | + # Mock requests.get to simulate an API error |
| 95 | + mock_get.side_effect = requests.exceptions.RequestException("Simulated Error") |
| 96 | + response = self.url_open("/web_iconify_proxy/mdi/home.svg") |
| 97 | + self.assertEqual(response.status_code, 404) |
0 commit comments