1
+ import base64
1
2
from unittest .mock import patch
2
3
3
4
import requests
8
9
9
10
@tagged ("post_install" , "-at_install" )
10
11
class TestIconifyProxyController (HttpCase ):
11
- def test_get_svg_success (self ):
12
+ @patch ("odoo.addons.web_iconify_proxy.controllers.main.requests.get" )
13
+ def test_get_svg_success (self , mock_get ):
14
+ mock_response = requests .Response ()
15
+ mock_response .status_code = 200
16
+ mock_response ._content = b"<svg>dummy content</svg>"
17
+ mock_response .headers ["Content-Type" ] = "image/svg+xml"
18
+ mock_get .return_value = mock_response
19
+
12
20
response = self .url_open ("/web_iconify_proxy/mdi/home.svg" )
13
21
self .assertEqual (response .status_code , 200 )
14
22
self .assertEqual (response .headers ["Content-Type" ], "image/svg+xml" )
15
23
# Add basic check for SVG content (can be improved)
16
24
self .assertTrue (b"<svg" in response .content )
17
25
18
- def test_get_css_success (self ):
26
+ @patch ("odoo.addons.web_iconify_proxy.controllers.main.requests.get" )
27
+ def test_get_css_success (self , mock_get ):
28
+ mock_response = requests .Response ()
29
+ mock_response .status_code = 200
30
+ mock_response ._content = b".iconify { color: red; }"
31
+ mock_response .headers ["Content-Type" ] = "text/css"
32
+ mock_get .return_value = mock_response
33
+
19
34
response = self .url_open ("/web_iconify_proxy/mdi.css?icons=home,account" )
20
35
self .assertEqual (response .status_code , 200 )
21
36
self .assertEqual (response .headers ["Content-Type" ], "text/css" )
22
37
# Add basic check for CSS content
23
38
self .assertTrue (b".iconify" in response .content )
24
39
25
- def test_get_json_success (self ):
40
+ @patch ("odoo.addons.web_iconify_proxy.controllers.main.requests.get" )
41
+ def test_get_json_success (self , mock_get ):
42
+ mock_response = requests .Response ()
43
+ mock_response .status_code = 200
44
+ mock_response ._content = b'{"prefix": "mdi", "icons": {"home": {}}}'
45
+ mock_response .headers ["Content-Type" ] = "application/json"
46
+ mock_get .return_value = mock_response
47
+
26
48
response = self .url_open ("/web_iconify_proxy/mdi.json?icons=home,account" )
27
49
self .assertEqual (response .status_code , 200 )
28
50
self .assertEqual (response .headers ["Content-Type" ], "application/json" )
@@ -46,6 +68,21 @@ def test_get_json_invalid_icons(self):
46
68
self .assertEqual (response .status_code , 404 )
47
69
48
70
def test_get_last_modified (self ):
71
+ # Create a dummy attachment
72
+ attachment = (
73
+ self .env ["ir.attachment" ]
74
+ .sudo ()
75
+ .create (
76
+ {
77
+ "name" : "mdi-test" ,
78
+ "datas" : base64 .b64encode (b"dummy data" ).decode ("utf-8" ),
79
+ "res_model" : "iconify.svg" ,
80
+ "res_id" : 0 ,
81
+ "type" : "binary" ,
82
+ }
83
+ )
84
+ )
85
+
49
86
response = self .url_open ("/web_iconify_proxy/last-modified?prefixes=mdi" )
50
87
self .assertEqual (response .status_code , 200 )
51
88
self .assertEqual (response .headers ["Content-Type" ], "application/json" )
@@ -55,6 +92,9 @@ def test_get_last_modified(self):
55
92
except ValueError :
56
93
self .fail ("last-modified did not return a valid timestamp" )
57
94
95
+ # Clean up the attachment
96
+ attachment .unlink ()
97
+
58
98
def test_get_last_modified_no_prefix (self ):
59
99
response = self .url_open ("/web_iconify_proxy/last-modified" )
60
100
self .assertEqual (response .status_code , 404 )
@@ -93,5 +133,7 @@ def test_caching(self, mock_get):
93
133
def test_api_error (self , mock_get ):
94
134
# Mock requests.get to simulate an API error
95
135
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 )
136
+ with self .assertLogs (level = "ERROR" ) as log :
137
+ response = self .url_open ("/web_iconify_proxy/mdi/home.svg" )
138
+ self .assertEqual (response .status_code , 404 ) # Expect 404 Not Found
139
+ self .assertIn ("Simulated Error" , log .output [0 ])
0 commit comments