Skip to content

Commit

Permalink
test: for module arboretum.common.iam_ibm
Browse files Browse the repository at this point in the history
  • Loading branch information
tmishina committed Sep 1, 2020
1 parent bee7a95 commit b6453f9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/resource/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
credentials
80 changes: 80 additions & 0 deletions test/test_iam_ibm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# -*- mode:python; coding:utf-8 -*-
# Copyright (c) 2020 IBM Corp. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# 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 arboretum.common.iam_ibm import get_tokens

from requests import HTTPError


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

@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)
[ibm_cloud]
test_api_key=S1A2_DQp...
"""
)
if len(config_files) == 0:

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}'
)

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)

# spec of access_token and refresh_token
# https://cloud.ibm.com/docs/mobilefoundation?topic=mobilefoundation-basic_authentication
# the doc above says tokens are longer than its examples.
self.assertIsInstance(access_token, str)
self.assertGreater(
len(access_token), len('yI6ICJodHRwOi8vc2VydmVyLmV4YW1')
)
self.assertIsInstance(refresh_token, str)
self.assertGreater(
len(refresh_token), len('yI7ICasdsdJodHRwOi8vc2Vashnneh')
)

def test_get_tokens_error(self):
"""Ensure that an API call with invalid API key returns error."""
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)

0 comments on commit b6453f9

Please sign in to comment.