-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: for module arboretum.common.iam_ibm
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
credentials |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |