An ebook/comic library service and web client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.4 KiB

  1. from datetime import datetime, timedelta
  2. import pytest
  3. from mock import patch, MagicMock
  4. from nacl.exceptions import InvalidkeyError
  5. from atheneum import errors
  6. from atheneum.model import User, UserToken
  7. from atheneum.service.authentication_service import (
  8. validate_password_strength,
  9. is_valid_token,
  10. is_valid_password
  11. )
  12. service_module = 'atheneum.service.authentication_service'
  13. def test_validate_password_strength_happy_path():
  14. valid_password = "HorseStapleBattery9"
  15. assert valid_password == validate_password_strength(valid_password)
  16. def test_validate_password_strength_length_failure():
  17. invalid_password = "TooShor"
  18. with pytest.raises(errors.ValidationError) as e_info:
  19. error = e_info
  20. validate_password_strength(invalid_password)
  21. assert error is not None
  22. def test_validate_password_strength_uppercase_failure():
  23. invalid_password = "NOUPPERCASE9"
  24. with pytest.raises(errors.ValidationError) as e_info:
  25. error = e_info
  26. validate_password_strength(invalid_password)
  27. assert error is not None
  28. def test_validate_password_strength_lowercase_failure():
  29. invalid_password = "NOLOWERCASE9"
  30. with pytest.raises(errors.ValidationError) as e_info:
  31. error = e_info
  32. validate_password_strength(invalid_password)
  33. assert error is not None
  34. def test_validate_password_strength_number_failure():
  35. invalid_password = "NoNumber"
  36. with pytest.raises(errors.ValidationError) as e_info:
  37. error = e_info
  38. validate_password_strength(invalid_password)
  39. assert error is not None
  40. @patch(service_module + '.pwhash.verify')
  41. def test_is_valid_password_invalid_key_error(
  42. mock_pwhash_verify: MagicMock):
  43. user = User()
  44. user.password_hash = ''
  45. mock_pwhash_verify.side_effect = InvalidkeyError()
  46. assert not is_valid_password(user, '')
  47. def test_is_valid_token_happy_path():
  48. user_token = UserToken()
  49. user_token.enabled = True
  50. assert is_valid_token(user_token)
  51. def test_is_valid_token_no_token():
  52. assert not is_valid_token(None)
  53. def test_is_valid_token_disabled():
  54. user_token = UserToken()
  55. user_token.enabled = False
  56. assert not is_valid_token(user_token)
  57. def test_is_valid_token_expired():
  58. user_token = UserToken()
  59. user_token.enabled = True
  60. user_token.expiration_time = datetime.now() - timedelta(weeks=1)
  61. assert not is_valid_token(user_token)