Skip to content

Commit

Permalink
API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrco committed Nov 7, 2019
1 parent 5e6eca7 commit 60450b4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/api/test_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import json

from tests.factories import EventFactory

from django.urls import reverse
from rest_framework.test import force_authenticate

from response.core.views import EventsViewSet

def test_list_events(arf, api_user):

persisted_events = EventFactory.create_batch(10)

req = arf.get(reverse("event-list"))
force_authenticate(req, user=api_user)
response = EventsViewSet.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_events,
), "Didn't get expected number of events back"
2 changes: 2 additions & 0 deletions tests/factories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
from .incident import IncidentFactory
from .timeline import TimelineEventFactory
from .user import ExternalUserFactory, UserFactory
from .event import EventFactory

__all__ = (
"IncidentFactory",
"TimelineEventFactory",
"UserFactory",
"ActionFactory",
"ExternalUserFactory",
"EventFactory",
)
3 changes: 3 additions & 0 deletions tests/factories/action.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import factory
from faker import Factory

from django.db.models.signals import post_save

from response.core.models import Action

faker = Factory.create()


@factory.django.mute_signals(post_save)
class ActionFactory(factory.DjangoModelFactory):
class Meta:
model = Action
Expand Down
32 changes: 32 additions & 0 deletions tests/factories/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import factory
import random

from .incident import IncidentFactory
from .action import ActionFactory

from faker import Factory

from response.core.models import Event
from response.core.serializers import IncidentSerializer, ActionSerializer

faker = Factory.create()


def incident_payload():
return IncidentSerializer(IncidentFactory.create()).data


def action_payload():
return ActionSerializer(ActionFactory.create()).data


class EventFactory(factory.DjangoModelFactory):
class Meta:
model = Event

timestamp = factory.LazyFunction(
lambda: faker.date_time_between(start_date="-6m", end_date="now", tzinfo=None)
)
event_type = random.choice([Event.ACTION_EVENT_TYPE, Event.INCIDENT_EVENT_TYPE])

payload = random.choice([incident_payload, action_payload])()

0 comments on commit 60450b4

Please sign in to comment.