|
| 1 | +import pytest |
| 2 | +from django.contrib.auth.models import User |
| 3 | + |
| 4 | +from firetower.incidents.models import ( |
| 5 | + ExternalLink, |
| 6 | + ExternalLinkType, |
| 7 | + Incident, |
| 8 | + IncidentSeverity, |
| 9 | + IncidentStatus, |
| 10 | + Tag, |
| 11 | + TagType, |
| 12 | +) |
| 13 | +from firetower.incidents.serializers import ( |
| 14 | + IncidentDetailSerializer, |
| 15 | + IncidentListSerializer, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.django_db |
| 20 | +class TestIncidentListSerializer: |
| 21 | + def test_incident_list_serialization(self): |
| 22 | + """Test incident serialization for list view""" |
| 23 | + captain = User.objects.create_user( |
| 24 | + |
| 25 | + |
| 26 | + first_name="Jane", |
| 27 | + last_name="Captain", |
| 28 | + ) |
| 29 | + reporter = User.objects.create_user( |
| 30 | + |
| 31 | + |
| 32 | + first_name="John", |
| 33 | + last_name="Reporter", |
| 34 | + ) |
| 35 | + |
| 36 | + incident = Incident.objects.create( |
| 37 | + title="Test Incident", |
| 38 | + description="Test description", |
| 39 | + impact="Test impact", |
| 40 | + status=IncidentStatus.ACTIVE, |
| 41 | + severity=IncidentSeverity.P1, |
| 42 | + is_private=False, |
| 43 | + captain=captain, |
| 44 | + reporter=reporter, |
| 45 | + ) |
| 46 | + |
| 47 | + # Add tags |
| 48 | + area_tag = Tag.objects.create(name="API", type=TagType.AFFECTED_AREA) |
| 49 | + cause_tag = Tag.objects.create(name="Database", type=TagType.ROOT_CAUSE) |
| 50 | + incident.affected_area_tags.add(area_tag) |
| 51 | + incident.root_cause_tags.add(cause_tag) |
| 52 | + |
| 53 | + serializer = IncidentListSerializer(incident) |
| 54 | + data = serializer.data |
| 55 | + |
| 56 | + # Check id is incident_number string (frontend compatibility) |
| 57 | + assert data["id"] == f"INC-{incident.id}" |
| 58 | + assert data["title"] == "Test Incident" |
| 59 | + assert data["status"] == IncidentStatus.ACTIVE |
| 60 | + assert data["severity"] == IncidentSeverity.P1 |
| 61 | + |
| 62 | + # List view should not include captain/reporter/tags |
| 63 | + assert "captain" not in data |
| 64 | + assert "reporter" not in data |
| 65 | + assert "affected_areas" not in data |
| 66 | + assert "root_causes" not in data |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.django_db |
| 70 | +class TestIncidentDetailSerializer: |
| 71 | + def test_incident_detail_serialization(self): |
| 72 | + """Test incident serialization for detail view (matches frontend expectations)""" |
| 73 | + captain = User.objects.create_user( |
| 74 | + |
| 75 | + |
| 76 | + first_name="Jane", |
| 77 | + last_name="Captain", |
| 78 | + ) |
| 79 | + reporter = User.objects.create_user( |
| 80 | + |
| 81 | + |
| 82 | + first_name="John", |
| 83 | + last_name="Reporter", |
| 84 | + ) |
| 85 | + participant = User.objects.create_user( |
| 86 | + |
| 87 | + |
| 88 | + first_name="Alice", |
| 89 | + last_name="Participant", |
| 90 | + ) |
| 91 | + |
| 92 | + incident = Incident.objects.create( |
| 93 | + title="Test Incident", |
| 94 | + description="Test description", |
| 95 | + status=IncidentStatus.MITIGATED, |
| 96 | + severity=IncidentSeverity.P2, |
| 97 | + captain=captain, |
| 98 | + reporter=reporter, |
| 99 | + ) |
| 100 | + incident.participants.add(participant) |
| 101 | + |
| 102 | + # Add tags |
| 103 | + area_tag = Tag.objects.create(name="API", type=TagType.AFFECTED_AREA) |
| 104 | + cause_tag = Tag.objects.create(name="Database", type=TagType.ROOT_CAUSE) |
| 105 | + incident.affected_area_tags.add(area_tag) |
| 106 | + incident.root_cause_tags.add(cause_tag) |
| 107 | + |
| 108 | + # Add external links |
| 109 | + ExternalLink.objects.create( |
| 110 | + incident=incident, |
| 111 | + type=ExternalLinkType.SLACK, |
| 112 | + url="https://slack.com/channels/incident-123", |
| 113 | + ) |
| 114 | + |
| 115 | + serializer = IncidentDetailSerializer(incident) |
| 116 | + data = serializer.data |
| 117 | + |
| 118 | + # Check id is incident_number string (frontend compatibility) |
| 119 | + assert data["id"] == f"INC-{incident.id}" |
| 120 | + assert data["title"] == "Test Incident" |
| 121 | + |
| 122 | + # Check nested users for captain/reporter (name and avatar only) |
| 123 | + assert data["captain"]["name"] == "Jane Captain" |
| 124 | + assert "avatar_url" in data["captain"] |
| 125 | + assert "email" not in data["captain"] |
| 126 | + |
| 127 | + assert data["reporter"]["name"] == "John Reporter" |
| 128 | + assert "avatar_url" in data["reporter"] |
| 129 | + assert "email" not in data["reporter"] |
| 130 | + |
| 131 | + # Check participants structure (matches frontend expectation) |
| 132 | + assert len(data["participants"]) == 3 # captain, reporter, participant |
| 133 | + |
| 134 | + # Find each participant |
| 135 | + captain_participant = next( |
| 136 | + p for p in data["participants"] if p["role"] == "Captain" |
| 137 | + ) |
| 138 | + assert captain_participant["name"] == "Jane Captain" |
| 139 | + assert "avatar_url" in captain_participant |
| 140 | + |
| 141 | + reporter_participant = next( |
| 142 | + p for p in data["participants"] if p["role"] == "Reporter" |
| 143 | + ) |
| 144 | + assert reporter_participant["name"] == "John Reporter" |
| 145 | + |
| 146 | + other_participant = next( |
| 147 | + p for p in data["participants"] if p["role"] == "Participant" |
| 148 | + ) |
| 149 | + assert other_participant["name"] == "Alice Participant" |
| 150 | + |
| 151 | + # Check affected_areas and root_causes as arrays of strings |
| 152 | + assert "API" in data["affected_areas"] |
| 153 | + assert "Database" in data["root_causes"] |
| 154 | + |
| 155 | + # Check external links (dict format for frontend compatibility) |
| 156 | + assert "slack" in data["external_links"] |
| 157 | + assert ( |
| 158 | + data["external_links"]["slack"] == "https://slack.com/channels/incident-123" |
| 159 | + ) |
| 160 | + assert data["external_links"]["jira"] is None # Not set |
0 commit comments