test_key.py 1021 B

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