Skip to content

Commit

Permalink
initial release web_iconify
Browse files Browse the repository at this point in the history
  • Loading branch information
jacodottech committed Feb 23, 2025
1 parent 6c72295 commit 4c4a5e1
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions web_iconify_proxy/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_main
97 changes: 97 additions & 0 deletions web_iconify_proxy/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from unittest.mock import patch

import requests

from odoo.tests import tagged
from odoo.tests.common import HttpCase


@tagged("post_install", "-at_install")
class TestIconifyProxyController(HttpCase):
def test_get_svg_success(self):
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"<svg" in response.content)

def test_get_css_success(self):
response = self.url_open("/web_iconify_proxy/mdi.css?icons=home,account")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], "text/css")
# Add basic check for CSS content
self.assertTrue(b".iconify" in response.content)

def test_get_json_success(self):
response = self.url_open("/web_iconify_proxy/mdi.json?icons=home,account")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], "application/json")
# Add basic check for JSON content (can be improved)
self.assertTrue(b'{"prefix":' in response.content)

def test_get_svg_invalid_prefix(self):
response = self.url_open("/web_iconify_proxy/invalid!prefix/home.svg")
self.assertEqual(response.status_code, 404)

def test_get_svg_invalid_icon(self):
response = self.url_open("/web_iconify_proxy/mdi/invalid!icon.svg")
self.assertEqual(response.status_code, 404)

def test_get_css_invalid_icons(self):
response = self.url_open("/web_iconify_proxy/mdi.css?icons=home,invalid!icon")
self.assertEqual(response.status_code, 404)

def test_get_json_invalid_icons(self):
response = self.url_open("/web_iconify_proxy/mdi.json?icons=home,invalid!icon")
self.assertEqual(response.status_code, 404)

def test_get_last_modified(self):
response = self.url_open("/web_iconify_proxy/last-modified?prefixes=mdi")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], "application/json")
# Check if the response is a valid timestamp
try:
float(response.content)
except ValueError:
self.fail("last-modified did not return a valid timestamp")

def test_get_last_modified_no_prefix(self):
response = self.url_open("/web_iconify_proxy/last-modified")
self.assertEqual(response.status_code, 404)

def test_get_css_no_icons(self):
response = self.url_open("/web_iconify_proxy/mdi.css")
self.assertEqual(response.status_code, 404)

def test_get_json_no_icons(self):
response = self.url_open("/web_iconify_proxy/mdi.json")
self.assertEqual(response.status_code, 404)

@patch("odoo.addons.web_iconify_proxy.controllers.main.requests.get")
def test_caching(self, mock_get):
# Mock the requests.get method to return a dummy response
mock_response = requests.Response()
mock_response.status_code = 200
mock_response._content = b"<svg>dummy content</svg>"
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_api_error(self, mock_get):
# Mock requests.get to simulate an API error
mock_get.side_effect = requests.exceptions.RequestException("Simulated Error")
response = self.url_open("/web_iconify_proxy/mdi/home.svg")
self.assertEqual(response.status_code, 404)

0 comments on commit 4c4a5e1

Please sign in to comment.