diff --git a/setup.py b/setup.py
index fa704e0221..5cf3344a0b 100755
--- a/setup.py
+++ b/setup.py
@@ -44,6 +44,7 @@ testing_extras = [
'elasticsearch>=1.0.0,<3.0',
'Jinja2>=2.8,<3.0',
'boto3>=1.4,<1.5',
+ 'freezegun>=0.3.8',
# For coverage and PEP8 linting
'coverage>=3.7.0',
diff --git a/wagtail/wagtailadmin/tests/test_pages_views.py b/wagtail/wagtailadmin/tests/test_pages_views.py
index ad0dc61d30..dd414a8ca5 100644
--- a/wagtail/wagtailadmin/tests/test_pages_views.py
+++ b/wagtail/wagtailadmin/tests/test_pages_views.py
@@ -6,6 +6,7 @@ import os
import django
import mock
+from freezegun import freeze_time
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission
@@ -28,6 +29,7 @@ from wagtail.wagtailcore.models import GroupPagePermission, Page, PageRevision,
from wagtail.wagtailcore.signals import page_published, page_unpublished
from wagtail.wagtailsearch.index import SearchField
from wagtail.wagtailusers.models import UserProfile
+from wagtail.wagtailadmin.views.pages import PreviewOnEdit
def submittable_timestamp(timestamp):
@@ -4272,3 +4274,30 @@ class TestPreview(TestCase, WagtailTestUtils):
self.assertContains(response, "Beach party")
self.assertContains(response, "
Parties")
self.assertContains(response, "Holidays")
+
+ def test_preview_on_edit_expiry(self):
+ initial_datetime = timezone.now()
+ expiry_datetime = initial_datetime + datetime.timedelta(
+ seconds=PreviewOnEdit.preview_expiration_timeout + 1)
+
+ with freeze_time(initial_datetime) as frozen_datetime:
+ preview_url = reverse('wagtailadmin_pages:preview_on_edit',
+ args=(self.event_page.id,))
+ response = self.client.post(preview_url, self.post_data)
+
+ # Check the JSON response
+ self.assertEqual(response.status_code, 200)
+
+ response = self.client.get(preview_url)
+
+ # Check the HTML response
+ self.assertEqual(response.status_code, 200)
+
+ frozen_datetime.move_to(expiry_datetime)
+
+ preview_url = reverse('wagtailadmin_pages:preview_on_edit',
+ args=(self.home_page.id,))
+ response = self.client.post(preview_url, self.post_data)
+ self.assertEqual(response.status_code, 200)
+ response = self.client.get(preview_url)
+ self.assertEqual(response.status_code, 200)
diff --git a/wagtail/wagtailadmin/views/pages.py b/wagtail/wagtailadmin/views/pages.py
index 9f93fa0495..448331a2d9 100644
--- a/wagtail/wagtailadmin/views/pages.py
+++ b/wagtail/wagtailadmin/views/pages.py
@@ -562,13 +562,12 @@ class PreviewOnEdit(View):
def remove_old_preview_data(self):
expiration = time() - self.preview_expiration_timeout
- for k, v in self.request.session.items():
- if not k.startswith(self.session_key_prefix):
- continue
- post_data, timestamp = v
- if timestamp < expiration:
- # Removes the session key gracefully
- self.request.session.pop(k)
+ expired_keys = [
+ k for k, v in self.request.session.items()
+ if k.startswith(self.session_key_prefix) and v[1] < expiration]
+ # Removes the session key gracefully
+ for k in expired_keys:
+ self.request.session.pop(k)
@property
def session_key(self):