Files
PyAPNs2/test/test_credentials.py
T
shintonik fbcf36f955 Use only one JWT token per connection (#80)
This fixes #76 where you a TooManyProviderTokenUpdates if you send
notifications to multiple topics over one connection.

As Apple states here https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_token-based_connection_to_apns#2943374
one connection shall use only one single JWT token which should be
refreshed no more than once every 20 minutes and no less than once every
60 minutes.
2019-05-23 02:01:31 +03:00

54 lines
1.9 KiB
Python

# This only tests the TokenCredentials test case, since the
# CertificateCredentials would be mocked out anyway.
# Namely:
# - timing out of the token
# - creating multiple tokens for different topics
from unittest import TestCase, main
from freezegun import freeze_time
import time
from apns2.credentials import TokenCredentials
class TokenCredentialsTestCase(TestCase):
@classmethod
def setUpClass(cls):
cls.key_path = 'test/eckey.pem'
cls.team_id = '3Z24IP123A'
cls.key_id = '1QBCDJ9RST'
cls.topics = ('com.example.first_app', 'com.example.second_app',)
cls.token_lifetime = 0.5
def setUp(self):
# Create an 'ephemeral' token so we can test token timeouts. We
# want a timeout long enough to last the test, but we don't want to
# slow down the tests too much either.
self.normal_creds = TokenCredentials(self.key_path, self.key_id,
self.team_id)
self.lasting_header = self.normal_creds.get_authorization_header(
self.topics[0])
with freeze_time('2012-01-14'):
self.expiring_creds = \
TokenCredentials(self.key_path, self.key_id,
self.team_id,
token_lifetime=self.token_lifetime)
self.expiring_header = self.expiring_creds.get_authorization_header(
self.topics[0])
def test_token_expiration(self):
# 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...
time.sleep(self.token_lifetime)
h3 = self.expiring_creds.get_authorization_header(self.topics[0])
self.assertNotEqual(self.expiring_header, h3)
if __name__ == '__main__':
main()