1
0

test_twilio_txt_msg.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 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. 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(ret, (False, ('Configuration for twilio_txt_msg '
  101. 'beacon must be a list.')))
  102. def test_validate_empty_config(self):
  103. '''
  104. Test empty configuration
  105. '''
  106. config = [{}]
  107. ret = twilio_txt_msg.validate(config)
  108. self.assertEqual(ret, (False, ('Configuration for twilio_txt_msg '
  109. 'beacon must contain account_sid, '
  110. 'auth_token and twilio_number items.')))
  111. def test_validate_missing_config_item(self):
  112. '''
  113. Test empty configuration
  114. '''
  115. config = [{'account_sid': 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  116. 'twilio_number': '+15555555555'}]
  117. ret = twilio_txt_msg.validate(config)
  118. self.assertEqual(ret, (False, ('Configuration for twilio_txt_msg '
  119. 'beacon must contain account_sid, '
  120. 'auth_token and twilio_number items.')))
  121. def test_receive_message(self):
  122. '''
  123. Test receive a message
  124. '''
  125. config = [{'account_sid': 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  126. 'auth_token': 'my_token',
  127. 'twilio_number': '+15555555555'}]
  128. ret = twilio_txt_msg.validate(config)
  129. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  130. _expected_return = [{'texts': [{'body': 'None',
  131. 'images': [],
  132. 'from': 'None',
  133. 'id': '011',
  134. 'sent': '01-01-2015'}]}]
  135. mock = MagicMock(return_value=MockTwilioRestClient())
  136. with patch.object(twilio_txt_msg, 'TwilioRestClient', mock):
  137. ret = twilio_txt_msg.beacon(config)
  138. self.assertEqual(ret, _expected_return)