105 lines
3.6 KiB
Python
105 lines
3.6 KiB
Python
import pytest
|
|
from datetime import datetime, timedelta
|
|
from bson import ObjectId
|
|
from pydantic import ValidationError
|
|
from src.db.models.api_key import ApiKeyModel
|
|
|
|
class TestApiKeyModel:
|
|
def test_create_api_key(self):
|
|
"""Test creating an API key with valid data"""
|
|
user_id = ObjectId()
|
|
team_id = ObjectId()
|
|
api_key_data = {
|
|
"key_hash": "hashed_api_key_value",
|
|
"user_id": user_id,
|
|
"team_id": team_id,
|
|
"name": "Test API Key"
|
|
}
|
|
api_key = ApiKeyModel(**api_key_data)
|
|
|
|
assert api_key.key_hash == "hashed_api_key_value"
|
|
assert api_key.user_id == user_id
|
|
assert api_key.team_id == team_id
|
|
assert api_key.name == "Test API Key"
|
|
assert api_key.is_active is True
|
|
assert api_key.description is None
|
|
assert isinstance(api_key.created_at, datetime)
|
|
assert api_key.expiry_date is None
|
|
assert api_key.last_used is None
|
|
|
|
def test_api_key_with_all_fields(self):
|
|
"""Test creating an API key with all fields populated"""
|
|
user_id = ObjectId()
|
|
team_id = ObjectId()
|
|
expiry_date = datetime.utcnow() + timedelta(days=30)
|
|
last_used = datetime.utcnow()
|
|
|
|
api_key = ApiKeyModel(
|
|
key_hash="hashed_api_key_value",
|
|
user_id=user_id,
|
|
team_id=team_id,
|
|
name="Test API Key",
|
|
description="API key for testing",
|
|
expiry_date=expiry_date,
|
|
last_used=last_used,
|
|
is_active=False
|
|
)
|
|
|
|
assert api_key.description == "API key for testing"
|
|
assert api_key.expiry_date == expiry_date
|
|
assert api_key.last_used == last_used
|
|
assert api_key.is_active is False
|
|
|
|
def test_api_key_with_missing_required_fields(self):
|
|
"""Test that required fields must be provided"""
|
|
# Missing key_hash
|
|
with pytest.raises(ValidationError):
|
|
ApiKeyModel(
|
|
user_id=ObjectId(),
|
|
team_id=ObjectId(),
|
|
name="Test API Key"
|
|
)
|
|
|
|
# Missing user_id
|
|
with pytest.raises(ValidationError):
|
|
ApiKeyModel(
|
|
key_hash="hashed_api_key_value",
|
|
team_id=ObjectId(),
|
|
name="Test API Key"
|
|
)
|
|
|
|
# Missing team_id
|
|
with pytest.raises(ValidationError):
|
|
ApiKeyModel(
|
|
key_hash="hashed_api_key_value",
|
|
user_id=ObjectId(),
|
|
name="Test API Key"
|
|
)
|
|
|
|
# Missing name
|
|
with pytest.raises(ValidationError):
|
|
ApiKeyModel(
|
|
key_hash="hashed_api_key_value",
|
|
user_id=ObjectId(),
|
|
team_id=ObjectId()
|
|
)
|
|
|
|
def test_api_key_dict_conversion(self):
|
|
"""Test the API key model can be converted to dict with correct field mapping"""
|
|
user_id = ObjectId()
|
|
team_id = ObjectId()
|
|
api_key = ApiKeyModel(
|
|
key_hash="hashed_api_key_value",
|
|
user_id=user_id,
|
|
team_id=team_id,
|
|
name="Test API Key"
|
|
)
|
|
api_key_dict = api_key.model_dump(by_alias=True)
|
|
|
|
assert "_id" in api_key_dict
|
|
assert "key_hash" in api_key_dict
|
|
assert "user_id" in api_key_dict
|
|
assert "team_id" in api_key_dict
|
|
assert "name" in api_key_dict
|
|
assert str(api_key_dict["user_id"]) == str(user_id)
|
|
assert str(api_key_dict["team_id"]) == str(team_id) |