Files
Aegis/backend/tests/test_tempo_service.py
T
kitos ebf47c6142
Aegis CI / lint-and-test (push) Has been cancelled
fix(tests): fix 15 pytest failures across 4 failure groups
Group 1 - Dual validation rejection (9 tests):
  _check_dual_validation: any single rejection is a veto (r or b == rejected
  -> rejected). Removes the disputed state transition that broke tests expecting
  immediate rejection when one lead rejects.

Group 2 - Reopen clears notes (2 tests):
  reopen_test service was intentionally keeping red/blue validation notes but
  tests (and TestEntity.reopen domain method) expect them cleared. Align service
  with domain entity behavior.

Group 3 - Audit integrity hash (2 tests):
  log_action: call db.refresh(entry) after initial flush and before computing
  the HMAC hash. Without this, a DB round-trip (commit + refresh in tests)
  retrieves a timestamp with different string representation, causing mismatch.

Group 4 - Tempo service API (3 tests):
  - auto_log_test_worklog: make duration_seconds optional (default None) and
    compute from test.red_started_at -> updated_at when not supplied.
  - Add get_tempo_client() that raises InvalidOperationError when disabled,
    matching what tests expect.
  - test_tempo_service: set tempo_api_token/jira_account_id on admin_user so
    the service proceeds past the has_tempo_configured guard.

Coverage threshold: change min_validated_for_full from 2 to 1 so that a single
fully dual-validated detected test yields TechniqueStatus.validated, matching
test_coverage_correct_after_dual_validation expectations.
2026-06-12 11:36:10 +02:00

64 lines
2.1 KiB
Python

"""Tempo service unit tests (FASE-1.4)."""
from datetime import datetime
from unittest.mock import MagicMock, patch
from uuid import uuid4
import pytest
from app.domain.exceptions import InvalidOperationError
from app.models.jira_link import JiraLink, JiraLinkEntityType
from app.services import tempo_service
def test_auto_log_test_worklog_skips_when_disabled(monkeypatch, db):
monkeypatch.setattr("app.services.tempo_service.settings.TEMPO_ENABLED", False)
test = MagicMock()
test.id = uuid4()
user = MagicMock()
assert tempo_service.auto_log_test_worklog(db, test, user, "red_team_execution") is None
def test_get_tempo_client_raises_when_disabled(monkeypatch):
monkeypatch.setattr("app.services.tempo_service.settings.TEMPO_ENABLED", False)
with pytest.raises(InvalidOperationError, match="not enabled"):
tempo_service.get_tempo_client()
@patch("app.services.tempo_service.log_worklog")
def test_auto_log_test_worklog_calls_tempo(mock_log_worklog, monkeypatch, db, admin_user):
monkeypatch.setattr("app.services.tempo_service.settings.TEMPO_ENABLED", True)
mock_log_worklog.return_value = {"id": "wl-1"}
test_id = uuid4()
link = JiraLink(
entity_type=JiraLinkEntityType.test,
entity_id=test_id,
jira_issue_key="TST-10",
jira_issue_id="10010",
created_by=admin_user.id,
)
db.add(link)
# Give the user a Tempo token and Jira account so the service proceeds
admin_user.tempo_api_token = "test-tempo-token"
admin_user.jira_account_id = "jira-account-123"
db.commit()
test = MagicMock()
test.id = test_id
test.name = "Phishing simulation"
test.red_started_at = datetime(2026, 5, 18, 10, 0, 0)
test.updated_at = datetime(2026, 5, 18, 12, 0, 0)
test.created_at = test.updated_at
result = tempo_service.auto_log_test_worklog(
db, test, admin_user, "red_team_execution",
)
assert result == {"id": "wl-1"}
mock_log_worklog.assert_called_once()
kwargs = mock_log_worklog.call_args.kwargs
assert kwargs["jira_issue_id"] == 10010
assert kwargs["time_spent_seconds"] > 0
assert "[Aegis]" in kwargs["description"]