test_actor_base_device_info.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # switchbot-mqtt - MQTT client controlling SwitchBot button & curtain automators,
  2. # compatible with home-assistant.io's MQTT Switch & Cover platform
  3. #
  4. # Copyright (C) 2021 Fabian Peter Hammerle <fabian@hammerle.me>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. import os
  19. import re
  20. import unittest.mock
  21. import bluepy.btle
  22. import pytest
  23. import switchbot_mqtt
  24. # pylint: disable=protected-access
  25. _LE_ON_PERMISSION_DENIED_ERROR = bluepy.btle.BTLEManagementError(
  26. "Failed to execute management command 'le on'",
  27. {
  28. "rsp": ["mgmt"],
  29. "code": ["mgmterr"],
  30. "estat": [20],
  31. "emsg": ["Permission Denied"],
  32. },
  33. )
  34. @pytest.mark.parametrize(
  35. "actor_class", [switchbot_mqtt._CurtainMotor, switchbot_mqtt._ButtonAutomator]
  36. )
  37. def test__update_device_info_le_on_permission_denied_log(
  38. actor_class,
  39. ): # pySwitchbot>=v0.10.0
  40. actor = actor_class(mac_address="dummy", retry_count=21, password=None)
  41. with unittest.mock.patch(
  42. "bluepy.btle.Scanner.scan",
  43. side_effect=_LE_ON_PERMISSION_DENIED_ERROR,
  44. ), pytest.raises(PermissionError) as exc_info:
  45. actor._update_device_info()
  46. assert "sudo setcap cap_net_admin+ep /" in exc_info.exconly()
  47. assert exc_info.value.__cause__ == _LE_ON_PERMISSION_DENIED_ERROR
  48. @pytest.mark.parametrize(
  49. "actor_class", [switchbot_mqtt._CurtainMotor, switchbot_mqtt._ButtonAutomator]
  50. )
  51. def test__update_device_info_le_on_permission_denied_exc(
  52. actor_class,
  53. ): # pySwitchbot<v0.10.1
  54. actor = actor_class(mac_address="dummy", retry_count=21, password=None)
  55. with unittest.mock.patch.object(
  56. actor._get_device(),
  57. "update",
  58. side_effect=_LE_ON_PERMISSION_DENIED_ERROR,
  59. ) as update_mock, pytest.raises(PermissionError) as exc_info:
  60. actor._update_device_info()
  61. update_mock.assert_called_once_with()
  62. assert os.path.isfile(
  63. re.search(
  64. r"sudo setcap cap_net_admin\+ep (\S+/bluepy-helper)\b",
  65. exc_info.exconly(),
  66. ).group(1)
  67. )
  68. assert exc_info.value.__cause__ == _LE_ON_PERMISSION_DENIED_ERROR
  69. @pytest.mark.parametrize(
  70. "actor_class", [switchbot_mqtt._CurtainMotor, switchbot_mqtt._ButtonAutomator]
  71. )
  72. def test__update_device_info_other_error(actor_class):
  73. actor = actor_class(mac_address="dummy", retry_count=21, password=None)
  74. side_effect = bluepy.btle.BTLEManagementError("test")
  75. with unittest.mock.patch.object(
  76. actor._get_device(), "update", side_effect=side_effect
  77. ) as update_mock, pytest.raises(type(side_effect)) as exc_info:
  78. actor._update_device_info()
  79. update_mock.assert_called_once_with()
  80. assert exc_info.value == side_effect