diff --git a/apns2/credentials.py b/apns2/credentials.py index 77fd54f..592dd8e 100644 --- a/apns2/credentials.py +++ b/apns2/credentials.py @@ -60,9 +60,8 @@ def get_authorization_header(self, topic: Optional[str]) -> str: token = self._get_or_create_topic_token() return 'bearer %s' % token - @staticmethod - def _is_expired_token(issue_date: float) -> bool: - return time.time() > issue_date + DEFAULT_TOKEN_LIFETIME + def _is_expired_token(self, issue_date: float) -> bool: + return time.time() > issue_date + self.__token_lifetime @staticmethod def _get_signing_key(key_path: str) -> str: diff --git a/test/test_credentials.py b/test/test_credentials.py index 9c4525b..21b1eab 100644 --- a/test/test_credentials.py +++ b/test/test_credentials.py @@ -14,17 +14,24 @@ @pytest.fixture def token_credentials(): - return TokenCredentials('test/eckey.pem', '1QBCDJ9RST', '3Z24IP123A') + return TokenCredentials( + auth_key_path='test/eckey.pem', + auth_key_id='1QBCDJ9RST', + team_id='3Z24IP123A', + token_lifetime=30, # seconds + ) def test_token_expiration(token_credentials): - # As long as the token lifetime hasn't elapsed, this should work. To - # be really careful, we should check how much time has elapsed to - # know if it fail. But, either way, we'd have to come up with a good - # lifetime for future tests... - - with freeze_time('2012-01-14'): - expiring_header = token_credentials.get_authorization_header(TOPIC) - - new_header = token_credentials.get_authorization_header(TOPIC) - assert expiring_header != new_header + with freeze_time('2012-01-14 12:00:00'): + header1 = token_credentials.get_authorization_header(TOPIC) + + # 20 seconds later, before expiration, same JWT + with freeze_time('2012-01-14 12:00:20'): + header2 = token_credentials.get_authorization_header(TOPIC) + assert header1 == header2 + + # 35 seconds later, after expiration, new JWT + with freeze_time('2012-01-14 12:00:40'): + header3 = token_credentials.get_authorization_header(TOPIC) + assert header3 != header1