Skip to content

Commit 626b2d7

Browse files
authored
Update credential file path check to current working directory (#50)
* fix: Update credential file path check to current working directory instead of core root
1 parent ef52165 commit 626b2d7

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

ibm_cloud_sdk_core/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
# from ibm_cloud_sdk_core.authenticators import Authenticator
1717
import datetime
1818
import json as json_import
19-
from os import getenv, environ
20-
from os.path import dirname, isfile, join, expanduser, abspath
19+
from os import getenv, environ, getcwd
20+
from os.path import isfile, join, expanduser
2121
from typing import List, Union
2222

2323
import dateutil.parser as date_parser
@@ -182,7 +182,7 @@ def __read_from_credential_file(service_name: str, separator: str = '=') -> dict
182182
# Current working directory
183183
if credential_file_path is None:
184184
file_path = join(
185-
dirname(dirname(abspath(__file__))), default_credentials_file_name)
185+
getcwd(), default_credentials_file_name)
186186
if isfile(file_path):
187187
credential_file_path = file_path
188188

resources/ibm-credentials.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
IBM_WATSON_APIKEY=5678efgh
2+
IBM_WATSON_AUTH_TYPE=iam
3+
IBM_WATSON_URL=https://cwdserviceurl
4+
IBM_WATSON_DISABLE_SSL=False

test/test_base_service.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import time
55
import os
6+
from shutil import copyfile
67
import pytest
78
import responses
89
import jwt
@@ -154,6 +155,29 @@ def test_fail_http_config():
154155
with pytest.raises(TypeError):
155156
service.with_http_config(None)
156157

158+
@responses.activate
159+
def test_cwd():
160+
file_path = os.path.join(
161+
os.path.dirname(__file__), '../resources/ibm-credentials.env')
162+
# Try changing working directories to test getting creds from cwd
163+
cwd = os.getcwd()
164+
os.chdir(os.path.dirname(file_path))
165+
iam_authenticator = get_authenticator_from_environment('ibm_watson')
166+
service = AnyServiceV1('2017-07-07', authenticator=iam_authenticator)
167+
service.configure_service('ibm_watson')
168+
os.chdir(cwd)
169+
assert service.service_url == 'https://cwdserviceurl'
170+
assert service.authenticator is not None
171+
172+
# Copy credentials file to cwd to test loading from current working directory
173+
temp_env_path = os.getcwd() + '/ibm-credentials.env'
174+
copyfile(file_path, temp_env_path)
175+
iam_authenticator = get_authenticator_from_environment('ibm_watson')
176+
service = AnyServiceV1('2017-07-07', authenticator=iam_authenticator)
177+
service.configure_service('ibm_watson')
178+
os.remove(temp_env_path)
179+
assert service.service_url == 'https://cwdserviceurl'
180+
assert service.authenticator is not None
157181

158182
@responses.activate
159183
def test_iam():

0 commit comments

Comments
 (0)