Files
AiSOC/services/api/tests/test_alert_queue.py
T
Beenu Arora ba636b49fa Lane A: identity context that changes the verdict — effective permissions, access review, sub-principal baselines, change timeline and identity resolution (#1590)
* depth2 Phase 0: kickoff, re-derived baseline and the SIEM-scope decision

Phase 0.1-0.4 of the wave-2 depth plan, which lands here locked beside the
other four plans.

0.2 re-derives every figure the plan captured. All twenty reproduce exactly
except the cloud/identity/SaaS/code condition count, which the plan itself
flagged as approximate: measured 344 against 'about 358'. D1 records why,
and the finding underneath it — no script in the tree implements that
sentence, so every published value of it was hand-counted with a slightly
different product set, and windowed rules contribute zero because they
carry no product field at all. Phase 2.1's gate has to decide that
explicitly.

0.3 captures the baselines, including 'make up-full' — the one the first
depth plan recorded as never captured (its D2). 22 containers on the full
profile; CORE passed 'make smoke' 10 of 10. The baseline host had nine
unrelated containers on it including a foreign Postgres on 5432, and
'make up-full' failed its first bind before the port resolver's overlay
landed. D2 records the sequence and, deliberately, does not claim a defect
from it: the preceding state is not replayable, so it carries a clean-host
reproduction as a follow-up instead.

0.4 is ADR-0015: AiSOC federates to the SIEM rather than replacing it. The
README's 'Not a drop-in SIEM replacement' is now a decided position with its
cost argument — compliance search needs raw-payload retention outside the
8 GB CORE budget and an availability promise this project has no measured
evidence it can keep. Proposed, and nothing in this plan builds from it.

* depth2 lane A: claim migrations 124-129 and ADRs 0016-0017

* depth2 1.1: reproduce the unconsumed posture and the four unmodelled AWS semantics

Fails on this tree: 15 of 20 api assertions and all 3 agents assertions.
The 5 that pass are the negative arms of the condition-operator matrix,
which pass for the wrong reason -- the resolver denies every operator it
cannot evaluate, so 'under-reports real access' and 'correctly refuses'
are indistinguishable until the operator is modelled.

* depth2 1.1: model the four AWS semantics, derive privilege facts, and wire them

- iam_conditions.py: three-valued condition evaluation across 24 operators
  plus IfExists / ForAnyValue / ForAllValues. Undecidable is a distinct
  answer resolved per effect, so an undecidable Allow does not grant and an
  undecidable Deny still denies -- both refusing to over-report access.
- aws.py: NotAction and NotResource invert their sets; session policies cap
  the identity arm (intersected across several, not unioned) and never the
  resource-policy arm; withheld actions land in deny_actions with a note.
- identity_privilege.py: tier plus can_escalate / data_access / third_party
  / cross_account with per-tag evidence; unknown is a value, tags are
  tri-state.
- migration 124: identity_privilege_facts versioned on the snapshot's own
  interval, and identity_permission_usage for the runtime overlay.
- fusion stamps the tier valid AT EVENT TIME; ConfidenceScorer gains a
  bounded identity_privilege factor and the eight existing weights are
  scaled by 0.94 so the total is still exactly 1.0.
- alert_queue resolved identity contexts point-in-time and now passes them:
  score_alert_priority has taken an identity since v16 and its only
  production caller passed None.
- the typed pivot and the agents tool registry both offer the tool.

* depth2 1.1: live Postgres proof, triage citation, ADR-0016

- test_identity_privilege_live.py: 10 live tests, each with a negative
  control, wired into integration.yml with MSSP_ISOLATION_REQUIRED.
- app/context/privilege.py: triage reads the answer through the same
  /investigate/query dispatch the registered tool uses, so the basis line
  has a matching tool_call row rather than being a side-channel claim.
- the new tool is declared in AGENT_TOOL_NAMES: the fabricated-tool-result
  guard caught it, which is the gate working.
- ADR-0016 records the bounded weights, the point-in-time rule and why
  unknown is a value.

* depth2 1.1: usage overlay from the lake, gate fixes, measurements recorded

- identity_usage_collector.py reports measured=False with a reason when the
  lake cannot be asked, because an unmeasured empty set makes a
  right-sizing finding recommend revoking the whole estate.
- the dispatch-coverage table gains a third category for Postgres-backed
  tools; the lake-tenant-predicate assertions cannot apply to a tool that
  composes no lake query, and a tool added to the first list would have
  passed them by having nothing to fail on.
- scorecard and alert-reduction before/after recorded in DEPTH2_PROGRESS,
  measured on the same model on both trees.

* depth2 1.2: read-only access review with an honest empty state

- access_review.py: granted vs used per principal, six finding kinds, each
  naming the object a human should change. Usage-dependent kinds are
  suppressed wholesale when usage was not measured rather than caveated,
  because a caveat on 'revoke this' is read by nobody and the finding is
  wrong in the direction that costs an outage.
- best_fit_role returns None rather than a role that does not cover
  something the principal used: recommending an outage is the one error
  this feature must not make.
- migration 125: runs and findings under RLS, one open finding per
  (principal, kind) so a nightly review updates rather than accumulates.
- five routes behind compliance:read, a console page, and three distinct
  empty states -- never_run, nothing_reviewed and no_findings -- because
  only the third is good news.
- the NOT IN retire query hit the recorded :name::type bind-cast trap and
  is spelled CAST(:name AS text[]); check_text_bind_casts stays green.

* depth2 1.4: a baseline per sub-principal, and a cross-language token key

- baselines.py gains api_token / oauth_app / service_account / ai_agent as
  subjects of their own. A stolen token acts AS the user, so the user's own
  rarity and volume stay normal while the token's do not -- which is
  backwards from folding it into the human's baseline.
- four signals: acting outside its own baseline, a machine cadence turning
  interactive, a new ASN within an hour of the token first appearing, and
  reactivation after 60 days idle.
- classify_cadence tests interactive FIRST, because a person working
  09:00-17:00 produces hourly gaps indistinguishable from a cron by
  regularity alone; what separates them is repeats within one hour.
- the GitHub join key crosses a language boundary. Go trims and does not
  case-fold; the Python module's general helper lowercases, which is right
  for a username and wrong for a digest. Both sides now read one committed
  fixture, and it immediately caught a second divergence: Go requires a
  string type while the Python side coerced a number into a key Go never
  writes.
- check_detection_fields.py's mirror of DERIVED_FIELDS moves with it; the
  bidirectional test caught the drift.

* depth2 1.4: C401 lint fix

* depth2 1.5: what changed before this alert, from posture and audit

- change_timeline.py joins versioned posture (which says what the estate
  looked like, completely, for collected providers) with audit events
  (which say who did it, for ingested sources). Neither alone carries both
  the attribution and the coverage.
- ChangeTimeline.measured distinguishes 'read both sources and found
  nothing' from 'could not read either'. An empty list with no such field
  turns a lake outage into evidence of a quiet week.
- the action vocabulary is closed. An unfamiliar action is not returned
  rather than guessed into a kind, because a reader cannot tell a guessed
  classification from a known one.
- registered as a typed pivot and in the agents registry, so triage and
  the console reach it through one dispatch table.
- the lake table is aisoc.raw_events, already declared ClickHouse-owned in
  the raw-SQL gate's UNMIGRATED_TABLES.

* depth2 3.1: identity resolution with confidence, evidence, and no weak merges

- identity_resolution.py links one person across IdP, EDR device user,
  proxy user, cloud principal, code platform and Workspace. Eight rules,
  each with its own confidence; the rule's confidence rather than the
  pair's, because a confidence that depends on the data is one nobody can
  reason about.
- the failure mode designed against is a WRONG link, not a missing one: a
  wrong merge makes the identity look richer rather than broken. The three
  canonical wrong merges are refused -- alice@example.com against
  alice@contoso.com, admin against admin, and a shared role mailbox.
- migration 128 stores weak links too. Dropping them at write time makes
  'we considered this and rejected it' unrecoverable, which is most of the
  value when somebody asks why two accounts were not joined.
- cluster_for walks merge-strength links only and is bounded, so three
  weak hops cannot join what no single rule would, and one bad strong link
  cannot return the whole tenant.
- a human decision overrides the rule in both directions without touching
  confidence: the rule did not become more certain, a person disagreed.
- written to Postgres for CORE (ADR-0010) and mirrored to the graph for
  full, read from Postgres only so the two profiles cannot answer
  differently for reasons no operator can see.

* depth2 lane A: record 1.2, 1.4, 1.5 and 3.1 in the progress tracker

* depth2 lane A: release the migration and ADR numbers that were claimed and not needed

* depth2 lane A: close the nine CodeQL alerts this branch opened

main is clean, so every one was mine and each is fixed rather than
dismissed.

- py/cyclic-import was real and worth breaking rather than deferring:
  default_registry now builds the identity tools, which live in
  customer_tools, which imported Tool from registry. It works today only
  because one import is inside a function, so the first module-level
  import of customer_tools anywhere would surface the failure at an
  unrelated call site. Tool moves to app/tools/tool.py and registry
  re-exports it, so every existing import keeps working.
- py/constant-conditional-expression was dead code left by an edit:
  'x if False else y'.
- py/mixed-returns in _ordered: a match whose arms all return still reads
  as mixing implicit and explicit returns, which is a real ambiguity in a
  function where None means undecidable. Replaced with a comparator dict
  and one explicit return None on the lookup miss.
- four py/unused-global-variable on the new tool descriptors: they are the
  module's public surface and belong in __all__.
- two more on loggers declared and never called; removed.

* depth2 lane A: close the three CI failures, none by weakening a gate

Reachability (the gate that names this repository's defining defect):
identity_resolution, identity_resolution_store and identity_usage_collector
had no production importer. Both are now wired rather than allowlisted --
five routes under /identity-resolution, and the usage collector is called
by access_review_store.run_review, which is the only thing that needs it.

Semgrep, +10 against a ceiling of 44/112: all ten were avoid-sqlalchemy-text
on statements composed with f-strings. The ceiling is not raised. The
column lists are written out per statement and every optional filter
becomes a bound parameter -- '(CAST(:provider AS text) IS NULL OR provider
= :provider)' -- so no f-string reaches text() at all. One statement with
one plan is the better shape anyway: there is no second query path that
only some callers exercise. Re-measured locally at exactly 112 total / 44
error with zero findings in the new files.

Migrations job: test_alert_queue_priority_live.py asserts the priority
basis is empty with no inventory, and 1.1 added an 'unknown privilege' row
to it. The sibling assertion in test_alert_queue.py was updated when the
change landed and this one was not -- the shape where fixing one surface
leaves its twin. Both now assert the row is present, says unknown, and
moves the score by exactly 1.0.

* fix(1.4): the baseline store reads the session projection instead of re-deriving it

check_session_projection caught this and it is a real defect, not a
lint. The sub-principal resolver read GitHub's hashed_token itself and
pinned its normalisation against the Go side byte for byte. That
passes and is still the wrong design: it is a second derivation of a
session key, and ADR-0010 accepted two stores for one projection only
on the condition that exactly one component derives it. Two
derivations agree until the next vendor-schema change and then
disagree in silence, leaving Postgres, the lake and the graph holding
different answers to which session an event belongs to.

resolve_sub_principal now reads activity.session and takes the key
only when key_kind says the session IS a credential — a vendor session
id is not a token identity, and keying on one would merge every token
used in that session into a single subject. The cross-language parity
test is deleted rather than updated: it pinned the design the
architecture forbids. Its replacement asserts the vendor field alone
resolves nothing, and that case survives the projection.

* style: ruff format

* fix(tests): bind the tenant for writes in the wave-2 identity live suites

All three suites wrote on a session with no tenant bound, so every insert
was refused: `new row violates row-level security policy`. 25 tests across
three files, none of which had ever reached the property in its name.

The policy is right and is not touched. Migrations 124/125/128 carry the
house form, `WITH CHECK (tenant_id = current_tenant_id())` with no
unbound-session arm, which 36 of the 43 policies in the chain use and which
is the stronger one -- the missing arm is exactly what made RLS fail open on
the connectors engine. Production writes arrive on a session get_tenant_db
has bound, or inside a worker calling set_config per tenant, so a suite that
wrote unbound was exercising a path no caller takes.

`tests/live_rls.py` carries the reasoning once and the three suites bind
immediately before each write. Reads stay unbound deliberately: the
cross-tenant assertions are about the predicate each store puts in its own
SQL, and binding the reader would let a store that forgot its predicate pass
behind RLS.

Two of the tests were the "passes for the wrong reason" shape rather than
plain failures -- an out-of-order identity_links insert and a second live
privilege row both assert on a *constraint* name, and were getting the RLS
error instead, so they needed the write to reach the constraint at all.

Also found because CI could not: the job aborts at its first failing step,
so steps 15-26 were skipped and the access-review and identity-resolution
suites had never been graded. Both had the identical defect. Verified
against real Postgres 16 as aisoc_app (rolsuper=f, rolbypassrls=f) with the
full chain applied: 25 failed before, 25 pass after.
2026-11-01 13:24:56 -08:00

1077 lines
39 KiB
Python

"""Unit tests for the investigation-queue workbench backend (W7 / PR-5).
The queue service in ``app.services.alert_queue`` is split into pure
helpers + an async orchestrator, mirroring the alert-explain pattern
we already test in ``test_alert_explain.py``. We test it the same way:
* **Pure helpers** (``first_asset``, ``first_action``) get
straightforward in-process tests with hand-built ``Alert``
stand-ins. These lock in the "one anchor entity per row" and
"top suggested action" projections the workbench renders.
* **SLA target resolution** (``load_sla_targets``) is exercised
against an in-memory ``MagicMock(AsyncSession)``. The defaults
matter: a tenant with zero overrides must still get a full
``severity → mttd_target_minutes`` map, otherwise the SQL
``CASE`` for ``sla_due_at`` would produce ``NULL`` rows and the
workbench would drop them silently.
* **`sla_due_at_expression`** is a SQL expression. We only verify it
compiles into a ``CASE`` block — full equivalence is covered by the
integration tests in CI which exercise real Postgres.
* **`build_queue`** is unit-tested by mocking every
``AsyncSession.execute`` call. We exercise the owner filter,
pagination math, the counts contract, and the row-projection
pipeline (alert → ``QueueItem``). The opinionated SQL ordering
itself is exercised end-to-end by the integration tests; here we
prove that whatever rows the DB returns in order are projected in
that same order, with the correct ``bucket`` / ``sla_*`` fields.
* **`claim_alert`** gets dedicated coverage for all four documented
branches: unassigned-grab-succeeds, already-mine-idempotent,
already-someone-else-409, lost-the-race-409. The latter walks the
fallback re-read path so the error carries the *actual* owner.
The tests deliberately do NOT spin up FastAPI's TestClient or a real
Postgres. The endpoint module is a thin orchestration layer; the
service layer here is what carries the business rules. Mocking the
session keeps the suite fast (<1s) and deterministic.
AiSOC — open-source AI Security Operations Center (MIT License)
Author: Beenu Arora <beenu@cyble.com>
"""
from __future__ import annotations
import uuid
from datetime import UTC, datetime, timedelta
from types import SimpleNamespace
from typing import Any
from unittest.mock import AsyncMock, MagicMock
import pytest
from app.services.alert_queue import (
QUEUE_CLOSED_STATUSES,
QUEUE_UNASSIGNED_SEVERITIES,
AlertAlreadyClaimedError,
AlertNotFoundError,
QueueAction,
QueueAsset,
build_queue,
claim_alert,
first_action,
first_asset,
load_sla_targets,
sla_due_at_expression,
)
from app.services.sla import DEFAULT_SLA_TARGETS
# ---------------------------------------------------------------------------
# Fixtures / helpers
# ---------------------------------------------------------------------------
def _alert(
*,
alert_id: uuid.UUID | None = None,
tenant_id: uuid.UUID | None = None,
title: str = "Suspicious login from new geo",
severity: str = "high",
status: str = "new",
priority: int = 50,
category: str | None = "identity",
connector_type: str | None = "okta",
assigned_to_id: uuid.UUID | None = None,
case_id: uuid.UUID | None = None,
affected_hosts: list[Any] | None = None,
affected_users: list[Any] | None = None,
affected_assets: list[Any] | None = None,
affected_ips: list[Any] | None = None,
ai_recommendations: list[Any] | None = None,
first_seen: datetime | None = None,
assigned_at: datetime | None = None,
snoozed_until: datetime | None = None,
) -> SimpleNamespace:
"""Build a stand-in for the ``Alert`` ORM object.
``SimpleNamespace`` is the same pattern we use in
``test_alert_explain.py``: the service code only ever
attribute-accesses fields it needs, never calls ORM-only methods.
Using a namespace keeps the test free of SQLAlchemy setup / Postgres
type wiring (``JSONB``/``UUID``) that would otherwise pin the
suite to an integration harness.
"""
return SimpleNamespace(
id=alert_id or uuid.uuid4(),
tenant_id=tenant_id or uuid.uuid4(),
title=title,
severity=severity,
status=status,
priority=priority,
category=category,
connector_type=connector_type,
assigned_to_id=assigned_to_id,
case_id=case_id,
affected_hosts=affected_hosts or [],
affected_users=affected_users or [],
affected_assets=affected_assets or [],
affected_ips=affected_ips or [],
ai_recommendations=ai_recommendations or [],
first_seen=first_seen or datetime.now(UTC) - timedelta(minutes=10),
assigned_at=assigned_at,
snoozed_until=snoozed_until,
)
def _scalar_one_or_none_result(value: Any) -> MagicMock:
"""Mock ``await session.execute(...).scalar_one_or_none() == value``."""
result = MagicMock()
result.scalar_one_or_none = MagicMock(return_value=value)
return result
def _scalar_one_result(value: Any) -> MagicMock:
"""Mock ``await session.execute(...).scalar_one() == value``."""
result = MagicMock()
result.scalar_one = MagicMock(return_value=value)
return result
def _scalars_all_result(values: list[Any]) -> MagicMock:
"""Mock ``await session.execute(...).scalars().all() == values``."""
result = MagicMock()
scalars = MagicMock()
scalars.all = MagicMock(return_value=values)
result.scalars = MagicMock(return_value=scalars)
return result
def _rows_all_result(rows: list[tuple[Any, ...]]) -> MagicMock:
"""Mock ``await session.execute(...).all() == rows``.
``build_queue`` projects ``select(Alert, bucket_expr, sla_expr)``
and iterates ``rows`` indexing as ``row[0]``, ``row[1]``,
``row[2]``. We use tuples to mirror that exactly.
"""
result = MagicMock()
result.all = MagicMock(return_value=rows)
return result
def _tenant_sla_cfg(severity: str, mttd_target: int) -> SimpleNamespace:
"""Stand-in for a ``TenantSLAConfig`` row."""
return SimpleNamespace(severity=severity, mttd_target=mttd_target)
# ---------------------------------------------------------------------------
# load_sla_targets — defaults + tenant overrides
# ---------------------------------------------------------------------------
class TestLoadSlaTargets:
"""Tenant overrides win, defaults fill the rest.
A regression here would either (a) drop a severity that the
tenant didn't override — silently dropping rows from the queue —
or (b) ignore tenant overrides — pinning every tenant to the
out-of-the-box MTTD targets.
"""
@pytest.mark.asyncio
async def test_returns_defaults_when_no_tenant_overrides(self) -> None:
# Empty config → fall back to ``DEFAULT_SLA_TARGETS`` for
# every severity. The dict must contain every key the
# ``CASE`` expression branches on, including ``info``.
db = MagicMock()
db.execute = AsyncMock(return_value=_scalars_all_result([]))
targets = await load_sla_targets(db, uuid.uuid4())
assert set(targets.keys()) == set(DEFAULT_SLA_TARGETS.keys())
for sev, default in DEFAULT_SLA_TARGETS.items():
assert targets[sev] == default["mttd_target"]
@pytest.mark.asyncio
async def test_tenant_override_wins(self) -> None:
# Only the overridden severity should change; the rest must
# keep their defaults.
db = MagicMock()
db.execute = AsyncMock(return_value=_scalars_all_result([_tenant_sla_cfg("critical", 5)]))
targets = await load_sla_targets(db, uuid.uuid4())
assert targets["critical"] == 5
# All other severities keep defaults.
assert targets["high"] == DEFAULT_SLA_TARGETS["high"]["mttd_target"]
assert targets["medium"] == DEFAULT_SLA_TARGETS["medium"]["mttd_target"]
assert targets["low"] == DEFAULT_SLA_TARGETS["low"]["mttd_target"]
assert targets["info"] == DEFAULT_SLA_TARGETS["info"]["mttd_target"]
@pytest.mark.asyncio
async def test_unknown_severity_override_is_ignored(self) -> None:
# A row with a severity outside the canonical ladder
# (mis-configured tenant table) must NOT inject a new key
# into the result — that would break the SQL ``CASE`` (which
# only branches on the canonical severities).
db = MagicMock()
db.execute = AsyncMock(return_value=_scalars_all_result([_tenant_sla_cfg("unknown_severity", 99)]))
targets = await load_sla_targets(db, uuid.uuid4())
assert "unknown_severity" not in targets
# Defaults still present and unmodified.
assert targets["critical"] == DEFAULT_SLA_TARGETS["critical"]["mttd_target"]
@pytest.mark.asyncio
async def test_multiple_overrides_apply_independently(self) -> None:
# Each row overrides exactly one severity. We expect the
# final dict to reflect every override, not just the last one.
db = MagicMock()
db.execute = AsyncMock(
return_value=_scalars_all_result(
[
_tenant_sla_cfg("critical", 10),
_tenant_sla_cfg("high", 20),
_tenant_sla_cfg("medium", 90),
]
)
)
targets = await load_sla_targets(db, uuid.uuid4())
assert targets["critical"] == 10
assert targets["high"] == 20
assert targets["medium"] == 90
# Untouched severities still default.
assert targets["low"] == DEFAULT_SLA_TARGETS["low"]["mttd_target"]
# ---------------------------------------------------------------------------
# sla_due_at_expression — compiles to a CASE expression
# ---------------------------------------------------------------------------
class TestSlaDueAtExpression:
"""Verify the SQL expression is well-formed.
We don't try to execute it here — that requires a real Postgres
session because ``Alert.first_seen + timedelta`` lowers to
Postgres' interval arithmetic. The integration tests cover that.
What we *can* lock in here is that the expression compiles into
a ``CASE`` block with one branch per severity in our target map.
"""
def test_returns_case_expression(self) -> None:
targets = {sev: cfg["mttd_target"] for sev, cfg in DEFAULT_SLA_TARGETS.items()}
expr = sla_due_at_expression(targets)
# ``case`` returns a ``Case`` element; compiling it as a
# generic SQL string is enough to prove it's well-formed.
compiled = str(expr).upper()
assert "CASE" in compiled
def test_empty_targets_still_compiles(self) -> None:
# Defensive: empty dict should not crash construction (the
# fallback branch using DEFAULT_SLA_TARGETS["info"] always
# exists).
expr = sla_due_at_expression({})
compiled = str(expr).upper()
assert "CASE" in compiled
# ---------------------------------------------------------------------------
# first_asset — host > user > asset > ip
# ---------------------------------------------------------------------------
class TestFirstAsset:
"""Single-pivot extraction for queue rows.
A regression here demotes the queue from "VPN brute force on
web-01" back to "VPN brute force" — the entity is what makes a
row scannable. We exhaustively cover every priority slot.
"""
def test_prefers_host_over_others(self) -> None:
alert = _alert(
affected_hosts=["web-01"],
affected_users=["alice@example.com"],
affected_assets=["asset-1"],
affected_ips=["10.0.0.1"],
)
asset = first_asset(alert)
assert isinstance(asset, QueueAsset)
assert asset.kind == "host"
assert asset.value == "web-01"
def test_falls_back_to_user_when_no_host(self) -> None:
alert = _alert(
affected_hosts=[],
affected_users=["alice@example.com"],
affected_ips=["10.0.0.1"],
)
asset = first_asset(alert)
assert asset is not None and asset.kind == "user"
assert asset.value == "alice@example.com"
def test_falls_back_to_asset_when_no_host_or_user(self) -> None:
alert = _alert(
affected_hosts=[],
affected_users=[],
affected_assets=["crown-jewel-db"],
affected_ips=["10.0.0.1"],
)
asset = first_asset(alert)
assert asset is not None and asset.kind == "asset"
assert asset.value == "crown-jewel-db"
def test_falls_back_to_ip_last(self) -> None:
alert = _alert(
affected_hosts=[],
affected_users=[],
affected_assets=[],
affected_ips=["192.168.1.42"],
)
asset = first_asset(alert)
assert asset is not None and asset.kind == "ip"
assert asset.value == "192.168.1.42"
def test_returns_none_when_no_entities(self) -> None:
# No entities → no anchor. The UI will omit the asset chip.
alert = _alert(
affected_hosts=[],
affected_users=[],
affected_assets=[],
affected_ips=[],
)
assert first_asset(alert) is None
def test_skips_blank_strings_and_non_strings(self) -> None:
# Lists sometimes contain ``""`` or ``None`` after legacy
# ingest pipelines stripped fields. The picker must skip
# them rather than emit ``QueueAsset(value="")``.
alert = _alert(
affected_hosts=["", None, 42, " "],
affected_users=["bob"],
)
asset = first_asset(alert)
assert asset is not None
assert asset.kind == "user"
assert asset.value == "bob"
def test_strips_surrounding_whitespace(self) -> None:
# Trailing spaces from a CSV-style ingest must not bleed
# into the rendered chip.
alert = _alert(affected_hosts=[" web-01 "])
asset = first_asset(alert)
assert asset is not None
assert asset.value == "web-01"
# ---------------------------------------------------------------------------
# first_action — lowest priority wins, normalises shapes
# ---------------------------------------------------------------------------
class TestFirstAction:
"""Top suggested-next-action projection."""
def test_returns_none_when_no_recommendations(self) -> None:
alert = _alert(ai_recommendations=[])
assert first_action(alert) is None
def test_returns_none_when_field_is_not_list(self) -> None:
# Defensive: older rows wrote dicts; the projection must
# bail out instead of crashing.
alert = _alert(ai_recommendations=None) # type: ignore[arg-type]
assert first_action(alert) is None
def test_accepts_legacy_string_form(self) -> None:
# Legacy: bare strings, no priority. Array index drives the
# priority assignment (1-indexed).
alert = _alert(ai_recommendations=["Reset password", "Notify user"])
action = first_action(alert)
assert isinstance(action, QueueAction)
assert action.action == "Reset password"
assert action.priority == 1
# Risk defaults to "low" for legacy entries.
assert action.risk == "low"
def test_lowest_priority_wins(self) -> None:
# Modern form: dict with explicit priority. Lower number =
# more urgent. Out-of-order recommendations must still pick
# the urgent one.
alert = _alert(
ai_recommendations=[
{"action": "Low urgency", "priority": 3, "risk": "low"},
{"action": "Highest urgency", "priority": 1, "risk": "high"},
{"action": "Medium urgency", "priority": 2, "risk": "medium"},
]
)
action = first_action(alert)
assert action is not None
assert action.action == "Highest urgency"
assert action.priority == 1
assert action.risk == "high"
def test_invalid_priority_falls_back_to_index(self) -> None:
# Non-integer priority → use array-index fallback.
alert = _alert(
ai_recommendations=[
{"action": "First", "priority": "garbage"},
{"action": "Second", "priority": "also bad"},
]
)
action = first_action(alert)
# Both fall back to idx+1 → first wins by index.
assert action is not None
assert action.action == "First"
assert action.priority == 1
def test_normalises_unknown_risk_to_low(self) -> None:
# Risk must be one of low/medium/high. Anything else gets
# clamped to ``low`` so the UI never paints a chip in an
# unknown colour.
alert = _alert(
ai_recommendations=[
{"action": "Quarantine host", "priority": 1, "risk": "EXTREME"},
]
)
action = first_action(alert)
assert action is not None
assert action.risk == "low"
def test_normalises_risk_case_insensitively(self) -> None:
# ``HIGH``/``High`` must be recognised, even though our enum
# is lowercase. Pipelines sometimes capitalise risk labels.
alert = _alert(
ai_recommendations=[
{"action": "Isolate", "priority": 1, "risk": "HIGH"},
]
)
action = first_action(alert)
assert action is not None
assert action.risk == "high"
def test_skips_empty_action_strings(self) -> None:
# An empty ``action`` field is useless to the analyst.
# Skip and pick the next candidate.
alert = _alert(
ai_recommendations=[
{"action": " ", "priority": 1},
{"action": "Real action", "priority": 2},
]
)
action = first_action(alert)
assert action is not None
assert action.action == "Real action"
def test_priority_is_clamped_to_minimum_one(self) -> None:
# ``priority=0`` or negative would distort the sort. We
# clamp at 1 so 0-priority entries don't outrank
# explicit-1 entries silently.
alert = _alert(
ai_recommendations=[
{"action": "Sneaky", "priority": -10},
{"action": "Honest", "priority": 1},
]
)
action = first_action(alert)
# Both end up priority=1; first in array wins the stable sort.
assert action is not None
assert action.action == "Sneaky"
assert action.priority == 1
# ---------------------------------------------------------------------------
# build_queue — owner filter, counts, ordering, pagination, projection
# ---------------------------------------------------------------------------
class TestBuildQueue:
"""End-to-end queue assembly with a mocked session.
The opinionated SQL ordering itself is exercised by the
integration tests in CI. Here we lock in:
* counts are populated for both buckets regardless of filter,
* the row projection produces correct ``QueueItem`` fields,
* pagination math (``pages`` and ``offset``) is correct,
* the ``owner=*`` selector picks the right total + filter.
"""
def _setup_db(
self,
*,
mine_count: int,
unassigned_count: int,
rows: list[tuple[Any, int, datetime]] | None = None,
tenant_overrides: list[Any] | None = None,
) -> MagicMock:
"""Wire a session with the queue's execute pattern.
Call order inside ``build_queue``:
1) ``load_sla_targets`` → ``scalars().all()`` on tenant SLA config
2) ``mine_count`` → ``scalar_one()``
3) ``unassigned_count`` → ``scalar_one()``
4) main projection query → ``.all()`` of row tuples
5) asset match (only when a page alert carries hosts/ips)
→ ``scalars().all()``; empty here, so the vulnerability
query never fires and priority falls back to severity
alone. The asset-aware ordering itself is exercised
against real Postgres in
``test_alert_queue_priority_live.py`` — a mock that
answered the array-overlap match would prove nothing.
"""
db = MagicMock()
db.execute = AsyncMock(
side_effect=[
_scalars_all_result(tenant_overrides or []),
_scalar_one_result(mine_count),
_scalar_one_result(unassigned_count),
_rows_all_result(rows or []),
_scalars_all_result([]),
]
)
return db
@pytest.mark.asyncio
async def test_counts_populated_for_both_buckets(self) -> None:
# Whatever the owner filter is, the response always carries
# ``counts.mine`` and ``counts.unassigned`` so the workbench
# tabs render without a second fetch.
tenant_id = uuid.uuid4()
user_id = uuid.uuid4()
db = self._setup_db(mine_count=3, unassigned_count=7)
resp = await build_queue(
db,
tenant_id=tenant_id,
user_id=user_id,
owner="me",
)
assert resp.counts == {"mine": 3, "unassigned": 7, "all": 10}
# Even though owner="me" filters to mine only, the counts
# *contract* never changes.
assert resp.owner == "me"
@pytest.mark.asyncio
async def test_owner_me_uses_mine_count_as_total(self) -> None:
db = self._setup_db(mine_count=3, unassigned_count=7)
resp = await build_queue(
db,
tenant_id=uuid.uuid4(),
user_id=uuid.uuid4(),
owner="me",
)
assert resp.total == 3
@pytest.mark.asyncio
async def test_owner_unassigned_uses_unassigned_count_as_total(self) -> None:
db = self._setup_db(mine_count=3, unassigned_count=7)
resp = await build_queue(
db,
tenant_id=uuid.uuid4(),
user_id=uuid.uuid4(),
owner="unassigned",
)
assert resp.total == 7
@pytest.mark.asyncio
async def test_owner_all_uses_sum_as_total(self) -> None:
db = self._setup_db(mine_count=3, unassigned_count=7)
resp = await build_queue(
db,
tenant_id=uuid.uuid4(),
user_id=uuid.uuid4(),
owner="all",
)
assert resp.total == 10
@pytest.mark.asyncio
async def test_empty_queue_returns_zero_total_and_no_pages(self) -> None:
# No alerts at all → total=0, pages=0, items=[]. The
# frontend uses ``pages > 0`` to decide whether to render the
# paginator.
db = self._setup_db(mine_count=0, unassigned_count=0, rows=[])
resp = await build_queue(
db,
tenant_id=uuid.uuid4(),
user_id=uuid.uuid4(),
owner="all",
)
assert resp.total == 0
assert resp.pages == 0
assert resp.items == []
@pytest.mark.asyncio
async def test_pages_math_rounds_up(self) -> None:
# ceil(7 / 3) = 3 pages. Off-by-one here would either hide
# the last page or show an empty trailing page.
db = self._setup_db(mine_count=7, unassigned_count=0, rows=[])
resp = await build_queue(
db,
tenant_id=uuid.uuid4(),
user_id=uuid.uuid4(),
owner="me",
page=1,
page_size=3,
)
assert resp.pages == 3
assert resp.page_size == 3
@pytest.mark.asyncio
async def test_page_size_clamped_to_max(self) -> None:
# A misbehaving client passing ``page_size=1_000_000`` must
# be clamped to ``QUEUE_MAX_PAGE_SIZE`` (200) so we don't
# let them yank the whole tenant in one request.
db = self._setup_db(mine_count=0, unassigned_count=0, rows=[])
resp = await build_queue(
db,
tenant_id=uuid.uuid4(),
user_id=uuid.uuid4(),
page_size=10_000,
)
assert resp.page_size == 200
@pytest.mark.asyncio
async def test_page_clamped_to_minimum_one(self) -> None:
# ``page=0`` or negative pages must collapse to page 1.
db = self._setup_db(mine_count=0, unassigned_count=0, rows=[])
resp = await build_queue(
db,
tenant_id=uuid.uuid4(),
user_id=uuid.uuid4(),
page=0,
)
assert resp.page == 1
@pytest.mark.asyncio
async def test_row_projection_fills_queue_item(self) -> None:
# Single-row sanity check: the projection must carry every
# field the frontend renders (severity, asset chip, action
# chip, SLA countdown, age, bucket label).
tenant_id = uuid.uuid4()
user_id = uuid.uuid4()
alert_id = uuid.uuid4()
first_seen = datetime.now(UTC) - timedelta(minutes=10)
sla_due = datetime.now(UTC) + timedelta(minutes=20)
alert = _alert(
alert_id=alert_id,
tenant_id=tenant_id,
severity="critical",
title="Pod crashlooping",
status="new",
priority=80,
assigned_to_id=user_id,
affected_hosts=["k8s-node-3"],
ai_recommendations=[{"action": "Drain node", "priority": 1, "risk": "high"}],
first_seen=first_seen,
)
# bucket=0 (mine), sla_due in the future (not breached).
db = self._setup_db(
mine_count=1,
unassigned_count=0,
rows=[(alert, 0, sla_due)],
)
resp = await build_queue(
db,
tenant_id=tenant_id,
user_id=user_id,
owner="me",
)
assert len(resp.items) == 1
item = resp.items[0]
assert item.id == alert_id
assert item.tenant_id == tenant_id
assert item.title == "Pod crashlooping"
assert item.severity == "critical"
assert item.priority == 80
assert item.bucket == "mine"
assert item.sla_breached is False
# Countdown is in the future, so > 0.
assert item.sla_remaining_seconds > 0
# No asset inventory in this harness, so priority is severity alone
# (critical base 900) and the basis says exactly that.
assert item.priority_score == 900
assert item.priority_basis["base"] == 900
# Since 1.1 the basis is not empty even with no context: a 1.0 row
# records that the actor's privilege at event time could not be
# resolved. The row has to be here rather than omitted, because an
# analyst reading "why is this ranked where it is" needs to be told
# the platform could not answer, not shown a list with the question
# silently missing. The score is unmoved, which is the other half.
assert [r["factor"] for r in item.priority_basis["rationale"]] == [1.0]
assert "unknown" in item.priority_basis["rationale"][0]["reason"]
# Age is positive (first_seen was 10 minutes ago).
assert item.age_seconds >= 0
# Asset chip pulls host first.
assert item.asset is not None
assert item.asset.kind == "host"
assert item.asset.value == "k8s-node-3"
# Action chip carries the structured recommendation.
assert item.suggested_action is not None
assert item.suggested_action.action == "Drain node"
assert item.suggested_action.risk == "high"
@pytest.mark.asyncio
async def test_breached_sla_flags_negative_remaining(self) -> None:
# The frontend flips the timer red when ``sla_breached``
# is true. We must report the boolean *and* a negative
# countdown for downstream UI to match.
tenant_id = uuid.uuid4()
user_id = uuid.uuid4()
alert = _alert(tenant_id=tenant_id)
sla_due_past = datetime.now(UTC) - timedelta(minutes=5)
db = self._setup_db(
mine_count=1,
unassigned_count=0,
rows=[(alert, 0, sla_due_past)],
)
resp = await build_queue(
db,
tenant_id=tenant_id,
user_id=user_id,
owner="me",
)
item = resp.items[0]
assert item.sla_breached is True
assert item.sla_remaining_seconds < 0
@pytest.mark.asyncio
async def test_unassigned_bucket_label_is_unassigned(self) -> None:
# bucket=1 maps to ``unassigned`` literal in the QueueItem.
# If this regresses, the UI's bucket grouping silently swaps.
tenant_id = uuid.uuid4()
user_id = uuid.uuid4()
alert = _alert(tenant_id=tenant_id, assigned_to_id=None)
sla_due = datetime.now(UTC) + timedelta(minutes=20)
db = self._setup_db(
mine_count=0,
unassigned_count=1,
rows=[(alert, 1, sla_due)],
)
resp = await build_queue(
db,
tenant_id=tenant_id,
user_id=user_id,
owner="unassigned",
)
assert resp.items[0].bucket == "unassigned"
@pytest.mark.asyncio
async def test_naive_datetimes_are_treated_as_utc(self) -> None:
# Postgres returns ``timestamptz`` rows as tz-aware datetimes
# in production, but a SQLite-style stand-in (or some test
# fixtures) may hand us naive datetimes. The projection must
# coerce them to UTC instead of crashing the arithmetic.
tenant_id = uuid.uuid4()
user_id = uuid.uuid4()
# Construct a naive datetime via UTC-now-stripped-tz so we are not
# tripped up by the ``utcnow()`` deprecation while still exercising
# the naive-coercion path that ``build_queue`` is meant to handle.
naive_first_seen = datetime.now(UTC).replace(tzinfo=None)
naive_sla_due = naive_first_seen + timedelta(minutes=10)
alert = _alert(tenant_id=tenant_id, first_seen=naive_first_seen)
db = self._setup_db(
mine_count=1,
unassigned_count=0,
rows=[(alert, 0, naive_sla_due)],
)
resp = await build_queue(
db,
tenant_id=tenant_id,
user_id=user_id,
owner="me",
)
# No crash + the projection coerced both stamps to UTC so
# they compare cleanly.
item = resp.items[0]
assert item.first_seen.tzinfo is not None
assert item.sla_due_at.tzinfo is not None
@pytest.mark.asyncio
async def test_generated_at_is_utc(self) -> None:
# The frontend uses ``generated_at`` for client-side clock
# drift correction; it MUST be tz-aware UTC.
db = self._setup_db(mine_count=0, unassigned_count=0, rows=[])
resp = await build_queue(
db,
tenant_id=uuid.uuid4(),
user_id=uuid.uuid4(),
)
assert resp.generated_at.tzinfo is not None
assert resp.generated_at.utcoffset() == timedelta(0)
@pytest.mark.asyncio
async def test_queue_closed_statuses_constant_matches_contract(self) -> None:
# Sanity check: any future status added to the alerts schema
# that "exits" the queue must be added here, or the SLA
# timer keeps ticking on resolved alerts.
assert "resolved" in QUEUE_CLOSED_STATUSES
assert "closed" in QUEUE_CLOSED_STATUSES
# Both "false_positive" (new canonical) and "fp" (legacy)
# must be honoured so a legacy disposition write doesn't
# keep the alert visible in the queue.
assert "false_positive" in QUEUE_CLOSED_STATUSES
assert "fp" in QUEUE_CLOSED_STATUSES
@pytest.mark.asyncio
async def test_unassigned_severities_constant_matches_contract(self) -> None:
# ``low``/``medium``/``info`` are deliberately NOT in the
# unassigned bucket — they're triaged in bulk on /alerts.
assert set(QUEUE_UNASSIGNED_SEVERITIES) == {"critical", "high"}
# ---------------------------------------------------------------------------
# claim_alert — atomic compare-and-set semantics
# ---------------------------------------------------------------------------
class TestClaimAlert:
"""All four documented branches of the claim path.
Two analysts can race against the same row; the function does a
SELECT, then an UPDATE guarded with ``WHERE assigned_to_id IS NULL``
so only one update can win. A regression in any branch produces a
bug visible in production (lost claims, false 409s, or silent
cross-tenant claim grants).
"""
@pytest.mark.asyncio
async def test_claim_unassigned_succeeds(self) -> None:
# Happy path. The SELECT returns an unassigned row, the
# UPDATE returns ``user_id``, commit + refresh fire.
tenant_id = uuid.uuid4()
user_id = uuid.uuid4()
alert_id = uuid.uuid4()
alert = _alert(alert_id=alert_id, tenant_id=tenant_id, assigned_to_id=None)
db = MagicMock()
db.execute = AsyncMock(
side_effect=[
# SELECT lookup
_scalar_one_or_none_result(alert),
# UPDATE ... RETURNING assigned_to_id
_scalar_one_or_none_result(user_id),
]
)
db.commit = AsyncMock()
db.refresh = AsyncMock()
result = await claim_alert(
db,
alert_id=alert_id,
tenant_id=tenant_id,
user_id=user_id,
)
assert result is alert
db.commit.assert_awaited_once()
db.refresh.assert_awaited_once_with(alert)
@pytest.mark.asyncio
async def test_claim_already_mine_is_idempotent(self) -> None:
# If I refresh the queue and re-click claim, we MUST NOT
# 409 — that would make the workbench feel broken. We
# return the existing alert without issuing an UPDATE.
tenant_id = uuid.uuid4()
user_id = uuid.uuid4()
alert_id = uuid.uuid4()
alert = _alert(alert_id=alert_id, tenant_id=tenant_id, assigned_to_id=user_id)
db = MagicMock()
db.execute = AsyncMock(return_value=_scalar_one_or_none_result(alert))
db.commit = AsyncMock()
db.refresh = AsyncMock()
result = await claim_alert(
db,
alert_id=alert_id,
tenant_id=tenant_id,
user_id=user_id,
)
assert result is alert
# Only ONE execute call (the SELECT). No UPDATE issued.
assert db.execute.await_count == 1
# No commit/refresh either — nothing changed.
db.commit.assert_not_awaited()
db.refresh.assert_not_awaited()
@pytest.mark.asyncio
async def test_claim_assigned_to_other_raises_409(self) -> None:
# The classic conflict: another analyst already owns this
# alert. We MUST surface the actual owner's ID so the UI
# toast can render ``"already claimed by @bob"``.
tenant_id = uuid.uuid4()
my_id = uuid.uuid4()
their_id = uuid.uuid4()
alert_id = uuid.uuid4()
alert = _alert(alert_id=alert_id, tenant_id=tenant_id, assigned_to_id=their_id)
db = MagicMock()
db.execute = AsyncMock(return_value=_scalar_one_or_none_result(alert))
with pytest.raises(AlertAlreadyClaimedError) as exc_info:
await claim_alert(
db,
alert_id=alert_id,
tenant_id=tenant_id,
user_id=my_id,
)
assert exc_info.value.alert_id == alert_id
assert exc_info.value.owner_id == their_id
@pytest.mark.asyncio
async def test_claim_missing_alert_raises_not_found(self) -> None:
# Wrong tenant, deleted alert, or typo in the URL → 404,
# NOT 409. The endpoint maps NotFound → 404 and Conflict →
# 409, so a regression here would silently swap the two.
tenant_id = uuid.uuid4()
user_id = uuid.uuid4()
alert_id = uuid.uuid4()
db = MagicMock()
db.execute = AsyncMock(return_value=_scalar_one_or_none_result(None))
with pytest.raises(AlertNotFoundError):
await claim_alert(
db,
alert_id=alert_id,
tenant_id=tenant_id,
user_id=user_id,
)
@pytest.mark.asyncio
async def test_claim_lost_race_falls_back_to_actual_owner(self) -> None:
# Two analysts hit "Claim" at the same time:
# * Both SELECTs see ``assigned_to_id=NULL``.
# * The first UPDATE wins (returns winner_id).
# * The second UPDATE's guarded WHERE no longer matches
# so RETURNING is empty.
# We must re-read to discover the *actual* owner and 409
# with the right ID — telling the loser they collided with
# themselves would be confusing.
tenant_id = uuid.uuid4()
my_id = uuid.uuid4()
winner_id = uuid.uuid4()
alert_id = uuid.uuid4()
# SELECT shows the alert as unassigned (we lost the race
# *after* this point).
alert = _alert(alert_id=alert_id, tenant_id=tenant_id, assigned_to_id=None)
db = MagicMock()
db.execute = AsyncMock(
side_effect=[
_scalar_one_or_none_result(alert), # SELECT
_scalar_one_or_none_result(None), # UPDATE RETURNING → empty (lost)
_scalar_one_or_none_result(winner_id), # re-read owner
]
)
with pytest.raises(AlertAlreadyClaimedError) as exc_info:
await claim_alert(
db,
alert_id=alert_id,
tenant_id=tenant_id,
user_id=my_id,
)
assert exc_info.value.owner_id == winner_id
@pytest.mark.asyncio
async def test_claim_lost_race_with_no_owner_raises_not_found(self) -> None:
# Pathological case: between our SELECT and UPDATE, the
# alert was deleted (admin scrubbed it, or tenant teardown).
# The re-read returns ``None`` and we surface 404 rather
# than a confusing 409 with a missing owner.
tenant_id = uuid.uuid4()
my_id = uuid.uuid4()
alert_id = uuid.uuid4()
alert = _alert(alert_id=alert_id, tenant_id=tenant_id, assigned_to_id=None)
db = MagicMock()
db.execute = AsyncMock(
side_effect=[
_scalar_one_or_none_result(alert),
_scalar_one_or_none_result(None),
_scalar_one_or_none_result(None), # re-read also returns None
]
)
with pytest.raises(AlertNotFoundError):
await claim_alert(
db,
alert_id=alert_id,
tenant_id=tenant_id,
user_id=my_id,
)
@pytest.mark.asyncio
async def test_claim_lost_race_where_we_actually_won(self) -> None:
# Rare but real: the UPDATE returns no row (driver hiccup,
# WHERE didn't trigger RETURNING), but the re-read shows
# we own the alert anyway. Treat this as a successful claim
# rather than 409-ing the legitimate owner.
tenant_id = uuid.uuid4()
my_id = uuid.uuid4()
alert_id = uuid.uuid4()
alert = _alert(alert_id=alert_id, tenant_id=tenant_id, assigned_to_id=None)
db = MagicMock()
db.execute = AsyncMock(
side_effect=[
_scalar_one_or_none_result(alert),
_scalar_one_or_none_result(None), # UPDATE returned nothing
_scalar_one_or_none_result(my_id), # but re-read shows we own it
]
)
db.commit = AsyncMock()
db.refresh = AsyncMock()
result = await claim_alert(
db,
alert_id=alert_id,
tenant_id=tenant_id,
user_id=my_id,
)
assert result is alert
db.commit.assert_awaited_once()
db.refresh.assert_awaited_once_with(alert)