test_twilio_txt_msg.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. import logging
  5. # Salt libs
  6. from salt.beacons import twilio_txt_msg
  7. # Salt testing libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.mock import MagicMock, patch
  10. from tests.support.unit import TestCase, skipIf
  11. # Import 3rd Party libs
  12. try:
  13. import twilio
  14. # Grab version, ensure elements are ints
  15. twilio_version = tuple([int(x) for x in twilio.__version_info__])
  16. if twilio_version > (5,):
  17. TWILIO_5 = False
  18. else:
  19. TWILIO_5 = True
  20. HAS_TWILIO = True
  21. except ImportError:
  22. HAS_TWILIO = False
  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. class TwilioMsgTxtBeaconTestCase(TestCase, LoaderModuleMockMixin):
  89. """
  90. Test case for salt.beacons.twilio_txt_msg
  91. """
  92. def setup_loader_modules(self):
  93. return {twilio_txt_msg: {}}
  94. def test_validate_dictionary_config(self):
  95. """
  96. Test empty configuration
  97. """
  98. config = {}
  99. ret = twilio_txt_msg.validate(config)
  100. self.assertEqual(
  101. ret, (False, ("Configuration for twilio_txt_msg beacon must be a list."))
  102. )
  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(
  110. ret,
  111. (
  112. False,
  113. (
  114. "Configuration for twilio_txt_msg "
  115. "beacon must contain account_sid, "
  116. "auth_token and twilio_number items."
  117. ),
  118. ),
  119. )
  120. def test_validate_missing_config_item(self):
  121. """
  122. Test empty configuration
  123. """
  124. config = [
  125. {
  126. "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  127. "twilio_number": "+15555555555",
  128. }
  129. ]
  130. ret = twilio_txt_msg.validate(config)
  131. self.assertEqual(
  132. ret,
  133. (
  134. False,
  135. (
  136. "Configuration for twilio_txt_msg "
  137. "beacon must contain account_sid, "
  138. "auth_token and twilio_number items."
  139. ),
  140. ),
  141. )
  142. def test_receive_message(self):
  143. """
  144. Test receive a message
  145. """
  146. config = [
  147. {
  148. "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  149. "auth_token": "my_token",
  150. "twilio_number": "+15555555555",
  151. }
  152. ]
  153. ret = twilio_txt_msg.validate(config)
  154. self.assertEqual(ret, (True, "Valid beacon configuration"))
  155. _expected_return = [
  156. {
  157. "texts": [
  158. {
  159. "body": "None",
  160. "images": [],
  161. "from": "None",
  162. "id": "011",
  163. "sent": "01-01-2015",
  164. }
  165. ]
  166. }
  167. ]
  168. mock = MagicMock(return_value=MockTwilioRestClient())
  169. with patch.object(twilio_txt_msg, "TwilioRestClient", mock):
  170. ret = twilio_txt_msg.beacon(config)
  171. self.assertEqual(ret, _expected_return)