Add basic page tests
This commit is contained in:
parent
49bd83ca95
commit
41b74da169
3 changed files with 110 additions and 1 deletions
|
@ -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):
|
||||
|
|
47
project/projects/tests.py
Normal file
47
project/projects/tests.py
Normal file
|
@ -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')
|
|
@ -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
|
||||
|
|
Reference in a new issue