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) )
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
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
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)
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()
def test_list_users(arf, api_user): users = ExternalUserFactory.create_batch(5) req = arf.get(reverse("externaluser-list")) force_authenticate(req, user=api_user) response = ExternalUserViewSet.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" assert len(content["results"]) == ( len(users) + 1) # account for the API user we created in a fixture for user in content["results"]: assert user["app_id"] assert user["external_id"] assert user["display_name"]
def api_user(transactional_db): e = ExternalUserFactory() return e.owner