Skip to content

Commit

Permalink
Merge pull request #520 from mzaja/fix/279-certs-path
Browse files Browse the repository at this point in the history
#279 Stop prepending pardir to certs path
  • Loading branch information
liampauling authored Jul 24, 2023
2 parents 66b76f3 + 3f7be28 commit 631bf0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
14 changes: 6 additions & 8 deletions betfairlightweight/baseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(
self.username = username
self.password = password
self.app_key = app_key
self.certs = certs
self.certs = certs or "/certs"
self.locale = locale
self.cert_files = cert_files
self.lightweight = lightweight
Expand Down Expand Up @@ -158,27 +158,25 @@ def cert(self) -> Union[Tuple[str], str]:
if self.cert_files is not None:
return self.cert_files

certs = self.certs or "/certs/"
ssl_path = os.path.join(os.pardir, certs)
try:
cert_path = os.listdir(ssl_path)
cert_path = os.listdir(self.certs)
except FileNotFoundError as e:
raise CertsError(str(e))

cert, key, pem = None, None, None
for file in cert_path:
ext = os.path.splitext(file)[-1]
if ext in [".crt", ".cert"]:
cert = os.path.join(ssl_path, file)
cert = os.path.join(self.certs, file)
elif ext == ".key":
key = os.path.join(ssl_path, file)
key = os.path.join(self.certs, file)
elif ext == ".pem":
pem = os.path.join(ssl_path, file)
pem = os.path.join(self.certs, file)
if cert and key:
return (cert, key)
if pem:
return pem
msg = "Certificates not found in directory: '%s'" % ssl_path
msg = "Certificates not found in directory: '%s'" % self.certs
hint = " (make sure .crt and .key pair or a single .pem is present)"
raise CertsError(msg + hint)

Expand Down
8 changes: 4 additions & 4 deletions tests/test_baseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_base_client_init(self):
assert client.password == "password"
assert client.app_key == "app_key"
assert client.lightweight is True
assert client.certs is None
assert client.certs == "/certs"
assert client.locale is None
assert client._login_time is None
assert client.session_token is None
Expand Down Expand Up @@ -198,21 +198,21 @@ def test_client_certs_mocked(self, mock_listdir):
[".DS_Store", "client-2048.crt", "client-2048.key"]
)
assert self.client.cert == normpaths(
["../fail/client-2048.crt", "../fail/client-2048.key"]
["fail/client-2048.crt", "fail/client-2048.key"]
)

@mock.patch("betfairlightweight.baseclient.os.listdir")
def test_client_single_file_cert_mocked(self, mock_listdir):
mock_listdir.return_value = normpaths([".DS_Store", "client-2048.pem"])
assert self.client.cert == os.path.normpath("../fail/client-2048.pem")
assert self.client.cert == os.path.normpath("fail/client-2048.pem")

@mock.patch("betfairlightweight.baseclient.os.listdir")
def test_client_crt_key_preferred_over_pem_mocked(self, mock_listdir):
mock_listdir.return_value = normpaths(
[".DS_Store", "client-2048.crt", "client-2048.key", "client-2048.pem"]
)
assert self.client.cert == normpaths(
["../fail/client-2048.crt", "../fail/client-2048.key"]
["fail/client-2048.crt", "fail/client-2048.key"]
)


Expand Down

0 comments on commit 631bf0f

Please sign in to comment.