diff --git a/wagtail/tests/models.py b/wagtail/tests/models.py index 2483b94b2d..eb18e86560 100644 --- a/wagtail/tests/models.py +++ b/wagtail/tests/models.py @@ -95,6 +95,11 @@ class RelatedLink(LinkFields): abstract = True +# Simple page +class SimplePage(Page): + content = models.TextField() + + # Event page class EventPageCarouselItem(Orderable, CarouselItem): diff --git a/wagtail/wagtailadmin/tests.py b/wagtail/wagtailadmin/tests.py index 9d7f5afb37..c3c529417a 100644 --- a/wagtail/wagtailadmin/tests.py +++ b/wagtail/wagtailadmin/tests.py @@ -1,6 +1,6 @@ from django.test import TestCase import unittest -from wagtail.tests.models import EventPage +from wagtail.tests.models import SimplePage, EventPage from wagtail.tests.utils import login from wagtail.wagtailcore.models import Page from django.core.urlresolvers import reverse @@ -22,7 +22,7 @@ class TestPageExplorer(TestCase): self.root_page = Page.objects.get(id=2) # Add child page - self.child_page = EventPage() + self.child_page = SimplePage() self.child_page.title = "Hello world!" self.child_page.slug = "hello-world" self.root_page.add_child(self.child_page) @@ -79,12 +79,68 @@ class TestPageCreation(TestCase): response = self.client.get(reverse('wagtailadmin_pages_add_subpage', args=(100000, ))) self.assertEqual(response.status_code, 404) - def test_create_testpage(self): - response = self.client.get(reverse('wagtailadmin_pages_create', args=('tests', 'eventpage', self.root_page.id))) + def test_create_simplepage(self): + response = self.client.get(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', self.root_page.id))) + self.assertEqual(response.status_code, 200) + + def test_create_simplepage_post(self): + post_data = { + 'title': "New page!", + 'content': "Some content", + 'slug': 'hello-world', + } + 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.assertEqual(page.title, post_data['title']) + self.assertIsInstance(page, SimplePage) + self.assertFalse(page.live) + + def test_create_simplepage_post_publish(self): + post_data = { + 'title': "New page!", + 'content': "Some content", + 'slug': 'hello-world', + 'action-publish': "Publish", + } + 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.assertEqual(page.title, post_data['title']) + self.assertIsInstance(page, SimplePage) + self.assertTrue(page.live) + + def test_create_simplepage_post_existingslug(self): + # This tests the existing slug checking on page save + + # Create a page + self.child_page = SimplePage() + self.child_page.title = "Hello world!" + self.child_page.slug = "hello-world" + self.root_page.add_child(self.child_page) + + # Attempt to create a new one with the same slug + post_data = { + 'title': "New page!", + 'content': "Some content", + 'slug': 'hello-world', + 'action-publish': "Publish", + } + response = self.client.post(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', self.root_page.id)), post_data) + + # Should not be redirected (as the save should fail) self.assertEqual(response.status_code, 200) def test_create_nonexistantparent(self): - response = self.client.get(reverse('wagtailadmin_pages_create', args=('tests', 'testpage', 100000))) + response = self.client.get(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', 100000))) self.assertEqual(response.status_code, 404) @unittest.expectedFailure # FIXME: Crashes! @@ -93,13 +149,77 @@ class TestPageCreation(TestCase): self.assertEqual(response.status_code, 404) -class TestPageEditDelete(TestCase): +class TestPageEdit(TestCase): def setUp(self): # Find root page self.root_page = Page.objects.get(id=2) # Add child page - self.child_page = EventPage() + self.child_page = SimplePage() + self.child_page.title = "Hello world!" + self.child_page.slug = "hello-world" + self.child_page.live = True + self.root_page.add_child(self.child_page) + self.child_page.save_revision() + + # Add event page (to test edit handlers) + self.event_page = EventPage() + self.event_page.title = "Event page" + self.event_page.slug = "event-page" + self.root_page.add_child(self.event_page) + + # Login + login(self.client) + + def test_edit_page(self): + # Tests that the edit page loads + response = self.client.get(reverse('wagtailadmin_pages_edit', args=(self.event_page.id, ))) + self.assertEqual(response.status_code, 200) + + def test_edit_post(self): + # Tests simple editing + post_data = { + 'title': "I've been edited!", + 'content': "Some content", + '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) + + # The page should have "has_unpublished_changes" flag set + child_page_new = SimplePage.objects.get(id=self.child_page.id) + self.assertTrue(child_page_new.has_unpublished_changes) + + def test_edit_post_publish(self): + # Tests publish from edit page + post_data = { + 'title': "I've been edited!", + 'content': "Some content", + 'slug': 'hello-world', + '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) + + # Check that the page was edited + child_page_new = SimplePage.objects.get(id=self.child_page.id) + self.assertEqual(child_page_new.title, post_data['title']) + + # The page shouldn't have "has_unpublished_changes" flag set + self.assertFalse(child_page_new.has_unpublished_changes) + + +class TestPageDelete(TestCase): + def setUp(self): + # Find root page + self.root_page = Page.objects.get(id=2) + + # Add child page + self.child_page = SimplePage() self.child_page.title = "Hello world!" self.child_page.slug = "hello-world" self.root_page.add_child(self.child_page) @@ -107,14 +227,20 @@ class TestPageEditDelete(TestCase): # Login login(self.client) - def test_edit(self): - response = self.client.get(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, ))) - self.assertEqual(response.status_code, 200) - def test_delete(self): response = self.client.get(reverse('wagtailadmin_pages_delete', args=(self.child_page.id, ))) 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 + response = self.client.post(reverse('wagtailadmin_pages_delete', args=(self.child_page.id, )), post_data) + + # Should be redirected to explorer page + self.assertEqual(response.status_code, 302) + + # Check that the page is gone + self.assertEqual(Page.objects.filter(path__startswith=self.root_page.path, slug='hello-world').count(), 0) + class TestPageSearch(TestCase): def setUp(self): @@ -152,18 +278,18 @@ class TestPageMove(TestCase): self.root_page = Page.objects.get(id=2) # Create two sections - self.section_a = EventPage() + self.section_a = SimplePage() self.section_a.title = "Section A" self.section_a.slug = "section-a" self.root_page.add_child(self.section_a) - self.section_b = EventPage() + self.section_b = SimplePage() self.section_b.title = "Section B" self.section_b.slug = "section-b" self.root_page.add_child(self.section_b) # Add test page into section A - self.test_page = EventPage() + self.test_page = SimplePage() self.test_page.title = "Hello world!" self.test_page.slug = "hello-world" self.section_a.add_child(self.test_page) diff --git a/wagtail/wagtailimages/tests.py b/wagtail/wagtailimages/tests.py index 2bd360320a..c899617734 100644 --- a/wagtail/wagtailimages/tests.py +++ b/wagtail/wagtailimages/tests.py @@ -2,6 +2,8 @@ from django.test import TestCase from django import template from django.contrib.auth.models import User, Group, Permission from django.core.urlresolvers import reverse +from django.core.files.uploadedfile import SimpleUploadedFile + from wagtail.tests.utils import login from wagtail.wagtailimages.models import get_image_model from wagtail.wagtailimages.templatetags import image_tags @@ -166,9 +168,30 @@ class TestImageAddView(TestCase): def get(self, params={}): return self.client.get(reverse('wagtailimages_add_image'), params) + def post(self, post_data={}): + return self.client.post(reverse('wagtailimages_add_image'), post_data) + def test_status_code(self): self.assertEqual(self.get().status_code, 200) + def test_add(self): + response = self.post({ + 'title': "Test image", + 'file': SimpleUploadedFile('test.png', get_test_image_file().file.getvalue()), + }) + + # Should redirect back to index + self.assertEqual(response.status_code, 302) + + # Check that the image was created + images = Image.objects.filter(title="Test image") + self.assertEqual(images.count(), 1) + + # Test that size was populated correctly + image = images.first() + self.assertEqual(image.width, 640) + self.assertEqual(image.height, 480) + class TestImageEditView(TestCase): def setUp(self): @@ -183,9 +206,24 @@ class TestImageEditView(TestCase): def get(self, params={}): return self.client.get(reverse('wagtailimages_edit_image', args=(self.image.id,)), params) + def post(self, post_data={}): + return self.client.post(reverse('wagtailimages_edit_image', args=(self.image.id,)), post_data) + def test_status_code(self): self.assertEqual(self.get().status_code, 200) + def test_edit(self): + response = self.post({ + 'title': "Edited", + }) + + # Should redirect back to index + self.assertEqual(response.status_code, 302) + + # Check that the image was edited + image = Image.objects.get(id=self.image.id) + self.assertEqual(image.title, "Edited") + class TestImageDeleteView(TestCase): def setUp(self): @@ -200,9 +238,24 @@ class TestImageDeleteView(TestCase): def get(self, params={}): return self.client.get(reverse('wagtailimages_delete_image', args=(self.image.id,)), params) + def post(self, post_data={}): + return self.client.post(reverse('wagtailimages_delete_image', args=(self.image.id,)), post_data) + def test_status_code(self): self.assertEqual(self.get().status_code, 200) + def test_delete(self): + response = self.post({ + 'hello': 'world' + }) + + # Should redirect back to index + self.assertEqual(response.status_code, 302) + + # Check that the image was deleted + images = Image.objects.filter(title="Test image") + self.assertEqual(images.count(), 0) + class TestImageChooserView(TestCase): def setUp(self): diff --git a/wagtail/wagtailredirects/tests.py b/wagtail/wagtailredirects/tests.py index 495c14d851..a14a853fa7 100644 --- a/wagtail/wagtailredirects/tests.py +++ b/wagtail/wagtailredirects/tests.py @@ -1,6 +1,8 @@ from django.test import TestCase from django.test.client import Client from wagtail.wagtailredirects import models +from wagtail.tests.utils import login +from django.core.urlresolvers import reverse class TestRedirects(TestCase): @@ -62,3 +64,144 @@ class TestRedirects(TestCase): # Check that we were redirected temporarily self.assertEqual(r.status_code, 302) self.assertTrue(r.has_header('Location')) + + +class TestRedirectsIndexView(TestCase): + def setUp(self): + login(self.client) + + def get(self, params={}): + return self.client.get(reverse('wagtailredirects_index'), params) + + def test_status_code(self): + self.assertEqual(self.get().status_code, 200) + + def test_search(self): + response = self.get({'q': "Hello"}) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context['query_string'], "Hello") + + def test_pagination(self): + pages = ['0', '1', '-1', '9999', 'Not a page'] + for page in pages: + response = self.get({'p': page}) + self.assertEqual(response.status_code, 200) + + +class TestRedirectsAddView(TestCase): + def setUp(self): + login(self.client) + + def get(self, params={}): + return self.client.get(reverse('wagtailredirects_add_redirect'), params) + + def post(self, post_data={}): + return self.client.post(reverse('wagtailredirects_add_redirect'), post_data) + + def test_status_code(self): + self.assertEqual(self.get().status_code, 200) + + def test_add(self): + response = self.post({ + 'old_path': '/test', + 'is_permanent': 'on', + 'redirect_link': 'http://www.test.com/', + }) + + # Should redirect back to index + self.assertEqual(response.status_code, 302) + + # Check that the redirect was created + redirects = models.Redirect.objects.filter(old_path='/test') + self.assertEqual(redirects.count(), 1) + self.assertEqual(redirects.first().redirect_link, 'http://www.test.com/') + + def test_add_validation_error(self): + response = self.post({ + 'old_path': '', + 'is_permanent': 'on', + 'redirect_link': 'http://www.test.com/', + }) + + # Should not redirect to index + self.assertEqual(response.status_code, 200) + + +class TestRedirectsEditView(TestCase): + def setUp(self): + # Create a redirect to edit + self.redirect = models.Redirect(old_path='/test', redirect_link='http://www.test.com/') + self.redirect.save() + + # Login + login(self.client) + + def get(self, params={}, redirect_id=None): + return self.client.get(reverse('wagtailredirects_edit_redirect', args=(redirect_id or self.redirect.id, )), params) + + def post(self, post_data={}, redirect_id=None): + return self.client.post(reverse('wagtailredirects_edit_redirect', args=(redirect_id or self.redirect.id, )), post_data) + + def test_status_code(self): + self.assertEqual(self.get().status_code, 200) + + def test_nonexistant_redirect(self): + self.assertEqual(self.get(redirect_id=100000).status_code, 404) + + def test_edit(self): + response = self.post({ + 'old_path': '/test', + 'is_permanent': 'on', + 'redirect_link': 'http://www.test.com/ive-been-edited', + }) + + # Should redirect back to index + self.assertEqual(response.status_code, 302) + + # Check that the redirect was edited + redirects = models.Redirect.objects.filter(old_path='/test') + self.assertEqual(redirects.count(), 1) + self.assertEqual(redirects.first().redirect_link, 'http://www.test.com/ive-been-edited') + + def test_edit_validation_error(self): + response = self.post({ + 'old_path': '', + 'is_permanent': 'on', + 'redirect_link': 'http://www.test.com/ive-been-edited', + }) + + # Should not redirect to index + self.assertEqual(response.status_code, 200) + +class TestRedirectsDeleteView(TestCase): + def setUp(self): + # Create a redirect to edit + self.redirect = models.Redirect(old_path='/test', redirect_link='http://www.test.com/') + self.redirect.save() + + # Login + login(self.client) + + def get(self, params={}, redirect_id=None): + return self.client.get(reverse('wagtailredirects_delete_redirect', args=(redirect_id or self.redirect.id, )), params) + + def post(self, post_data={}, redirect_id=None): + return self.client.post(reverse('wagtailredirects_delete_redirect', args=(redirect_id or self.redirect.id, )), post_data) + + def test_status_code(self): + self.assertEqual(self.get().status_code, 200) + + def test_nonexistant_redirect(self): + self.assertEqual(self.get(redirect_id=100000).status_code, 404) + + def test_delete(self): + response = self.post({ + 'hello': 'world' + }) + + # Should redirect back to index + self.assertEqual(response.status_code, 302) + + # Check that the redirect was deleted + redirects = models.Redirect.objects.filter(old_path='/test') + self.assertEqual(redirects.count(), 0) diff --git a/wagtail/wagtailusers/tests.py b/wagtail/wagtailusers/tests.py index 95cbacbf0c..bfe0fe4ef0 100644 --- a/wagtail/wagtailusers/tests.py +++ b/wagtail/wagtailusers/tests.py @@ -1,19 +1,110 @@ -""" -This file demonstrates writing tests using the unittest module. These will pass -when you run "manage.py test". - -Replace this with more appropriate tests for your application. -""" - -import unittest - from django.test import TestCase +from django.core.urlresolvers import reverse +from django.contrib.auth.models import User +from wagtail.tests.utils import login -@unittest.skip("Need real tests") -class SimpleTest(TestCase): - def test_basic_addition(self): - """ - Tests that 1 + 1 always equals 2. - """ - self.assertEqual(1 + 1, 2) +class TestUserIndexView(TestCase): + def setUp(self): + login(self.client) + + def get(self, params={}): + return self.client.get(reverse('wagtailusers_index'), params) + + def test_status_code(self): + self.assertEqual(self.get().status_code, 200) + + def test_search(self): + response = self.get({'q': "Hello"}) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context['query_string'], "Hello") + + def test_pagination(self): + pages = ['0', '1', '-1', '9999', 'Not a page'] + for page in pages: + response = self.get({'p': page}) + self.assertEqual(response.status_code, 200) + + +class TestUserCreateView(TestCase): + def setUp(self): + login(self.client) + + def get(self, params={}): + return self.client.get(reverse('wagtailusers_create'), params) + + def post(self, post_data={}): + return self.client.post(reverse('wagtailusers_create'), post_data) + + def test_status_code(self): + self.assertEqual(self.get().status_code, 200) + + def test_create(self): + response = self.post({ + 'username': "testuser", + 'email': "test@user.com", + 'first_name': "Test", + 'last_name': "User", + 'password1': "password", + 'password2': "password", + }) + + # Should redirect back to index + self.assertEqual(response.status_code, 302) + + # Check that the user was created + users = User.objects.filter(username='testuser') + self.assertEqual(users.count(), 1) + self.assertEqual(users.first().email, 'test@user.com') + + +class TestUserEditView(TestCase): + def setUp(self): + # Create a user to edit + self.test_user = User.objects.create_user(username='testuser', email='testuser@email.com', password='password') + + # Login + login(self.client) + + def get(self, params={}, user_id=None): + return self.client.get(reverse('wagtailusers_edit', args=(user_id or self.test_user.id, )), params) + + def post(self, post_data={}, user_id=None): + return self.client.post(reverse('wagtailusers_edit', args=(user_id or self.test_user.id, )), post_data) + + def test_status_code(self): + self.assertEqual(self.get().status_code, 200) + + def test_nonexistant_redirect(self): + self.assertEqual(self.get(user_id=100000).status_code, 404) + + def test_edit(self): + response = self.post({ + 'username': "testuser", + 'email': "test@user.com", + 'first_name': "Edited", + 'last_name': "User", + 'password1': "password", + 'password2': "password", + }) + + # Should redirect back to index + self.assertEqual(response.status_code, 302) + + # Check that the user was edited + user = User.objects.get(id=self.test_user.id) + self.assertEqual(user.first_name, 'Edited') + + def test_edit_validation_error(self): + # Leave "username" field blank. This should give a validation error + response = self.post({ + 'username': "", + 'email': "test@user.com", + 'first_name': "Teset", + 'last_name': "User", + 'password1': "password", + 'password2': "password", + }) + + # Should not redirect to index + self.assertEqual(response.status_code, 200)