Skip to content

Commit

Permalink
test: use mock to simulate behaviour of IBM Cloud API
Browse files Browse the repository at this point in the history
  • Loading branch information
tmishina committed Sep 1, 2020
1 parent b6453f9 commit ad7542f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
1 change: 0 additions & 1 deletion test/resource/.gitignore

This file was deleted.

66 changes: 31 additions & 35 deletions test/test_iam_ibm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,45 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Arboretim IBM Cloud IAM utitity module tests."""
import configparser
import textwrap
import unittest
from unittest import mock

from arboretum.common.iam_ibm import get_tokens

from requests import HTTPError
from requests import HTTPError, Response


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

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

[ibm_cloud]
test_api_key=S1A2_DQp...
"""
)
if len(config_files) == 0:
resp = Response()
if len(api_key) > len('yI6ICJodHRwOi8vc2VydmVyLmV4YW1'):
resp.status_code = 200
resp.json = json_function
else:
raise HTTPError() # status_code is automatically set to 400.
return resp

raise RuntimeError(
'Failed to read credential file: '
f'{cred_filepath}\n'
f'{config_example}'
)
try:
cls._api_key = creds['ibm_cloud']['test_api_key']
except KeyError:
raise RuntimeError(
'Failed to read API key from file: '
f'{cred_filepath}\n'
f'{config_example}'
)

class IamIbmTest(unittest.TestCase):
"""Arboretim IBM Cloud IAM functions tests."""

@mock.patch('requests.post', new=_mock_requests_post)
def test_get_tokens_success(self):
"""Ensure that an API call with valid API key returns valid tokens."""
access_token, refresh_token = get_tokens(self._api_key)
api_key = 'yI6ICJodHRwOi8vc2VydmVyLmV4YW1xxxx'
access_token, refresh_token = get_tokens(api_key)

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

0 comments on commit ad7542f

Please sign in to comment.