poulpe/api/test_event.py
2017-07-23 18:23:25 +02:00

118 lines
4.3 KiB
Python

from datetime import timedelta
from django.contrib.auth import get_user_model
from django.utils import timezone
from django.urls import reverse
from rest_framework.test import APIRequestFactory, APITestCase
from rest_framework import status
from event.models import Event, Place, ActivityTag, ActivityTemplate
User = get_user_model()
class EventTest(APITestCase):
@classmethod
def setUpTestData(cls):
cls.user = User.objects.create(
username='enarmanli',
email='erkan.narmanli@ens.fr',
first_name='Erkan',
last_name='Narmanli',
)
cls.event_data = {
"title": "test event",
"slug": "test-event",
"description": "C'est trop cool !",
"beginning_date": "2017-07-18T18:05:00Z",
"ending_date": "2017-07-19T18:05:00Z",
}
# def test_anonymous_create(self):
# """
# ensure anonymous can't create a new event object using API
# """
# url = reverse('event-list')
# response = self.client.post(url, self.event_data, format='json')
# self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_user_create(self):
"""
ensure we can create a new event object using API
"""
url = reverse('event-list')
self.client.force_authenticate(user=self.user)
response = self.client.post(url, self.event_data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Event.objects.count(), 1)
self.assertEqual(Event.objects.get().title, 'test event')
class EventSpecificTest(APITestCase):
"""
Tests is the EventSpecifics querysets are rendered correctly
using the API
"""
@classmethod
def setUpTestData(cls):
cls.user = User.objects.create_user(username='user', password='pass', )
cls.event = Event.objects.create(
title='La Nuit 2042',
slug='nuit42',
created_by=cls.user,
creation_date=timezone.now(),
description="La nuit c'est lol",
beginning_date=timezone.now()
+ timedelta(days=30),
ending_date=timezone.now()
+ timedelta(days=31),
)
cls.factory = APIRequestFactory()
def test_place(self):
Place.objects.create(name="root-place", event=None)
Place.objects.create(name="event-place", event=self.event)
self.client.force_authenticate(user=self.user)
url = reverse('place-list')
response = self.client.get(url, format='json')
self.assertEqual(response.json()['count'], 1)
event_id = self.event.id
url = reverse('event-place-list', kwargs={'event_pk': event_id})
response = self.client.get(url, format='json')
self.assertEqual(response.json()['count'], 2)
def test_tag(self):
ActivityTag.objects.create(name="root-tag",
is_public=True,
color="#000",
event=None)
ActivityTag.objects.create(name="event-tag",
is_public=True,
color="#FFF",
event=self.event)
self.client.force_authenticate(user=self.user)
url = reverse('activitytag-list')
response = self.client.get(url, format='json')
self.assertEqual(response.json()['count'], 1)
event_id = self.event.id
url = reverse('event-activitytag-list', kwargs={'event_pk': event_id})
response = self.client.get(url, format='json')
self.assertEqual(response.json()['count'], 2)
def test_activitytemplate(self):
ActivityTemplate.objects.create(title="test", event=self.event)
self.client.force_authenticate(user=self.user)
event_id = self.event.id
url = reverse('event-activitytemplate-list',
kwargs={'event_pk': event_id})
response = self.client.get(url, format='json')
self.assertEqual(response.json()['count'], 1)