From 41b74da169d6a5c104061f951a575e033ca48942 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Mon, 26 Dec 2016 19:59:09 +0000 Subject: [PATCH] Add basic page tests --- project/common/tests.py | 63 ++++++++++++++++++++++++++++++++++++++- project/projects/tests.py | 47 +++++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 project/projects/tests.py diff --git a/project/common/tests.py b/project/common/tests.py index 66b6a65..c61eb29 100644 --- a/project/common/tests.py +++ b/project/common/tests.py @@ -1,10 +1,71 @@ from wagtail.tests.utils import WagtailPageTests from .context import SETTINGS_KEYS from django.conf import settings +from project.home.models import HomePage +from wagtail.wagtailcore.models import Site, Page +from django.contrib.auth.models import User +from django.core.urlresolvers import reverse +from rest_framework.test import APIClient +from django.utils.text import slugify +from bs4 import BeautifulSoup class BaseTestCase(WagtailPageTests): - pass + USERNAME = 'test_user' + EMAIL = 'test@example.com' + PASSWORD = 'test' + + def setUp(self): + super().setUp() + + self.client = APIClient() + self.root = self.create_initial_homepage() + self.user = User.objects.create_superuser( + self.USERNAME, + self.EMAIL, + self.PASSWORD + ) + self.client.login( + username=self.USERNAME, + password=self.PASSWORD + ) + + def create_model(self, model, data={}): + add_url = reverse('wagtailadmin_pages:add', args=[ + model._meta.app_label, model._meta.model_name, self.root.pk + ]) + data['action-publish'] = 'action-publish' + data['body-count'] = 1 + data['body-0-deleted'] = '' + data['body-0-order'] = 0 + data['body-0-type'] = 'raw_html' + data['body-0-value'] = data['body'] + data['slug'] = slugify(data['title']) + return self.client.post(add_url, data) + + def parse_content(self, content): + parsed_content = BeautifulSoup(content, 'html.parser') + for tag in parsed_content(["noscript"]): # Remove noscript tags + tag.extract() + return parsed_content + + def create_initial_homepage(self): + """ + from https://github.com/wagtail/wagtail/blob/master/wagtail/project_template/home/migrations/0002_create_homepage.py + """ + Page.objects.filter(id=2).delete() + + # Create a new homepage + homepage = HomePage.objects.create( + title="Homepage", + slug='home', + path='00010001', + depth=2, + numchild=0, + url_path='/', + ) + Site.objects.create(hostname='localhost', root_page=homepage, is_default_site=True) + return homepage class ContextInjectorTestCase(BaseTestCase): diff --git a/project/projects/tests.py b/project/projects/tests.py new file mode 100644 index 0000000..49d0516 --- /dev/null +++ b/project/projects/tests.py @@ -0,0 +1,47 @@ +from project.common.tests import BaseTestCase +from .models import ProjectPage, validate_url, ALLOWED_DOMAINS +from django.core.exceptions import ValidationError + + +class ProjectPageTestCase(BaseTestCase): + def setUp(self): + super().setUp() + self.create_model(ProjectPage, { + 'title': 'Test', + 'body': 'test', + 'download_url': 'https://github.com/test', + 'project_url': 'https://github.com/test' + }) + self.project = ProjectPage.objects.first() + + def test_page(self): + response = self.client.get(self.project.url) + self.assertEqual(response.status_code, 200) + content = self.parse_content(response.content) + self.assertIn(self.project.title, content.title.string) + + def test_download_url(self): + self.assertEqual(self.project.get_download_url(), self.project.download_url) + + def test_validator_on_model(self): + response = self.create_model(ProjectPage, { + 'title': 'Test', + 'body': 'test', + 'download_url': 'https://github.com/test', + 'project_url': 'https://foo.com/test' + }) + self.assertNotEqual(response.status_code, 302) + response = self.create_model(ProjectPage, { + 'title': 'Test', + 'body': 'test', + 'download_url': 'https://foo.com/test', + 'project_url': 'https://github.com/test' + }) + self.assertNotEqual(response.status_code, 302) + + def test_validator(self): + for domain in ALLOWED_DOMAINS: + validate_url('https://{}.com/test'.format(domain)) + + with self.assertRaises(ValidationError): + validate_url('https://foo.com') diff --git a/requirements.txt b/requirements.txt index 3ed99c0..94b0fc5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,6 +7,7 @@ Django>=1.10,<1.11 django-bootstrap-form==3.2.1 flake8==3.2.1 honcho==0.7.1 +model_mommy==1.3.1 psycopg2==2.6.2 safety==0.5.1 wagtail>=1.8,<1.9