[publicip] fix tests and bugs

This commit is contained in:
tobi-wan-kenobi 2022-07-29 14:00:28 +02:00
parent 61ebc3aea6
commit a78403d3e8
2 changed files with 61 additions and 55 deletions

View file

@ -38,19 +38,45 @@ import util.format
import util.location
def update_publicip_information(module):
widget = module.widget()
__previous_default_route = None
__current_default_route = None
__interval = 5 # Interval between default route change checks
class Module(core.module.Module):
@core.decorators.every(minutes=60)
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.publicip))
while True:
__current_default_route = netifaces.gateways()["default"][2]
self.__previous_default_route = None
self.__current_default_route = None
self.background = True
# Immediate update (override default) when left click on widget
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__click_update)
# By default show: <ip> (<2 letter country code>)
self._format = self.parameter("format", "{ip} ({country_code})")
def publicip(self, widget):
if widget.get("public_ip") == None:
return "n/a"
return self._format.format(
ip=widget.get("public_ip", "-"),
country_name=widget.get("country_name", "-"),
country_code=widget.get("country_code", "-"),
city_name=widget.get("city_name", "-"),
coordinates=widget.get("coordinates", "-"),
)
def __click_update(self, event):
util.location.reset()
def update(self):
widget = self.widget()
self.__current_default_route = netifaces.gateways()["default"][2]
try:
# Updates public ip information if a change to default route is detected
if __current_default_route != __previous_default_route:
if self.__current_default_route != self.__previous_default_route:
# Sets __previous_default_route in preparation for next change check
__previous_default_route = __current_default_route
self.__previous_default_route = self.__current_default_route
# Refresh location information
util.location.reset()
@ -72,42 +98,8 @@ def update_publicip_information(module):
# Update widget values
core.event.trigger("update", [widget.module.id], redraw_only=True)
# Wait __interval seconds before checking for default route changes again
time.sleep(__interval)
class Module(core.module.Module):
@core.decorators.every(minutes=60)
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.publicip))
self.__thread = None
# Immediate update (override default) when left click on widget
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__click_update)
# By default show: <ip> (<2 letter country code>)
self._format = self.parameter("format", "{ip} ({country_code})")
def publicip(self, widget):
return self._format.format(
ip=widget.get("public_ip", "-"),
country_name=widget.get("country_name", "-"),
country_code=widget.get("country_code", "-"),
city_name=widget.get("city_name", "-"),
coordinates=widget.get("coordinates", "-"),
)
def __click_update(self, event):
util.location.reset()
def update(self):
if self.__thread is not None and self.__thread.is_alive():
return
self.__thread = threading.Thread(
target=update_publicip_information, args=(self,)
)
self.__thread.start()
except:
widget.set("public_ip", None)
def state(self, widget):
return widget.get("state", None)

View file

@ -17,27 +17,41 @@ class PublicIPTest(TestCase):
def test_load_module(self):
__import__("modules.contrib.publicip")
@mock.patch('util.location.public_ip')
def test_public_ip(self, public_ip_mock):
public_ip_mock.return_value = '5.12.220.2'
@mock.patch('util.location.location_info')
def test_public_ip(self, location_mock):
location_mock.return_value = {
'public_ip': '5.12.220.2',
'latitude': 0,
'longitude': 2,
'country': 'some country',
'country_code': 'sc',
'city_name': '???',
}
module = build_module()
module.update()
assert widget(module).full_text() == '5.12.220.2'
assert widget(module).full_text() == '5.12.220.2 (sc)'
@mock.patch('util.location.public_ip')
def test_public_ip(self, public_ip_mock):
public_ip_mock.return_value = None
@mock.patch('util.location.location_info')
def test_public_ip2(self, location_mock):
location_mock.return_value = {
'public_ip': None,
'latitude': 0,
'longitude': 2,
'country': 'some country',
'country_code': 'sc',
'city_name': '???',
}
module = build_module()
module.update()
assert widget(module).full_text() == 'n/a'
@mock.patch('util.location.public_ip')
def test_public_ip_with_exception(self, public_ip_mock):
public_ip_mock.side_effect = Exception
@mock.patch('util.location.location_info')
def test_public_ip_with_exception(self, location_mock):
location_mock.side_effect = Exception
module = build_module()
module.update()