13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
15
"""Arboretim IBM Cloud IAM utitity module tests."""
16
- import configparser
17
- import textwrap
18
16
import unittest
17
+ from unittest import mock
19
18
20
19
from arboretum .common .iam_ibm import get_tokens
21
20
22
- from requests import HTTPError
21
+ from requests import HTTPError , Response
23
22
24
23
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 ):]
27
29
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
+ }
38
37
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
44
45
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
- )
58
46
47
+ class IamIbmTest (unittest .TestCase ):
48
+ """Arboretim IBM Cloud IAM functions tests."""
49
+
50
+ @mock .patch ('requests.post' , new = _mock_requests_post )
59
51
def test_get_tokens_success (self ):
60
52
"""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 )
62
55
63
56
# spec of access_token and refresh_token
64
57
# https://cloud.ibm.com/docs/mobilefoundation?topic=mobilefoundation-basic_authentication
@@ -77,4 +70,7 @@ def test_get_tokens_error(self):
77
70
fake_api_key = 'xxx'
78
71
with self .assertRaises (HTTPError ) as cm :
79
72
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