|
| 1 | +""" |
| 2 | +api_tests.py |
| 3 | +
|
| 4 | +Integration tests for API requests |
| 5 | +
|
| 6 | +@copyright Bandwidth INC |
| 7 | +""" |
| 8 | +from bandwidth.bandwidth_client import BandwidthClient |
| 9 | +from bandwidth.messaging.exceptions.messaging_exception import MessagingException |
| 10 | +from bandwidth.voice.exceptions.api_error_response_exception import ApiErrorResponseException |
| 11 | +from bandwidth.messaging.models.message_request import MessageRequest |
| 12 | +from bandwidth.voice.models.api_create_call_request import ApiCreateCallRequest |
| 13 | +from bandwidth.twofactorauth.models.two_factor_code_request_schema import TwoFactorCodeRequestSchema |
| 14 | +from bandwidth.twofactorauth.models.two_factor_verify_request_schema import TwoFactorVerifyRequestSchema |
| 15 | + |
| 16 | +import unittest |
| 17 | + |
| 18 | +import os |
| 19 | + |
| 20 | +try: |
| 21 | + USERNAME = os.environ["USERNAME"] |
| 22 | + PASSWORD = os.environ["PASSWORD"] |
| 23 | + ACCOUNT_ID = os.environ["ACCOUNT_ID"] |
| 24 | + VOICE_APPLICATION_ID = os.environ["VOICE_APPLICATION_ID"] |
| 25 | + MESSAGING_APPLICATION_ID = os.environ["MESSAGING_APPLICATION_ID"] |
| 26 | + CALLBACK_URL = os.environ["CALLBACK_URL"] |
| 27 | + PHONE_NUMBER_OUTBOUND = os.environ["PHONE_NUMBER_OUTBOUND"] |
| 28 | + PHONE_NUMBER_INBOUND = os.environ["PHONE_NUMBER_INBOUND"] |
| 29 | + MFA_MESSAGING_APPLICATION_ID = os.environ["MFA_MESSAGING_APPLICATION_ID"] |
| 30 | + MFA_VOICE_APPLICATION_ID = os.environ["MFA_VOICE_APPLICATION_ID"] |
| 31 | + PHONE_NUMBER_MFA = os.environ["PHONE_NUMBER_MFA"] |
| 32 | +except: |
| 33 | + raise Exception("Environmental variables not found") |
| 34 | + |
| 35 | +class MonitorTest(unittest.TestCase): |
| 36 | + """ |
| 37 | + Class that holds basic monitoring tests for the Python SDK. Makes requests to cover JSON call and response, |
| 38 | + error handling, and binary string uploads and downloads |
| 39 | + """ |
| 40 | + def setUp(self): |
| 41 | + """ |
| 42 | + Creates the client object |
| 43 | + """ |
| 44 | + self.bandwidth_client = BandwidthClient( |
| 45 | + voice_basic_auth_user_name=USERNAME, |
| 46 | + voice_basic_auth_password=PASSWORD, |
| 47 | + messaging_basic_auth_user_name=USERNAME, |
| 48 | + messaging_basic_auth_password=PASSWORD, |
| 49 | + two_factor_auth_basic_auth_user_name=USERNAME, |
| 50 | + two_factor_auth_basic_auth_password=PASSWORD |
| 51 | + ) |
| 52 | + self.voice_client = self.bandwidth_client.voice_client.client |
| 53 | + self.messaging_client = self.bandwidth_client.messaging_client.client |
| 54 | + self.auth_client = self.bandwidth_client.two_factor_auth_client.mfa |
| 55 | + |
| 56 | + def test_create_message(self): |
| 57 | + body = MessageRequest() |
| 58 | + body.application_id = MESSAGING_APPLICATION_ID |
| 59 | + body.to = [PHONE_NUMBER_INBOUND] |
| 60 | + body.mfrom = PHONE_NUMBER_OUTBOUND |
| 61 | + body.text = "Python Monitoring" |
| 62 | + response = self.messaging_client.create_message(ACCOUNT_ID, body) |
| 63 | + self.assertTrue(len(response.body.id) > 0) #validate that _some_ id was returned |
| 64 | + |
| 65 | + def test_create_message_invalid_phone_number(self): |
| 66 | + body = MessageRequest() |
| 67 | + body.application_id = MESSAGING_APPLICATION_ID |
| 68 | + body.to = ["+1invalid"] |
| 69 | + body.mfrom = PHONE_NUMBER_OUTBOUND |
| 70 | + body.text = "Python Monitoring" |
| 71 | + try: |
| 72 | + self.messaging_client.create_message(ACCOUNT_ID, body) |
| 73 | + self.assertTrue(False) |
| 74 | + except MessagingException as e: |
| 75 | + self.assertTrue(len(e.description) > 0) |
| 76 | + except: |
| 77 | + self.assertTrue(False) |
| 78 | + |
| 79 | + def test_upload_download_media(self): |
| 80 | + #define constants for upload media and download media |
| 81 | + media_file_name = 'python_monitoring' #future update to add special symbols |
| 82 | + media_file = b'12345' |
| 83 | + |
| 84 | + #media upload |
| 85 | + self.messaging_client.upload_media(ACCOUNT_ID, media_file_name, str(len(media_file)), body=media_file) |
| 86 | + |
| 87 | + #media download |
| 88 | + downloaded_media_file = self.messaging_client.get_media(ACCOUNT_ID, media_file_name).body |
| 89 | + |
| 90 | + #validate that the response is the same as the upload |
| 91 | + self.assertEqual(media_file, downloaded_media_file) |
| 92 | + |
| 93 | + def test_create_call_and_get_call_state(self): |
| 94 | + body = ApiCreateCallRequest() |
| 95 | + body.mfrom = PHONE_NUMBER_OUTBOUND |
| 96 | + body.to = PHONE_NUMBER_INBOUND |
| 97 | + body.application_id = VOICE_APPLICATION_ID |
| 98 | + body.answer_url = CALLBACK_URL |
| 99 | + response = self.voice_client.create_call(ACCOUNT_ID, body) |
| 100 | + self.assertTrue(len(response.body.call_id) > 1) |
| 101 | + |
| 102 | + #get phone call information |
| 103 | + import time |
| 104 | + time.sleep(1) #No guarantee that the info will be immediately available |
| 105 | + response = self.voice_client.get_call_state(ACCOUNT_ID, response.body.call_id) |
| 106 | + self.assertTrue(len(response.body.state) > 1) |
| 107 | + |
| 108 | + def test_create_call_invalid_phone_number(self): |
| 109 | + body = ApiCreateCallRequest() |
| 110 | + body.mfrom = PHONE_NUMBER_OUTBOUND |
| 111 | + body.to = "+1invalid" |
| 112 | + body.application_id = VOICE_APPLICATION_ID |
| 113 | + body.answer_url = CALLBACK_URL |
| 114 | + try: |
| 115 | + self.voice_client.create_call(ACCOUNT_ID, body) |
| 116 | + self.assertTrue(False) |
| 117 | + except ApiErrorResponseException as e: |
| 118 | + self.assertTrue(len(e.description) > 0) |
| 119 | + except: |
| 120 | + self.assertTrue(False); |
| 121 | + |
| 122 | + def test_mfa_messaging(self): |
| 123 | + body = TwoFactorCodeRequestSchema( |
| 124 | + mfrom = PHONE_NUMBER_MFA, |
| 125 | + to = PHONE_NUMBER_INBOUND, |
| 126 | + application_id = MFA_MESSAGING_APPLICATION_ID, |
| 127 | + scope = "scope", |
| 128 | + digits = 6, |
| 129 | + message = "Your temporary {NAME} {SCOPE} code is {CODE}" |
| 130 | + ) |
| 131 | + response = self.auth_client.create_messaging_two_factor(ACCOUNT_ID, body) |
| 132 | + self.assertTrue(len(response.body.message_id) > 0) |
| 133 | + |
| 134 | + def test_mfa_voice(self): |
| 135 | + body = TwoFactorCodeRequestSchema( |
| 136 | + mfrom = PHONE_NUMBER_MFA, |
| 137 | + to = PHONE_NUMBER_INBOUND, |
| 138 | + application_id = MFA_VOICE_APPLICATION_ID, |
| 139 | + scope = "scope", |
| 140 | + digits = 6, |
| 141 | + message = "Your temporary {NAME} {SCOPE} code is {CODE}" |
| 142 | + ) |
| 143 | + response = self.auth_client.create_voice_two_factor(ACCOUNT_ID, body) |
| 144 | + self.assertTrue(len(response.body.call_id) > 0) |
| 145 | + |
| 146 | + def test_mfa_verify(self): |
| 147 | + body = TwoFactorVerifyRequestSchema( |
| 148 | + to = PHONE_NUMBER_INBOUND, |
| 149 | + application_id = MFA_VOICE_APPLICATION_ID, |
| 150 | + scope = "scope", |
| 151 | + code = "123456", |
| 152 | + expiration_time_in_minutes = 3 |
| 153 | + ) |
| 154 | + response = self.auth_client.create_verify_two_factor(ACCOUNT_ID, body) |
| 155 | + self.assertTrue(isinstance(response.body.valid, bool)) |
| 156 | + |
| 157 | +if __name__ == '__main__': |
| 158 | + unittest.main() |
0 commit comments