Skip to content

Commit cdef16e

Browse files
committed
Expands the keypath to honour ~
1 parent 7184780 commit cdef16e

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

yoti/client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import time
66
import uuid
77
from os import environ
8-
from os.path import isfile
8+
from os.path import isfile, expanduser
99

1010
import requests
1111
from past.builtins import basestring
@@ -40,12 +40,14 @@ def __init__(self, sdk_id=None, pem_file_path=None):
4040
@staticmethod
4141
def __read_pem_file(key_file_path, error_source):
4242
try:
43+
key_file_path = expanduser(key_file_path)
44+
4345
if not isinstance(key_file_path, basestring) or not isfile(key_file_path):
4446
raise IOError('File not found: {0}'.format(key_file_path))
4547
with open(key_file_path, 'rb') as pem_file:
4648
return pem_file.read().strip()
47-
except (IOError, TypeError, OSError) as exc:
48-
error = 'Invalid private key file ' + error_source
49+
except (AttributeError, IOError, TypeError, OSError) as exc:
50+
error = 'Could not read private key file: "{0}", passed as: {1} '.format(key_file_path, error_source)
4951
exception = '{0}: {1}'.format(type(exc).__name__, exc)
5052
raise RuntimeError('{0}: {1}'.format(error, exception))
5153

yoti/tests/test_client.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_creating_client_instance_without_private_key_file():
6464
def test_creating_client_instance_with_invalid_key_file_arg(key_file):
6565
with pytest.raises(RuntimeError) as exc:
6666
Client(YOTI_CLIENT_SDK_ID, key_file)
67-
expected_error = 'Invalid private key file argument specified in Client()'
67+
expected_error = 'Could not read private key file'
6868
assert expected_error in str(exc)
6969
assert str(key_file) in str(exc)
7070

@@ -74,9 +74,10 @@ def test_creating_client_instance_with_invalid_key_file_env(key_file):
7474
environ['YOTI_KEY_FILE_PATH'] = str(key_file)
7575
with pytest.raises(RuntimeError) as exc:
7676
Client(YOTI_CLIENT_SDK_ID)
77-
expected_error = 'Invalid private key file specified by the ' \
78-
'YOTI_KEY_FILE_PATH env variable'
77+
expected_error = 'Could not read private key file'
78+
expected_error_source = 'specified by the YOTI_KEY_FILE_PATH env variable'
7979
assert expected_error in str(exc)
80+
assert expected_error_source in str(exc)
8081
assert str(key_file) in str(exc)
8182

8283

@@ -89,7 +90,7 @@ def test_creating_client_instance_with_valid_key_file_env_but_invalid_key_file_a
8990
environ['YOTI_KEY_FILE_PATH'] = PEM_FILE_PATH
9091
with pytest.raises(RuntimeError) as exc:
9192
Client(YOTI_CLIENT_SDK_ID, INVALID_KEY_FILE_PATH)
92-
expected_error = 'Invalid private key file argument specified in Client()'
93+
expected_error = 'Could not read private key file'
9394
assert expected_error in str(exc)
9495
assert str(INVALID_KEY_FILE_PATH) in str(exc)
9596

0 commit comments

Comments
 (0)