Skip to content

Commit

Permalink
Removed principalID and principalIDNS from token requests (#79)
Browse files Browse the repository at this point in the history
* removed unnecessary params from token requests

* fixed docstring
  • Loading branch information
charlottekostelic authored Feb 23, 2024
1 parent 198ea0b commit ab1e6df
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 150 deletions.
22 changes: 0 additions & 22 deletions bookops_worldcat/authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class WorldcatAccessToken:
secret: your WSKey secret
scopes: request scopes for the access token as a string,
separate different scopes with space
principal_id: principalID (required for read/write endpoints)
principal_idns: principalIDNS (required for read/write endpoints)
agent: "User-agent" parameter to be passed in the request
header; usage strongly encouraged
timeout: how long to wait for server to send data before
Expand All @@ -43,8 +41,6 @@ class WorldcatAccessToken:
key="my_WSKey_client_id",
secret="my_WSKey_secret",
scope="WorldCatMetadataAPI",
principal_id="your principalID here",
principal_idns="your principalIDNS here",
agent="my_app/1.0.0")
>>> token.token_str
"tk_Yebz4BpEp9dAsghA7KpWx6dYD1OZKWBlHjqW"
Expand Down Expand Up @@ -75,8 +71,6 @@ def __init__(
key: str,
secret: str,
scopes: str,
principal_id: str,
principal_idns: str,
agent: str = "",
timeout: Optional[
Union[int, float, Tuple[int, int], Tuple[float, float]]
Expand All @@ -88,8 +82,6 @@ def __init__(
self.grant_type = "client_credentials"
self.key = key
self.oauth_server = "https://oauth.oclc.org"
self.principal_id = principal_id
self.principal_idns = principal_idns
self.scopes = scopes
self.secret = secret
self.server_response: Optional[requests.Response] = None
Expand Down Expand Up @@ -118,18 +110,6 @@ def __init__(
else:
raise TypeError("Argument 'secret' must be a string.")

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 isinstance(self.scopes, str):
if not self.scopes.strip():
Expand Down Expand Up @@ -183,8 +163,6 @@ def _payload(self) -> Dict[str, str]:
return {
"grant_type": self.grant_type,
"scope": self.scopes,
"principalID": self.principal_id,
"principalIDNS": self.principal_idns,
}

def _post_token_request(self) -> requests.Response:
Expand Down
4 changes: 0 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ def live_keys():
os.environ["WCKey"] = data["key"]
os.environ["WCSecret"] = data["secret"]
os.environ["WCScopes"] = data["scopes"]
os.environ["WCPrincipalID"] = data["principal_id"]
os.environ["WCPrincipalIDNS"] = data["principal_idns"]


class FakeUtcNow(datetime.datetime):
Expand Down Expand Up @@ -135,8 +133,6 @@ def mock_credentials():
"key": "my_WSkey",
"secret": "my_WSsecret",
"scopes": "scope1 scope2",
"principal_id": "my_principalID",
"principal_idns": "my_principalIDNS",
}


Expand Down
100 changes: 0 additions & 100 deletions tests/test_authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ def test_key_exceptions(self, argm, expectation, msg):
key=argm,
secret="my_secret",
scopes=["scope1"],
principal_id="my_principalID",
principal_idns="my_principalIDNS",
)
assert msg in str(exp.value)

Expand Down Expand Up @@ -71,8 +69,6 @@ def test_secret_exceptions(self, argm, expectation, msg):
key="my_key",
secret=argm,
scopes=["scope1"],
principal_id="my_principalID",
principal_idns="my_principalIDNS",
)
assert msg in str(exp.value)

Expand All @@ -82,74 +78,10 @@ def test_agent_exceptions(self):
key="my_key",
secret="my_secret",
scopes="scope1",
principal_id="my_principalID",
principal_idns="my_principalIDNS",
agent=124,
)
assert "Argument 'agent' must be a string." in str(exp.value)

def test_agent_default_values(self, mock_successful_post_token_response):
token = WorldcatAccessToken(
key="my_key",
secret="my_secret",
scopes="scope1",
principal_id="my_principalID",
principal_idns="my_principalIDNS",
)
assert token.agent == f"{__title__}/{__version__}"

@pytest.mark.parametrize(
"arg,expectation,msg",
[
(
None,
pytest.raises(TypeError),
"Argument 'principal_id' must be a string.",
),
(
"",
pytest.raises(ValueError),
"Argument 'principal_id' cannot be an empty string.",
),
],
)
def test_principal_id_exception(self, arg, expectation, msg):
with expectation as exc:
WorldcatAccessToken(
key="my_key",
secret="my_secret",
scopes="scope1",
principal_id=arg,
principal_idns="my_principalIDNS",
)
assert msg in str(exc.value)

@pytest.mark.parametrize(
"arg,expectation,msg",
[
(
None,
pytest.raises(TypeError),
"Argument 'principal_idns' must be a string.",
),
(
"",
pytest.raises(ValueError),
"Argument 'principal_idns' cannot be an empty string.",
),
],
)
def test_principal_idns_exception(self, arg, expectation, msg):
with expectation as exc:
WorldcatAccessToken(
key="my_key",
secret="my_secret",
scopes="scope1",
principal_id="my_principalID",
principal_idns=arg,
)
assert msg in str(exc.value)

@pytest.mark.parametrize(
"argm,expectation,msg",
[
Expand Down Expand Up @@ -181,8 +113,6 @@ def test_scope_exceptions(self, argm, expectation, msg):
key="my_key",
secret="my_secret",
scopes=argm,
principal_id="my_principalID",
principal_idns="my_principalIDNS",
)
assert msg in str(exp.value)

Expand All @@ -203,8 +133,6 @@ def test_timeout_argument(
key="my_key",
secret="my_secret",
scopes="scope1",
principal_id="my_principalID",
principal_idns="my_principalIDNS",
timeout=argm,
)
assert token.timeout == expectation
Expand All @@ -220,8 +148,6 @@ def test_scope_manipulation(
key="my_key",
secret="my_secret",
scopes=argm,
principal_id="my_principalID",
principal_idns="my_principalIDNS",
)
assert token.scopes == expectation

Expand All @@ -230,8 +156,6 @@ def test_token_url(self, mock_successful_post_token_response):
key="my_key",
secret="my_secret",
scopes="scope1",
principal_id="my_principalID",
principal_idns="my_principalIDNS",
)
assert token._token_url() == "https://oauth.oclc.org/token"

Expand All @@ -240,8 +164,6 @@ def test_token_headers(self, mock_successful_post_token_response):
key="my_key",
secret="my_secret",
scopes="scope1",
principal_id="my_principalID",
principal_idns="my_principalIDNS",
agent="foo",
)
assert token._token_headers() == {
Expand All @@ -254,8 +176,6 @@ def test_auth(self, mock_successful_post_token_response):
key="my_key",
secret="my_secret",
scopes="scope1",
principal_id="my_principalID",
principal_idns="my_principalIDNS",
agent="foo",
)
assert token._auth() == ("my_key", "my_secret")
Expand All @@ -274,15 +194,11 @@ def test_payload(self, mock_successful_post_token_response):
key="my_key",
secret="my_secret",
scopes="scope1",
principal_id="my_principalID",
principal_idns="my_principalIDNS",
agent="foo",
)
assert token._payload() == {
"grant_type": "client_credentials",
"scope": "scope1",
"principalID": "my_principalID",
"principalIDNS": "my_principalIDNS",
}

def test_post_token_request_timout(self, mock_credentials, mock_timeout):
Expand All @@ -292,8 +208,6 @@ def test_post_token_request_timout(self, mock_credentials, mock_timeout):
key=creds["key"],
secret=creds["secret"],
scopes=creds["scopes"],
principal_id=creds["principal_id"],
principal_idns=creds["principal_idns"],
)

def test_post_token_request_connectionerror(
Expand All @@ -305,8 +219,6 @@ def test_post_token_request_connectionerror(
key=creds["key"],
secret=creds["secret"],
scopes=creds["scopes"],
principal_id=creds["principal_id"],
principal_idns=creds["principal_idns"],
)

def test_post_token_request_unexpectederror(
Expand All @@ -318,8 +230,6 @@ def test_post_token_request_unexpectederror(
key=creds["key"],
secret=creds["secret"],
scopes=creds["scopes"],
principal_id=creds["principal_id"],
principal_idns=creds["principal_idns"],
)

def test_invalid_post_token_request(
Expand All @@ -331,8 +241,6 @@ def test_invalid_post_token_request(
key=creds["key"],
secret=creds["secret"],
scopes=creds["scopes"],
principal_id=creds["principal_id"],
principal_idns=creds["principal_idns"],
)

def test_is_expired_false(
Expand All @@ -343,8 +251,6 @@ def test_is_expired_false(
key=creds["key"],
secret=creds["secret"],
scopes=creds["scopes"],
principal_id=creds["principal_id"],
principal_idns=creds["principal_idns"],
)
assert token.is_expired() is False

Expand Down Expand Up @@ -376,8 +282,6 @@ def test_post_token_request(
key=creds["key"],
secret=creds["secret"],
scopes=creds["scopes"],
principal_id=creds["principal_id"],
principal_idns=creds["principal_idns"],
)
assert token.token_str == "tk_Yebz4BpEp9dAsghA7KpWx6dYD1OZKWBlHjqW"
assert token.token_type == "bearer"
Expand All @@ -404,17 +308,13 @@ def test_cred_in_env_variables(self, live_keys):
assert os.getenv("WCKey") is not None
assert os.getenv("WCSecret") is not None
assert os.getenv("WCScopes") == "WorldCatMetadataAPI"
assert os.getenv("WCPrincipalID") is not None
assert os.getenv("WCPrincipalIDNS") is not None

@pytest.mark.webtest
def test_post_token_request_with_live_service(self, live_keys):
token = WorldcatAccessToken(
key=os.getenv("WCKey"),
secret=os.getenv("WCSecret"),
scopes=os.getenv("WCScopes"),
principal_id=os.getenv("WCPrincipalID"),
principal_idns=os.getenv("WCPrincipalIDNS"),
)

assert token.server_response.status_code == 200
Expand Down
Loading

0 comments on commit ab1e6df

Please sign in to comment.