Exemplo n.º 1
0
def test_update_timeline_event(arf, api_user, update_key, update_value,
                               expected_value):
    incident = IncidentFactory.create()

    event_model = incident.timeline_events()[0]
    event_data = serializers.TimelineEventSerializer(event_model).data

    if update_key:
        event_data[update_key] = update_value

    req = arf.put(
        reverse("incident-timeline-event-list",
                kwargs={"incident_pk": incident.pk}),
        event_data,
        format="json",
    )
    force_authenticate(req, user=api_user)
    response = IncidentTimelineEventViewSet.as_view({"put": "update"})(
        req, incident_pk=incident.pk, pk=event_model.pk)

    assert response.status_code == 200, "Got non-200 response from API"
    if update_key:
        new_event = TimelineEvent.objects.get(pk=event_model.pk)

        expected = expected_value or update_value
        assert (getattr(new_event, update_key) == expected
                ), "Updated value wasn't persisted to the DB"
Exemplo n.º 2
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
Exemplo n.º 3
0
    def test_add_and_remove_pin(self, get_user_profile):
        incident = IncidentFactory.create()
        user = ExternalUserFactory.create()

        get_user_profile.return_value = {"name": user.display_name}

        text = faker.paragraph(nb_sentences=2, variable_nb_sentences=True)
        handle_pin_added(
            incident,
            {"item": {"message": {"user": user.external_id, "ts": 123, "text": text}}},
        )
        get_user_profile.assert_called_with(user.external_id)

        message = PinnedMessage.objects.get(incident=incident, message_ts=123)
        assert message.text == text

        handle_pin_removed(
            incident,
            {"item": {"message": {"user": user.external_id, "ts": 123, "text": text}}},
        )

        get_user_profile.assert_called_with(user.external_id)

        with pytest.raises(PinnedMessage.DoesNotExist):
            PinnedMessage.objects.get(incident=incident, message_ts=123)
            TimelineEvent.objects.get(
                incident=incident, timestamp=datetime.fromtimestamp(123)
            )
Exemplo n.º 4
0
def test_create_timeline_event(arf, api_user):
    incident = IncidentFactory.create()

    event_model = TimelineEventFactory.build(incident=incident)
    event_data = serializers.TimelineEventSerializer(event_model).data

    assert_create_timeline_event(arf, api_user, incident, event_data)
Exemplo n.º 5
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)
Exemplo n.º 6
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"
Exemplo n.º 7
0
def test_can_access_incident_logged_out_if_configured(client, db, settings):
    settings.RESPONSE_LOGIN_REQUIRED = False

    incident = IncidentFactory()

    response = client.get(reverse("incident_doc", args=(incident.pk, )))

    assert response.status_code == 200
Exemplo n.º 8
0
def test_cannot_access_incident_logged_out_if_configured(client, db, settings):
    settings.RESPONSE_LOGIN_REQUIRED = True

    incident = IncidentFactory()

    response = client.get(reverse("incident_doc", args=(incident.pk, )))

    assert response.status_code == 302
    assert response["location"].startswith(settings.LOGIN_URL)
Exemplo n.º 9
0
def test_update_incident_impact():
    incident = IncidentFactory.create()
    new_impact = faker.paragraph(nb_sentences=1)

    incident.impact = new_impact
    incident.save()

    event = TimelineEvent.objects.filter(incident=incident,
                                         event_type="incident_update").last()
    assert event.metadata["new_value"] == new_impact
Exemplo n.º 10
0
def test_update_incident_lead():
    incident = IncidentFactory.create()
    new_lead = ExternalUserFactory.create()

    incident.lead = new_lead
    incident.save()

    event = TimelineEvent.objects.filter(incident=incident,
                                         event_type="incident_update").last()
    assert event.metadata["new_value"]["display_name"] == new_lead.display_name
Exemplo n.º 11
0
def test_update_incident_summary():
    incident = IncidentFactory.create()
    new_summary = faker.paragraph(nb_sentences=3, variable_nb_sentences=True)

    incident.summary = new_summary
    incident.save()

    event = TimelineEvent.objects.filter(incident=incident,
                                         event_type="incident_update").last()
    assert event.metadata["new_value"] == new_summary
Exemplo n.º 12
0
def test_update_action_sanitized(arf, api_user):
    incident = IncidentFactory.create()
    action = incident.action_items()[0]
    action_data = serializers.ActionSerializer(action).data

    action_data["details"] = "<iframe>this should be escaped</iframe>"
    response = update_action(arf, api_user, incident.pk, action_data)
    assert response.status_code == 200, "Got non-201 response from API"

    updated_action = Action.objects.get(pk=action.pk)
    assert (updated_action.details ==
            "&lt;iframe&gt;this should be escaped&lt;/iframe&gt;")
Exemplo n.º 13
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)
Exemplo n.º 14
0
def test_update_incident_severity():
    incident = IncidentFactory.create()
    new_severity = random.choice(
        [x for x, _ in Incident.SEVERITIES if x != incident.severity])

    incident.severity = new_severity
    incident.save()

    event = TimelineEvent.objects.filter(incident=incident,
                                         event_type="incident_update").last()
    assert event.metadata["new_value"] == {
        "id": new_severity,
        "text": incident.severity_text(),
    }
Exemplo n.º 15
0
def test_delete_timeline_event(arf, api_user):
    incident = IncidentFactory.create()

    event_model = incident.timeline_events()[0]

    req = arf.delete(
        reverse("incident-timeline-event-list",
                kwargs={"incident_pk": incident.pk}))
    force_authenticate(req, user=api_user)
    response = IncidentTimelineEventViewSet.as_view({"delete": "destroy"})(
        req, incident_pk=incident.pk, pk=event_model.pk)

    assert response.status_code == 204, "Got non-204 response from API"
    with pytest.raises(TimelineEvent.DoesNotExist):
        TimelineEvent.objects.get(pk=event_model.pk)
Exemplo n.º 16
0
def test_update_action_user(arf, api_user):
    incident = IncidentFactory.create()
    user = ExternalUserFactory.create()

    action = incident.action_items()[0]

    action_data = serializers.ActionSerializer(action).data
    action_data["user"] = {
        "app_id": "slack",
        "display_name": user.display_name,
        "external_id": user.external_id,
    }
    response = update_action(arf, api_user, incident.pk, action_data)
    print(response.rendered_content)
    assert response.status_code == 200, "Got non-201 response from API"

    updated_action = Action.objects.get(pk=action.pk)
    assert updated_action.user == user
Exemplo n.º 17
0
def test_list_actions_by_incident(arf, api_user):
    incident = IncidentFactory.create()

    req = arf.get(reverse("incident-action-list", kwargs={"incident_pk": incident.pk}))
    force_authenticate(req, user=api_user)
    response = IncidentActionViewSet.as_view({"get": "list"})(
        req, incident_pk=incident.pk
    )

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

    assert len(content["results"]) == len(incident.action_items())
    for action in content["results"]:
        assert action["details"]
        assert "done" in action
        assert action["user"]
Exemplo n.º 18
0
def test_delete_action(arf, api_user):
    incident = IncidentFactory.create()
    user = ExternalUserFactory.create()

    action = ActionFactory.create(user=user, incident=incident)

    req = arf.delete(
        reverse("incident-action-list", kwargs={"incident_pk": incident.pk}),
        format="json",
    )
    force_authenticate(req, user=api_user)
    response = IncidentActionViewSet.as_view({"delete": "destroy"})(
        req, incident_pk=incident.pk, pk=action.pk
    )

    assert response.status_code == 204, "Got non-204 response from API"
    with pytest.raises(Action.DoesNotExist):
        Action.objects.get(pk=action.pk)
Exemplo n.º 19
0
def test_create_action(arf, api_user):
    incident = IncidentFactory.create()
    user = ExternalUserFactory.create()

    action_model = ActionFactory.build(user=user)
    action = serializers.ActionSerializer(action_model).data

    req = arf.post(
        reverse("incident-action-list", kwargs={"incident_pk": incident.pk}),
        action,
        format="json",
    )
    force_authenticate(req, user=api_user)
    response = IncidentActionViewSet.as_view({"post": "create"
                                              })(req, incident_pk=incident.pk)

    assert response.status_code == 201, "Got non-201 response from API"
    assert Action.objects.filter(details=action_model.details).exists()
Exemplo n.º 20
0
def test_list_actions_by_incident(arf, api_user):
    incident = IncidentFactory.create()

    req = arf.get(
        reverse("incident-timeline-event-list",
                kwargs={"incident_pk": incident.pk}))
    force_authenticate(req, user=api_user)
    response = IncidentTimelineEventViewSet.as_view({"get": "list"})(
        req, incident_pk=incident.pk)

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

    assert len(content["results"]) == len(incident.timeline_events())
    for event in content["results"]:
        assert event["timestamp"]
        assert event["text"]
        assert event["event_type"]
Exemplo n.º 21
0
def test_create_timeline_event(arf, api_user):
    incident = IncidentFactory.create()

    event_model = TimelineEventFactory.build(incident=incident)
    event_data = serializers.TimelineEventSerializer(event_model).data

    req = arf.post(
        reverse("incident-timeline-event-list",
                kwargs={"incident_pk": incident.pk}),
        event_data,
        format="json",
    )
    force_authenticate(req, user=api_user)
    response = IncidentTimelineEventViewSet.as_view({"post": "create"})(
        req, incident_pk=incident.pk)

    assert response.status_code == 201, "Got non-201 response from API"

    new_action = TimelineEvent.objects.get(incident=incident,
                                           timestamp=event_model.timestamp)
Exemplo n.º 22
0
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"
Exemplo n.º 23
0
def test_update_action(arf, api_user):
    incident = IncidentFactory.create()

    action = incident.action_items()[0]

    action_data = serializers.ActionSerializer(action).data

    action_data["details"] = faker.paragraph(nb_sentences=2, variable_nb_sentences=True)
    response = update_action(arf, api_user, incident.pk, action_data)
    print(response.rendered_content)
    assert response.status_code == 200, "Got non-201 response from API"

    updated_action = Action.objects.get(pk=action.pk)
    assert updated_action.details == action_data["details"]

    action_data["done"] = not action_data["done"]
    response = update_action(arf, api_user, incident.pk, action_data)
    print(response.rendered_content)
    assert response.status_code == 200, "Got non-201 response from API"

    updated_action = Action.objects.get(pk=action.pk)
    assert updated_action.done == action_data["done"]
Exemplo n.º 24
0
def test_update_action(arf, api_user):
    incident = IncidentFactory.create()

    action = incident.action_items()[0]

    action_data = serializers.ActionSerializer(action).data

    action_data["details"] = faker.paragraph(nb_sentences=2,
                                             variable_nb_sentences=True)
    response = update_action(arf, api_user, incident.pk, action_data)
    print(response.rendered_content)
    assert response.status_code == 200, "Got non-201 response from API"

    updated_action = Action.objects.get(pk=action.pk)
    assert updated_action.details == action_data["details"]

    priorityChoices = ["1", "2", "3"]
    priorityChoices.remove(action.priority)
    action_data["done"] = not action_data["done"]
    action_data["priority"] = str(random.choice(priorityChoices))
    typeChoices = ["1", "2", "3"]
    typeChoices.remove(action.type)
    action_data["type"] = str(random.choice(typeChoices))
    action_data["done_date"] = faker.date_time_between(
        start_date=action.created_date, end_date="now")
    action_data["due_date"] = faker.date_time_between(start_date="-6m",
                                                      end_date="+6m")
    response = update_action(arf, api_user, incident.pk, action_data)
    print(response.rendered_content)
    assert response.status_code == 200, "Got non-201 response from API"

    updated_action = Action.objects.get(pk=action.pk)
    assert updated_action.done == action_data["done"]
    assert updated_action.priority == action_data["priority"]
    assert updated_action.type == action_data["type"]
    assert updated_action.done_date == action_data["done_date"]
    assert updated_action.due_date == action_data["due_date"]
Exemplo n.º 25
0
def test_handle_channel_rename(mock_slack):
    incident = IncidentFactory.create()

    handle_channel_rename(incident, {"channel": {"name": "new-channel-name"}})
    assert incident.comms_channel().channel_name == "new-channel-name"