test_key.py 961 B

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