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')