Skip to content

Commit

Permalink
changed utcnow to now(timezone.utc)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlottekostelic committed Feb 8, 2024
1 parent a0ba334 commit acbd928
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 77 deletions.
2 changes: 1 addition & 1 deletion bookops_worldcat/authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def is_expired(self) -> bool:
False
"""
if isinstance(self.token_expires_at, datetime.datetime):
if self.token_expires_at < datetime.datetime.utcnow():
if self.token_expires_at < datetime.datetime.now(datetime.timezone.utc):
return True
else:
return False
Expand Down
10 changes: 5 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ def live_keys():

class FakeUtcNow(datetime.datetime):
@classmethod
def utcnow(cls):
def now(cls, tzinfo=datetime.timezone.utc):
return cls(2020, 1, 1, 17, 0, 0, 0)


@pytest.fixture
def mock_utcnow(monkeypatch):
def mock_now(monkeypatch):
monkeypatch.setattr(datetime, "datetime", FakeUtcNow)


Expand All @@ -44,7 +44,7 @@ def __init__(self):

def json(self):
expires_at = datetime.datetime.strftime(
datetime.datetime.utcnow() + datetime.timedelta(0, 1199),
datetime.datetime.now() + datetime.timedelta(0, 1199),
"%Y-%m-%d %H:%M:%SZ",
)

Expand Down Expand Up @@ -141,12 +141,12 @@ def mock_credentials():


@pytest.fixture
def mock_oauth_server_response(mock_utcnow, *args, **kwargs):
def mock_oauth_server_response(mock_now, *args, **kwargs):
return MockAuthServerResponseSuccess()


@pytest.fixture
def mock_successful_post_token_response(mock_utcnow, monkeypatch):
def mock_successful_post_token_response(mock_now, monkeypatch):
def mock_oauth_server_response(*args, **kwargs):
return MockAuthServerResponseSuccess()

Expand Down
12 changes: 6 additions & 6 deletions tests/test_authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def test_invalid_post_token_request(
)

def test_is_expired_false(
self, mock_utcnow, mock_credentials, mock_successful_post_token_response
self, mock_now, mock_credentials, mock_successful_post_token_response
):
creds = mock_credentials
token = WorldcatAccessToken(
Expand All @@ -346,11 +346,11 @@ def test_is_expired_false(
)
assert token.is_expired() is False

def test_is_expired_true(self, mock_utcnow, mock_token):
def test_is_expired_true(self, mock_now, mock_token):
mock_token.is_expired() is False
mock_token.token_expires_at = datetime.datetime.utcnow() - datetime.timedelta(
0, 1
)
mock_token.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)

assert mock_token.is_expired() is True

Expand Down Expand Up @@ -390,7 +390,7 @@ def test_post_token_request(
def test_token_repr(
self,
mock_token,
mock_utcnow,
mock_now,
):
assert (
str(mock_token)
Expand Down
122 changes: 61 additions & 61 deletions tests/test_metadata_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ def test_invalid_authorizaiton(self):
MetadataSession(authorization="my_token")
assert err_msg in str(exc.value)

def test_get_new_access_token(self, mock_token, mock_utcnow):
def test_get_new_access_token(self, mock_token, mock_now):
assert mock_token.is_expired() is False
with MetadataSession(authorization=mock_token) as session:
session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
assert session.authorization.is_expired() is True
session._get_new_access_token()
assert session.authorization.token_expires_at == datetime.datetime(
Expand Down Expand Up @@ -201,11 +201,11 @@ def test_get_brief_bib_None_oclcNumber_passed(self, stub_session):

@pytest.mark.http_code(200)
def test_get_brief_bib_with_stale_token(
self, mock_utcnow, stub_session, mock_session_response
self, mock_now, stub_session, mock_session_response
):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
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(
Expand Down Expand Up @@ -246,9 +246,9 @@ def test_get_full_bib_None_oclcNumber_passed(self, stub_session):

@pytest.mark.http_code(200)
def test_get_full_bib_with_stale_token(self, stub_session, mock_session_response):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)

assert stub_session.authorization.is_expired() is True
response = stub_session.get_full_bib(12345)
Expand All @@ -274,9 +274,9 @@ def test_holding_get_status_None_oclcNumber_passed(self, stub_session):
def test_holding_get_status_with_stale_token(
self, stub_session, mock_session_response
):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
assert stub_session.authorization.is_expired() is True
response = stub_session.holding_get_status(12345)
assert stub_session.authorization.is_expired() is False
Expand All @@ -299,9 +299,9 @@ def test_holding_set_None_oclcNumber_passed(self, stub_session):

@pytest.mark.http_code(201)
def test_holding_set_stale_token(self, stub_session, mock_session_response):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
assert stub_session.authorization.is_expired() is True
response = stub_session.holding_set(850940548)
assert stub_session.authorization.token_expires_at == datetime.datetime(
Expand All @@ -324,11 +324,11 @@ def test_holding_unset_None_oclcNumber_passed(self, stub_session):

@pytest.mark.http_code(200)
def test_holding_unset_stale_token(
self, mock_utcnow, stub_session, mock_session_response
self, mock_now, stub_session, mock_session_response
):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
assert stub_session.authorization.is_expired() is True
response = stub_session.holding_unset(850940548)
assert stub_session.authorization.token_expires_at == datetime.datetime(
Expand Down Expand Up @@ -361,11 +361,11 @@ def test_holdings_set_no_oclcNumber_passed(self, stub_session):

@pytest.mark.http_code(207)
def test_holdings_set_stale_token(
self, mock_utcnow, stub_session, mock_session_response
self, mock_now, stub_session, mock_session_response
):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
with does_not_raise():
assert stub_session.authorization.is_expired() is True
stub_session.holdings_set([850940548, 850940552, 850940554])
Expand Down Expand Up @@ -400,11 +400,11 @@ def test_holdings_unset_no_oclcNumber_passed(self, stub_session):

@pytest.mark.http_code(207)
def test_holdings_unset_stale_token(
self, mock_utcnow, stub_session, mock_session_response
self, mock_now, stub_session, mock_session_response
):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
with does_not_raise():
assert stub_session.authorization.is_expired() is True
stub_session.holdings_unset([850940548, 850940552, 850940554])
Expand Down Expand Up @@ -436,11 +436,11 @@ def test_holdings_set_multi_institutions_invalid_oclc_number(self, stub_session)

@pytest.mark.http_code(200)
def test_holdings_set_multi_institutions_stale_token(
self, mock_utcnow, stub_session, mock_session_response
self, mock_now, stub_session, mock_session_response
):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
with does_not_raise():
assert stub_session.authorization.is_expired() is True
stub_session.holdings_set_multi_institutions(
Expand Down Expand Up @@ -490,11 +490,11 @@ def test_holdings_unset_multi_institutions_invalid_oclc_number(self, stub_sessio

@pytest.mark.http_code(200)
def test_holdings_unset_multi_institutions_stale_token(
self, mock_utcnow, stub_session, mock_session_response
self, mock_now, stub_session, mock_session_response
):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
with does_not_raise():
assert stub_session.authorization.is_expired() is True
stub_session.holdings_unset_multi_institutions(
Expand All @@ -513,11 +513,11 @@ def test_search_brief_bibs_other_editions(

@pytest.mark.http_code(200)
def test_search_brief_bibs_other_editions_stale_token(
self, mock_utcnow, stub_session, mock_session_response
self, mock_now, stub_session, mock_session_response
):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
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(
Expand All @@ -544,11 +544,11 @@ def test_search_brief_bibs_missing_query(self, stub_session, argm):

@pytest.mark.http_code(200)
def test_search_brief_bibs_with_stale_token(
self, mock_utcnow, stub_session, mock_session_response
self, mock_now, stub_session, mock_session_response
):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
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(
Expand Down Expand Up @@ -586,11 +586,11 @@ def test_search_current_control_numbers_missing_numbers(self, stub_session, argm

@pytest.mark.http_code(207)
def test_search_current_control_numbers_with_stale_token(
self, mock_utcnow, stub_session, mock_session_response
self, mock_now, stub_session, mock_session_response
):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
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(
Expand All @@ -617,11 +617,11 @@ def test_search_general_holdings_invalid_oclc_number(self, stub_session):

@pytest.mark.http_code(200)
def test_search_general_holdings_with_stale_token(
self, mock_utcnow, stub_session, mock_session_response
self, mock_now, stub_session, mock_session_response
):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
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(
Expand Down Expand Up @@ -653,11 +653,11 @@ def test_search_shared_print_holdings_with_invalid_oclc_number_passsed(

@pytest.mark.http_code(200)
def test_search_shared_print_holdings_with_stale_token(
self, mock_utcnow, stub_session, mock_session_response
self, mock_now, stub_session, mock_session_response
):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
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(
Expand Down Expand Up @@ -716,7 +716,7 @@ def test_get_brief_bib_401_error(self, live_keys):
token.token_str = "invalid-token"
err_msg = "401 Client Error: Unauthorized for url: https://americas.metadata.api.oclc.org/worldcat/search/v1/brief-bibs/41266045"
with MetadataSession(authorization=token) as session:
session.headers.update({"Authorization": f"Bearer invalid-token"})
session.headers.update({"Authorization": "Bearer invalid-token"})
with pytest.raises(WorldcatRequestError) as exc:
session.get_brief_bib(41266045)

Expand All @@ -732,9 +732,9 @@ def test_get_brief_bib_with_stale_token(self, live_keys):
)
with MetadataSession(authorization=token) as session:
session.authorization.is_expired() is False
session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
assert session.authorization.is_expired() is True
response = session.get_brief_bib(oclcNumber=41266045)
assert session.authorization.is_expired() is False
Expand Down
8 changes: 4 additions & 4 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ def test_query_not_prepared_request(stub_session):


@pytest.mark.http_code(200)
def test_query_with_stale_token(stub_session, mock_utcnow, mock_session_response):
stub_session.authorization.token_expires_at = (
datetime.datetime.utcnow() - datetime.timedelta(0, 1)
)
def test_query_with_stale_token(stub_session, mock_now, mock_session_response):
stub_session.authorization.token_expires_at = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(0, 1)
assert stub_session.authorization.is_expired() is True

req = Request("GET", "http://foo.org")
Expand Down

0 comments on commit acbd928

Please sign in to comment.