Skip to content

Commit ceb123b

Browse files
authored
DX-2686 Add Media Integration Tests (#95)
* Write Media API Integration Test Added FIxtures * Update test to conform to binary format definition * PEP8 format * Remove unused libraries * Add Media Integration Tests * Utilize a steps function to run tests in a specific order https://stackoverflow.com/questions/5387299/python-unittest-testcase-execution-order/5387956#5387956 * Test Cleanup * Update mediaId github actions runId is not unique * wrong env * Address PR Comments
1 parent 2b4c190 commit ceb123b

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ jobs:
4343
ATT_NUMBER: ${{ secrets.ATT_NUMBER }}
4444
T_MOBILE_NUMBER: ${{ secrets.T_MOBILE_NUMBER }}
4545
BASE_CALLBACK_URL: ${{ secrets.BASE_CALLBACK_URL }}
46+
PYTHON_VERSION: ${{ matrix.python-version }}
4647
run: pytest
4748

4849
- name: Notify Slack of Failures

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ __pycache__/
66
# C extensions
77
*.so
88

9-
# MacOS Files
9+
# MacOS Files
1010
.DS_Store
1111

1212
# Distribution / packaging
@@ -67,3 +67,9 @@ target/
6767

6868
#Ipython Notebook
6969
.ipynb_checkpoints
70+
71+
# VS Code
72+
.vscode
73+
74+
# Test Fixtures
75+
test/fixtures/

test/fixtures/python_cat.jpeg

31.4 KB
Loading

test/integration/test_media_api.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
"""
2+
Integration test for Bandwidth's Media API
3+
"""
4+
5+
import os
6+
import filecmp
7+
import unittest
8+
9+
import bandwidth
10+
from bandwidth.api import media_api
11+
from bandwidth.model.media import Media
12+
from bandwidth.exceptions import ApiException, NotFoundException
13+
14+
15+
class TestMedia(unittest.TestCase):
16+
"""Media API integration Test
17+
"""
18+
19+
def setUp(self) -> None:
20+
configuration = bandwidth.Configuration(
21+
username=os.environ['BW_USERNAME'],
22+
password=os.environ['BW_PASSWORD']
23+
)
24+
api_client = bandwidth.ApiClient(configuration)
25+
self.api_instance = media_api.MediaApi(api_client)
26+
self.account_id = os.environ['BW_ACCOUNT_ID']
27+
self.media_path = "./test/fixtures/"
28+
self.media_file = "python_cat.jpeg"
29+
self.media_id = os.environ['PYTHON_VERSION'] + "_" + os.environ['RUNNER_OS'] + "_" + os.environ['GITHUB_RUN_ID'] + "_" + self.media_file
30+
self.download_file_path = "cat_download.jpeg"
31+
32+
self.original_file = open(self.media_path + self.media_file, "rb")
33+
34+
def uploadMedia(self) -> None:
35+
"""Test uploading media
36+
"""
37+
media_id = self.media_id
38+
content_type = "image/jpeg"
39+
cache_control = "no-cache"
40+
41+
api_response_with_http_info = self.api_instance.upload_media(
42+
account_id=self.account_id,
43+
media_id=media_id,
44+
body=self.original_file,
45+
content_type=content_type,
46+
cache_control=cache_control,
47+
_return_http_data_only=False
48+
)
49+
50+
self.assertEqual(api_response_with_http_info[1], 204)
51+
52+
# reopen the media file
53+
# the client automatically closes any files passed into request bodies
54+
reopened_file = open(self.media_path + self.media_file, "rb")
55+
56+
# returns void
57+
self.api_instance.upload_media(
58+
account_id=self.account_id,
59+
media_id=media_id,
60+
body=reopened_file,
61+
content_type=content_type,
62+
cache_control=cache_control,
63+
_return_http_data_only=False
64+
)
65+
66+
def listMedia(self) -> None:
67+
"""Test listing all media on the account
68+
"""
69+
api_response_with_http_info = self.api_instance.list_media(
70+
self.account_id, _return_http_data_only=False)
71+
72+
self.assertEqual(api_response_with_http_info[1], 200)
73+
74+
api_response = self.api_instance.list_media(self.account_id)
75+
76+
self.assertIs(type(api_response[0]), Media)
77+
pass
78+
79+
def getMedia(self) -> None:
80+
"""Test downloading the media we uploaded in step 1
81+
"""
82+
api_response_with_http_info = self.api_instance.get_media(
83+
self.account_id, self.media_id, _return_http_data_only=False)
84+
85+
self.assertEqual(api_response_with_http_info[1], 200)
86+
87+
api_response = self.api_instance.get_media(
88+
self.account_id, self.media_id, _preload_content=False)
89+
90+
with open(self.media_path + self.download_file_path, "wb") as download_file:
91+
download_file.write(api_response.data)
92+
93+
self.assertTrue(filecmp.cmp(self.media_path + self.media_file,
94+
self.media_path + self.download_file_path))
95+
download_file.close()
96+
97+
def deleteMedia(self) -> None:
98+
"""Test deleting the media that was uploaded in step 1
99+
"""
100+
api_response_with_http_info = self.api_instance.delete_media(
101+
self.account_id, self.media_id, _return_http_data_only=False)
102+
103+
self.assertEqual(api_response_with_http_info[1], 204)
104+
105+
# returns void
106+
self.api_instance.delete_media(self.account_id, self.media_id)
107+
108+
def _steps(self) -> None:
109+
call_order = ['uploadMedia', 'listMedia', 'getMedia', 'deleteMedia']
110+
for name in call_order:
111+
yield name, getattr(self, name)
112+
113+
def test_steps(self) -> None:
114+
"""Test each function from _steps.call_order in specified order
115+
"""
116+
for name, step in self._steps():
117+
try:
118+
step()
119+
except ApiException as e:
120+
self.fail("{} failed ({}: {})".format(step, type(e), e))
121+
122+
@unittest.skip("API does not support url encoded characters in path")
123+
def testGetMediaWithBandwidthId(self) -> None:
124+
# use a nonexistent mediaId - results in a 404
125+
media_id = "abcd1234-e5f6-1111-2222-3456ghi7890/image123456.jpg"
126+
127+
with self.assertRaises(NotFoundException) as context:
128+
api_response = self.api_instance.get_media(
129+
self.account_id,
130+
media_id,
131+
_preload_content=False
132+
)
133+
134+
self.assertEqual(context.exception.status, 404)

0 commit comments

Comments
 (0)