def test_list_incidents(arf, api_user): persisted_incidents = IncidentFactory.create_batch(5) req = arf.get(reverse("incident-list")) force_authenticate(req, user=api_user) response = IncidentViewSet.as_view({"get": "list"})(req) assert response.status_code == 200, "Got non-200 response from API" content = json.loads(response.rendered_content) assert "results" in content, "Response didn't have results key" incidents = content["results"] assert len(incidents) == len( persisted_incidents), "Didn't get expected number of incidents back" for idx, incident in enumerate(incidents): assert incident["report_time"] # incidents should be in order of newest to oldest if idx != len(incidents) - 1: assert ( incident["report_time"] >= incidents[idx + 1]["report_time"] ), "Incidents are not in order of newest to oldest by report time" assert_incident_response(incident)
def test_update_incident(arf, api_user, update_key, update_value): """ Tests that we can PUT /incidents/<pk> and mutate fields that get saved to the DB. """ persisted_incidents = IncidentFactory.create_batch(5) incident = persisted_incidents[0] serializer = serializers.IncidentSerializer(incident) serialized = serializer.data updated = serialized del updated["reporter"] # can't update reporter if update_key: updated[update_key] = update_value req = arf.put(reverse("incident-detail", kwargs={"pk": incident.pk}), updated, format="json") force_authenticate(req, user=api_user) response = IncidentViewSet.as_view({"put": "update"})(req, pk=incident.pk) print(response.rendered_content) assert response.status_code == 200, "Got non-200 response from API" if update_key: new_incident = Incident.objects.get(pk=incident.pk) assert (getattr(new_incident, update_key) == update_value ), "Updated value wasn't persisted to the DB"
def test_update_incident_lead(arf, api_user): """ Tests that we can update the incident lead by name """ persisted_incidents = IncidentFactory.create_batch(5) incident = persisted_incidents[0] serializer = serializers.IncidentSerializer(incident) updated = serializer.data users = ExternalUser.objects.all() new_lead = users[0] while new_lead == incident.lead: new_lead = random.choices(users) updated["lead"] = serializers.ExternalUserSerializer(new_lead).data del updated["reporter"] # can't update reporter req = arf.put(reverse("incident-detail", kwargs={"pk": incident.pk}), updated, format="json") force_authenticate(req, user=api_user) response = IncidentViewSet.as_view({"put": "update"})(req, pk=incident.pk) print(response.rendered_content) assert response.status_code == 200, "Got non-200 response from API" new_incident = Incident.objects.get(pk=incident.pk) assert new_incident.lead == new_lead
def test_cannot_unset_severity(arf, api_user): """ Tests that we cannot unset the incident severity """ incident = IncidentFactory.create() serializer = serializers.IncidentSerializer(incident) updated = serializer.data updated["severity"] = None # unset severity req = arf.put(reverse("incident-detail", kwargs={"pk": incident.pk}), updated, format="json") force_authenticate(req, user=api_user) response = IncidentViewSet.as_view({"put": "update"})(req, pk=incident.pk) print(response.rendered_content) assert (response.status_code != 200), "Got 200 response from API when we expected an error"