Skip to content

Commit 9ce13df

Browse files
DX-2684
1 parent e060c30 commit 9ce13df

File tree

4 files changed

+109
-105
lines changed

4 files changed

+109
-105
lines changed

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pytest-cov>=2.8.1
2+
pyhamcrest>=2.0.3

test/integration/test_calls.py

Lines changed: 88 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
Integration test for Bandwidth's Voice Voice Calls API
33
"""
44

5-
import os
5+
from test.utils.env_variables import *
66
import time
77
import unittest
88
import datetime
9-
9+
from hamcrest import assert_that, has_properties, not_none, instance_of
1010

1111
import bandwidth
1212
from bandwidth.api import calls_api
@@ -22,44 +22,61 @@
2222
from bandwidth.model.update_call import UpdateCall
2323
from bandwidth.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException
2424

25-
try:
26-
BW_USERNAME = os.environ["BW_USERNAME"]
27-
BW_PASSWORD = os.environ["BW_PASSWORD"]
28-
BW_ACCOUNT_ID = os.environ["BW_ACCOUNT_ID"]
29-
BW_VOICE_APPLICATION_ID = os.environ["BW_VOICE_APPLICATION_ID"]
30-
BASE_CALLBACK_URL = os.environ["BASE_CALLBACK_URL"]
31-
BW_NUMBER = os.environ["BW_NUMBER"]
32-
USER_NUMBER = os.environ["USER_NUMBER"]
33-
FORBIDDEN_USERNAME = os.environ['BW_USERNAME_FORBIDDEN']
34-
FORBIDDEN_PASSWORD = os.environ['BW_PASSWORD_FORBIDDEN']
35-
MANTECA_ACTIVE_NUMBER = os.environ["MANTECA_ACTIVE_NUMBER"]
36-
MANTECA_IDLE_NUMBER = os.environ["MANTECA_IDLE_NUMBER"]
37-
MANTECA_BASE_URL = os.environ["MANTECA_BASE_URL"]
38-
MANTECA_STATUS_URL = MANTECA_BASE_URL + "tests/"
39-
MANTECA_APPLICATION_ID = os.environ["MANTECA_APPLICATION_ID"]
40-
41-
except KeyError as e:
42-
raise Exception("Environmental variables not found")
43-
44-
global testCallBody
45-
testCallBody = CreateCall(to=USER_NUMBER, _from=BW_NUMBER, application_id=BW_VOICE_APPLICATION_ID, answer_url=BASE_CALLBACK_URL)
46-
47-
global testMantecaCallBody
48-
testMantecaCallBody = CreateCall(to=MANTECA_IDLE_NUMBER, _from=MANTECA_ACTIVE_NUMBER, application_id=MANTECA_APPLICATION_ID, answer_url=MANTECA_BASE_URL + "/bxml/pause")
49-
50-
global updateStateCompleted
51-
updateStateCompleted = UpdateCall(state=CallStateEnum("completed"))
52-
53-
global testCallId
54-
testCallId = "Call-Id"
55-
56-
global testBxmlBody
57-
testBxmlBody = '<?xml version="1.0" encoding="UTF-8"?><Bxml><SpeakSentence locale="en_US" gender="female" voice="susan">This is a test bxml response</SpeakSentence><Pause duration="3"/></Bxml>'
58-
59-
6025
class CallsIntegration(unittest.TestCase):
6126
"""Voice Calls API integration test"""
6227

28+
global createCallBody
29+
createCallBody = CreateCall(
30+
to=USER_NUMBER,
31+
_from=BW_NUMBER,
32+
application_id=BW_VOICE_APPLICATION_ID,
33+
answer_url=BASE_CALLBACK_URL,
34+
answer_method=CallbackMethodEnum("POST"),
35+
username="mySecretUsername",
36+
password="mySecretPassword1!",
37+
answer_fallback_url="https://www.myFallbackServer.com/webhooks/answer",
38+
answer_fallback_method=CallbackMethodEnum("POST"),
39+
fallback_username="mySecretUsername",
40+
fallback_password="mySecretPassword1!",
41+
disconnect_url="https://myServer.com/bandwidth/webhooks/disconnectUrl",
42+
disconnect_method=CallbackMethodEnum("POST"),
43+
call_timeout=30.0,
44+
callback_timeout=15.0,
45+
machine_detection=MachineDetectionConfiguration(
46+
mode=MachineDetectionModeEnum("async"),
47+
detection_timeout=15.0,
48+
silence_timeout=10.0,
49+
speech_threshold=10.0,
50+
speech_end_threshold=5.0,
51+
machine_speech_end_threshold=5.0,
52+
delay_result=False,
53+
callback_url="https://myServer.com/bandwidth/webhooks/machineDetectionComplete",
54+
callback_method=CallbackMethodEnum("POST"),
55+
username="mySecretUsername",
56+
password="mySecretPassword1!",
57+
fallback_url="https://myFallbackServer.com/bandwidth/webhooks/machineDetectionComplete",
58+
fallback_method=CallbackMethodEnum("POST"),
59+
fallback_username="mySecretUsername",
60+
fallback_password="mySecretPassword1!",
61+
),
62+
priority=5,
63+
tag="tag_example",
64+
)
65+
66+
global testCallBody
67+
testCallBody = CreateCall(to=USER_NUMBER, _from=BW_NUMBER, application_id=BW_VOICE_APPLICATION_ID, answer_url=BASE_CALLBACK_URL)
68+
69+
global testMantecaCallBody
70+
testMantecaCallBody = CreateCall(to=MANTECA_IDLE_NUMBER, _from=MANTECA_ACTIVE_NUMBER, application_id=MANTECA_APPLICATION_ID, answer_url=MANTECA_BASE_URL + "/bxml/pause")
71+
72+
global updateStateCompleted
73+
updateStateCompleted = UpdateCall(state=CallStateEnum("completed"))
74+
75+
global testCallId
76+
testCallId = "Call-Id"
77+
78+
global testBxmlBody
79+
testBxmlBody = '<?xml version="1.0" encoding="UTF-8"?><Bxml><SpeakSentence locale="en_US" gender="female" voice="susan">This is a test bxml response</SpeakSentence><Pause duration="3"/></Bxml>'
6380
global callIdArray
6481
callIdArray = []
6582

@@ -119,65 +136,29 @@ def assertApiException(self, context: ApiException, expectedException: ApiExcept
119136
expectedException (ApiException): Expected exception type
120137
expected_status_code (int): Expected status code
121138
"""
122-
self.assertIs(type(context.exception.status), int)
123-
self.assertEqual(context.exception.status, expected_status_code)
124-
self.assertIs(type(context.exception.body), str)
139+
assert_that(context.exception, has_properties(
140+
'status', expected_status_code,
141+
'body', not_none()
142+
))
125143

126144
def test_create_call(self):
127145
"""
128146
Validate a Create Call request with all optional parameters
129147
"""
130-
time.sleep(3)
131-
call_body = CreateCall(
132-
to=USER_NUMBER,
133-
_from=BW_NUMBER,
134-
application_id=BW_VOICE_APPLICATION_ID,
135-
answer_url=BASE_CALLBACK_URL,
136-
answer_method=CallbackMethodEnum("POST"),
137-
username="mySecretUsername",
138-
password="mySecretPassword1!",
139-
answer_fallback_url="https://www.myFallbackServer.com/webhooks/answer",
140-
answer_fallback_method=CallbackMethodEnum("POST"),
141-
fallback_username="mySecretUsername",
142-
fallback_password="mySecretPassword1!",
143-
disconnect_url="https://myServer.com/bandwidth/webhooks/disconnectUrl",
144-
disconnect_method=CallbackMethodEnum("POST"),
145-
call_timeout=30.0,
146-
callback_timeout=15.0,
147-
machine_detection=MachineDetectionConfiguration(
148-
mode=MachineDetectionModeEnum("async"),
149-
detection_timeout=15.0,
150-
silence_timeout=10.0,
151-
speech_threshold=10.0,
152-
speech_end_threshold=5.0,
153-
machine_speech_end_threshold=5.0,
154-
delay_result=False,
155-
callback_url="https://myServer.com/bandwidth/webhooks/machineDetectionComplete",
156-
callback_method=CallbackMethodEnum("POST"),
157-
username="mySecretUsername",
158-
password="mySecretPassword1!",
159-
fallback_url="https://myFallbackServer.com/bandwidth/webhooks/machineDetectionComplete",
160-
fallback_method=CallbackMethodEnum("POST"),
161-
fallback_username="mySecretUsername",
162-
fallback_password="mySecretPassword1!",
163-
),
164-
priority=5,
165-
tag="tag_example",
166-
)
167-
168-
create_call_response: CreateCallResponse = self.api_instance.create_call(BW_ACCOUNT_ID, call_body, _return_http_data_only=False)
148+
time.sleep(3)
149+
create_call_response: CreateCallResponse = self.api_instance.create_call(BW_ACCOUNT_ID, createCallBody, _return_http_data_only=False)
169150

170151
#Adding the call to the callIdArray
171152
callIdArray.append(create_call_response[0].call_id)
172153

173-
self.assertEqual(create_call_response[1], 201)
174-
self.assertIs(type(create_call_response[0].call_id), str)
175-
self.assertEqual(create_call_response[0].account_id, BW_ACCOUNT_ID)
176-
self.assertEqual(create_call_response[0].application_id, BW_VOICE_APPLICATION_ID)
177-
self.assertEqual(create_call_response[0].to, USER_NUMBER)
178-
self.assertEqual(create_call_response[0]._from, BW_NUMBER)
179-
self.assertEqual(create_call_response[0].call_url, ("https://voice.bandwidth.com/api/v2/accounts/" + \
180-
BW_ACCOUNT_ID + "/calls/" + create_call_response[0].call_id))
154+
assert_that(create_call_response[1], 201)
155+
assert_that(create_call_response[0], has_properties(
156+
'call_id', instance_of(str),
157+
'account_id', BW_ACCOUNT_ID,
158+
'application_id', BW_VOICE_APPLICATION_ID,
159+
'to', USER_NUMBER,
160+
'_from', BW_NUMBER
161+
))
181162

182163
def test_create_call_bad_request(self):
183164
"""Validate a bad (400) request
@@ -187,7 +168,7 @@ def test_create_call_bad_request(self):
187168
with self.assertRaises(ApiException) as context:
188169
self.api_instance.create_call(BW_ACCOUNT_ID, call_body, _return_http_data_only=False)
189170

190-
self.assertEqual(context.exception.status, 400)
171+
assert_that(context.exception.status, 400)
191172

192173
def test_create_call_unauthorized(self) -> None:
193174
"""Validate an unauthorized (401) request
@@ -218,14 +199,16 @@ def test_get_call_state(self):
218199
time.sleep(3)
219200

220201
get_call_response: CallState = self.api_instance.get_call_state(BW_ACCOUNT_ID, call_id, _return_http_data_only=False)
221-
222-
self.assertEqual(get_call_response[1], 200)
223-
self.assertEqual(get_call_response[0].call_id, call_id)
224-
self.assertIs(type(get_call_response[0].state), str)
225-
self.assertEqual(get_call_response[0].direction, CallDirectionEnum("outbound"))
226-
self.assertIs(type(get_call_response[0].enqueued_time), datetime.datetime)
227-
self.assertIs(type(get_call_response[0].last_update), datetime.datetime)
228-
self.assertIs(type(get_call_response[0].start_time), datetime.datetime)
202+
203+
assert_that(get_call_response[1], 200)
204+
assert_that(get_call_response[0], has_properties(
205+
"call_id", call_id,
206+
"state", instance_of(str),
207+
"direction", CallDirectionEnum("outbound"),
208+
"enqueued_time", instance_of(datetime.datetime),
209+
"last_update", instance_of(datetime.datetime),
210+
"start_time", instance_of(datetime.datetime)
211+
))
229212

230213
def test_get_call_state_unauthorized(self) -> None:
231214
"""Validate an unauthorized (401) request
@@ -276,12 +259,12 @@ def test_update_call(self):
276259

277260
time.sleep(3)
278261
update_call_response: UpdateCall = self.api_instance.update_call(BW_ACCOUNT_ID, call_id, updateCallBody, _return_http_data_only=False)
279-
self.assertEqual(update_call_response[1], 200)
262+
assert_that(update_call_response[1], 200)
280263

281264
time.sleep(3)
282265
# hanging-up the call
283266
update_call_response: UpdateCall = self.api_instance.update_call(BW_ACCOUNT_ID, call_id, updateStateCompleted, _return_http_data_only=False)
284-
self.assertEqual(update_call_response[1], 200)
267+
assert_that(update_call_response[1], 200)
285268

286269
def test_update_call_bad_request(self):
287270
"""Validate a bad (400) update call request
@@ -298,12 +281,12 @@ def test_update_call_bad_request(self):
298281
badRequestBody = UpdateCall(states="badRequest")
299282
self.api_instance.update_call(BW_ACCOUNT_ID, call_id, badRequestBody, _return_http_data_only=False)
300283

301-
self.assertEqual(context.exception.status, 400)
284+
assert_that(context.exception.status, 400)
302285

303286
# hanging-up the call
304287
time.sleep(3)
305288
update_call_response: UpdateCall = self.api_instance.update_call(BW_ACCOUNT_ID, call_id, updateStateCompleted, _return_http_data_only=False)
306-
self.assertEqual(update_call_response[1], 200)
289+
assert_that(update_call_response[1], 200)
307290

308291
def test_update_call_unauthorized(self):
309292
"""Validate an unauthorized (401) update call request
@@ -332,7 +315,7 @@ def test_update_call_forbidden(self):
332315
time.sleep(12)
333316
# hanging-up the call
334317
update_call_response: UpdateCall = self.api_instance.update_call(BW_ACCOUNT_ID, call_id, updateStateCompleted, _return_http_data_only=False)
335-
self.assertEqual(update_call_response[1], 200)
318+
assert_that(update_call_response[1], 200)
336319

337320
def test_update_call_not_found(self):
338321
"""Validate a not found update call request
@@ -353,12 +336,12 @@ def test_update_call_bxml(self):
353336

354337
time.sleep(5)
355338
update_call_bxml_response: UpdateCall = self.api_instance.update_call_bxml(BW_ACCOUNT_ID, call_id, testBxmlBody, _return_http_data_only=False)
356-
self.assertEqual(update_call_bxml_response[1], 204)
339+
assert_that(update_call_bxml_response[1], 204)
357340

358341
time.sleep(5)
359342
# hanging-up the call
360343
update_call_response: UpdateCall = self.api_instance.update_call(BW_ACCOUNT_ID, call_id, updateStateCompleted, _return_http_data_only=False)
361-
self.assertEqual(update_call_response[1], 200)
344+
assert_that(update_call_response[1], 200)
362345

363346
def test_update_call_bxml_bad_request(self):
364347
"""Validate a bad (400) update call bxml request
@@ -376,12 +359,12 @@ def test_update_call_bxml_bad_request(self):
376359
with self.assertRaises(ApiException) as context:
377360
self.api_instance.update_call_bxml(BW_ACCOUNT_ID, call_id, invalidBxmlBody, _return_http_data_only=False)
378361

379-
self.assertEqual(context.exception.status, 400)
362+
assert_that(context.exception.status, 400)
380363

381364
time.sleep(2)
382365
# hanging-up the call
383366
update_call_response: UpdateCall = self.api_instance.update_call(BW_ACCOUNT_ID, call_id, updateStateCompleted, _return_http_data_only=False)
384-
self.assertEqual(update_call_response[1], 200)
367+
assert_that(update_call_response[1], 200)
385368

386369

387370
def test_update_call_bxml_unauthorized(self):

test/utils/__init__.py

Whitespace-only changes.

test/utils/env_variables.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
3+
try:
4+
BW_USERNAME = os.environ["BW_USERNAME"]
5+
BW_PASSWORD = os.environ["BW_PASSWORD"]
6+
BW_ACCOUNT_ID = os.environ["BW_ACCOUNT_ID"]
7+
BW_VOICE_APPLICATION_ID = os.environ["BW_VOICE_APPLICATION_ID"]
8+
BASE_CALLBACK_URL = os.environ["BASE_CALLBACK_URL"]
9+
BW_NUMBER = os.environ["BW_NUMBER"]
10+
USER_NUMBER = os.environ["USER_NUMBER"]
11+
FORBIDDEN_USERNAME = os.environ['BW_USERNAME_FORBIDDEN']
12+
FORBIDDEN_PASSWORD = os.environ['BW_PASSWORD_FORBIDDEN']
13+
MANTECA_ACTIVE_NUMBER = os.environ["MANTECA_ACTIVE_NUMBER"]
14+
MANTECA_IDLE_NUMBER = os.environ["MANTECA_IDLE_NUMBER"]
15+
MANTECA_BASE_URL = os.environ["MANTECA_BASE_URL"]
16+
MANTECA_STATUS_URL = MANTECA_BASE_URL + "tests/"
17+
MANTECA_APPLICATION_ID = os.environ["MANTECA_APPLICATION_ID"]
18+
19+
except KeyError as e:
20+
raise Exception("Environmental variables not found")

0 commit comments

Comments
 (0)