Files
crewAI/tests/security/test_examples.py
2025-03-11 03:06:25 -07:00

185 lines
5.6 KiB
Python

"""Test for the examples in the fingerprinting documentation."""
import pytest
from crewai import Agent, Crew, Task
from crewai.security import Fingerprint, SecurityConfig
def test_basic_usage_examples():
"""Test the basic usage examples from the documentation."""
# Creating components with automatic fingerprinting
agent = Agent(
role="Data Scientist",
goal="Analyze data",
backstory="Expert in data analysis"
)
# Verify the agent has a fingerprint
assert agent.fingerprint is not None
assert isinstance(agent.fingerprint, Fingerprint)
assert agent.fingerprint.uuid_str is not None
# Create a crew and verify it has a fingerprint
crew = Crew(
agents=[agent],
tasks=[]
)
assert crew.fingerprint is not None
assert isinstance(crew.fingerprint, Fingerprint)
assert crew.fingerprint.uuid_str is not None
# Create a task and verify it has a fingerprint
task = Task(
description="Analyze customer data",
expected_output="Insights from data analysis",
agent=agent
)
assert task.fingerprint is not None
assert isinstance(task.fingerprint, Fingerprint)
assert task.fingerprint.uuid_str is not None
def test_accessing_fingerprints_example():
"""Test the accessing fingerprints example from the documentation."""
# Create components
agent = Agent(
role="Data Scientist",
goal="Analyze data",
backstory="Expert in data analysis"
)
crew = Crew(
agents=[agent],
tasks=[]
)
task = Task(
description="Analyze customer data",
expected_output="Insights from data analysis",
agent=agent
)
# Get and verify the agent's fingerprint
agent_fingerprint = agent.fingerprint
assert agent_fingerprint is not None
assert isinstance(agent_fingerprint, Fingerprint)
assert agent_fingerprint.uuid_str is not None
# Get and verify the crew's fingerprint
crew_fingerprint = crew.fingerprint
assert crew_fingerprint is not None
assert isinstance(crew_fingerprint, Fingerprint)
assert crew_fingerprint.uuid_str is not None
# Get and verify the task's fingerprint
task_fingerprint = task.fingerprint
assert task_fingerprint is not None
assert isinstance(task_fingerprint, Fingerprint)
assert task_fingerprint.uuid_str is not None
# Ensure the fingerprints are unique
fingerprints = [
agent_fingerprint.uuid_str,
crew_fingerprint.uuid_str,
task_fingerprint.uuid_str
]
assert len(fingerprints) == len(set(fingerprints)), "All fingerprints should be unique"
def test_fingerprint_metadata_example():
"""Test using the Fingerprint's metadata for additional information."""
# Create a SecurityConfig with custom metadata
security_config = SecurityConfig()
security_config.fingerprint.metadata = {"version": "1.0", "author": "John Doe"}
# Create an agent with the custom SecurityConfig
agent = Agent(
role="Data Scientist",
goal="Analyze data",
backstory="Expert in data analysis",
security_config=security_config
)
# Verify the metadata is attached to the fingerprint
assert agent.fingerprint.metadata == {"version": "1.0", "author": "John Doe"}
def test_fingerprint_with_security_config():
"""Test example of using a SecurityConfig with components."""
# Create a SecurityConfig
security_config = SecurityConfig()
# Create an agent with the SecurityConfig
agent = Agent(
role="Data Scientist",
goal="Analyze data",
backstory="Expert in data analysis",
security_config=security_config
)
# Verify the agent uses the same instance of SecurityConfig
assert agent.security_config is security_config
# Create a task with the same SecurityConfig
task = Task(
description="Analyze customer data",
expected_output="Insights from data analysis",
agent=agent,
security_config=security_config
)
# Verify the task uses the same instance of SecurityConfig
assert task.security_config is security_config
def test_complete_workflow_example():
"""Test the complete workflow example from the documentation."""
# Create agents with auto-generated fingerprints
researcher = Agent(
role="Researcher",
goal="Find information",
backstory="Expert researcher"
)
writer = Agent(
role="Writer",
goal="Create content",
backstory="Professional writer"
)
# Create tasks with auto-generated fingerprints
research_task = Task(
description="Research the topic",
expected_output="Research findings",
agent=researcher
)
writing_task = Task(
description="Write an article",
expected_output="Completed article",
agent=writer
)
# Create a crew with auto-generated fingerprint
content_crew = Crew(
agents=[researcher, writer],
tasks=[research_task, writing_task]
)
# Verify everything has auto-generated fingerprints
assert researcher.fingerprint is not None
assert writer.fingerprint is not None
assert research_task.fingerprint is not None
assert writing_task.fingerprint is not None
assert content_crew.fingerprint is not None
# Verify all fingerprints are unique
fingerprints = [
researcher.fingerprint.uuid_str,
writer.fingerprint.uuid_str,
research_task.fingerprint.uuid_str,
writing_task.fingerprint.uuid_str,
content_crew.fingerprint.uuid_str
]
assert len(fingerprints) == len(set(fingerprints)), "All fingerprints should be unique"