Ejemplo n.º 1
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)
Ejemplo n.º 2
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"
Ejemplo 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)
            )
Ejemplo n.º 4
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
Ejemplo n.º 5
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
Ejemplo n.º 6
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
Ejemplo n.º 7
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;")
Ejemplo n.º 8
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(),
    }
Ejemplo n.º 9
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)
Ejemplo n.º 10
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()
Ejemplo n.º 11
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
Ejemplo n.º 12
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"]
Ejemplo n.º 13
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)
Ejemplo n.º 14
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"]
Ejemplo n.º 15
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)
Ejemplo n.º 16
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"
Ejemplo n.º 17
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"]
Ejemplo n.º 18
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"]
Ejemplo n.º 19
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"