64 lines
2.8 KiB
Python
64 lines
2.8 KiB
Python
from django.test import TestCase
|
|
from django.contrib.auth.models import User
|
|
from rest_framework import status
|
|
from rest_framework.test import APITestCase, APIRequestFactory, force_authenticate
|
|
|
|
from webapp.models import Tag, Feed
|
|
from webapp.serializers import FeedSerializer
|
|
|
|
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,
|
|
context={
|
|
"request": APIRequestFactory().get(r"http://testserver/api/events/")
|
|
})
|
|
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_fi': 'testtitle', 'title_en': 'testtitle', 'visible': 'True', 'description_fi': 'liirumlaarum', 'description_en': 'liirumlaarum', 'content_fi': 'lorem ipsum', 'content_en': '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_fi="testtitle")
|
|
print(created.tags)
|
|
# self.assertEqual(created.tags.count(), 2)
|