50 lines
1.2 KiB
Python
50 lines
1.2 KiB
Python
"""Tests for authentication module."""
|
|
import pytest
|
|
from backend.auth import (
|
|
get_password_hash,
|
|
verify_password,
|
|
create_access_token,
|
|
decode_access_token,
|
|
encrypt_password,
|
|
decrypt_password,
|
|
)
|
|
|
|
|
|
def test_password_hashing():
|
|
"""Test password hashing and verification."""
|
|
password = "testpassword123"
|
|
hashed = get_password_hash(password)
|
|
|
|
assert hashed != password
|
|
assert verify_password(password, hashed)
|
|
assert not verify_password("wrongpassword", hashed)
|
|
|
|
|
|
def test_jwt_tokens():
|
|
"""Test JWT token creation and decoding."""
|
|
data = {"sub": "testuser"}
|
|
token = create_access_token(data)
|
|
|
|
assert token is not None
|
|
|
|
decoded = decode_access_token(token)
|
|
assert decoded is not None
|
|
assert decoded["sub"] == "testuser"
|
|
|
|
|
|
def test_invalid_token():
|
|
"""Test decoding invalid token."""
|
|
decoded = decode_access_token("invalid.token.here")
|
|
assert decoded is None
|
|
|
|
|
|
def test_password_encryption():
|
|
"""Test password encryption and decryption."""
|
|
password = "secret_password"
|
|
encrypted = encrypt_password(password)
|
|
|
|
assert encrypted != password
|
|
|
|
decrypted = decrypt_password(encrypted)
|
|
assert decrypted == password
|