Skip to content

Commit 08021ef

Browse files
authored
Merge pull request #14 from Bandwidth/DX-1793
DX-1793 added api integration tests
2 parents f0d8d57 + a90d8aa commit 08021ef

File tree

8 files changed

+175
-4
lines changed

8 files changed

+175
-4
lines changed

.github/actions/validate/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ FROM python:3
33

44
# Copies your code file from your action repository to the filesystem path `/` of the container
55
COPY entrypoint.sh /entrypoint.sh
6-
COPY bxml_tests.py /bxml_tests.py
76

87
#Make entrypoint.sh exacutable
98
RUN chmod +x /entrypoint.sh
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
22

3-
pip install -e .
4-
cp /bxml_tests.py .
5-
python bxml_tests.py
3+
pip install -r requirements.txt
4+
python -m unittest tests.integration.bxml_tests
5+
python -m unittest tests.integration.api_tests

.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 }}

.gitkeep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
README.md
22
bandwidth/voice/bxml/*
33
bandwidth/webrtc/utils/*
4+
tests/integration/*

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ jsonpickle~=1.4, >= 1.4.1
33
cachecontrol~=0.12.6
44
python-dateutil~=2.8.1
55
enum34~=1.1, >=1.1.10
6+
lxml==4.6.2

tests/integration/__init__.py

Whitespace-only changes.

tests/integration/api_tests.py

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.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()
File renamed without changes.

0 commit comments

Comments
 (0)