Skip to content

Commit

Permalink
Add testcase for plugin redirects / HttpRedirectRequestMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
vdboor committed Dec 15, 2016
1 parent 42b08d7 commit e6b4a60
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 3 deletions.
19 changes: 17 additions & 2 deletions fluent_contents/tests/test_rendering.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from django.middleware.csrf import get_token
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.template import Template
from django.test import RequestFactory

from fluent_contents import rendering
from fluent_contents.extensions import PluginContext
from fluent_contents.models import Placeholder, DEFAULT_TIMEOUT
from fluent_contents.rendering import utils as rendering_utils
from fluent_contents.tests import factories
from fluent_contents.tests.testapp.models import TestPage, RawHtmlTestItem, TimeoutTestItem, OverrideBase, MediaTestItem
from fluent_contents.tests.testapp.models import TestPage, RawHtmlTestItem, TimeoutTestItem, OverrideBase, MediaTestItem, \
RedirectTestItem
from fluent_contents.tests.utils import AppTestCase


Expand Down Expand Up @@ -52,6 +56,17 @@ def test_render_media(self):
self.assertEqual(output.media._js, ['testapp/media_item.js'])
self.assertEqual(output.media._css, {'screen': ['testapp/media_item.css']})

def test_render_redirect(self):
cache.clear()
page = factories.create_page()
placeholder = factories.create_placeholder(page=page)
factories.create_content_item(RedirectTestItem, placeholder=placeholder, html='MEDIA_TEST')

response = self.client.get(reverse('testpage', args=(page.pk,)))
self.assertTrue(response.status_code, 301)
self.assertIsInstance(response, HttpResponseRedirect)
self.assertEqual(response.url, 'http://testserver/contact/success/')

def test_debug_is_method_overwritten(self):
"""
Test the "is method overwritten" logic to detect template changes
Expand Down
16 changes: 15 additions & 1 deletion fluent_contents/tests/testapp/content_plugins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.utils.safestring import mark_safe
from fluent_contents.extensions import ContentPlugin, plugin_pool
from fluent_contents.tests.testapp.models import RawHtmlTestItem, TimeoutTestItem, MediaTestItem
from fluent_contents.tests.testapp.models import RawHtmlTestItem, TimeoutTestItem, MediaTestItem, RedirectTestItem


@plugin_pool.register
Expand Down Expand Up @@ -45,3 +45,17 @@ class FrontendMedia:
js = (
'testapp/media_item.js',
)


@plugin_pool.register
class RedirectTestPlugin(ContentPlugin):
"""
Testing a plugin timeout.
"""
model = RedirectTestItem

def render(self, request, instance, **kwargs):
# Plugins can issue redirects, for example a contact form plugin.
# Since this call happens inside a template render, the code flow
# is interrupted by an exception that is handled in middleware.
self.redirect('/contact/success/', status=301)
13 changes: 13 additions & 0 deletions fluent_contents/tests/testapp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ class Migration(migrations.Migration):
},
bases=('fluent_contents.contentitem',),
),
migrations.CreateModel(
name='RedirectTestItem',
fields=[
('contentitem_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='fluent_contents.ContentItem')),
('html', models.TextField(verbose_name=b'HTML code')),
],
options={
'db_table': 'contentitem_testapp_redirecttestitem',
'verbose_name': 'Redirect test',
'verbose_name_plural': 'Redirect test',
},
bases=('fluent_contents.contentitem',),
),
migrations.CreateModel(
name='TestPage',
fields=[
Expand Down
16 changes: 16 additions & 0 deletions fluent_contents/tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,19 @@ class Meta:

def __str__(self):
return self.html


@python_2_unicode_compatible
class RedirectTestItem(ContentItem):
"""
The most basic "media HTML" content item, for testing.
"""
html = models.TextField("HTML code")

class Meta:
app_label = 'testapp'
verbose_name = 'Redirect test'
verbose_name_plural = 'Redirect test'

def __str__(self):
return self.html
2 changes: 2 additions & 0 deletions fluent_contents/tests/testapp/templates/testapp/testpage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% load fluent_contents_tags %}
{% page_placeholder "field_slot1" %}
3 changes: 3 additions & 0 deletions fluent_contents/tests/testapp/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from django.conf.urls import include, url
from django.contrib import admin

from . import views

admin.autodiscover()

urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^testpage/(?P<pk>\d+)/$', views.TestPageView.as_view(), name='testpage')

#url(r'^comments/', include('django.contrib.comments.urls')),
#url(r'^forms/', include('form_designer.urls')),
Expand Down
9 changes: 9 additions & 0 deletions fluent_contents/tests/testapp/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.views.generic import DetailView

from fluent_contents.tests.testapp.models import PlaceholderFieldTestPage


class TestPageView(DetailView):
model = PlaceholderFieldTestPage
template_name = "testapp/testpage.html"
context_object_name = 'page' # the default variable {% page_placeholder %} looks for.
2 changes: 2 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'fluent_contents',
Expand Down Expand Up @@ -86,6 +87,7 @@
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'fluent_contents.middleware.HttpRedirectRequestMiddleware',
),
ROOT_URLCONF = 'fluent_contents.tests.testapp.urls',
TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner' if django.VERSION < (1,6) else 'django.test.runner.DiscoverRunner',
Expand Down

0 comments on commit e6b4a60

Please sign in to comment.