import base64 from unittest.mock import patch import requests from odoo.tests.common import HttpCase, tagged @tagged("post_install", "-at_install") class TestIconifyProxyController(HttpCase): @patch("odoo.addons.web_iconify_proxy.controllers.main.requests.get") def test_get_svg_success(self, mock_get): mock_response = requests.Response() mock_response.status_code = 200 mock_response._content = b"dummy content" mock_response.headers["Content-Type"] = "image/svg+xml" mock_get.return_value = mock_response response = self.url_open("/web_iconify_proxy/mdi/home.svg") self.assertEqual(response.status_code, 200) self.assertEqual(response.headers["Content-Type"], "image/svg+xml") # Add basic check for SVG content (can be improved) self.assertTrue(b"dummy content" mock_response.headers["Content-Type"] = "image/svg+xml" mock_get.return_value = mock_response # First request, should fetch from API response1 = self.url_open("/web_iconify_proxy/mdi/home.svg") self.assertEqual(response1.status_code, 200) self.assertFalse("X-Cached-At" in response1.headers) # Check that is NOT cached # Second request, should be served from cache response2 = self.url_open("/web_iconify_proxy/mdi/home.svg") self.assertEqual(response2.status_code, 200) self.assertTrue("X-Cached-At" in response2.headers) # Check that IS cached # Check that content is the same self.assertEqual(response1.content, response2.content) @patch("odoo.addons.web_iconify_proxy.controllers.main.requests.get") def test_caching_with_params(self, mock_get): """Test caching with different parameters.""" mock_response = requests.Response() mock_response.status_code = 200 mock_response._content = b"dummy content" mock_response.headers["Content-Type"] = "image/svg+xml" mock_get.return_value = mock_response # First request with specific parameters response1 = self.url_open( "/web_iconify_proxy/mdi/home.svg?color=red&width=50&flip=horizontal" ) self.assertEqual(response1.status_code, 200) self.assertFalse("X-Cached-At" in response1.headers) # Second request with the same parameters, should be cached response2 = self.url_open( "/web_iconify_proxy/mdi/home.svg?color=red&width=50&flip=horizontal" ) self.assertEqual(response2.status_code, 200) self.assertTrue("X-Cached-At" in response2.headers) # Third request with different parameters, should not be cached response3 = self.url_open( "/web_iconify_proxy/mdi/home.svg?color=blue&width=100" ) self.assertEqual(response3.status_code, 200) self.assertFalse("X-Cached-At" in response3.headers) @patch("odoo.addons.web_iconify_proxy.controllers.main.requests.get") def test_caching_parameter_order(self, mock_get): """Test that parameter order doesn't affect caching.""" mock_response = requests.Response() mock_response.status_code = 200 mock_response._content = b"dummy content" mock_response.headers["Content-Type"] = "image/svg+xml" mock_get.return_value = mock_response # First request with specific parameter order response1 = self.url_open( "/web_iconify_proxy/mdi/home.svg?color=red&width=50&flip=horizontal" ) self.assertEqual(response1.status_code, 200) self.assertFalse("X-Cached-At" in response1.headers) # Second request with different parameter order, should be cached response2 = self.url_open( "/web_iconify_proxy/mdi/home.svg?flip=horizontal&width=50&color=red" ) self.assertEqual(response2.status_code, 200) self.assertTrue("X-Cached-At" in response2.headers) # Check that content is the same self.assertEqual(response1.content, response2.content) @patch("odoo.addons.web_iconify_proxy.controllers.main.requests.get") def test_caching_boolean_values(self, mock_get): """Test that boolean values are case-insensitive for caching.""" mock_response = requests.Response() mock_response.status_code = 200 mock_response._content = b"dummy content" mock_response.headers["Content-Type"] = "image/svg+xml" mock_get.return_value = mock_response # First request with "true" response1 = self.url_open("/web_iconify_proxy/mdi/home.svg?box=true") self.assertEqual(response1.status_code, 200) self.assertFalse("X-Cached-At" in response1.headers) # Second request with "True", should be cached response2 = self.url_open("/web_iconify_proxy/mdi/home.svg?box=True") self.assertEqual(response2.status_code, 200) self.assertTrue("X-Cached-At" in response2.headers) # Third request with "TRUE", should be cached response3 = self.url_open("/web_iconify_proxy/mdi/home.svg?box=TRUE") self.assertEqual(response3.status_code, 200) self.assertTrue("X-Cached-At" in response3.headers) # Check that content is the same for all self.assertEqual(response1.content, response2.content) self.assertEqual(response1.content, response3.content) @patch("odoo.addons.web_iconify_proxy.controllers.main.requests.get") def test_get_svg_with_parameters(self, mock_get): """Test the get_svg route with various valid parameters.""" mock_response = requests.Response() mock_response.status_code = 200 mock_response._content = b"dummy content" mock_response.headers["Content-Type"] = "image/svg+xml" mock_get.return_value = mock_response test_cases = [ "/web_iconify_proxy/mdi/home.svg?color=red", "/web_iconify_proxy/mdi/home.svg?width=50", "/web_iconify_proxy/mdi/home.svg?height=50", "/web_iconify_proxy/mdi/home.svg?flip=horizontal", "/web_iconify_proxy/mdi/home.svg?flip=vertical", "/web_iconify_proxy/mdi/home.svg?flip=horizontal,vertical", "/web_iconify_proxy/mdi/home.svg?rotate=90", "/web_iconify_proxy/mdi/home.svg?rotate=180", "/web_iconify_proxy/mdi/home.svg?rotate=270", "/web_iconify_proxy/mdi/home.svg?box=true", "/web_iconify_proxy/mdi/home.svg?color=blue&width=64&flip=horizontal&rotate=180", ] for url in test_cases: with self.subTest(url=url): response = self.url_open(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.headers["Content-Type"], "image/svg+xml") @patch("odoo.addons.web_iconify_proxy.controllers.main.requests.get") def test_get_svg_with_invalid_parameters(self, mock_get): """Test the get_svg route with invalid parameters.""" mock_response = requests.Response() mock_response.status_code = 200 mock_response._content = b"dummy content" mock_response.headers["Content-Type"] = "image/svg+xml" mock_get.return_value = mock_response test_cases = [ "/web_iconify_proxy/mdi/home.svg?width=invalid", # Invalid width "/web_iconify_proxy/mdi/home.svg?height=invalid", # Invalid height "/web_iconify_proxy/mdi/home.svg?rotate=invalid", # Invalid rotate "/web_iconify_proxy/mdi/home.svg?box=invalid", # Invalid box "/web_iconify_proxy/mdi/home.svg?unknown=param", # Unknown parameter ] for url in test_cases: with self.subTest(url=url): response = self.url_open(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.headers["Content-Type"], "image/svg+xml") @patch("odoo.addons.web_iconify_proxy.controllers.main.requests.get") def test_api_error(self, mock_get): # Mock requests.get to simulate an API error mock_get.side_effect = requests.exceptions.RequestException("Simulated Error") with self.assertLogs(level="ERROR") as log: response = self.url_open("/web_iconify_proxy/mdi/home.svg") self.assertEqual(response.status_code, 404) # Expect 404 Not Found self.assertIn("Simulated Error", log.output[0])