Skip to content

Commit 07cb367

Browse files
DX-2684
1 parent bf787e0 commit 07cb367

File tree

1 file changed

+94
-12
lines changed

1 file changed

+94
-12
lines changed

test/integration/test_calls.py

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

5-
from email.quoprimime import body_check
65
import os
76
import time
87
import unittest
@@ -45,6 +44,8 @@
4544
class CallsIntegration(unittest.TestCase):
4645
"""Voice Calls API integration test"""
4746

47+
global callIdArray
48+
callIdArray = []
4849

4950
def setUp(self):
5051
configuration = bandwidth.Configuration(
@@ -55,8 +56,27 @@ def setUp(self):
5556
self.api_instance = calls_api.CallsApi(api_client)
5657
self.account_id = BW_ACCOUNT_ID
5758

59+
5860
def tearDown(self):
59-
pass
61+
"""
62+
Whenever we create an actual call, we'll add the call_id to the callIdArray. Then when the integration test is done, as part of tearDown we'll:
63+
Do a get to check is the call status is still active
64+
If so, update to completed to end the call
65+
If not, pop that callID off the array
66+
Once we go through the whole array, we clear the array so it's empty for the next integration test.
67+
if the status is active, send UpdateCall to change to completed
68+
"""
69+
70+
if len(callIdArray) > 0:
71+
for callId in callIdArray:
72+
body = UpdateCall(state=CallStateEnum("completed"))
73+
get_call_response: CallState = self.api_instance.get_call_state(BW_ACCOUNT_ID, callId, _return_http_data_only=False)
74+
if get_call_response[0].state == 'active':
75+
self.api_instance.update_call(BW_ACCOUNT_ID, callId, body, _return_http_data_only=False)
76+
elif get_call_response[0].state == 'complete':
77+
callIdArray.remove(callId)
78+
callIdArray.clear()
79+
pass
6080

6181
def assertApiException(self, context: ApiException, expectedException: ApiException, expected_status_code: int):
6282
"""Validates that common API exceptions, (401, 403, and 404) are properly formatted
@@ -71,7 +91,8 @@ def assertApiException(self, context: ApiException, expectedException: ApiExcept
7191
self.assertIs(type(context.exception.body), str)
7292

7393
def test_create_call(self):
74-
"""Validate a Create Call request with all optional parameters
94+
"""
95+
Validate a Create Call request with all optional parameters
7596
"""
7697
time.sleep(3)
7798
answer_url = BASE_CALLBACK_URL
@@ -114,6 +135,9 @@ def test_create_call(self):
114135

115136
create_call_response: CreateCallResponse = self.api_instance.create_call(BW_ACCOUNT_ID, call_body, _return_http_data_only=False)
116137

138+
#Adding the call to the callIdArray
139+
callIdArray.append(create_call_response[0].call_id)
140+
117141
self.assertEqual(create_call_response[1], 201)
118142
self.assertIs(type(create_call_response[0].call_id), str)
119143
self.assertEqual(create_call_response[0].account_id, BW_ACCOUNT_ID)
@@ -123,6 +147,11 @@ def test_create_call(self):
123147
self.assertEqual(create_call_response[0].call_url, ("https://voice.bandwidth.com/api/v2/accounts/" + \
124148
BW_ACCOUNT_ID + "/calls/" + create_call_response[0].call_id))
125149

150+
time.sleep(1)
151+
body = UpdateCall(state=CallStateEnum("completed"))
152+
self.api_instance.update_call(BW_ACCOUNT_ID, create_call_response[0].call_id, body, _return_http_data_only=False)
153+
154+
126155
def test_create_call_bad_request(self):
127156
"""Validate a bad (400) request
128157
"""
@@ -178,6 +207,10 @@ def test_get_call_state(self):
178207
call_body = CreateCall(to=USER_NUMBER, _from=BW_NUMBER, application_id=BW_VOICE_APPLICATION_ID, answer_url=answer_url)
179208
create_call_response: CreateCallResponse = self.api_instance.create_call(BW_ACCOUNT_ID, call_body, _return_http_data_only=False)
180209
call_id = create_call_response[0].call_id
210+
211+
#Adding the call to the callIdArray
212+
callIdArray.append(create_call_response[0].call_id)
213+
181214
time.sleep(3)
182215

183216
get_call_response: CallState = self.api_instance.get_call_state(BW_ACCOUNT_ID, call_id, _return_http_data_only=False)
@@ -238,30 +271,34 @@ def test_update_call(self):
238271
"""Validate an UpdateCall Request
239272
"""
240273
time.sleep(3)
241-
answer_url = MANTECA_BASE_URL + "/bxml/loop"
274+
answer_url = MANTECA_BASE_URL + "/bxml/idle"
242275
call_body = CreateCall(to=MANTECA_IDLE_NUMBER, _from=MANTECA_ACTIVE_NUMBER, application_id=MANTECA_APPLICATION_ID, answer_url=answer_url)
243276
create_call_response: CreateCallResponse = self.api_instance.create_call(BW_ACCOUNT_ID, call_body, _return_http_data_only=False)
244277
call_id = create_call_response[0].call_id
278+
279+
#Adding the call to the callIdArray
280+
callIdArray.append(create_call_response[0].call_id)
281+
245282
body = UpdateCall(
246283
state=CallStateEnum("active"),
247-
redirect_url=MANTECA_BASE_URL + "/bxml/loop",
284+
redirect_url=MANTECA_BASE_URL + "/bxml/idle",
248285
redirect_method=RedirectMethodEnum("POST"),
249286
username="mySecretUsername",
250287
password="mySecretPassword1!",
251-
redirect_fallback_url=MANTECA_BASE_URL + "/bxml/loop",
288+
redirect_fallback_url=MANTECA_BASE_URL + "/bxml/idle",
252289
redirect_fallback_method=RedirectMethodEnum("POST"),
253290
fallback_username="mySecretUsername",
254291
fallback_password="mySecretPassword1!",
255292
tag="My Custom Tag",
256293
)
257-
body2 = UpdateCall(state=CallStateEnum("completed"));
294+
body2 = UpdateCall(state=CallStateEnum("completed"))
258295

259296
time.sleep(3)
260297
update_call_response: UpdateCall = self.api_instance.update_call(BW_ACCOUNT_ID, call_id, body, _return_http_data_only=False)
261298

262299
self.assertEqual(update_call_response[1], 200)
263300

264-
time.sleep(2)
301+
time.sleep(3)
265302
# hanging-up the call
266303
update_call_response: UpdateCall = self.api_instance.update_call(BW_ACCOUNT_ID, call_id, body2, _return_http_data_only=False)
267304
self.assertEqual(update_call_response[1], 200)
@@ -273,9 +310,16 @@ def test_update_call_bad_request(self):
273310
call_body = CreateCall(to=MANTECA_IDLE_NUMBER, _from=MANTECA_ACTIVE_NUMBER, application_id=MANTECA_APPLICATION_ID, answer_url=answer_url)
274311
create_call_response: CreateCallResponse = self.api_instance.create_call(BW_ACCOUNT_ID, call_body, _return_http_data_only=False)
275312
call_id = create_call_response[0].call_id
313+
314+
#Adding the call to the callIdArray
315+
callIdArray.append(create_call_response[0].call_id)
316+
276317
body = UpdateCall(states="badReqeust")
277318
body2 = UpdateCall(state=CallStateEnum("completed"))
278319

320+
time.sleep(3)
321+
322+
279323
with self.assertRaises(ApiException) as context:
280324
self.api_instance.update_call(BW_ACCOUNT_ID, call_id, body, _return_http_data_only=False)
281325

@@ -317,13 +361,23 @@ def test_update_call_forbidden(self):
317361
call_body = CreateCall(to=MANTECA_IDLE_NUMBER, _from=MANTECA_ACTIVE_NUMBER, application_id=MANTECA_APPLICATION_ID, answer_url=answer_url)
318362
create_call_response: CreateCallResponse = self.api_instance.create_call(BW_ACCOUNT_ID, call_body, _return_http_data_only=False)
319363
call_id = create_call_response[0].call_id
364+
365+
#Adding the call to the callIdArray
366+
callIdArray.append(create_call_response[0].call_id)
367+
320368
body = UpdateCall(state=CallStateEnum("completed"))
321369

370+
time.sleep(2)
322371

323372
with self.assertRaises(ForbiddenException) as context:
324373
forbidden_api_instance.update_call(BW_ACCOUNT_ID, call_id, body, _return_http_data_only=False)
325374

326-
self.assertApiException(context, ForbiddenException, 403)
375+
self.assertApiException(context, ForbiddenException, 403)
376+
377+
time.sleep(3)
378+
# hanging-up the call
379+
update_call_response: UpdateCall = self.api_instance.update_call(BW_ACCOUNT_ID, call_id, body, _return_http_data_only=False)
380+
self.assertEqual(update_call_response[1], 200)
327381

328382
def test_update_call_not_found(self):
329383
"""Validate a not found update call request
@@ -340,10 +394,14 @@ def test_update_call_bxml(self):
340394
"""Validate an UpdateCallBxml Request
341395
"""
342396

343-
answer_url = MANTECA_BASE_URL + "/bxml/loop"
397+
answer_url = MANTECA_BASE_URL + "/bxml/idle"
344398
call_body = CreateCall(to=MANTECA_IDLE_NUMBER, _from=MANTECA_ACTIVE_NUMBER, application_id=MANTECA_APPLICATION_ID, answer_url=answer_url)
345399
create_call_response: CreateCallResponse = self.api_instance.create_call(BW_ACCOUNT_ID, call_body, _return_http_data_only=False)
346400
call_id = create_call_response[0].call_id
401+
402+
#Adding the call to the callIdArray
403+
callIdArray.append(create_call_response[0].call_id)
404+
347405
body = '<?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>'
348406

349407
time.sleep(2)
@@ -364,13 +422,26 @@ def test_update_call_bxml_bad_request(self):
364422
call_body = CreateCall(to=MANTECA_IDLE_NUMBER, _from=MANTECA_ACTIVE_NUMBER, application_id=MANTECA_APPLICATION_ID, answer_url=answer_url)
365423
create_call_response: CreateCallResponse = self.api_instance.create_call(BW_ACCOUNT_ID, call_body, _return_http_data_only=False)
366424
call_id = create_call_response[0].call_id
425+
426+
#Adding the call to the callIdArray
427+
callIdArray.append(create_call_response[0].call_id)
428+
429+
time.sleep(3)
430+
431+
367432
body = "invalidBXML"
368433

369434
with self.assertRaises(ApiException) as context:
370435
self.api_instance.update_call_bxml(BW_ACCOUNT_ID, call_id, body, _return_http_data_only=False)
371436

372437
self.assertEqual(context.exception.status, 400)
373438

439+
time.sleep(2)
440+
# hanging-up the call
441+
body2 = UpdateCall(state=CallStateEnum("completed"))
442+
update_call_response: UpdateCall = self.api_instance.update_call(BW_ACCOUNT_ID, call_id, body2, _return_http_data_only=False)
443+
self.assertEqual(update_call_response[1], 200)
444+
374445

375446
def test_update_call_bxml_unauthorized(self):
376447
"""Validate an unauthorized (401) update call bxml request
@@ -404,16 +475,27 @@ def test_update_call_bxml_forbidden(self):
404475
call_body = CreateCall(to=MANTECA_IDLE_NUMBER, _from=MANTECA_ACTIVE_NUMBER, application_id=MANTECA_APPLICATION_ID, answer_url=answer_url)
405476
create_call_response: CreateCallResponse = self.api_instance.create_call(BW_ACCOUNT_ID, call_body, _return_http_data_only=False)
406477
call_id = create_call_response[0].call_id
407-
body = '<?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>'
408478

479+
#Adding the call to the callIdArray
480+
callIdArray.append(create_call_response[0].call_id)
409481

482+
body = '<?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>'
483+
484+
time.sleep(2)
410485
with self.assertRaises(ForbiddenException) as context:
411486
forbidden_api_instance.update_call_bxml(BW_ACCOUNT_ID, call_id, body, _return_http_data_only=False)
412487

413488
self.assertApiException(context, ForbiddenException, 403)
414489

490+
time.sleep(2)
491+
# hanging-up the call
492+
body2 = UpdateCall(state=CallStateEnum("completed"))
493+
update_call_response: UpdateCall = self.api_instance.update_call(BW_ACCOUNT_ID, call_id, body2, _return_http_data_only=False)
494+
self.assertEqual(update_call_response[1], 200)
495+
415496
def test_update_call_bxml_not_found(self):
416-
"""Validate a not found update call bxml request
497+
"""
498+
Validate a not found update call bxml request
417499
"""
418500
body = '<?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>'
419501
call_id = "invalidCallId"

0 commit comments

Comments
 (0)