test_mac_assistive.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. # Import Salt Testing Libs
  5. from tests.support.mixins import LoaderModuleMockMixin
  6. from tests.support.unit import TestCase
  7. from tests.support.mock import (
  8. MagicMock,
  9. patch
  10. )
  11. # Import Salt Libs
  12. from salt.exceptions import CommandExecutionError
  13. import salt.modules.mac_assistive as assistive
  14. class AssistiveTestCase(TestCase, LoaderModuleMockMixin):
  15. def setup_loader_modules(self):
  16. return {assistive: {}}
  17. def test_install_assistive_bundle(self):
  18. '''
  19. Test installing a bundle ID as being allowed to run with assistive access
  20. '''
  21. mock_ret = MagicMock(return_value={'retcode': 0})
  22. with patch.dict(assistive.__salt__, {'cmd.run_all': mock_ret}):
  23. with patch.dict(assistive.__grains__, {'osrelease': '10.11.3'}):
  24. self.assertTrue(assistive.install('foo'))
  25. def test_install_assistive_error(self):
  26. '''
  27. Test installing a bundle ID as being allowed to run with assistive access
  28. '''
  29. mock_ret = MagicMock(return_value={'retcode': 1})
  30. with patch.dict(assistive.__salt__, {'cmd.run_all': mock_ret}):
  31. with patch.dict(assistive.__grains__, {'osrelease': '10.11.3'}):
  32. self.assertRaises(CommandExecutionError, assistive.install, 'foo')
  33. def test_installed_bundle(self):
  34. '''
  35. Test checking to see if a bundle id is installed as being able to use assistive access
  36. '''
  37. with patch('salt.modules.mac_assistive._get_assistive_access',
  38. MagicMock(return_value=[('foo', 0)])):
  39. self.assertTrue(assistive.installed('foo'))
  40. def test_installed_bundle_not(self):
  41. '''
  42. Test checking to see if a bundle id is installed as being able to use assistive access
  43. '''
  44. with patch('salt.modules.mac_assistive._get_assistive_access',
  45. MagicMock(return_value=[])):
  46. self.assertFalse(assistive.installed('foo'))
  47. def test_enable_assistive(self):
  48. '''
  49. Test enabling a bundle ID as being allowed to run with assistive access
  50. '''
  51. mock_ret = MagicMock(return_value={'retcode': 0})
  52. with patch.dict(assistive.__salt__, {'cmd.run_all': mock_ret}), \
  53. patch('salt.modules.mac_assistive._get_assistive_access',
  54. MagicMock(return_value=[('foo', 0)])):
  55. self.assertTrue(assistive.enable('foo', True))
  56. def test_enable_error(self):
  57. '''
  58. Test enabled a bundle ID that throws a command error
  59. '''
  60. mock_ret = MagicMock(return_value={'retcode': 1})
  61. with patch.dict(assistive.__salt__, {'cmd.run_all': mock_ret}), \
  62. patch('salt.modules.mac_assistive._get_assistive_access',
  63. MagicMock(return_value=[('foo', 0)])):
  64. self.assertRaises(CommandExecutionError,
  65. assistive.enable,
  66. 'foo')
  67. def test_enable_false(self):
  68. '''
  69. Test return of enable function when app isn't found.
  70. '''
  71. with patch('salt.modules.mac_assistive._get_assistive_access',
  72. MagicMock(return_value=[])):
  73. self.assertFalse(assistive.enable('foo'))
  74. def test_enabled_assistive(self):
  75. '''
  76. Test enabling a bundle ID as being allowed to run with assistive access
  77. '''
  78. with patch('salt.modules.mac_assistive._get_assistive_access',
  79. MagicMock(return_value=[('foo', '1')])):
  80. self.assertTrue(assistive.enabled('foo'))
  81. def test_enabled_assistive_false(self):
  82. '''
  83. Test if a bundle ID is disabled for assistive access
  84. '''
  85. with patch('salt.modules.mac_assistive._get_assistive_access',
  86. MagicMock(return_value=[])):
  87. self.assertFalse(assistive.enabled('foo'))
  88. def test_remove_assistive(self):
  89. '''
  90. Test removing an assitive bundle.
  91. '''
  92. mock_ret = MagicMock(return_value={'retcode': 0})
  93. with patch.dict(assistive.__salt__, {'cmd.run_all': mock_ret}):
  94. self.assertTrue(assistive.remove('foo'))
  95. def test_remove_assistive_error(self):
  96. '''
  97. Test removing an assitive bundle.
  98. '''
  99. mock_ret = MagicMock(return_value={'retcode': 1})
  100. with patch.dict(assistive.__salt__, {'cmd.run_all': mock_ret}):
  101. self.assertRaises(CommandExecutionError,
  102. assistive.remove,
  103. 'foo')
  104. def test_get_assistive_access(self):
  105. '''
  106. Test if a bundle ID is enabled for assistive access
  107. '''
  108. mock_out = 'kTCCServiceAccessibility|/bin/bash|1|1|1|\n' \
  109. 'kTCCServiceAccessibility|/usr/bin/osascript|1|1|1|'
  110. mock_ret = MagicMock(return_value={'retcode': 0, 'stdout': mock_out})
  111. expected = [('/bin/bash', '1'), ('/usr/bin/osascript', '1')]
  112. with patch.dict(assistive.__salt__, {'cmd.run_all': mock_ret}):
  113. self.assertEqual(assistive._get_assistive_access(), expected)
  114. def test_get_assistive_access_error(self):
  115. '''
  116. Test a CommandExecutionError is raised when something goes wrong.
  117. '''
  118. mock_ret = MagicMock(return_value={'retcode': 1})
  119. with patch.dict(assistive.__salt__, {'cmd.run_all': mock_ret}):
  120. self.assertRaises(CommandExecutionError,
  121. assistive._get_assistive_access)