Add authentication for post test
This commit is contained in:
+12
-1
@@ -2,8 +2,10 @@
|
||||
|
||||
from django.test import TestCase
|
||||
from django.core.files import File
|
||||
from django.contrib.auth.models import User
|
||||
from rest_framework.test import APITestCase
|
||||
from rest_framework import status
|
||||
from rest_framework.test import force_authenticate
|
||||
from webapp.models import Tag, Feed
|
||||
from webapp.serializers import TagSerializer, FeedSerializer
|
||||
|
||||
@@ -76,6 +78,9 @@ class FeedTestCase(APITestCase):
|
||||
self.assertEqual(Feed.objects.count(), 1)
|
||||
self.assertEqual(Feed.objects.all()[0].tags.count(), 2)
|
||||
|
||||
username, password = 'test_admin', 'password123'
|
||||
self.authClient = User.objects.create_superuser(username, 'myemail@test.com', password)
|
||||
|
||||
def test_get_feed(self):
|
||||
response = self.client.get('/api/feed/', format='json')
|
||||
self.assertTrue(status.is_success(response.status_code))
|
||||
@@ -92,9 +97,15 @@ class FeedTestCase(APITestCase):
|
||||
tag2_id = Tag.objects.get(slug="test2").id
|
||||
|
||||
data = {'tags': [tag1_id, tag2_id], 'title': 'testtitle', 'visible': 'True', 'description': 'liirumlaarum', 'content': 'lorem ipsum'}
|
||||
# Try post without authentication
|
||||
response = self.client.post('/api/feed/', data, format='multipart')
|
||||
self.assertTrue(status.is_client_error(response.status_code))
|
||||
self.assertEqual(Feed.objects.count(), 1)
|
||||
# Authenticate
|
||||
self.client.force_authenticate(user=self.authClient)
|
||||
response = self.client.post('/api/feed/', data, format='multipart')
|
||||
# Return success and check object was created
|
||||
self.assertTrue(status.is_success(response.status_code))
|
||||
|
||||
self.assertEqual(Feed.objects.count(), 2)
|
||||
|
||||
created = Feed.objects.get(title="testtitle")
|
||||
|
||||
Reference in New Issue
Block a user