Skip to content

Commit

Permalink
utc fixes (#73)
Browse files Browse the repository at this point in the history
* fixed datetime type errors

* moved datetime edits to _hasten_expiration_time
  • Loading branch information
charlottekostelic authored Feb 14, 2024
1 parent bd18297 commit cc786ab
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
1 change: 1 addition & 0 deletions bookops_worldcat/authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def _hasten_expiration_time(self, utc_stamp_str: str) -> datetime.datetime:
utcstamp = datetime.datetime.strptime(
utc_stamp_str, "%Y-%m-%d %H:%M:%SZ"
) - datetime.timedelta(seconds=1)
utcstamp = utcstamp.replace(tzinfo=datetime.timezone.utc)
return utcstamp

def _parse_server_response(self, response: requests.Response) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def live_keys():
class FakeUtcNow(datetime.datetime):
@classmethod
def now(cls, tzinfo=datetime.timezone.utc):
return cls(2020, 1, 1, 17, 0, 0, 0)
return cls(2020, 1, 1, 17, 0, 0, 0, tzinfo=datetime.timezone.utc)


@pytest.fixture
Expand Down
4 changes: 3 additions & 1 deletion tests/test_authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ def test_hasten_expiration_time(self, mock_token):
token = mock_token
timestamp = token._hasten_expiration_time(utc_stamp)
assert isinstance(timestamp, datetime.datetime)
assert timestamp == datetime.datetime(2020, 1, 1, 17, 19, 58, 0)
assert timestamp == datetime.datetime(
2020, 1, 1, 17, 19, 58, 0, tzinfo=datetime.timezone.utc
)

def test_payload(self, mock_successful_post_token_response):
token = WorldcatAccessToken(
Expand Down
30 changes: 15 additions & 15 deletions tests/test_metadata_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_get_new_access_token(self, mock_token, mock_now):
assert session.authorization.is_expired() is True
session._get_new_access_token()
assert session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert session.authorization.is_expired() is False

Expand Down Expand Up @@ -209,7 +209,7 @@ def test_get_brief_bib_with_stale_token(
assert stub_session.authorization.is_expired() is True
response = stub_session.get_brief_bib(oclcNumber=12345)
assert stub_session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert stub_session.authorization.is_expired() is False
assert response.status_code == 200
Expand Down Expand Up @@ -254,7 +254,7 @@ def test_get_full_bib_with_stale_token(self, stub_session, mock_session_response
response = stub_session.get_full_bib(12345)
assert stub_session.authorization.is_expired() is False
assert stub_session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert response.status_code == 200

Expand All @@ -281,7 +281,7 @@ def test_holding_get_status_with_stale_token(
response = stub_session.holding_get_status(12345)
assert stub_session.authorization.is_expired() is False
assert stub_session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert response.status_code == 200

Expand All @@ -305,7 +305,7 @@ def test_holding_set_stale_token(self, stub_session, mock_session_response):
assert stub_session.authorization.is_expired() is True
response = stub_session.holding_set(850940548)
assert stub_session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert stub_session.authorization.is_expired() is False
assert response.status_code == 201
Expand All @@ -332,7 +332,7 @@ def test_holding_unset_stale_token(
assert stub_session.authorization.is_expired() is True
response = stub_session.holding_unset(850940548)
assert stub_session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert stub_session.authorization.is_expired() is False
assert response.status_code == 200
Expand Down Expand Up @@ -370,7 +370,7 @@ def test_holdings_set_stale_token(
assert stub_session.authorization.is_expired() is True
stub_session.holdings_set([850940548, 850940552, 850940554])
assert stub_session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert stub_session.authorization.is_expired() is False

Expand Down Expand Up @@ -409,7 +409,7 @@ def test_holdings_unset_stale_token(
assert stub_session.authorization.is_expired() is True
stub_session.holdings_unset([850940548, 850940552, 850940554])
assert stub_session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert stub_session.authorization.is_expired() is False

Expand Down Expand Up @@ -447,7 +447,7 @@ def test_holdings_set_multi_institutions_stale_token(
oclcNumber=850940548, instSymbols="NYP,BKL"
)
assert stub_session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert stub_session.authorization.is_expired() is False

Expand Down Expand Up @@ -501,7 +501,7 @@ def test_holdings_unset_multi_institutions_stale_token(
oclcNumber=850940548, instSymbols="NYP,BKL"
)
assert stub_session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert stub_session.authorization.is_expired() is False

Expand All @@ -521,7 +521,7 @@ def test_search_brief_bibs_other_editions_stale_token(
assert stub_session.authorization.is_expired() is True
response = stub_session.search_brief_bib_other_editions(12345)
assert stub_session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert stub_session.authorization.is_expired() is False
assert response.status_code == 200
Expand Down Expand Up @@ -552,7 +552,7 @@ def test_search_brief_bibs_with_stale_token(
assert stub_session.authorization.is_expired() is True
response = stub_session.search_brief_bibs(q="ti:foo")
assert stub_session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert stub_session.authorization.is_expired() is False
assert response.status_code == 200
Expand Down Expand Up @@ -594,7 +594,7 @@ def test_search_current_control_numbers_with_stale_token(
assert stub_session.authorization.is_expired() is True
response = stub_session.search_current_control_numbers(["12345", "65891"])
assert stub_session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert stub_session.authorization.is_expired() is False
assert response.status_code == 207
Expand Down Expand Up @@ -625,7 +625,7 @@ def test_search_general_holdings_with_stale_token(
assert stub_session.authorization.is_expired() is True
response = stub_session.search_general_holdings(oclcNumber=12345)
assert stub_session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert stub_session.authorization.is_expired() is False
assert response.status_code == 200
Expand Down Expand Up @@ -661,7 +661,7 @@ def test_search_shared_print_holdings_with_stale_token(
assert stub_session.authorization.is_expired() is True
response = stub_session.search_shared_print_holdings(oclcNumber=12345)
assert stub_session.authorization.token_expires_at == datetime.datetime(
2020, 1, 1, 17, 19, 58
2020, 1, 1, 17, 19, 58, tzinfo=datetime.timezone.utc
)
assert stub_session.authorization.is_expired() is False
assert response.status_code == 200
Expand Down

0 comments on commit cc786ab

Please sign in to comment.