164 lines
5.8 KiB
Python
164 lines
5.8 KiB
Python
"""Tests for the Nextcloud playbook module."""
|
|
|
|
import uuid
|
|
|
|
import pytest
|
|
import pytest_asyncio
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.models.task import Task, TaskStatus
|
|
from app.models.tenant import Tenant
|
|
from app.playbooks.nextcloud import (
|
|
NEXTCLOUD_STACK_DIR,
|
|
CompositeStep,
|
|
build_nextcloud_set_domain_steps,
|
|
create_nextcloud_set_domain_task,
|
|
)
|
|
|
|
|
|
class TestBuildNextcloudSetDomainSteps:
|
|
"""Tests for the build_nextcloud_set_domain_steps function."""
|
|
|
|
def test_returns_two_steps(self):
|
|
"""Verify that build_nextcloud_set_domain_steps returns exactly 2 steps."""
|
|
steps = build_nextcloud_set_domain_steps(
|
|
public_url="https://cloud.example.com", pull=False
|
|
)
|
|
assert len(steps) == 2
|
|
assert all(isinstance(step, CompositeStep) for step in steps)
|
|
|
|
def test_first_step_is_nextcloud_set_domain(self):
|
|
"""Verify the first step is NEXTCLOUD_SET_DOMAIN."""
|
|
steps = build_nextcloud_set_domain_steps(
|
|
public_url="https://cloud.example.com", pull=False
|
|
)
|
|
assert steps[0].type == "NEXTCLOUD_SET_DOMAIN"
|
|
|
|
def test_nextcloud_set_domain_payload(self):
|
|
"""Verify NEXTCLOUD_SET_DOMAIN step has correct payload."""
|
|
public_url = "https://cloud.example.com"
|
|
steps = build_nextcloud_set_domain_steps(public_url=public_url, pull=False)
|
|
assert steps[0].payload["public_url"] == public_url
|
|
|
|
def test_docker_reload_payload(self):
|
|
"""Verify the DOCKER_RELOAD step has the correct payload structure."""
|
|
steps = build_nextcloud_set_domain_steps(
|
|
public_url="https://cloud.example.com", pull=False
|
|
)
|
|
|
|
docker_step = steps[1]
|
|
assert docker_step.type == "DOCKER_RELOAD"
|
|
assert docker_step.payload["compose_dir"] == NEXTCLOUD_STACK_DIR
|
|
assert docker_step.payload["pull"] is False
|
|
|
|
def test_pull_flag_true(self):
|
|
"""Verify that pull=True is passed correctly."""
|
|
steps = build_nextcloud_set_domain_steps(
|
|
public_url="https://cloud.example.com", pull=True
|
|
)
|
|
|
|
docker_step = steps[1]
|
|
assert docker_step.payload["pull"] is True
|
|
|
|
def test_pull_flag_false(self):
|
|
"""Verify that pull=False is passed correctly."""
|
|
steps = build_nextcloud_set_domain_steps(
|
|
public_url="https://cloud.example.com", pull=False
|
|
)
|
|
|
|
docker_step = steps[1]
|
|
assert docker_step.payload["pull"] is False
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
class TestCreateNextcloudSetDomainTask:
|
|
"""Tests for the create_nextcloud_set_domain_task function."""
|
|
|
|
async def test_persists_composite_task(self, db: AsyncSession, test_tenant: Tenant):
|
|
"""Verify that create_nextcloud_set_domain_task persists a COMPOSITE task."""
|
|
agent_id = uuid.uuid4()
|
|
|
|
task = await create_nextcloud_set_domain_task(
|
|
db=db,
|
|
tenant_id=test_tenant.id,
|
|
agent_id=agent_id,
|
|
public_url="https://cloud.example.com",
|
|
pull=False,
|
|
)
|
|
|
|
assert task.id is not None
|
|
assert task.tenant_id == test_tenant.id
|
|
assert task.agent_id == agent_id
|
|
assert task.type == "COMPOSITE"
|
|
assert task.status == TaskStatus.PENDING.value
|
|
|
|
async def test_task_payload_contains_steps(
|
|
self, db: AsyncSession, test_tenant: Tenant
|
|
):
|
|
"""Verify that the task payload contains the steps array."""
|
|
task = await create_nextcloud_set_domain_task(
|
|
db=db,
|
|
tenant_id=test_tenant.id,
|
|
agent_id=uuid.uuid4(),
|
|
public_url="https://cloud.example.com",
|
|
pull=False,
|
|
)
|
|
|
|
assert "steps" in task.payload
|
|
assert len(task.payload["steps"]) == 2
|
|
|
|
async def test_task_steps_structure(self, db: AsyncSession, test_tenant: Tenant):
|
|
"""Verify that the steps in the payload have the correct structure."""
|
|
task = await create_nextcloud_set_domain_task(
|
|
db=db,
|
|
tenant_id=test_tenant.id,
|
|
agent_id=uuid.uuid4(),
|
|
public_url="https://cloud.example.com",
|
|
pull=True,
|
|
)
|
|
|
|
steps = task.payload["steps"]
|
|
|
|
# First step should be NEXTCLOUD_SET_DOMAIN
|
|
assert steps[0]["type"] == "NEXTCLOUD_SET_DOMAIN"
|
|
assert steps[0]["payload"]["public_url"] == "https://cloud.example.com"
|
|
|
|
# Second step should be DOCKER_RELOAD
|
|
assert steps[1]["type"] == "DOCKER_RELOAD"
|
|
assert steps[1]["payload"]["compose_dir"] == NEXTCLOUD_STACK_DIR
|
|
assert steps[1]["payload"]["pull"] is True
|
|
|
|
async def test_task_with_pull_false(self, db: AsyncSession, test_tenant: Tenant):
|
|
"""Verify that pull=False is correctly stored in the task payload."""
|
|
task = await create_nextcloud_set_domain_task(
|
|
db=db,
|
|
tenant_id=test_tenant.id,
|
|
agent_id=uuid.uuid4(),
|
|
public_url="https://cloud.example.com",
|
|
pull=False,
|
|
)
|
|
|
|
steps = task.payload["steps"]
|
|
assert steps[1]["payload"]["pull"] is False
|
|
|
|
async def test_task_persisted_to_database(
|
|
self, db: AsyncSession, test_tenant: Tenant
|
|
):
|
|
"""Verify the task is actually persisted and can be retrieved."""
|
|
task = await create_nextcloud_set_domain_task(
|
|
db=db,
|
|
tenant_id=test_tenant.id,
|
|
agent_id=uuid.uuid4(),
|
|
public_url="https://cloud.example.com",
|
|
pull=False,
|
|
)
|
|
|
|
# Query the task back from the database
|
|
result = await db.execute(select(Task).where(Task.id == task.id))
|
|
retrieved_task = result.scalar_one_or_none()
|
|
|
|
assert retrieved_task is not None
|
|
assert retrieved_task.type == "COMPOSITE"
|
|
assert retrieved_task.tenant_id == test_tenant.id
|