def test_creation(self): """Ensure that no two tokens are identical.""" s = TimedSecretLinkSerializer( expires_at=datetime.now()+timedelta(days=1)) t1 = s.create_token(98765, dict(recid=4321)) t2 = s.create_token(98765, dict(recid=4321)) self.assertNotEqual(t1, t2)
def test_timed_secretlink_serializer_creation(app, db): """Ensure that no two tokens are identical.""" with app.app_context(): s = TimedSecretLinkSerializer( expires_at=datetime.utcnow()+timedelta(days=1)) t1 = s.create_token(98765, dict(recid=4321)) t2 = s.create_token(98765, dict(recid=4321)) assert t1 != t2
def test_expired(self): """Test token expiry.""" s = TimedSecretLinkSerializer( expires_at=datetime.now()-timedelta(seconds=20)) t = s.create_token(1, dict(recid=1)) self.assertIsNone(s.validate_token(t)) self.assertIsNone(s.validate_token(t, expected_data=dict(recid=1))) self.assertRaises(SignatureExpired, s.load_token, t) self.assertIsNotNone(s.load_token(t, force=True))
def test_create_validate(self): """Test token creation.""" s = TimedSecretLinkSerializer( expires_at=datetime.now()+timedelta(days=1)) t = s.create_token(1234, dict(recid=56789)) data = s.validate_token(t, expected_data=dict(recid=56789)) self.assertEqual(data['id'], 1234) self.assertEqual(data['data']['recid'], 56789) self.assertIsNone(s.validate_token(t, expected_data=dict(recid=1)))
def test_timed_secretlink_serializer_create_validate(app, db): """Test token creation.""" with app.app_context(): s = TimedSecretLinkSerializer( expires_at=datetime.utcnow()+timedelta(days=1)) t = s.create_token(1234, dict(recid=56789)) data = s.validate_token(t, expected_data=dict(recid=56789)) assert data['id'] == 1234 assert data['data']['recid'] == 56789 assert s.validate_token(t, expected_data=dict(recid=1)) is None
def test_timed_secretlink_serializer_expired(app, db): """Test token expiry.""" with app.app_context(): s = TimedSecretLinkSerializer( expires_at=datetime.utcnow()-timedelta(seconds=20)) t = s.create_token(1, dict(recid=1)) assert s.validate_token(t) is None assert s.validate_token(t, expected_data=dict(recid=1)) is None with pytest.raises(SignatureExpired): s.load_token(t) assert s.load_token(t, force=True) is not None
def test_secretlink_factory_creation(app, db): """Test token creation.""" extra_data = dict(recid='1') with app.app_context(): d = datetime.utcnow()+timedelta(days=1) t = SecretLinkFactory.create_token(1, extra_data) assert SecretLinkSerializer().validate_token( t, expected_data=extra_data) is not None assert TimedSecretLinkSerializer().validate_token( t, expected_data=extra_data) is None t1 = SecretLinkFactory.create_token( 1, extra_data, expires_at=d ) t2 = SecretLinkFactory.create_token(1, extra_data) assert SecretLinkSerializer().validate_token( t1, expected_data=extra_data) is None assert TimedSecretLinkSerializer().validate_token( t1, expected_data=extra_data) is not None assert t1 != t2