diff --git a/cms_helper.py b/cms_helper.py index 4eff90b..30751a0 100755 --- a/cms_helper.py +++ b/cms_helper.py @@ -6,9 +6,6 @@ from tempfile import mkdtemp gettext = lambda s: s HELPER_SETTINGS = dict( - NOSE_ARGS=[ - '-s', - ], ROOT_URLCONF='tests.test_utils.urls', INSTALLED_APPS=[ 'filer', @@ -87,8 +84,6 @@ HELPER_SETTINGS = dict( FILE_UPLOAD_TEMP_DIR=mkdtemp(), SITE_ID=1 ) -if 'test' in sys.argv or len(sys.argv) == 1: - HELPER_SETTINGS['INSTALLED_APPS'].append('django_nose') try: import admin_enhancer # pragma: no cover # NOQA @@ -99,8 +94,6 @@ except ImportError: def run(): from djangocms_helper import runner - if 'test' in sys.argv or len(sys.argv) == 1: - sys.argv.append('--nose-runner') runner.cms('djangocms_blog') if __name__ == "__main__": diff --git a/tests/__init__.py b/tests/__init__.py index 7df385f..56ee3b5 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -2,22 +2,13 @@ """ Tests for `djangocms_blog` module. """ -import os -from cms.utils.i18n import get_language_list from cmsplugin_filer_image.models import ThumbnailOption -from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.sites.models import Site -from django.core.files import File as DjangoFile -from django.http import SimpleCookie -from django.test import TestCase, RequestFactory from django.utils.translation import activate -from filer.models import File, Image -from PIL import Image as PilImage, ImageDraw -from six import StringIO +from djangocms_helper.base_test import BaseTestCase -from djangocms_helper.utils import create_user from djangocms_blog.models import BlogCategory, Post User = get_user_model() @@ -27,17 +18,23 @@ def _get_cat_pk(lang, name): return lambda: BlogCategory.objects.translated(lang, name=name).get().pk -class BaseTest(TestCase): +class BaseTest(BaseTestCase): """ Base class with utility function """ - request_factory = None - user = None - languages = get_language_list() category_1 = None thumb_1 = None thumb_2 = None + _pages_data = ( + {'en': {'title': 'page one', 'template': 'page.html', 'publish': True}, + 'fr': {'title': 'page un', 'publish': True}, + 'it': {'title': 'pagina uno', 'publish': True}}, + {'en': {'title': 'page two', 'template': 'page.html', 'publish': True}, + 'fr': {'title': 'page deux', 'publish': True}, + 'it': {'title': 'pagina due', 'publish': True}}, + ) + data = { 'it': [ {'title': u'Primo post', 'abstract': u'

prima riga

', @@ -81,11 +78,7 @@ class BaseTest(TestCase): @classmethod def setUpClass(cls): - cls.request_factory = RequestFactory() - 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') - cls.site_1 = Site.objects.get(pk=1) + super(BaseTest, cls).setUpClass() cls.site_2 = Site.objects.create(domain='http://example2.com', name='example 2') def setUp(self): @@ -101,15 +94,7 @@ class BaseTest(TestCase): self.thumb_2 = ThumbnailOption.objects.create( name='main', width=200, height=200, crop=False, upscale=False ) - 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) + self.img = self.create_filer_image_object() def _get_category(self, data, category=None, lang='en'): for k, v in data.items(): @@ -147,67 +132,6 @@ class BaseTest(TestCase): post.sites.add(site) return post - @classmethod - def tearDownClass(cls): - User.objects.all().delete() - - def tearDown(self): - for post in Post.objects.all(): - post.delete() - os.remove(self.filename) - for f in File.objects.all(): - f.delete() - - def get_pages(self): - from cms.api import create_page, create_title - page = create_page(u'page one', 'fullwidth.html', language='en') - page_2 = create_page(u'page two', 'fullwidth.html', language='en') - 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 - - def post_request(self, page, lang, data, path=None): - if not path: - path = page.get_path(lang) - request = self.request_factory.post(path, 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 - - 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 - def get_posts(self, sites=None): post1 = self._get_post(self.data['en'][0], sites=sites) post1 = self._get_post(self.data['it'][0], post1, 'it') @@ -219,12 +143,3 @@ class BaseTest(TestCase): post2.main_image = self.img post2.save() 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 diff --git a/tests/test_models.py b/tests/test_models.py index 3a1d0ee..594af0b 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -54,61 +54,66 @@ class AdminTest(BaseTest): def test_admin_auto_author(self): page1, page2 = self.get_pages() - request = self.get_page_request('/', self.user_staff, r'/en/blog/', edit=False) data = deepcopy(self.data['en'][0]) - with self.settings(BLOG_AUTHOR_DEFAULT=True): - data['date_published_0'] = now().strftime('%Y-%m-%d') - data['date_published_1'] = now().strftime('%H:%M:%S') - data['categories'] = self.category_1.pk - request = self.post_request(page1, 'en', data=data) - msg_mid = MessageMiddleware() - msg_mid.process_request(request) - post_admin = admin.site._registry[Post] - post_admin.add_view(request) - self.assertEqual(Post.objects.count(), 1) - self.assertEqual(Post.objects.get(translations__slug='first-post').author_id, - request.user.pk) + with self.login_user_context(self.user): + with self.settings(BLOG_AUTHOR_DEFAULT=True): + data['date_published_0'] = now().strftime('%Y-%m-%d') + data['date_published_1'] = now().strftime('%H:%M:%S') + data['categories'] = self.category_1.pk + request = self.post_request(page1, 'en', user=self.user, data=data, path='/en/?edit_fields=post_text') + msg_mid = MessageMiddleware() + msg_mid.process_request(request) + post_admin = admin.site._registry[Post] + response = post_admin.add_view(request) + self.assertEqual(response.status_code, 302) + self.assertEqual(Post.objects.count(), 1) + self.assertEqual(Post.objects.get(translations__slug='first-post').author_id, + request.user.pk) - with self.settings(BLOG_AUTHOR_DEFAULT=False): - data = deepcopy(self.data['en'][1]) - data['date_published_0'] = now().strftime('%Y-%m-%d') - data['date_published_1'] = now().strftime('%H:%M:%S') - data['categories'] = self.category_1.pk - request = self.post_request(page1, 'en', data=data) - msg_mid = MessageMiddleware() - msg_mid.process_request(request) - post_admin = admin.site._registry[Post] - post_admin.add_view(request) - self.assertEqual(Post.objects.count(), 2) - self.assertEqual(Post.objects.get(translations__slug='second-post').author_id, None) + with self.settings(BLOG_AUTHOR_DEFAULT=False): + data = deepcopy(self.data['en'][1]) + data['date_published_0'] = now().strftime('%Y-%m-%d') + data['date_published_1'] = now().strftime('%H:%M:%S') + data['categories'] = self.category_1.pk + request = self.post_request(page1, 'en', user=self.user, data=data, path='/en/?edit_fields=post_text') + msg_mid = MessageMiddleware() + msg_mid.process_request(request) + post_admin = admin.site._registry[Post] + response = post_admin.add_view(request) + self.assertEqual(response.status_code, 302) + self.assertEqual(Post.objects.count(), 2) + self.assertEqual(Post.objects.get(translations__slug='second-post').author_id, None) - with self.settings(BLOG_AUTHOR_DEFAULT='staff'): - data = deepcopy(self.data['en'][2]) - data['date_published_0'] = now().strftime('%Y-%m-%d') - data['date_published_1'] = now().strftime('%H:%M:%S') - data['categories'] = self.category_1.pk - request = self.post_request(page1, 'en', data=data) - msg_mid = MessageMiddleware() - msg_mid.process_request(request) - post_admin = admin.site._registry[Post] - post_admin.add_view(request) - self.assertEqual(Post.objects.count(), 3) - self.assertEqual(Post.objects.get(translations__slug='third-post').author.username, 'staff') + with self.settings(BLOG_AUTHOR_DEFAULT='staff'): + data = deepcopy(self.data['en'][2]) + data['date_published_0'] = now().strftime('%Y-%m-%d') + data['date_published_1'] = now().strftime('%H:%M:%S') + data['categories'] = self.category_1.pk + request = self.post_request(page1, 'en', user=self.user, data=data, path='/en/?edit_fields=post_text') + msg_mid = MessageMiddleware() + msg_mid.process_request(request) + post_admin = admin.site._registry[Post] + response = post_admin.add_view(request) + self.assertEqual(response.status_code, 302) + self.assertEqual(Post.objects.count(), 3) + self.assertEqual(Post.objects.get(translations__slug='third-post').author.username, 'staff') def test_admin_post_text(self): page1, page2 = self.get_pages() post = self._get_post(self.data['en'][0]) - with self.settings(BLOG_USE_PLACEHOLDER=False): - data = {'post_text': 'ehi text'} - request = self.post_request(page1, 'en', data=data, path='/en/?edit_fields=post_text') - msg_mid = MessageMiddleware() - msg_mid.process_request(request) - post_admin = admin.site._registry[Post] - post_admin.edit_field(request, post.pk, 'en') - modified_post = Post.objects.get(pk=post.pk) - self.assertEqual(modified_post.safe_translation_getter('post_text'), data['post_text']) + with self.login_user_context(self.user): + with self.settings(BLOG_USE_PLACEHOLDER=False): + data = {'post_text': 'ehi text'} + request = self.post_request(page1, 'en', user=self.user, data=data, path='/en/?edit_fields=post_text') + msg_mid = MessageMiddleware() + msg_mid.process_request(request) + post_admin = admin.site._registry[Post] + response = post_admin.edit_field(request, post.pk, 'en') + self.assertEqual(response.status_code, 200) + modified_post = Post.objects.language('en').get(pk=post.pk) + self.assertEqual(modified_post.safe_translation_getter('post_text'), data['post_text']) class ModelsTest(BaseTest): diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 7c9e789..6b2a6df 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -2,6 +2,7 @@ import re from cms.api import add_plugin +from cms.plugin_rendering import PluginContext, render_placeholder from django.core.urlresolvers import reverse from django.template import RequestContext from django.template.loader import get_template @@ -26,15 +27,9 @@ class PluginTest(BaseTest): plugin = add_plugin(ph, 'BlogLatestEntriesPlugin', language='en') tag = Tag.objects.get(slug='tag-1') plugin.tags.add(tag) - request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True) - context = RequestContext(request) - try: - template = get_template('page.html').template - with context.bind_template(template): - rendered = plugin.render_plugin(context, ph) - except AttributeError: - rendered = plugin.render_plugin(context, ph) + context = self.get_plugin_context(page1, 'en', plugin, edit=True) + rendered = plugin.render_plugin(context, ph) self.assertTrue(rendered.find('cms_plugin-djangocms_blog-post-abstract-1') > -1) self.assertTrue(rendered.find(reverse('djangocms_blog:posts-tagged', kwargs={'tag': tag.slug})) > -1) self.assertTrue(rendered.find('

first line

') > -1) @@ -49,14 +44,9 @@ class PluginTest(BaseTest): post2.categories.add(category_2) plugin = add_plugin(ph, 'BlogLatestEntriesPlugin', language='en') plugin.categories.add(category_2) - request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True) - context = RequestContext(request) - try: - template = get_template('page.html').template - with context.bind_template(template): - rendered = plugin.render_plugin(context, ph) - except AttributeError: - rendered = plugin.render_plugin(context, ph) + + context = self.get_plugin_context(page1, 'en', plugin, edit=True) + rendered = plugin.render_plugin(context, ph) self.assertTrue(rendered.find('cms_plugin-djangocms_blog-post-abstract-2') > -1) self.assertTrue(rendered.find(reverse('djangocms_blog:posts-category', kwargs={'category': category_2.slug})) > -1) self.assertTrue(rendered.find('

second post first line

') > -1) @@ -74,14 +64,9 @@ class PluginTest(BaseTest): ph = page1.placeholders.get(slot='content') plugin = add_plugin(ph, 'BlogAuthorPostsPlugin', language='en') plugin.authors.add(self.user) - request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True) - context = RequestContext(request) - try: - template = get_template('page.html').template - with context.bind_template(template): - rendered = plugin.render_plugin(context, ph) - except AttributeError: - rendered = plugin.render_plugin(context, ph) + + context = self.get_plugin_context(page1, 'en', plugin, edit=True) + rendered = plugin.render_plugin(context, ph) self.assertTrue(rendered.find(reverse('djangocms_blog:posts-author', kwargs={'username': self.user.get_username()})) > -1) self.assertTrue(rendered.find('2 articles') > -1) @@ -97,14 +82,8 @@ class PluginTest(BaseTest): post2.save() ph = page1.placeholders.get(slot='content') plugin = add_plugin(ph, 'BlogTagsPlugin', language='en') - request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True) - context = RequestContext(request) - try: - template = get_template('page.html').template - with context.bind_template(template): - rendered = plugin.render_plugin(context, ph).replace('\n', '') - except AttributeError: - rendered = plugin.render_plugin(context, ph).replace('\n', '') + context = self.get_plugin_context(page1, 'en', plugin, edit=True) + rendered = plugin.render_plugin(context, ph) for tag in Tag.objects.all(): self.assertTrue(rendered.find(reverse('djangocms_blog:posts-tagged', kwargs={'tag': tag.slug})) > -1) if tag.slug == 'test-tag': @@ -123,15 +102,9 @@ class PluginTest(BaseTest): post2.save() ph = page1.placeholders.get(slot='content') plugin = add_plugin(ph, 'BlogCategoryPlugin', language='en') - request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True) plugin_class = plugin.get_plugin_class_instance() - context = RequestContext(request) - try: - template = get_template('page.html').template - with context.bind_template(template): - context = plugin_class.render(context, plugin, ph) - except AttributeError: - context = plugin_class.render(context, plugin, ph) + context = self.get_plugin_context(page1, 'en', plugin, edit=True) + context = plugin_class.render(context, plugin, ph) self.assertTrue(context['categories']) self.assertEqual(list(context['categories']), [self.category_1]) @@ -144,20 +117,15 @@ class PluginTest(BaseTest): post2.save() ph = page1.placeholders.get(slot='content') plugin = add_plugin(ph, 'BlogArchivePlugin', language='en') - request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True) plugin_class = plugin.get_plugin_class_instance() - context = RequestContext(request) - try: - template = get_template('page.html').template - with context.bind_template(template): - context = plugin_class.render(context, plugin, ph) - except AttributeError: - context = plugin_class.render(context, plugin, ph) + + context = self.get_plugin_context(page1, 'en', plugin, edit=True) + context = plugin_class.render(context, plugin, ph) self.assertEqual(context['dates'][0]['date'].date(), now().replace(year=now().year, month=now().month, day=1).date()) self.assertEqual(context['dates'][0]['count'], 2) post2.publish = False post2.save() - context = plugin_class.render(RequestContext(request, {'request': request}), plugin, ph) + context = plugin_class.render(context, plugin, ph) self.assertEqual(context['dates'][0]['date'].date(), now().replace(year=now().year, month=now().month, day=1).date()) self.assertEqual(context['dates'][0]['count'], 1) diff --git a/tests/test_views.py b/tests/test_views.py index f5023c9..071ec15 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -50,7 +50,7 @@ class ViewTest(BaseTest): response = view_obj.render_to_response(context) self.assertContains(response, context['post_list'][0].get_absolute_url()) - request = self.get_page_request(page1, self.user, r'/it/blog/', lang_code='it', edit=True) + request = self.get_page_request(page1, self.user, r'/it/blog/', lang='it', edit=True) activate('it') view_obj.request = request view_obj.object_list = view_obj.get_queryset() @@ -76,7 +76,7 @@ class ViewTest(BaseTest): } view_obj = PostListView() - request = self.get_page_request(page1, self.user, r'/fr/blog/', lang_code='fr', edit=True) + request = self.get_page_request(page1, self.user, r'/fr/blog/', lang='fr', edit=True) activate('fr') view_obj.request = request view_obj.kwargs = {} @@ -88,7 +88,7 @@ class ViewTest(BaseTest): with override_parler_settings(PARLER_LANGUAGES=PARLER_FALLBACK): view_obj = PostListView() - request = self.get_page_request(page1, self.user, r'/fr/blog/', lang_code='fr', edit=True) + request = self.get_page_request(page1, self.user, r'/fr/blog/', lang='fr', edit=True) activate('fr') view_obj.request = request view_obj.kwargs = {} @@ -115,7 +115,7 @@ class ViewTest(BaseTest): self.assertEqual(post_obj.language_code, 'en') with switch_language(post1, 'it'): - request = self.get_page_request(page1, AnonymousUser(), r'/it/blog/', lang_code='it', edit=False) + request = self.get_page_request(page1, AnonymousUser(), r'/it/blog/', lang='it', edit=False) view_obj.request = request view_obj.kwargs = {'slug': post1.slug} post_obj = view_obj.get_object() @@ -230,7 +230,7 @@ class ViewTest(BaseTest): feed = LatestEntriesFeed() self.assertEqual(list(feed.items()), [post1]) - request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=False) + request = self.get_page_request(page1, self.user, r'/en/blog/', lang='en', edit=False) xml = feed(request) self.assertContains(xml, post1.get_absolute_url()) self.assertContains(xml, 'Blog articles on example.com') @@ -239,7 +239,7 @@ class ViewTest(BaseTest): post1.set_current_language('it') feed = LatestEntriesFeed() self.assertEqual(list(feed.items()), [post1]) - request = self.get_page_request(page1, self.user, r'/it/blog/', lang_code='it', edit=False) + request = self.get_page_request(page1, self.user, r'/it/blog/', lang='it', edit=False) xml = feed(request) self.assertContains(xml, post1.get_absolute_url()) self.assertContains(xml, 'Articoli del blog su example.com')