test_key.py 898 B

123456789101112131415161718192021222324252627282930
  1. import pytest
  2. from celery.exceptions import SecurityError
  3. from celery.security.key import PrivateKey
  4. from . import CERT1, KEY1, KEY2
  5. from .case import SecurityCase
  6. class test_PrivateKey(SecurityCase):
  7. def test_valid_private_key(self):
  8. PrivateKey(KEY1)
  9. PrivateKey(KEY2)
  10. def test_invalid_private_key(self):
  11. with pytest.raises((SecurityError, TypeError)):
  12. PrivateKey(None)
  13. with pytest.raises(SecurityError):
  14. PrivateKey('')
  15. with pytest.raises(SecurityError):
  16. PrivateKey('foo')
  17. with pytest.raises(SecurityError):
  18. PrivateKey(KEY1[:20] + KEY1[21:])
  19. with pytest.raises(SecurityError):
  20. PrivateKey(CERT1)
  21. def test_sign(self):
  22. pkey = PrivateKey(KEY1)
  23. pkey.sign('test', b'sha1')
  24. with pytest.raises(ValueError):
  25. pkey.sign('test', b'unknown')