test started

This commit is contained in:
Qwann 2017-07-23 16:06:52 +02:00
parent d7ee270fbf
commit ac6b8058f4
2 changed files with 47 additions and 1 deletions

46
api/test_event.py Normal file
View file

@ -0,0 +1,46 @@
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from rest_framework.test import APIRequestFactory, APITestCase,\
force_authenticate, APIClient
from rest_framework import status
from event.models import Event
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')

View file

@ -5,7 +5,7 @@ from api.event.views import EventViewSet, PlaceViewSet, ActivityTagViewSet,\
# Create a router and register our viewsets with it.
router = SimpleRouter()
router.register(r'event', EventViewSet)
router.register(r'event', EventViewSet, 'event')
router.register(r'place', PlaceViewSet)
router.register(r'tag', ActivityTagViewSet)