69 lines
3.0 KiB
Python
69 lines
3.0 KiB
Python
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
|
|
|
|
from collections import OrderedDict
|
|
from itertools import islice
|
|
import tempfile
|
|
|
|
|
|
class FeedTestCase(APITestCase):
|
|
|
|
def setUp(self):
|
|
self.icon = tempfile.NamedTemporaryFile(suffix=".jpg").name
|
|
Tag.objects.create(slug='testtag1', name='test1', icon=self.icon)
|
|
tag1 = Tag.objects.get(slug="testtag1")
|
|
Tag.objects.create(slug="testtag2", name='test2', icon=self.icon)
|
|
tag2 = Tag.objects.get(slug="testtag2")
|
|
self.assertEqual(Tag.objects.count(), 2)
|
|
|
|
Feed.objects.create(title="TestFeed", visible=True, description="diidadaapa", content="lorem ipsum")
|
|
Feed.objects.get(title="TestFeed").tags.add(tag1)
|
|
Feed.objects.get(title="TestFeed").tags.add(tag2)
|
|
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))
|
|
|
|
feeds = Feed.objects.all()
|
|
serializer = FeedSerializer(feeds, many=True)
|
|
|
|
# DRF extends path given by serializer with the protocol and domain for icon
|
|
# Ignore tag on serializer and response. This is tested on TagTestCase.
|
|
# Note that we assume the length here to be 1
|
|
response.data['results'][0].pop('tags')
|
|
serializer.data[0].pop('tags')
|
|
self.assertEqual(response.data['results'], serializer.data)
|
|
|
|
def test_post_feed(self):
|
|
Tag.objects.create(slug="test1", name="testsds")
|
|
Tag.objects.create(slug="test2", name="testsdsd")
|
|
tag1_id = Tag.objects.get(slug="test1").id
|
|
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")
|
|
print(created.tags)
|
|
# self.assertEqual(created.tags.count(), 2)
|