Luxx/tests/test_helpers.py

111 lines
3.6 KiB
Python

"""Tests for utils/helpers module"""
from luxx.utils.helpers import (
generate_id,
hash_password,
verify_password,
create_access_token,
decode_access_token,
success_response,
error_response
)
class TestGenerateId:
"""Tests for generate_id function"""
def test_generate_id_returns_string(self):
"""Should return a string"""
result = generate_id()
assert isinstance(result, str)
def test_generate_id_with_prefix(self):
"""Should return id with prefix"""
result = generate_id("task")
assert result.startswith("task_")
def test_generate_id_unique(self):
"""Should generate unique ids"""
ids = [generate_id() for _ in range(100)]
assert len(set(ids)) == 100
class TestPasswordHashing:
"""Tests for password hashing functions"""
def test_hash_password_returns_string(self):
"""Should return a hashed string"""
password = "test_password_123"
hashed = hash_password(password)
assert isinstance(hashed, str)
assert hashed != password
def test_verify_password_correct(self):
"""Should return True for correct password"""
password = "test_password_123"
hashed = hash_password(password)
assert verify_password(password, hashed) is True
def test_verify_password_incorrect(self):
"""Should return False for incorrect password"""
password = "test_password_123"
wrong_password = "wrong_password"
hashed = hash_password(password)
assert verify_password(wrong_password, hashed) is False
class TestJWTToken:
"""Tests for JWT token functions"""
def test_create_access_token_returns_string(self):
"""Should return a JWT token string"""
token = create_access_token({"user_id": 1})
assert isinstance(token, str)
assert len(token) > 0
def test_decode_access_token_valid(self):
"""Should decode valid token"""
payload = {"user_id": 1, "username": "test"}
token = create_access_token(payload)
decoded = decode_access_token(token)
assert decoded is not None
assert decoded["user_id"] == 1
assert decoded["username"] == "test"
def test_decode_access_token_invalid(self):
"""Should return None for invalid token"""
result = decode_access_token("invalid.token.here")
assert result is None
class TestResponseWrappers:
"""Tests for response wrapper functions"""
def test_success_response_format(self):
"""Should return correct success format"""
result = success_response({"key": "value"}, "Success message")
assert result["success"] is True
assert result["message"] == "Success message"
assert result["data"] == {"key": "value"}
def test_success_response_default(self):
"""Should use default values"""
result = success_response()
assert result["success"] is True
assert result["message"] == "Success"
assert result["data"] is None
def test_error_response_format(self):
"""Should return correct error format"""
result = error_response("Error occurred", code=404)
assert result["success"] is False
assert result["message"] == "Error occurred"
assert result["code"] == 404
assert "errors" not in result
def test_error_response_with_errors(self):
"""Should include errors field"""
errors = {"field": ["required"]}
result = error_response("Validation failed", code=400, errors=errors)
assert result["success"] is False
assert result["errors"] == errors