Example #1
0
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
Example #2
0
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)
Example #3
0
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"
Example #4
0
def test_list_incidents_by_month(arf, api_user):
    IncidentFactory.create_batch(5)

    today = datetime.date.today()
    if today.month == 1:
        last_month = 12
        year = today.year - 1
    else:
        last_month = today.month - 1
        year = today.year

    req = arf.get(
        reverse(
            "incidents-bymonth-list",
            kwargs={
                "year": str(year),
                "month": f"{last_month:02d}"
            },
        ))
    print(req)
    force_authenticate(req, user=api_user)
    response = IncidentsByMonthViewSet.as_view({"get": "list"})(req, year,
                                                                last_month)

    assert response.status_code == 200, "Got non-200 response from API"
    content = json.loads(response.rendered_content)

    print(content)
    assert "results" in content, "Response didn't have results key"

    for incident in content["results"]:
        assert incident["report_time"]
        report_time = datetime.datetime.strptime(incident["report_time"],
                                                 "%Y-%m-%dT%H:%M:%S")
        assert report_time.month == last_month
        assert report_time.year == year

        assert_incident_response(incident)