Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:theonion/django-bulbs
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Sinchok committed Nov 5, 2014
2 parents 7272433 + 4da1233 commit f303365
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 165 deletions.
98 changes: 28 additions & 70 deletions tests/api/test_content_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
from django.template.defaultfilters import slugify
from django.test.client import Client
from django.utils import timezone

from bulbs.content.models import LogEntry, Tag, Content, ObfuscatedUrlInfo
from bulbs.content.serializers import TagSerializer

from tests.testcontent.models import TestContentObj, TestContentDetailImage
from tests.utils import JsonEncoder, BaseAPITestCase
from tests.utils import JsonEncoder, BaseAPITestCase, make_content


User = get_user_model()
Expand All @@ -23,29 +25,15 @@ class TestContentListingAPI(BaseAPITestCase):
def setUp(self):
super(TestContentListingAPI, self).setUp()
for i in range(47):
TestContentObj.objects.create(
title="Testing published content {}".format(i),
description="Doesn't matter what it is, AS LONG AS IT GETS CLICKZ",
foo="SUCK IT, NERDS.",
published=timezone.now() - timedelta(hours=1)
)
make_content(published=timezone.now() - timedelta(hours=1))

for i in range(32):
TestContentObj.objects.create(
title="Testing published content {}".format(i),
description="Doesn't matter what it is, AS LONG AS IT GETS CLICKZ",
foo="SUCK IT, NERDS.",
published=timezone.now() + timedelta(hours=1)
)
make_content(published=timezone.now() + timedelta(hours=1))

for i in range(13):
TestContentObj.objects.create(
title="Testing published content {}".format(i),
description="Doesn't matter what it is, AS LONG AS IT GETS CLICKZ",
foo="SUCK IT, NERDS."
)
make_content(published=None)

TestContentObj.search_objects.refresh()
Content.search_objects.refresh()

def test_list_final(self):

Expand All @@ -64,9 +52,7 @@ def test_list_final(self):
class TestContentStatusAPI(BaseAPITestCase):

def test_status_endpoint(self):
content = TestContentObj.objects.create(
title="Unpublished article"
)
content = make_content(published=None)
client = Client()
client.login(username="admin", password="secret")
response = client.get(reverse("content-status", kwargs={"pk": content.id}), content_type="application/json")
Expand Down Expand Up @@ -138,10 +124,11 @@ class TestPublishContentAPI(BaseAPITestCase):

def test_publish_now(self):

content = TestContentObj.objects.create(
content = make_content(
title="Django Unchained: How a framework tried to run using async IO",
description="Spoiler alert: it didn't go great, unless you measure by the number of HN articles about it",
foo="SUCK IT, NERDS."
foo="SUCK IT, NERDS.",
published=None
)

client = Client()
Expand All @@ -159,18 +146,14 @@ def test_publish_now(self):
self.assertEqual(response_data["status"], "final")

# assert that we can load it up
article = TestContentObj.objects.get(id=content.id)
article = Content.objects.get(id=content.id)
self.assertIsNotNone(article.published)
# check for a log
LogEntry.objects.filter(object_id=article.pk).get(change_message="final")

def test_author_publish_permissions(self):

content = TestContentObj.objects.create(
title="Django Unchained: How a framework tried to run using async IO",
description="Spoiler alert: it didn't go great, unless you measure by the number of HN articles about it",
foo="SUCK IT, NERDS."
)
content = make_content(published=None)
content.authors.add(self.admin)

client = Client()
Expand All @@ -188,18 +171,14 @@ def test_author_publish_permissions(self):
self.assertEqual(response_data["status"], "final")

# assert that we can load it up
article = TestContentObj.objects.get(id=content.id)
article = Content.objects.get(id=content.id)
self.assertIsNotNone(article.published)
# check for a log
LogEntry.objects.filter(object_id=article.pk).get(change_message="final")

def test_publish_specific(self):

content = TestContentObj.objects.create(
title="Django Unchained: How a framework tried to run using async IO",
description="Spoiler alert: it didn't go great, unless you measure by the number of HN articles about it",
foo="SUCK IT, NERDS."
)
content = make_content(published=None)

client = Client()
client.login(username="admin", password="secret")
Expand All @@ -221,20 +200,15 @@ def test_publish_specific(self):
self.assertEqual(response_data["status"], "final")

# assert that we can load it up
article = TestContentObj.objects.get(id=content.id)
article = Content.objects.get(id=content.id)
self.assertEqual(article.published.year, 2013)
self.assertEqual(article.published.month, 6)
self.assertEqual(article.published.day, 9)
# check for a log
LogEntry.objects.filter(object_id=article.pk).get(change_message="final")

def test_unpublish(self):
content = TestContentObj.objects.create(
title="Django Unchained: How a framework tried to run using async IO",
description="Spoiler alert: it didn't go great, unless you measure by the number of HN articles about it",
foo="SUCK IT, NERDS.",
published=timezone.now()
)
content = make_content(published=timezone.now())

client = Client()
client.login(username="admin", password="secret")
Expand All @@ -257,7 +231,7 @@ def test_unpublish(self):
self.assertEqual(response_data["status"], "draft")

# assert that we can load it up
article = TestContentObj.objects.get(id=content.id)
article = Content.objects.get(id=content.id)
self.assertEqual(article.published, None)
# check for a log
LogEntry.objects.filter(object_id=article.pk).get(change_message="draft")
Expand Down Expand Up @@ -317,11 +291,7 @@ def _test_update_content(self):
class TestUpdateContentAPI(BaseUpdateContentAPI):
"""Tests updating an `Article`"""
def create_content(self):
self.content = TestContentObj.objects.create(
title="Booyah: The Cramer Story",
description="Learn how one man booyahed his way to the top.",
foo="booyah"
)
self.content = make_content(TestContentObj, title="Booyah: The Cramer Story", foo="booyah")

def updated_data(self):
return dict(
Expand All @@ -341,11 +311,7 @@ def create_content(self):
first_name="Chris",
last_name="Sinchok"
)
self.content = TestContentObj.objects.create(
title="Booyah: The Cramer Story",
description="Learn how one man booyahed his way to the top.",
foo="booyah"
)
self.content = make_content(TestContentObj, foo="booyah")

def updated_data(self):
return dict(
Expand Down Expand Up @@ -408,9 +374,9 @@ def test_image_serializer(self):
client = Client()
client.login(username="admin", password="secret")

content = TestContentDetailImage.objects.create(
title="Some Test Article",
description="NO IMAGES HERE"
content = make_content(
TestContentDetailImage,
detail_image=None
)
content_detail_url = reverse("content-detail", kwargs={"pk": content.id})

Expand Down Expand Up @@ -473,14 +439,10 @@ def test_me_as_superuser(self):

class TestTrashContentAPI(BaseAPITestCase):
def test_trash(self):
content = TestContentObj.objects.create(
title="Test Article",
description="Testing out trash.",
foo="Lorem ipsum dolor, oh myyyy!"
)
content = make_content()
self.assertTrue(content.indexed)
data = self.es.get(index=content.get_index_name(), doc_type=content.get_mapping_type_name(), id=content.id)
self.assertEqual(data["_source"]["title"], "Test Article")
self.assertEqual(data["_source"]["title"], content.title)

client = Client()
client.login(username="admin", password="secret")
Expand Down Expand Up @@ -508,11 +470,7 @@ def test_trash(self):
def test_trash_404(self):
client = Client()
client.login(username="admin", password="secret")
content = TestContentObj.objects.create(
title="Test Article",
description="Testing out trash.",
foo="Lorem ipsum dolor, oh myyyy!"
)
content = make_content()
content_rest_url = reverse("content-trash", kwargs={"pk": content.id})
response = client.post(content_rest_url, content_type="application/json")
# no permissions, no trashing
Expand Down Expand Up @@ -562,8 +520,8 @@ def test_list_tokens(self):
"""Test token listing."""

# create some test content
content = Content.objects.create()
content_2 = Content.objects.create()
content = make_content()
content_2 = make_content()
create_date = datetime.now()
expire_date = create_date + timedelta(days=3)
info_1 = ObfuscatedUrlInfo.objects.create(
Expand Down
36 changes: 15 additions & 21 deletions tests/content/test_content_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
from elastimorphic.tests.base import BaseIndexableTestCase
import elasticsearch

from tests.testcontent.models import TestContentObj
from bulbs.content.models import Content

from tests.utils import make_content


class SerializerTestCase(BaseIndexableTestCase):

def test_content_status(self):

content = TestContentObj.objects.create(
title="Unpublished article"
)
content = make_content(published=None)
self.assertEqual(content.get_status(), "draft")

content.published = timezone.now() - datetime.timedelta(hours=1)
Expand All @@ -24,9 +24,7 @@ def test_content_status(self):
self.assertEqual(content.get_status(), "final")

def test_is_published(self):
content = TestContentObj.objects.create(
title="Unpublished article"
)
content = make_content(published=None)
self.assertFalse(content.is_published)

content.published = timezone.now() - datetime.timedelta(hours=1)
Expand All @@ -36,39 +34,35 @@ def test_is_published(self):
self.assertFalse(content.is_published)

def test_content_deletion(self):
content = TestContentObj.objects.create(
title="Some article"
)
content = make_content(published=None)

TestContentObj.search_objects.refresh()
Content.search_objects.refresh()

q = TestContentObj.search_objects.query(_id=content.id)
q = Content.search_objects.query(_id=content.id)
self.assertEqual(q.count(), 1)

c = TestContentObj.search_objects.es.get(
c = Content.search_objects.es.get(
index=content.get_index_name(),
doc_type=TestContentObj.get_mapping_type_name(),
doc_type=content.get_mapping_type_name(),
id=content.id)
self.assertTrue(c.get("found"), True)

content.delete()

with self.assertRaises(elasticsearch.exceptions.NotFoundError):
TestContentObj.search_objects.es.get(
Content.search_objects.es.get(
index=content.get_index_name(),
doc_type=TestContentObj.get_mapping_type_name(),
doc_type=content.get_mapping_type_name(),
id=content.id)

TestContentObj.search_objects.refresh()
Content.search_objects.refresh()

q = TestContentObj.search_objects.query(_id=content.id)
q = Content.search_objects.query(_id=content.id)
self.assertEqual(q.count(), 0)

def test_first_image_none(self):

content = TestContentObj.objects.create(
title="Some article"
)
content = make_content(published=None)

content.thumbnail_override = 666

Expand Down
20 changes: 5 additions & 15 deletions tests/content/test_content_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from elastimorphic.tests.base import BaseIndexableTestCase

from tests.testcontent.models import TestContentObj, TestContentObjTwo
from tests.utils import make_content


class PolyContentTestCase(BaseIndexableTestCase):
Expand All @@ -36,23 +37,12 @@ def setUp(self):
tag, created = Tag.objects.get_or_create(name=atom, slug=slugify(atom))
tags.append(tag)
self.all_tags.append(tag)
obj = TestContentObj.objects.create(
title=' '.join(combo),
description=' '.join(reversed(combo)),
foo=combo[0],
published=one_hour_ago,
feature_type=ft_one
)

obj = make_content(TestContentObj, published=one_hour_ago, feature_type=ft_one)
obj.tags.add(*tags)
obj.index()
obj2 = TestContentObjTwo.objects.create(
title=' '.join(reversed(combo)),
description=' '.join(combo),
foo=combo[1],
bar=i,
published=two_days_ago,
feature_type=ft_two
)

obj2 = make_content(TestContentObjTwo, published=two_days_ago, feature_type=ft_two)
obj2.tags.add(*tags)
obj2.index()

Expand Down
12 changes: 5 additions & 7 deletions tests/content/test_content_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from bulbs.content.models import ObfuscatedUrlInfo

from tests.testcontent.models import TestContentObj
from tests.utils import make_content


class TestContentViews(BaseIndexableTestCase):
Expand Down Expand Up @@ -37,10 +38,7 @@ def test_unpublished_article(self):
self.assertEqual(response.status_code, 200)

def published_article(self):
content = TestContentObj.objects.create(
title="Testing Content",
published=timezone.now() - timedelta(hours=2)
)
content = make_content(published=timezone.now() - timedelta(hours=2))
response = self.client.get(reverse("published", kwargs={"pk": content.id}))
self.assertEqual(response.status_code, 200)

Expand All @@ -50,7 +48,7 @@ def test_base_content_detail_view_tokenized_url(self):
# create test content and token
create_date = timezone.now()
expire_date = create_date + timedelta(days=3)
content = TestContentObj.objects.create()
content = make_content(published=None)
obfuscated_url_info = ObfuscatedUrlInfo.objects.create(
content=content,
create_date=create_date.isoformat(),
Expand All @@ -71,7 +69,7 @@ def test_base_content_detail_view_tokenized_url_outside_date_window(self):
# create test content and token
create_date = timezone.now() + timedelta(days=3)
expire_date = create_date + timedelta(days=3)
content = TestContentObj.objects.create()
content = make_content(published=None)
obfuscated_url_info = ObfuscatedUrlInfo.objects.create(
content=content,
create_date=create_date.isoformat(),
Expand All @@ -92,7 +90,7 @@ def test_base_content_detail_view_tokenized_url_invalid(self):
create_date = timezone.now()
expire_date = create_date + timedelta(days=3)
ObfuscatedUrlInfo.objects.create(
content=TestContentObj.objects.create(),
content=make_content(),
create_date=create_date.isoformat(),
expire_date=expire_date.isoformat()
)
Expand Down
Loading

0 comments on commit f303365

Please sign in to comment.