Skip to content

Commit ad13bf4

Browse files
committed
DX-1793 added api integration tests
1 parent f0d8d57 commit ad13bf4

File tree

3 files changed

+172
-2
lines changed

3 files changed

+172
-2
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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.client
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=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=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=body)
100+
self.assertTrue(len(response.body.call_id) > 1)
101+
102+
#get phone call information
103+
response = self.voice_client.get_call_state(ACCOUNT_ID, response.body.call_id)
104+
self.assertTrue(len(response.body.state) > 1)
105+
106+
def test_create_call_invalid_phone_number(self):
107+
body = ApiCreateCallRequest()
108+
body.mfrom = PHONE_NUMBER_OUTBOUND
109+
body.to = "+1invalid"
110+
body.application_id = VOICE_APPLICATION_ID
111+
body.answer_url = CALLBACK_URL
112+
try:
113+
self.voice_client.create_call(ACCOUNT_ID, body=body)
114+
self.assertTrue(False)
115+
except ApiErrorResponseException as e:
116+
self.assertTrue(len(e.description) > 0)
117+
except:
118+
self.assertTrue(False);
119+
120+
def test_mfa_messaging(self):
121+
body = TwoFactorCodeRequestSchema(
122+
mfrom = PHONE_NUMBER_MFA,
123+
to = PHONE_NUMBER_INBOUND,
124+
application_id = MFA_MESSAGING_APPLICATION_ID,
125+
scope = "scope",
126+
digits = 6,
127+
message = "Your temporary {NAME} {SCOPE} code is {CODE}"
128+
)
129+
response = self.auth_client.create_messaging_two_factor(ACCOUNT_ID, body)
130+
self.assertTrue(len(response.body.message_id) > 0)
131+
132+
def test_mfa_voice(self):
133+
body = TwoFactorCodeRequestSchema(
134+
mfrom = PHONE_NUMBER_MFA,
135+
to = PHONE_NUMBER_INBOUND,
136+
application_id = MFA_VOICE_APPLICATION_ID,
137+
scope = "scope",
138+
digits = 6,
139+
message = "Your temporary {NAME} {SCOPE} code is {CODE}"
140+
)
141+
response = self.auth_client.create_voice_two_factor(ACCOUNT_ID, body)
142+
self.assertTrue(len(response.body.call_id) > 0)
143+
144+
def test_mfa_verify(self):
145+
body = TwoFactorVerifyRequestSchema(
146+
mfrom = PHONE_NUMBER_MFA,
147+
to = PHONE_NUMBER_INBOUND,
148+
application_id = MFA_VOICE_APPLICATION_ID,
149+
scope = "scope",
150+
code = "123456",
151+
digits = 6,
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()
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
22

33
pip install -e .
4-
cp /bxml_tests.py .
5-
python bxml_tests.py
4+
python /bxml_tests.py
5+
python /api_tests.py

.github/workflows/validate.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,15 @@ jobs:
1414
uses: actions/checkout@v2
1515
- name: Validate
1616
uses: ./.github/actions/validate
17+
env:
18+
USERNAME: ${{ secrets.USERNAME }}
19+
PASSWORD: ${{ secrets.PASSWORD }}
20+
ACCOUNT_ID: ${{ secrets.ACCOUNT_ID }}
21+
VOICE_APPLICATION_ID: ${{ secrets.VOICE_APPLICATION_ID }}
22+
MESSAGING_APPLICATION_ID: ${{ secrets.MESSAGING_APPLICATION_ID }}
23+
CALLBACK_URL: ${{ secrets.CALLBACK_URL }}
24+
PHONE_NUMBER_OUTBOUND: ${{ secrets.PHONE_NUMBER_OUTBOUND }}
25+
PHONE_NUMBER_INBOUND: ${{ secrets.PHONE_NUMBER_INBOUND }}
26+
MFA_MESSAGING_APPLICATION_ID: ${{ secrets.MFA_MESSAGING_APPLICATION_ID }}
27+
MFA_VOICE_APPLICATION_ID: ${{ secrets.MFA_VOICE_APPLICATION_ID }}
28+
PHONE_NUMBER_MFA: ${{ secrets.PHONE_NUMBER_MFA }}

0 commit comments

Comments
 (0)