Skip to content

Commit 044d8a8

Browse files
[fix] Fixed naive boolean coercion #383
Fixes #383 --------- Co-authored-by: Federico Capoano <f.capoano@openwisp.io>
1 parent 6cc0fa1 commit 044d8a8

5 files changed

Lines changed: 38 additions & 2 deletions

File tree

netjsonconfig/backends/openwisp/templates/openwrt.jinja2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
{% for list_value in value %}
1010
list '{{ key }}' '{{ list_value }}'
1111
{% endfor %}
12+
{% elif value is boolean %}
13+
option '{{ key }}' '{{ value | int }}'
1214
{% else %}
1315
option '{{ key }}' '{{ value }}'
1416
{% endif %}

netjsonconfig/backends/openwrt/renderer.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ def cleanup(self, output):
1616
.replace("\noption", "\n\toption")
1717
.replace("\nlist", "\n\tlist")
1818
)
19-
# convert True to 1 and False to 0
20-
output = output.replace("True", "1").replace("False", "0")
2119
# max 2 consecutive \n delimiters
2220
output = output.replace("\n\n\n", "\n\n")
2321
# if output is present

netjsonconfig/backends/openwrt/templates/openwrt.jinja2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
{% for list_value in value %}
1010
list {{ key }} '{{ list_value }}'
1111
{% endfor %}
12+
{% elif value is boolean %}
13+
option {{ key }} '{{ value | int }}'
1214
{% else %}
1315
option {{ key }} '{{ value }}'
1416
{% endif %}

tests/openwisp/test_backend.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,23 @@ def test_wireless_radio_disabled_0(self):
194194
output = o.render()
195195
self.assertIn("option 'disabled' '0'", output)
196196

197+
def test_render_ssid_boolean_bug(self):
198+
"""Regression test for https://github.com/openwisp/netjsonconfig/issues/383"""
199+
ssid_values = [
200+
"TrueGait Living Guest",
201+
"FalseGait Living Guest",
202+
"MyTrueNetwork",
203+
"True",
204+
"False",
205+
]
206+
for ssid in ssid_values:
207+
with self.subTest(ssid=ssid):
208+
config = deepcopy(self.config)
209+
config["interfaces"][2]["wireless"]["ssid"] = ssid
210+
o = OpenWisp(config)
211+
output = o.render()
212+
self.assertIn(f"option 'ssid' '{ssid}'", output)
213+
197214
def test_tc_script(self):
198215
config = deepcopy(self.config)
199216
o = OpenWisp(config)

tests/openwrt/test_wireless.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ def test_render_wifi_interface(self):
6363
expected = self._tabs(self._wifi_uci)
6464
self.assertEqual(o.render(), expected)
6565

66+
def test_render_ssid_boolean_bug(self):
67+
"""Regression test for https://github.com/openwisp/netjsonconfig/issues/383"""
68+
ssid_values = [
69+
"TrueGait Living Guest",
70+
"FalseGait Living Guest",
71+
"MyTrueNetwork",
72+
"True",
73+
"False",
74+
]
75+
for ssid in ssid_values:
76+
with self.subTest(ssid=ssid):
77+
netjson = deepcopy(self._wifi_netjson)
78+
netjson["interfaces"][0]["wireless"]["ssid"] = ssid
79+
o = OpenWrt(netjson)
80+
expected_uci = self._wifi_uci.replace("'MyWifiAP'", f"'{ssid}'")
81+
self.assertEqual(o.render(), self._tabs(expected_uci))
82+
6683
def test_parse_wifi_interface(self):
6784
o = OpenWrt(native=self._wifi_uci)
6885
self.assertDictEqual(o.config, self._wifi_netjson)

0 commit comments

Comments
 (0)