from datetime import datetime, timedelta import pytest from mock import patch, MagicMock from nacl.exceptions import InvalidkeyError from atheneum import errors from atheneum.model import User, UserToken from atheneum.service.authentication_service import ( validate_password_strength, is_valid_token, is_valid_password ) service_module = 'atheneum.service.authentication_service' def test_validate_password_strength_happy_path(): valid_password = "HorseStapleBattery9" assert valid_password == validate_password_strength(valid_password) def test_validate_password_strength_length_failure(): invalid_password = "TooShor" with pytest.raises(errors.ValidationError) as e_info: error = e_info validate_password_strength(invalid_password) assert error is not None def test_validate_password_strength_uppercase_failure(): invalid_password = "NOUPPERCASE9" with pytest.raises(errors.ValidationError) as e_info: error = e_info validate_password_strength(invalid_password) assert error is not None def test_validate_password_strength_lowercase_failure(): invalid_password = "NOLOWERCASE9" with pytest.raises(errors.ValidationError) as e_info: error = e_info validate_password_strength(invalid_password) assert error is not None def test_validate_password_strength_number_failure(): invalid_password = "NoNumber" with pytest.raises(errors.ValidationError) as e_info: error = e_info validate_password_strength(invalid_password) assert error is not None @patch(service_module + '.pwhash.verify') def test_is_valid_password_invalid_key_error( mock_pwhash_verify: MagicMock): user = User() user.password_hash = '' mock_pwhash_verify.side_effect = InvalidkeyError() assert not is_valid_password(user, '') def test_is_valid_token_happy_path(): user_token = UserToken() user_token.enabled = True assert is_valid_token(user_token) def test_is_valid_token_no_token(): assert not is_valid_token(None) def test_is_valid_token_disabled(): user_token = UserToken() user_token.enabled = False assert not is_valid_token(user_token) def test_is_valid_token_expired(): user_token = UserToken() user_token.enabled = True user_token.expiration_time = datetime.now() - timedelta(weeks=1) assert not is_valid_token(user_token)