1
0

test_twilio_txt_msg.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. # Salt libs
  5. from salt.beacons import twilio_txt_msg
  6. # Salt testing libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.unit import TestCase, skipIf
  9. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
  10. # Import 3rd Party libs
  11. try:
  12. import twilio
  13. # Grab version, ensure elements are ints
  14. twilio_version = tuple([int(x) for x in twilio.__version_info__])
  15. if twilio_version > (5, ):
  16. TWILIO_5 = False
  17. else:
  18. TWILIO_5 = True
  19. HAS_TWILIO = True
  20. except ImportError:
  21. HAS_TWILIO = False
  22. import logging
  23. log = logging.getLogger(__name__)
  24. class MockTwilioRestException(Exception):
  25. '''
  26. Mock TwilioRestException class
  27. '''
  28. def __init__(self):
  29. self.code = 'error code'
  30. self.msg = 'Exception error'
  31. self.status = 'Not send'
  32. super(MockTwilioRestException, self).__init__(self.msg)
  33. class MockMessages(object):
  34. '''
  35. Mock SMS class
  36. '''
  37. flag = None
  38. def __init__(self):
  39. self.sid = '011'
  40. self.price = '200'
  41. self.price_unit = '1'
  42. self.status = 'Sent'
  43. self.num_segments = '2'
  44. self.num_media = '0'
  45. self.body = None
  46. self.date_sent = '01-01-2015'
  47. self.date_created = '01-01-2015'
  48. self.to = None
  49. self.from_ = None
  50. def create(self, body, to, from_):
  51. '''
  52. Mock create method
  53. '''
  54. msg = MockMessages()
  55. if self.flag == 1:
  56. raise MockTwilioRestException()
  57. msg.body = body
  58. msg.to = to
  59. msg.from_ = from_
  60. return msg
  61. def list(self, to):
  62. '''
  63. Mock list method
  64. '''
  65. msg = MockMessages()
  66. return [msg]
  67. def delete(self):
  68. '''
  69. Mock delete method
  70. '''
  71. return None
  72. class MockSMS(object):
  73. '''
  74. Mock SMS class
  75. '''
  76. def __init__(self):
  77. self.messages = MockMessages()
  78. class MockTwilioRestClient(object):
  79. '''
  80. Mock TwilioRestClient class
  81. '''
  82. def __init__(self):
  83. if TWILIO_5:
  84. self.sms = MockSMS()
  85. else:
  86. self.messages = MockMessages()
  87. @skipIf(not HAS_TWILIO, 'twilio.rest is not available')
  88. @skipIf(NO_MOCK, NO_MOCK_REASON)
  89. class TwilioMsgTxtBeaconTestCase(TestCase, LoaderModuleMockMixin):
  90. '''
  91. Test case for salt.beacons.twilio_txt_msg
  92. '''
  93. def setup_loader_modules(self):
  94. return {twilio_txt_msg: {}}
  95. def test_validate_dictionary_config(self):
  96. '''
  97. Test empty configuration
  98. '''
  99. config = {}
  100. ret = twilio_txt_msg.validate(config)
  101. self.assertEqual(ret, (False, ('Configuration for twilio_txt_msg '
  102. 'beacon must be a list.')))
  103. def test_validate_empty_config(self):
  104. '''
  105. Test empty configuration
  106. '''
  107. config = [{}]
  108. ret = twilio_txt_msg.validate(config)
  109. self.assertEqual(ret, (False, ('Configuration for twilio_txt_msg '
  110. 'beacon must contain account_sid, '
  111. 'auth_token and twilio_number items.')))
  112. def test_validate_missing_config_item(self):
  113. '''
  114. Test empty configuration
  115. '''
  116. config = [{'account_sid': 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  117. 'twilio_number': '+15555555555'}]
  118. ret = twilio_txt_msg.validate(config)
  119. self.assertEqual(ret, (False, ('Configuration for twilio_txt_msg '
  120. 'beacon must contain account_sid, '
  121. 'auth_token and twilio_number items.')))
  122. def test_receive_message(self):
  123. '''
  124. Test receive a message
  125. '''
  126. config = [{'account_sid': 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  127. 'auth_token': 'my_token',
  128. 'twilio_number': '+15555555555'}]
  129. ret = twilio_txt_msg.validate(config)
  130. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  131. _expected_return = [{'texts': [{'body': 'None',
  132. 'images': [],
  133. 'from': 'None',
  134. 'id': '011',
  135. 'sent': '01-01-2015'}]}]
  136. mock = MagicMock(return_value=MockTwilioRestClient())
  137. with patch.object(twilio_txt_msg, 'TwilioRestClient', mock):
  138. ret = twilio_txt_msg.beacon(config)
  139. self.assertEqual(ret, _expected_return)