From ab1e6dfb827d73ed7c7345b784ceaf4fb123eb00 Mon Sep 17 00:00:00 2001 From: Charlotte Kostelic Date: Fri, 23 Feb 2024 14:16:12 -0500 Subject: [PATCH] Removed principalID and principalIDNS from token requests (#79) * removed unnecessary params from token requests * fixed docstring --- bookops_worldcat/authorize.py | 22 -------- tests/conftest.py | 4 -- tests/test_authorize.py | 100 ---------------------------------- tests/test_metadata_api.py | 22 -------- tests/test_query.py | 2 - 5 files changed, 150 deletions(-) diff --git a/bookops_worldcat/authorize.py b/bookops_worldcat/authorize.py index 4b3d3bb..2ed33a0 100644 --- a/bookops_worldcat/authorize.py +++ b/bookops_worldcat/authorize.py @@ -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 @@ -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" @@ -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]] @@ -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 @@ -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(): @@ -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: diff --git a/tests/conftest.py b/tests/conftest.py index e78db1e..72522d3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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): @@ -135,8 +133,6 @@ def mock_credentials(): "key": "my_WSkey", "secret": "my_WSsecret", "scopes": "scope1 scope2", - "principal_id": "my_principalID", - "principal_idns": "my_principalIDNS", } diff --git a/tests/test_authorize.py b/tests/test_authorize.py index f9b1f79..07625bb 100644 --- a/tests/test_authorize.py +++ b/tests/test_authorize.py @@ -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) @@ -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) @@ -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", [ @@ -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) @@ -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 @@ -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 @@ -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" @@ -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() == { @@ -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") @@ -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): @@ -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( @@ -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( @@ -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( @@ -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( @@ -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 @@ -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" @@ -404,8 +308,6 @@ 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): @@ -413,8 +315,6 @@ def test_post_token_request_with_live_service(self, live_keys): 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 diff --git a/tests/test_metadata_api.py b/tests/test_metadata_api.py index b6b63c2..8626b4c 100644 --- a/tests/test_metadata_api.py +++ b/tests/test_metadata_api.py @@ -538,8 +538,6 @@ def test_get_brief_bib_print_mat_request(self, live_keys): key=os.getenv("WCKey"), secret=os.getenv("WCSecret"), scopes=os.getenv("WCScopes"), - principal_id=os.getenv("WCPrincipalID"), - principal_idns=os.getenv("WCPrincipalIDNS"), ) with MetadataSession(authorization=token) as session: @@ -553,8 +551,6 @@ def test_get_brief_bib_401_error(self, live_keys): key=os.getenv("WCKey"), secret=os.getenv("WCSecret"), scopes=os.getenv("WCScopes"), - principal_id=os.getenv("WCPrincipalID"), - principal_idns=os.getenv("WCPrincipalIDNS"), ) token.token_str = "invalid-token" err_msg = "401 Client Error: Unauthorized for url: https://metadata.api.oclc.org/worldcat/search/brief-bibs/41266045" @@ -570,8 +566,6 @@ def test_get_full_bib(self, live_keys): key=os.getenv("WCKey"), secret=os.getenv("WCSecret"), scopes=os.getenv("WCScopes"), - principal_id=os.getenv("WCPrincipalID"), - principal_idns=os.getenv("WCPrincipalIDNS"), ) with MetadataSession(authorization=token) as session: @@ -588,8 +582,6 @@ def test_get_institution_holdings(self, live_keys): key=os.getenv("WCKey"), secret=os.getenv("WCSecret"), scopes=os.getenv("WCScopes"), - principal_id=os.getenv("WCPrincipalID"), - principal_idns=os.getenv("WCPrincipalIDNS"), ) with MetadataSession(authorization=token) as session: @@ -616,8 +608,6 @@ def test_holding_set_unset(self, live_keys): key=os.getenv("WCKey"), secret=os.getenv("WCSecret"), scopes=os.getenv("WCScopes"), - principal_id=os.getenv("WCPrincipalID"), - principal_idns=os.getenv("WCPrincipalIDNS"), ) with MetadataSession(authorization=token) as session: @@ -651,8 +641,6 @@ def test_holdings_set(self, live_keys): key=os.getenv("WCKey"), secret=os.getenv("WCSecret"), scopes=os.getenv("WCScopes"), - principal_id=os.getenv("WCPrincipalID"), - principal_idns=os.getenv("WCPrincipalIDNS"), ) with MetadataSession(authorization=token) as session: @@ -685,8 +673,6 @@ def test_holdings_unset(self, live_keys): key=os.getenv("WCKey"), secret=os.getenv("WCSecret"), scopes=os.getenv("WCScopes"), - principal_id=os.getenv("WCPrincipalID"), - principal_idns=os.getenv("WCPrincipalIDNS"), ) with MetadataSession(authorization=token) as session: @@ -719,8 +705,6 @@ def test_brief_bibs_other_editions(self, live_keys): key=os.getenv("WCKey"), secret=os.getenv("WCSecret"), scopes=os.getenv("WCScopes"), - principal_id=os.getenv("WCPrincipalID"), - principal_idns=os.getenv("WCPrincipalIDNS"), ) with MetadataSession(authorization=token) as session: @@ -735,8 +719,6 @@ def test_search_brief_bibs(self, live_keys): key=os.getenv("WCKey"), secret=os.getenv("WCSecret"), scopes=os.getenv("WCScopes"), - principal_id=os.getenv("WCPrincipalID"), - principal_idns=os.getenv("WCPrincipalIDNS"), ) with MetadataSession(authorization=token) as session: @@ -763,8 +745,6 @@ def test_search_bibs_holdings(self, live_keys): key=os.getenv("WCKey"), secret=os.getenv("WCSecret"), scopes=os.getenv("WCScopes"), - principal_id=os.getenv("WCPrincipalID"), - principal_idns=os.getenv("WCPrincipalIDNS"), ) with MetadataSession(authorization=token) as session: @@ -778,8 +758,6 @@ def test_get_current_oclc_number(self, live_keys): key=os.getenv("WCKey"), secret=os.getenv("WCSecret"), scopes=os.getenv("WCScopes"), - principal_id=os.getenv("WCPrincipalID"), - principal_idns=os.getenv("WCPrincipalIDNS"), ) with MetadataSession(authorization=token) as session: diff --git a/tests/test_query.py b/tests/test_query.py index 7725145..b4f626b 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -18,8 +18,6 @@ def test_query_live(live_keys): key=os.getenv("WCKey"), secret=os.getenv("WCSecret"), scopes=os.getenv("WCScopes"), - principal_id=os.getenv("WCPrincipalID"), - principal_idns=os.getenv("WCPrincipalIDNS"), ) with MetadataSession(authorization=token) as session: header = {"Accept": "application/json"}