poulpe/api/test_event.py

119 lines
4.3 KiB
Python
Raw Normal View History

2017-07-23 18:23:25 +02:00
from datetime import timedelta
2017-07-23 16:06:52 +02:00
from django.contrib.auth import get_user_model
2017-07-23 18:23:25 +02:00
from django.utils import timezone
2017-07-23 16:06:52 +02:00
from django.urls import reverse
2017-07-23 18:23:25 +02:00
from rest_framework.test import APIRequestFactory, APITestCase
2017-07-23 16:06:52 +02:00
from rest_framework import status
2017-07-23 18:23:25 +02:00
from event.models import Event, Place, ActivityTag, ActivityTemplate
2017-07-23 16:06:52 +02:00
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",
}
2017-07-23 18:23:25 +02:00
# 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)
2017-07-23 16:06:52 +02:00
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')
2017-07-23 18:23:25 +02:00
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)