From 686a9beaa9099ac8f4cca20004f35321e6bdd09e Mon Sep 17 00:00:00 2001 From: Serafeim Papastefanos Date: Tue, 22 Apr 2014 20:12:20 +0300 Subject: [PATCH] Add tests for scheduled publishing This does not actually test the management command for scheduled pages. --- wagtail/wagtailadmin/tests.py | 140 ++++++++++++++++++++++++++++++++-- 1 file changed, 134 insertions(+), 6 deletions(-) diff --git a/wagtail/wagtailadmin/tests.py b/wagtail/wagtailadmin/tests.py index 84b00b335..3df92ec22 100644 --- a/wagtail/wagtailadmin/tests.py +++ b/wagtail/wagtailadmin/tests.py @@ -1,8 +1,10 @@ +from datetime import datetime, timedelta +from django.utils import timezone from django.test import TestCase import unittest from wagtail.tests.models import SimplePage, EventPage from wagtail.tests.utils import login -from wagtail.wagtailcore.models import Page +from wagtail.wagtailcore.models import Page, PageRevision from django.core.urlresolvers import reverse @@ -49,7 +51,7 @@ class TestPageSelectTypeLocation(TestCase): response = self.client.get(reverse('wagtailadmin_pages_select_type')) self.assertEqual(response.status_code, 200) - @unittest.expectedFailure # For some reason, this returns a 302... + @unittest.expectedFailure # For some reason, this returns a 302... def test_select_location_testpage(self): response = self.client.get(reverse('wagtailadmin_pages_select_location', args=('tests', 'eventpage'))) self.assertEqual(response.status_code, 200) @@ -100,6 +102,57 @@ class TestPageCreation(TestCase): self.assertIsInstance(page, SimplePage) self.assertFalse(page.live) + def test_create_simplepage_scheduled(self): + go_live_datetime = timezone.now() + timedelta(days=1) + expiry_datetime = timezone.now() + timedelta(days=2) + post_data = { + 'title': "New page!", + 'content': "Some content", + 'slug': 'hello-world', + 'go_live_datetime': str(go_live_datetime), + 'expiry_datetime': str(expiry_datetime), + } + response = self.client.post(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', self.root_page.id)), post_data) + + # Should be redirected to explorer page + self.assertEqual(response.status_code, 302) + + # Find the page and check the scheduled times + page = Page.objects.get(path__startswith=self.root_page.path, slug='hello-world').specific + self.assertEquals(page.go_live_datetime.date(), go_live_datetime.date()) + self.assertEquals(page.expiry_datetime.date(), expiry_datetime.date()) + self.assertEquals(page.expired, False) + self.assertTrue(page.status_string, "draft") + + # No revisions with approved_go_live_datetime + self.assertFalse(PageRevision.objects.filter(page=page).exclude(approved_go_live_datetime__isnull=True).exists()) + + def test_create_simplepage_scheduled_errored(self): + post_data = { + 'title': "New page!", + 'content': "Some content", + 'slug': 'hello-world', + 'go_live_datetime': str(timezone.now() + timedelta(days=2)), + 'expiry_datetime': str(timezone.now() + timedelta(days=1)), + } + response = self.client.post(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', self.root_page.id)), post_data) + + # Should be redirected to explorer page + self.assertEqual(response.status_code, 200) + self.assertTrue(response.context['edit_handler'].form.errors) + + post_data = { + 'title': "New page!", + 'content': "Some content", + 'slug': 'hello-world', + 'expiry_datetime': str(timezone.now() + timedelta(days=-1)), + } + response = self.client.post(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', self.root_page.id)), post_data) + + # Should be redirected to explorer page + self.assertEqual(response.status_code, 200) + self.assertTrue(response.context['edit_handler'].form.errors) + def test_create_simplepage_post_publish(self): post_data = { 'title': "New page!", @@ -118,6 +171,34 @@ class TestPageCreation(TestCase): self.assertIsInstance(page, SimplePage) self.assertTrue(page.live) + def test_create_simplepage_post_publish_scheduled(self): + go_live_datetime = timezone.now() + timedelta(days=1) + expiry_datetime = timezone.now() + timedelta(days=2) + post_data = { + 'title': "New page!", + 'content': "Some content", + 'slug': 'hello-world', + 'action-publish': "Publish", + 'go_live_datetime': str(go_live_datetime), + 'expiry_datetime': str(expiry_datetime), + } + response = self.client.post(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', self.root_page.id)), post_data) + + # Should be redirected to explorer page + self.assertEqual(response.status_code, 302) + + # Find the page and check it + page = Page.objects.get(path__startswith=self.root_page.path, slug='hello-world').specific + self.assertEquals(page.go_live_datetime.date(), go_live_datetime.date()) + self.assertEquals(page.expiry_datetime.date(), expiry_datetime.date()) + self.assertEquals(page.expired, False) + + # A revision with approved_go_live_datetime should exist now + self.assertTrue(PageRevision.objects.filter(page=page).exclude(approved_go_live_datetime__isnull=True).exists()) + # But Page won't be live + self.assertFalse(page.live) + self.assertTrue(page.status_string, "scheduled") + def test_create_simplepage_post_existingslug(self): # This tests the existing slug checking on page save @@ -143,7 +224,7 @@ class TestPageCreation(TestCase): response = self.client.get(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', 100000))) self.assertEqual(response.status_code, 404) - @unittest.expectedFailure # FIXME: Crashes! + @unittest.expectedFailure # FIXME: Crashes! def test_create_nonpagetype(self): response = self.client.get(reverse('wagtailadmin_pages_create', args=('wagtailimages', 'image', self.root_page.id))) self.assertEqual(response.status_code, 404) @@ -184,7 +265,7 @@ class TestPageEdit(TestCase): 'slug': 'hello-world', } response = self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), post_data) - + # Should be redirected to explorer page self.assertEqual(response.status_code, 302) @@ -192,6 +273,31 @@ class TestPageEdit(TestCase): child_page_new = SimplePage.objects.get(id=self.child_page.id) self.assertTrue(child_page_new.has_unpublished_changes) + def test_edit_post_scheduled(self): + go_live_datetime = timezone.now() + timedelta(days=1) + expiry_datetime = timezone.now() + timedelta(days=2) + post_data = { + 'title': "I've been edited!", + 'content': "Some content", + 'slug': 'hello-world', + 'go_live_datetime': str(go_live_datetime), + 'expiry_datetime': str(expiry_datetime), + } + response = self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), post_data) + + # Should be redirected to explorer page + self.assertEqual(response.status_code, 302) + + child_page_new = SimplePage.objects.get(id=self.child_page.id) + + # The page will still be live + self.assertTrue(child_page_new.live) + # A revision with approved_go_live_datetime should not exist + self.assertFalse(PageRevision.objects.filter(page=child_page_new).exclude(approved_go_live_datetime__isnull=True).exists()) + # But a revision with go_live_datetime and expiry_datetime in their content json *should* exist + self.assertTrue(PageRevision.objects.filter(page=child_page_new, content_json__contains=str(go_live_datetime.date())).exists()) + self.assertTrue(PageRevision.objects.filter(page=child_page_new, content_json__contains=str(expiry_datetime.date())).exists()) + def test_edit_post_publish(self): # Tests publish from edit page post_data = { @@ -201,7 +307,7 @@ class TestPageEdit(TestCase): 'action-publish': "Publish", } response = self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), post_data) - + # Should be redirected to explorer page self.assertEqual(response.status_code, 302) @@ -212,6 +318,28 @@ class TestPageEdit(TestCase): # The page shouldn't have "has_unpublished_changes" flag set self.assertFalse(child_page_new.has_unpublished_changes) + def test_edit_post_publish_scheduled(self): + go_live_datetime = timezone.now() + timedelta(days=1) + expiry_datetime = timezone.now() + timedelta(days=2) + post_data = { + 'title': "I've been edited!", + 'content': "Some content", + 'slug': 'hello-world', + 'action-publish': "Publish", + 'go_live_datetime': str(go_live_datetime), + 'expiry_datetime': str(expiry_datetime), + } + response = self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), post_data) + + # Should be redirected to explorer page + self.assertEqual(response.status_code, 302) + + child_page_new = SimplePage.objects.get(id=self.child_page.id) + # The page should not be live anymore + self.assertFalse(child_page_new.live) + # Instead a revision with approved_go_live_datetime should not exist + self.assertTrue(PageRevision.objects.filter(page=child_page_new).exclude(approved_go_live_datetime__isnull=True).exists()) + class TestPageDelete(TestCase): def setUp(self): @@ -232,7 +360,7 @@ class TestPageDelete(TestCase): self.assertEqual(response.status_code, 200) def test_delete_post(self): - post_data = {'hello': 'world'} # For some reason, this test doesn't work without a bit of POST data + post_data = {'hello': 'world'} # For some reason, this test doesn't work without a bit of POST data response = self.client.post(reverse('wagtailadmin_pages_delete', args=(self.child_page.id, )), post_data) # Should be redirected to explorer page