Debian Codegraph @

test_integration/test_cp4d_authenticator_integration.py

raw
1 # pylint: disable=missing-docstring
2 import logging
3 import os
4
5 from test.utils.logger_utils import setup_test_logger
6 from ibm_cloud_sdk_core import get_authenticator_from_environment
7
8 # To enable debug logging as well as HTTP message logging,
9 # change WARNING to DEBUG:
10 setup_test_logger(logging.WARNING)
11
12 # Note: Only the unit tests are run by default.
13 #
14 # In order to test with a live CP4D server, rename "ibm-credentials-cp4dtest.env.example" to
15 # "ibm-credentials-cp4dtest.env" in the resources folder and populate the fields.
16 # Then run this command:
17 # pytest test_integration/test_cp4d_authenticator_integration.py
18
19 IBM_CREDENTIALS_FILE = '../resources/ibm-credentials-cp4dtest.env'
20
21
22 def test_cp4d_authenticator_password():
23 file_path = os.path.join(os.path.dirname(__file__), IBM_CREDENTIALS_FILE)
24 os.environ['IBM_CREDENTIALS_FILE'] = file_path
25
26 authenticator = get_authenticator_from_environment('cp4d_password_test')
27 assert authenticator is not None
28 assert authenticator.token_manager.password is not None
29 assert authenticator.token_manager.apikey is None
30
31 request = {'headers': {}}
32 authenticator.authenticate(request)
33 assert request['headers']['Authorization'] is not None
34 assert 'Bearer' in request['headers']['Authorization']
35
36
37 def test_cp4d_authenticator_apikey():
38 file_path = os.path.join(os.path.dirname(__file__), IBM_CREDENTIALS_FILE)
39 os.environ['IBM_CREDENTIALS_FILE'] = file_path
40
41 authenticator = get_authenticator_from_environment('cp4d_apikey_test')
42 assert authenticator is not None
43 assert authenticator.token_manager.password is None
44 assert authenticator.token_manager.apikey is not None
45
46 request = {'headers': {}}
47 authenticator.authenticate(request)
48 assert request['headers']['Authorization'] is not None
49 assert 'Bearer' in request['headers']['Authorization']