Skip to content

Commit

Permalink
test: Test auth token expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Dec 5, 2023
1 parent 9b4f96c commit badb905
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions tests/core/rest/test_authenticators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

from __future__ import annotations

import datetime
import typing as t

import jwt
import pytest
import time_machine
from cryptography.hazmat.primitives.asymmetric.rsa import (
RSAPrivateKey,
RSAPublicKey,
Expand Down Expand Up @@ -125,7 +127,7 @@ def test_oauth_authenticator_token_expiry_handling(
requests_mock: requests_mock.Mocker,
oauth_response_expires_in: int,
default_expiration: int,
result: bool,
result: int | None,
):
"""Validate various combinations of expires_in and default_expiration."""
response = {"access_token": "an-access-token"}
Expand All @@ -143,10 +145,26 @@ def test_oauth_authenticator_token_expiry_handling(
auth_endpoint="https://example.com/oauth",
default_expiration=default_expiration,
)
authenticator.update_access_token()
with time_machine.travel(
datetime.datetime(2023, 1, 1, tzinfo=datetime.timezone.utc),
tick=False,
):
authenticator.update_access_token()

assert authenticator.expires_in == result

with time_machine.travel(
datetime.datetime(2023, 1, 1, 0, 1, tzinfo=datetime.timezone.utc),
tick=False,
):
assert authenticator.is_token_valid()

with time_machine.travel(
datetime.datetime(2023, 1, 1, 0, 5, tzinfo=datetime.timezone.utc),
tick=False,
):
assert not authenticator.expires_in or not authenticator.is_token_valid()


@pytest.fixture
def private_key() -> RSAPrivateKey:
Expand Down

0 comments on commit badb905

Please sign in to comment.