djangocms_blog/tests/__init__.py

196 lines
7.4 KiB
Python
Raw Normal View History

2014-06-08 22:06:01 +00:00
# -*- coding: utf-8 -*-
"""
Tests for `djangocms_blog` module.
"""
2014-06-20 16:28:06 +00:00
import os
2014-06-08 22:06:01 +00:00
from cms.utils.i18n import get_language_list
2014-06-09 22:11:48 +00:00
from cmsplugin_filer_image.models import ThumbnailOption
2014-06-20 16:28:06 +00:00
from django.conf import settings
2014-09-04 07:17:03 +00:00
from django.contrib.auth import get_user_model
2014-10-05 06:25:18 +00:00
from django.contrib.sites.models import Site
2014-06-20 16:28:06 +00:00
from django.core.files import File as DjangoFile
2014-06-08 22:06:01 +00:00
from django.http import SimpleCookie
from django.test import TestCase, RequestFactory
2014-10-05 06:52:04 +00:00
from django.utils.translation import activate
2014-06-20 16:28:06 +00:00
from filer.models import File, Image
2014-09-04 07:17:03 +00:00
from PIL import Image as PilImage, ImageDraw
2014-06-08 22:06:01 +00:00
from six import StringIO
2014-09-04 07:17:03 +00:00
from djangocms_helper.utils import create_user
2014-10-05 06:25:18 +00:00
from djangocms_blog.models import BlogCategory, Post
2014-09-04 07:17:03 +00:00
User = get_user_model()
2014-06-09 22:11:48 +00:00
2014-06-08 22:06:01 +00:00
class BaseTest(TestCase):
"""
Base class with utility function
"""
request_factory = None
user = None
languages = get_language_list()
2014-06-09 22:11:48 +00:00
category_1 = None
thumb_1 = None
thumb_2 = None
data = {
'it': [
{'title': u'Primo post', 'abstract': u'<p>prima riga</p>',
'description': u'Questa è la descrizione', 'keywords': u'keyword1, keyword2',
2014-10-04 18:05:17 +00:00
'text': u'Testo del post'},
2014-06-09 22:11:48 +00:00
{'title': u'Secondo post', 'abstract': u'<p>prima riga del secondo post</p>',
'description': u'Descrizione del secondo post', 'keywords': u'keyword3, keyword4',
'text': u'Testo del secondo post'},
2014-10-04 18:05:17 +00:00
{'title': u'Terzo post', 'abstract': u'<p>prima riga del terzo post</p>',
'description': u'Descrizione del terzo post', 'keywords': u'keyword5, keyword6',
'text': u'Testo del terzo post'},
2014-06-09 22:11:48 +00:00
],
'en': [
{'title': u'First post', 'abstract': u'<p>first line</p>',
'description': u'This is the description', 'keywords': u'keyword1, keyword2',
'text': u'Post text'},
{'title': u'Second post', 'abstract': u'<p>second post first line</p>',
'description': u'Second post description', 'keywords': u'keyword3, keyword4',
2014-10-04 18:05:17 +00:00
'text': u'Second post text'},
{'title': u'Third post', 'abstract': u'<p>third post first line</p>',
'description': u'third post description', 'keywords': u'keyword5, keyword6',
'text': u'Third post text'}
2014-06-09 22:11:48 +00:00
]
}
2014-06-08 22:06:01 +00:00
@classmethod
def setUpClass(cls):
cls.request_factory = RequestFactory()
2014-09-04 07:17:03 +00:00
cls.user = create_user('admin', 'admin@admin.com', 'admin', is_staff=True, is_superuser=True)
cls.user_staff = create_user('staff', 'staff@admin.com', 'staff', is_staff=True)
cls.user_normal = create_user('normal', 'normal@admin.com', 'normal')
2014-10-04 18:05:17 +00:00
cls.site_1 = Site.objects.get(pk=1)
2014-10-04 17:22:22 +00:00
cls.site_2 = Site.objects.create(domain='http://example2.com', name='example 2')
2014-06-08 22:06:01 +00:00
2014-06-09 22:11:48 +00:00
def setUp(self):
2014-06-10 23:06:57 +00:00
activate('en')
2014-06-09 22:11:48 +00:00
super(BaseTest, self).setUp()
2014-10-05 06:25:18 +00:00
self.category_1 = BlogCategory.objects.create(name=u'category 1')
self.category_1.set_current_language('it', initialize=True)
2014-06-10 23:06:57 +00:00
self.category_1.name = u'categoria 1'
self.category_1.save()
2014-06-09 22:11:48 +00:00
self.thumb_1 = ThumbnailOption.objects.create(
name='base', width=100, height=100, crop=True, upscale=False
)
self.thumb_2 = ThumbnailOption.objects.create(
name='main', width=200, height=200, crop=False, upscale=False
)
2014-06-20 16:28:06 +00:00
img = create_image()
self.image_name = 'test_file.jpg'
self.filename = os.path.join(settings.FILE_UPLOAD_TEMP_DIR,
self.image_name)
img.save(self.filename, 'JPEG')
file_obj = DjangoFile(open(self.filename, 'rb'), name=self.image_name)
self.img = Image.objects.create(owner=self.user,
original_filename=self.image_name,
file=file_obj)
2014-06-09 22:11:48 +00:00
2014-10-04 18:05:17 +00:00
def _get_post(self, data, post=None, lang='en', sites=None):
2014-06-09 22:11:48 +00:00
if not post:
2014-10-05 06:25:18 +00:00
post_data = {
'author': self.user,
'title': data['title'],
'abstract': data['abstract'],
'meta_description': data['description'],
'meta_keywords': data['keywords'],
}
post = Post.objects.create(**post_data)
else:
post.set_current_language(lang)
post.title = data['title']
post.abstract = data['abstract']
post.meta_description = data['description']
post.meta_keywords = data['keywords']
post.save()
2014-06-09 22:11:48 +00:00
post.categories.add(self.category_1)
2014-10-04 18:05:17 +00:00
if sites:
for site in sites:
post.sites.add(site)
2014-06-09 22:11:48 +00:00
return post
@classmethod
def tearDownClass(cls):
User.objects.all().delete()
2014-06-20 16:28:06 +00:00
def tearDown(self):
2014-10-05 06:25:18 +00:00
for post in Post.objects.all():
post.delete()
2014-06-20 16:28:06 +00:00
os.remove(self.filename)
for f in File.objects.all():
f.delete()
2014-06-09 22:11:48 +00:00
2014-06-08 22:06:01 +00:00
def get_pages(self):
from cms.api import create_page, create_title
2014-08-13 06:42:03 +00:00
page = create_page(u'page one', 'fullwidth.html', language='en')
page_2 = create_page(u'page two', 'fullwidth.html', language='en')
2014-06-08 22:06:01 +00:00
create_title(language='fr', title=u'page un', page=page)
create_title(language='it', title=u'pagina uno', page=page)
for lang in self.languages:
page.publish(lang)
page_2.publish('en')
return page.get_draft_object(), page_2.get_draft_object()
def get_request(self, page, lang):
request = self.request_factory.get(page.get_path(lang))
request.current_page = page
request.user = self.user
request.session = {}
request.cookies = SimpleCookie()
request.errors = StringIO()
return request
2014-10-11 07:05:02 +00:00
def post_request(self, page, lang, data):
request = self.request_factory.post(page.get_path(lang), data)
request.current_page = page
request.user = self.user
request.session = {}
request.cookies = SimpleCookie()
request.errors = StringIO()
request._dont_enforce_csrf_checks = True
return request
2014-06-08 22:06:01 +00:00
def get_page_request(self, page, user, path=None, edit=False, lang_code='en'):
from cms.middleware.toolbar import ToolbarMiddleware
path = path or page and page.get_absolute_url()
if edit:
path += '?edit'
request = RequestFactory().get(path)
request.session = {}
request.user = user
request.LANGUAGE_CODE = lang_code
if edit:
request.GET = {'edit': None}
else:
request.GET = {'edit_off': None}
request.current_page = page
mid = ToolbarMiddleware()
mid.process_request(request)
return request
2014-06-11 15:48:28 +00:00
2014-10-04 18:05:17 +00:00
def get_posts(self, sites=None):
2014-10-05 06:52:04 +00:00
post1 = self._get_post(self.data['en'][0], sites=sites)
post1 = self._get_post(self.data['it'][0], post1, 'it')
post1.publish = True
post1.main_image = self.img
post1.save()
post2 = self._get_post(self.data['en'][1], sites=sites)
post2 = self._get_post(self.data['it'][1], post2, 'it')
post2.main_image = self.img
post2.save()
2014-06-20 16:28:06 +00:00
return post1, post2
def create_image(mode='RGB', size=(800, 600)):
image = PilImage.new(mode, size)
draw = ImageDraw.Draw(image)
x_bit, y_bit = size[0] // 10, size[1] // 10
draw.rectangle((x_bit, y_bit * 2, x_bit * 7, y_bit * 3), 'red')
draw.rectangle((x_bit * 2, y_bit, x_bit * 3, y_bit * 8), 'red')
return image