Skip to content

Commit ad7542f

Browse files
committed
test: use mock to simulate behaviour of IBM Cloud API
1 parent b6453f9 commit ad7542f

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

test/resource/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/test_iam_ibm.py

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,45 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
"""Arboretim IBM Cloud IAM utitity module tests."""
16-
import configparser
17-
import textwrap
1816
import unittest
17+
from unittest import mock
1918

2019
from arboretum.common.iam_ibm import get_tokens
2120

22-
from requests import HTTPError
21+
from requests import HTTPError, Response
2322

2423

25-
class IamIbmTest(unittest.TestCase):
26-
"""Arboretim IBM Cloud IAM functions tests."""
24+
def _mock_requests_post(*args, **kwargs):
25+
data = kwargs['data']
26+
kw = 'apikey='
27+
idx = data.find(kw)
28+
api_key = data[idx + len(kw):]
2729

28-
@classmethod
29-
def setUpClass(cls):
30-
"""Initialize test class."""
31-
creds = configparser.ConfigParser()
32-
cred_filepath = 'test/resource/credentials'
33-
config_files = creds.read(cred_filepath, encoding='utf-8')
34-
config_example = textwrap.dedent(
35-
"""
36-
Example credential file contents:
37-
(replace "S1A2_DQp..." with valid API key string)
30+
def json_function(**kwargs):
31+
return {
32+
'access_token': 'yI6ICJodHRwOi8vc2VydmVyLmV4YW1y'
33+
'I6ICJodHRwOi8vc2VydmVyLmV4YW1',
34+
'refresh_token': 'yI6ICJodHRwOi8vc2VydmVyLmV4YW1y'
35+
'I6ICJodHRwOi8vc2VydmVyLmV4YW1'
36+
}
3837

39-
[ibm_cloud]
40-
test_api_key=S1A2_DQp...
41-
"""
42-
)
43-
if len(config_files) == 0:
38+
resp = Response()
39+
if len(api_key) > len('yI6ICJodHRwOi8vc2VydmVyLmV4YW1'):
40+
resp.status_code = 200
41+
resp.json = json_function
42+
else:
43+
raise HTTPError() # status_code is automatically set to 400.
44+
return resp
4445

45-
raise RuntimeError(
46-
'Failed to read credential file: '
47-
f'{cred_filepath}\n'
48-
f'{config_example}'
49-
)
50-
try:
51-
cls._api_key = creds['ibm_cloud']['test_api_key']
52-
except KeyError:
53-
raise RuntimeError(
54-
'Failed to read API key from file: '
55-
f'{cred_filepath}\n'
56-
f'{config_example}'
57-
)
5846

47+
class IamIbmTest(unittest.TestCase):
48+
"""Arboretim IBM Cloud IAM functions tests."""
49+
50+
@mock.patch('requests.post', new=_mock_requests_post)
5951
def test_get_tokens_success(self):
6052
"""Ensure that an API call with valid API key returns valid tokens."""
61-
access_token, refresh_token = get_tokens(self._api_key)
53+
api_key = 'yI6ICJodHRwOi8vc2VydmVyLmV4YW1xxxx'
54+
access_token, refresh_token = get_tokens(api_key)
6255

6356
# spec of access_token and refresh_token
6457
# https://cloud.ibm.com/docs/mobilefoundation?topic=mobilefoundation-basic_authentication
@@ -77,4 +70,7 @@ def test_get_tokens_error(self):
7770
fake_api_key = 'xxx'
7871
with self.assertRaises(HTTPError) as cm:
7972
access_token, refresh_token = get_tokens(fake_api_key)
80-
self.assertEqual(400, cm.exception.response.status_code)
73+
status_code = cm.exception.response.status_code
74+
self.assertEqual(
75+
400, status_code, f'status_code is {status_code}, not 400.'
76+
)

0 commit comments

Comments
 (0)