test_mac_assistive.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Libs
  5. import salt.states.mac_assistive as assistive
  6. # Import Salt Testing Libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, patch
  9. from tests.support.unit import TestCase
  10. class AssistiveTestCase(TestCase, LoaderModuleMockMixin):
  11. def setup_loader_modules(self):
  12. return {assistive: {}}
  13. def test_installed(self):
  14. """
  15. Test installing a bundle ID as being allowed to run with assistive access
  16. """
  17. expected = {
  18. "changes": {},
  19. "comment": "Installed com.apple.Chess into the assistive access panel",
  20. "name": "com.apple.Chess",
  21. "result": True,
  22. }
  23. installed_mock = MagicMock(return_value=False)
  24. install_mock = MagicMock()
  25. with patch.dict(
  26. assistive.__salt__,
  27. {"assistive.installed": installed_mock, "assistive.install": install_mock},
  28. ):
  29. out = assistive.installed("com.apple.Chess")
  30. installed_mock.assert_called_once_with("com.apple.Chess")
  31. install_mock.assert_called_once_with("com.apple.Chess", True)
  32. self.assertEqual(out, expected)
  33. def test_installed_not_enabled(self):
  34. """
  35. Test installing a bundle ID as being allowed to run with assistive access
  36. """
  37. expected = {
  38. "changes": {},
  39. "comment": "Updated enable to True",
  40. "name": "com.apple.Chess",
  41. "result": True,
  42. }
  43. installed_mock = MagicMock(return_value=True)
  44. install_mock = MagicMock()
  45. enabled_mock = MagicMock(return_value=False)
  46. enable_mock = MagicMock()
  47. with patch.dict(
  48. assistive.__salt__,
  49. {
  50. "assistive.installed": installed_mock,
  51. "assistive.install": install_mock,
  52. "assistive.enabled": enabled_mock,
  53. "assistive.enable": enable_mock,
  54. },
  55. ):
  56. out = assistive.installed("com.apple.Chess")
  57. enabled_mock.assert_called_once_with("com.apple.Chess")
  58. enable_mock.assert_called_once_with("com.apple.Chess", True)
  59. assert not install_mock.called
  60. self.assertEqual(out, expected)
  61. def test_installed_enabled(self):
  62. """
  63. Test enabling an already enabled bundle ID
  64. """
  65. expected = {
  66. "changes": {},
  67. "comment": "Already in the correct state",
  68. "name": "com.apple.Chess",
  69. "result": True,
  70. }
  71. installed_mock = MagicMock(return_value=True)
  72. install_mock = MagicMock()
  73. enabled_mock = MagicMock(return_value=True)
  74. enable_mock = MagicMock()
  75. with patch.dict(
  76. assistive.__salt__,
  77. {
  78. "assistive.installed": installed_mock,
  79. "assistive.install": install_mock,
  80. "assistive.enabled": enabled_mock,
  81. "assistive.enable": enable_mock,
  82. },
  83. ):
  84. out = assistive.installed("com.apple.Chess")
  85. enabled_mock.assert_called_once_with("com.apple.Chess")
  86. assert not enable_mock.called
  87. assert not install_mock.called
  88. self.assertEqual(out, expected)
  89. def test_installed_not_disabled(self):
  90. """
  91. Test disabling an enabled and installed bundle ID
  92. """
  93. expected = {
  94. "changes": {},
  95. "comment": "Updated enable to False",
  96. "name": "com.apple.Chess",
  97. "result": True,
  98. }
  99. installed_mock = MagicMock(return_value=True)
  100. install_mock = MagicMock()
  101. enabled_mock = MagicMock(return_value=True)
  102. enable_mock = MagicMock()
  103. with patch.dict(
  104. assistive.__salt__,
  105. {
  106. "assistive.installed": installed_mock,
  107. "assistive.install": install_mock,
  108. "assistive.enabled": enabled_mock,
  109. "assistive.enable": enable_mock,
  110. },
  111. ):
  112. out = assistive.installed("com.apple.Chess", False)
  113. enabled_mock.assert_called_once_with("com.apple.Chess")
  114. enable_mock.assert_called_once_with("com.apple.Chess", False)
  115. assert not install_mock.called
  116. self.assertEqual(out, expected)