Browse Source

adapt log message shown when dbus method call fails due to authorization failure

Fabian Peter Hammerle 3 years ago
parent
commit
6f2f541aa3
2 changed files with 20 additions and 4 deletions
  1. 8 1
      systemctl_mqtt/__init__.py
  2. 12 3
      tests/test_dbus.py

+ 8 - 1
systemctl_mqtt/__init__.py

@@ -55,7 +55,14 @@ def _schedule_shutdown(action: str) -> None:
     try:
         _get_login_manager().ScheduleShutdown(action, shutdown_epoch_usec)
     except dbus.DBusException as exc:
-        _LOGGER.error("failed to schedule %s: %s", action, exc.get_dbus_message())
+        exc_msg = exc.get_dbus_message()
+        if "authentication required" in exc_msg.lower():
+            _LOGGER.error(
+                "failed to schedule %s: unauthorized; missing polkit authorization rules?",
+                action,
+            )
+        else:
+            _LOGGER.error("failed to schedule %s: %s", action, exc_msg)
 
 
 _MQTT_TOPIC_SUFFIX_ACTION_MAPPING = {

+ 12 - 3
tests/test_dbus.py

@@ -59,8 +59,17 @@ def test__schedule_shutdown(action):
 
 
 @pytest.mark.parametrize("action", ["poweroff"])
-@pytest.mark.parametrize("exception_message", ["test message"])
-def test__schedule_shutdown_fail(caplog, action, exception_message):
+@pytest.mark.parametrize(
+    ("exception_message", "log_message"),
+    [
+        ("test message", "test message"),
+        (
+            "Interactive authentication required.",
+            "unauthorized; missing polkit authorization rules?",
+        ),
+    ],
+)
+def test__schedule_shutdown_fail(caplog, action, exception_message, log_message):
     login_manager_mock = unittest.mock.MagicMock()
     login_manager_mock.ScheduleShutdown.side_effect = dbus.DBusException(
         exception_message
@@ -75,7 +84,7 @@ def test__schedule_shutdown_fail(caplog, action, exception_message):
     assert caplog.records[0].message.startswith("scheduling {} for ".format(action))
     assert caplog.records[1].levelno == logging.ERROR
     assert caplog.records[1].message == "failed to schedule {}: {}".format(
-        action, exception_message
+        action, log_message
     )