Skip to content

Commit

Permalink
replaces WorldcatAuthorizationError with TypeError and ValueError for…
Browse files Browse the repository at this point in the history
… configuration
  • Loading branch information
klinga committed Feb 17, 2024
1 parent 20bd073 commit 3dd65da
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
54 changes: 28 additions & 26 deletions bookops_worldcat/authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,41 +99,43 @@ def __init__(
self.token_type = ""

# default bookops-worldcat request header
if not self.agent:
self.agent = f"{__title__}/{__version__}"
if isinstance(self.agent, str):
if not self.agent.strip():
self.agent = f"{__title__}/{__version__}"
else:
if not isinstance(self.agent, str):
raise WorldcatAuthorizationError("Argument 'agent' must be a string.")
raise TypeError("Argument 'agent' must be a string.")

# asure passed arguments are valid
if not self.key:
raise WorldcatAuthorizationError("Argument 'key' is required.")
if isinstance(self.key, str):
if not self.key.strip():
raise ValueError("Argument 'key' cannot be an empty string.")
else:
if not isinstance(self.key, str):
raise WorldcatAuthorizationError("Argument 'key' must be a string.")
raise TypeError("Argument 'key' must be a string.")

if not self.secret:
raise WorldcatAuthorizationError("Argument 'secret' is required.")
if isinstance(self.secret, str):
if not self.secret.strip():
raise ValueError("Argument 'secret' cannot be an empty string.")
else:
if not isinstance(self.secret, str):
raise WorldcatAuthorizationError("Argument 'secret' must be a string.")
raise TypeError("Argument 'secret' must be a string.")

if not self.principal_id:
raise WorldcatAuthorizationError(
"Argument 'principal_id' is required for read/write endpoint of "
"Metadata API."
)
if not self.principal_idns:
raise WorldcatAuthorizationError(
"Argument 'principal_idns' is required for read/write endpoint of "
"Metadata API."
)
if isinstance(self.principal_id, str):
if not self.principal_id.strip():
raise ValueError("Argument 'principal_id' cannot be an empty string.")
else:
raise TypeError("Argument 'principal_id' must be a string.")

if isinstance(self.principal_idns, str):
if not self.principal_idns.strip():
raise ValueError("Argument 'principal_idns' cannot be an empty string.")
else:
raise TypeError("Argument 'principal_idns' must be a string.")

# validate passed scopes
if not self.scopes:
raise WorldcatAuthorizationError("Argument 'scopes' is required.")
elif not isinstance(self.scopes, str):
raise WorldcatAuthorizationError("Argument 'scopes' must a string.")
if isinstance(self.scopes, str):
if not self.scopes.strip():
raise ValueError("Argument 'scopes' cannot be an empty string.")
else:
raise TypeError("Argument 'scopes' must a string.")
self.scopes = self.scopes.strip()

# assign default value for timout
Expand Down
58 changes: 29 additions & 29 deletions tests/test_authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ class TestWorldcatAccessToken:
[
(
None,
pytest.raises(WorldcatAuthorizationError),
"Argument 'key' is required.",
pytest.raises(TypeError),
"Argument 'key' must be a string.",
),
(
"",
pytest.raises(WorldcatAuthorizationError),
"Argument 'key' is required.",
pytest.raises(ValueError),
"Argument 'key' cannot be an empty string.",
),
(
124,
pytest.raises(WorldcatAuthorizationError),
pytest.raises(TypeError),
"Argument 'key' must be a string.",
),
],
Expand All @@ -43,24 +43,24 @@ def test_key_exceptions(self, argm, expectation, msg):
principal_id="my_principalID",
principal_idns="my_principalIDNS",
)
assert msg in str(exp.value)
assert msg in str(exp.value)

@pytest.mark.parametrize(
"argm,expectation,msg",
[
(
None,
pytest.raises(WorldcatAuthorizationError),
"Argument 'secret' is required.",
pytest.raises(TypeError),
"Argument 'secret' must be a string.",
),
(
"",
pytest.raises(WorldcatAuthorizationError),
"Argument 'secret' is required.",
pytest.raises(ValueError),
"Argument 'secret' cannot be an empty string.",
),
(
123,
pytest.raises(WorldcatAuthorizationError),
pytest.raises(TypeError),
"Argument 'secret' must be a string.",
),
],
Expand All @@ -74,10 +74,10 @@ def test_secret_exceptions(self, argm, expectation, msg):
principal_id="my_principalID",
principal_idns="my_principalIDNS",
)
assert msg in str(exp.value)
assert msg in str(exp.value)

def test_agent_exceptions(self):
with pytest.raises(WorldcatAuthorizationError) as exp:
with pytest.raises(TypeError) as exp:
WorldcatAccessToken(
key="my_key",
secret="my_secret",
Expand All @@ -86,7 +86,7 @@ def test_agent_exceptions(self):
principal_idns="my_principalIDNS",
agent=124,
)
assert "Argument 'agent' must be a string." in str(exp.value)
assert "Argument 'agent' must be a string." in str(exp.value)

def test_agent_default_values(self, mock_successful_post_token_response):
token = WorldcatAccessToken(
Expand All @@ -103,13 +103,13 @@ def test_agent_default_values(self, mock_successful_post_token_response):
[
(
None,
pytest.raises(WorldcatAuthorizationError),
"Argument 'principal_id' is required for read/write endpoint of Metadata API.",
pytest.raises(TypeError),
"Argument 'principal_id' must be a string.",
),
(
"",
pytest.raises(WorldcatAuthorizationError),
"Argument 'principal_id' is required for read/write endpoint of Metadata API.",
pytest.raises(ValueError),
"Argument 'principal_id' cannot be an empty string.",
),
],
)
Expand All @@ -122,20 +122,20 @@ def test_principal_id_exception(self, arg, expectation, msg):
principal_id=arg,
principal_idns="my_principalIDNS",
)
assert msg in str(exc.value)
assert msg in str(exc.value)

@pytest.mark.parametrize(
"arg,expectation,msg",
[
(
None,
pytest.raises(WorldcatAuthorizationError),
"Argument 'principal_idns' is required for read/write endpoint of Metadata API.",
pytest.raises(TypeError),
"Argument 'principal_idns' must be a string.",
),
(
"",
pytest.raises(WorldcatAuthorizationError),
"Argument 'principal_idns' is required for read/write endpoint of Metadata API.",
pytest.raises(ValueError),
"Argument 'principal_idns' cannot be an empty string.",
),
],
)
Expand All @@ -148,29 +148,29 @@ def test_principal_idns_exception(self, arg, expectation, msg):
principal_id="my_principalID",
principal_idns=arg,
)
assert msg in str(exc.value)
assert msg in str(exc.value)

@pytest.mark.parametrize(
"argm,expectation,msg",
[
(
None,
pytest.raises(WorldcatAuthorizationError),
pytest.raises(TypeError),
"Argument 'scopes' must a string.",
),
(
123,
pytest.raises(WorldcatAuthorizationError),
pytest.raises(TypeError),
"Argument 'scopes' must a string.",
),
(
" ",
pytest.raises(WorldcatAuthorizationError),
"Argument 'scopes' is required.",
pytest.raises(ValueError),
"Argument 'scopes' cannot be an empty string.",
),
(
["", ""],
pytest.raises(WorldcatAuthorizationError),
pytest.raises(TypeError),
"Argument 'scopes' is required.",
),
],
Expand Down

0 comments on commit 3dd65da

Please sign in to comment.