Using pytest from now on

This commit is contained in:
Sergey Petrov
2019-06-02 19:39:40 +03:00
parent fbcf36f955
commit be7438a653
9 changed files with 207 additions and 230 deletions
+18 -41
View File
@@ -4,50 +4,27 @@
# - timing out of the token
# - creating multiple tokens for different topics
from unittest import TestCase, main
import pytest
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)
TOPIC = 'com.example.first_app'
if __name__ == '__main__':
main()
@pytest.fixture
def token_credentials():
return TokenCredentials('test/eckey.pem', '1QBCDJ9RST', '3Z24IP123A')
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