152 lines
5.0 KiB
Python
152 lines
5.0 KiB
Python
"""Regression tests for durable managed-validation deduplication."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from uuid import UUID, uuid4
|
|
|
|
import pytest
|
|
|
|
from connectors.gitea import FakeGiteaServer, GiteaConnector
|
|
from connectors.gitea.managed_validation import MANAGED_VALIDATION_WORKFLOW
|
|
from packages.connector_kit import (
|
|
ConnectorContext,
|
|
GiteaClient,
|
|
GiteaError,
|
|
GiteaRepositoryRegistry,
|
|
GiteaSettings,
|
|
RegisteredRepository,
|
|
StaticTokenProvider,
|
|
)
|
|
|
|
PROJECT_ID = UUID("11111111-1111-1111-1111-111111111111")
|
|
|
|
|
|
def _context() -> ConnectorContext:
|
|
return ConnectorContext(
|
|
workspace_id=uuid4(), project_id=PROJECT_ID, project_slug="hub", environment="test"
|
|
)
|
|
|
|
|
|
def _connector(server: FakeGiteaServer) -> GiteaConnector:
|
|
settings = GiteaSettings(
|
|
base_url="http://gitea.test",
|
|
token_reference="docker-secret://gitea_mcp_token", # noqa: S106
|
|
allow_insecure_http_for_tests=True,
|
|
)
|
|
client = GiteaClient(
|
|
settings,
|
|
StaticTokenProvider("fixture-token-must-not-leak"),
|
|
transport=server.transport(),
|
|
)
|
|
registry = GiteaRepositoryRegistry(
|
|
(
|
|
RegisteredRepository(
|
|
repository_ref="hub-repository",
|
|
project_id=PROJECT_ID,
|
|
owner="acme",
|
|
name="hub",
|
|
source_uri="itworx-git://hub/hub-repository",
|
|
private=True,
|
|
),
|
|
)
|
|
)
|
|
return GiteaConnector(client, registry, write_client=client)
|
|
|
|
|
|
def _add_validation_branch(server: FakeGiteaServer) -> None:
|
|
server._branches.append(
|
|
{"name": "codex/workflows", "commit": {"id": "e" * 40}, "protected": False}
|
|
)
|
|
|
|
|
|
def test_workspace_run_reuses_validation_run_across_connector_instances() -> None:
|
|
fixture = FakeGiteaServer()
|
|
_add_validation_branch(fixture)
|
|
first = _connector(fixture)
|
|
second = _connector(fixture)
|
|
try:
|
|
dispatched = first.validation_run(_context(), "hub-repository", "codex/workflows", "full")[
|
|
"data"
|
|
]
|
|
assert dispatched["dispatched"] is True
|
|
assert dispatched["deduplicationSource"] == "dispatch"
|
|
|
|
reused = second.workspace_run(_context(), "hub-repository", "codex/workflows", "full")[
|
|
"data"
|
|
]
|
|
assert reused["id"] == dispatched["id"]
|
|
assert reused["dispatched"] is False
|
|
assert reused["reused"] is True
|
|
assert reused["deduplicatedRunId"] == dispatched["id"]
|
|
assert reused["deduplicationSource"] == "gitea-actions"
|
|
assert sum(path.endswith("/dispatches") for path in fixture.recorded_paths) == 1
|
|
finally:
|
|
first.client.close()
|
|
second.client.close()
|
|
|
|
|
|
def test_durable_dedup_does_not_cross_validation_profiles() -> None:
|
|
fixture = FakeGiteaServer()
|
|
_add_validation_branch(fixture)
|
|
first = _connector(fixture)
|
|
second = _connector(fixture)
|
|
try:
|
|
first.validation_run(_context(), "hub-repository", "codex/workflows", "test")
|
|
second.validation_run(_context(), "hub-repository", "codex/workflows", "full")
|
|
assert sum(path.endswith("/dispatches") for path in fixture.recorded_paths) == 2
|
|
finally:
|
|
first.client.close()
|
|
second.client.close()
|
|
|
|
|
|
def test_validation_history_filters_unrelated_workflows_when_identity_is_available() -> None:
|
|
fixture = FakeGiteaServer()
|
|
_add_validation_branch(fixture)
|
|
fixture._action_run.update(
|
|
{
|
|
"event": "workflow_dispatch",
|
|
"workflow_id": "unrelated.yml",
|
|
"head_branch": "codex/workflows",
|
|
"head_sha": "e" * 40,
|
|
}
|
|
)
|
|
connector = _connector(fixture)
|
|
try:
|
|
items = connector.validation_list(_context(), "hub-repository", "codex/workflows")["data"][
|
|
"items"
|
|
]
|
|
assert items == []
|
|
finally:
|
|
connector.client.close()
|
|
|
|
|
|
def test_explicit_unrelated_successful_workflow_cannot_authorize_merge() -> None:
|
|
fixture = FakeGiteaServer()
|
|
_add_validation_branch(fixture)
|
|
fixture._action_run.update(
|
|
{
|
|
"event": "workflow_dispatch",
|
|
"workflow_id": "unrelated.yml",
|
|
"head_branch": "codex/workflows",
|
|
"head_sha": "e" * 40,
|
|
"status": "completed",
|
|
"conclusion": "success",
|
|
}
|
|
)
|
|
connector = _connector(fixture)
|
|
try:
|
|
with pytest.raises(GiteaError, match="successful canonical pull_request run"):
|
|
connector.pull_request_merge(_context(), "hub-repository", 7, "e" * 40, "squash")
|
|
assert not any(path.endswith("/pulls/7/merge") for path in fixture.recorded_paths)
|
|
finally:
|
|
connector.client.close()
|
|
|
|
|
|
def test_managed_workflow_run_name_persists_dispatch_profile_without_changing_pr_context() -> None:
|
|
assert (
|
|
"run-name: \"Managed validation / ${{ inputs.profile || 'full' }}\""
|
|
in MANAGED_VALIDATION_WORKFLOW
|
|
)
|
|
assert " full:\n name: full" in MANAGED_VALIDATION_WORKFLOW
|
|
assert " pull_request:" in MANAGED_VALIDATION_WORKFLOW
|