djangocms_blog/tests/test_liveblog.py

225 lines
9.6 KiB
Python
Raw Permalink Normal View History

2016-06-26 09:54:40 +00:00
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import json
2016-09-16 23:26:30 +00:00
from datetime import timedelta
2016-06-26 09:54:40 +00:00
from unittest import SkipTest
2016-09-16 23:26:30 +00:00
from django.utils.timezone import now
2016-06-26 09:54:40 +00:00
try:
from channels import Channel
from channels.tests import ChannelTestCase
from cms.api import add_plugin
from djangocms_blog.liveblog.consumers import liveblog_connect, liveblog_disconnect
2016-09-16 23:26:30 +00:00
from djangocms_blog.liveblog.models import DATE_FORMAT, Liveblog
2016-06-26 09:54:40 +00:00
from .base import BaseTest
class LiveBlogTest(BaseTest, ChannelTestCase):
@classmethod
def setUpClass(cls):
try:
import knocker
super(LiveBlogTest, cls).setUpClass()
except ImportError:
raise SkipTest('channels not installed, skipping tests')
def test_add_plugin(self):
2016-09-16 23:26:30 +00:00
pages = self.get_pages()
2016-06-26 09:54:40 +00:00
posts = self.get_posts()
post = posts[0]
post.enable_liveblog = True
post.save()
2016-09-16 23:26:30 +00:00
request = self.get_request(pages[0], user=self.user, lang='en')
2016-06-26 09:54:40 +00:00
Channel('setup').send({'connect': 1, 'reply_channel': 'reply'})
message = self.get_next_message('setup', require=True)
liveblog_connect(message, self.app_config_1.namespace, 'en', post.slug)
2017-12-31 12:45:05 +00:00
result = self.get_next_message(message.reply_channel.name, require=True)
self.assertTrue(result['accept'])
2016-06-26 09:54:40 +00:00
plugin = add_plugin(
post.liveblog, 'LiveblogPlugin', language='en', body='live text', publish=True
)
2016-09-16 23:26:30 +00:00
__, admin = plugin.get_plugin_instance()
admin.save_model(request, plugin, None, None)
2016-06-26 09:54:40 +00:00
result = self.get_next_message(message.reply_channel.name, require=True)
self.assertTrue(result['text'])
rendered = json.loads(result['text'])
self.assertEqual(plugin.pk, rendered['id'])
self.assertEqual(plugin.creation_date.strftime(DATE_FORMAT), rendered['creation_date'])
self.assertEqual(plugin.changed_date.strftime(DATE_FORMAT), rendered['changed_date'])
self.assertTrue(rendered['content'].find('data-post-id="{}"'.format(plugin.pk)) > -1)
self.assertTrue(rendered['content'].find('live text') > -1)
plugin.body = 'modified text'
plugin.save()
2016-09-16 23:26:30 +00:00
admin.save_model(request, plugin, None, None)
2016-06-26 09:54:40 +00:00
result = self.get_next_message(message.reply_channel.name, require=True)
self.assertTrue(result['text'])
rendered = json.loads(result['text'])
self.assertEqual(plugin.pk, rendered['id'])
self.assertEqual(plugin.creation_date.strftime(DATE_FORMAT), rendered['creation_date'])
self.assertEqual(plugin.changed_date.strftime(DATE_FORMAT), rendered['changed_date'])
self.assertTrue(rendered['content'].find('data-post-id="{}"'.format(plugin.pk)) > -1)
self.assertTrue(rendered['content'].find('modified text') > -1)
self.assertTrue(rendered['content'].find('live text') == -1)
def test_add_plugin_no_publish(self):
2016-09-16 23:26:30 +00:00
pages = self.get_pages()
2016-06-26 09:54:40 +00:00
posts = self.get_posts()
post = posts[0]
post.enable_liveblog = True
post.save()
2016-09-16 23:26:30 +00:00
request = self.get_request(pages[0], user=self.user, lang='en')
2016-06-26 09:54:40 +00:00
Channel('setup').send({'connect': 1, 'reply_channel': 'reply'})
message = self.get_next_message('setup', require=True)
liveblog_connect(message, self.app_config_1.namespace, 'en', post.slug)
2017-12-31 12:45:05 +00:00
result = self.get_next_message(message.reply_channel.name, require=True)
self.assertTrue(result['accept'])
2016-06-26 09:54:40 +00:00
plugin = add_plugin(
post.liveblog, 'LiveblogPlugin', language='en', body='live text', publish=False
)
2016-09-16 23:26:30 +00:00
__, admin = plugin.get_plugin_instance()
admin.save_model(request, plugin, None, None)
2016-06-26 09:54:40 +00:00
result = self.get_next_message(message.reply_channel.name, require=False)
self.assertIsNone(result)
plugin.publish = True
plugin.save()
2016-09-16 23:26:30 +00:00
admin.save_model(request, plugin, None, None)
2016-06-26 09:54:40 +00:00
result = self.get_next_message(message.reply_channel.name, require=True)
self.assertTrue(result['text'])
rendered = json.loads(result['text'])
self.assertEqual(plugin.pk, rendered['id'])
self.assertEqual(plugin.creation_date.strftime(DATE_FORMAT), rendered['creation_date'])
self.assertEqual(plugin.changed_date.strftime(DATE_FORMAT), rendered['changed_date'])
self.assertTrue(rendered['content'].find('data-post-id="{}"'.format(plugin.pk)) > -1)
self.assertTrue(rendered['content'].find('live text') > -1)
def test_disconnect(self):
2016-09-16 23:26:30 +00:00
pages = self.get_pages()
2016-06-26 09:54:40 +00:00
posts = self.get_posts()
post = posts[0]
post.enable_liveblog = True
post.save()
2016-09-16 23:26:30 +00:00
request = self.get_request(pages[0], user=self.user, lang='en')
2016-06-26 09:54:40 +00:00
Channel('setup').send({'connect': 1, 'reply_channel': 'reply'})
message = self.get_next_message('setup', require=True)
liveblog_connect(message, self.app_config_1.namespace, 'en', post.slug)
2017-12-31 12:45:05 +00:00
result = self.get_next_message(message.reply_channel.name, require=True)
self.assertTrue(result['accept'])
2016-06-26 09:54:40 +00:00
plugin = add_plugin(
post.liveblog, 'LiveblogPlugin', language='en', body='live text', publish=True
)
2016-09-16 23:26:30 +00:00
__, admin = plugin.get_plugin_instance()
admin.save_model(request, plugin, None, None)
2016-06-26 09:54:40 +00:00
result = self.get_next_message(message.reply_channel.name, require=True)
self.assertTrue(result['text'])
liveblog_disconnect(message, self.app_config_1.namespace, 'en', post.slug)
plugin.body = 'modified text'
plugin.save()
2016-09-16 23:26:30 +00:00
admin.save_model(request, plugin, None, None)
2016-06-26 09:54:40 +00:00
result = self.get_next_message(message.reply_channel.name, require=False)
self.assertIsNone(result)
def test_nopost(self):
self.get_pages()
Channel('setup').send({'connect': 1, 'reply_channel': 'reply'})
message = self.get_next_message('setup', require=True)
liveblog_connect(message, self.app_config_1.namespace, 'en', 'random-post')
result = self.get_next_message(message.reply_channel.name, require=True)
self.assertTrue(result['text'])
rendered = json.loads(result['text'])
self.assertTrue(rendered['error'], 'no_post')
liveblog_disconnect(message, self.app_config_1.namespace, 'en', 'random-post')
result = self.get_next_message(message.reply_channel.name, require=True)
self.assertTrue(result['text'])
rendered = json.loads(result['text'])
self.assertTrue(rendered['error'], 'no_post')
def test_plugin_without_post(self):
pages = self.get_pages()
placeholder = pages[0].get_placeholders().get(slot='content')
Channel('setup').send({'connect': 1, 'reply_channel': 'reply'})
message = self.get_next_message('setup', require=True)
liveblog_connect(message, self.app_config_1.namespace, 'en', 'random post')
self.get_next_message(message.reply_channel.name, require=True)
plugin = add_plugin(
placeholder, 'LiveblogPlugin', language='en', body='live text', publish=True
)
self.assertIsNone(plugin.liveblog_group)
result = self.get_next_message(message.reply_channel.name, require=False)
self.assertIsNone(result)
def test_plugin_render(self):
posts = self.get_posts()
pages = self.get_pages()
post = posts[0]
post.enable_liveblog = True
post.save()
plugin = add_plugin(
post.liveblog, 'LiveblogPlugin', language='en', body='live text', publish=False
)
2018-01-14 11:54:44 +00:00
rendered = self.render_plugin(pages[0], 'en', plugin, edit=True)
2016-06-26 10:48:40 +00:00
self.assertFalse(rendered.strip())
2016-06-26 09:54:40 +00:00
plugin.publish = True
plugin.save()
2018-01-14 11:54:44 +00:00
rendered = self.render_plugin(pages[0], 'en', plugin, edit=True)
2016-06-26 09:54:40 +00:00
self.assertTrue(rendered.find('data-post-id="{}"'.format(plugin.pk)) > -1)
self.assertTrue(rendered.find('live text') > -1)
2016-09-16 23:26:30 +00:00
def test_plugins_order(self):
posts = self.get_posts()
self.get_pages()
post = posts[0]
post.enable_liveblog = True
post.save()
current_date = now()
plugin = add_plugin(
post.liveblog, 'LiveblogPlugin', language='en', body='plugin 1', publish=True,
post_date=current_date - timedelta(seconds=1)
)
add_plugin(
post.liveblog, 'LiveblogPlugin', language='en', body='plugin 2', publish=True,
post_date=current_date - timedelta(seconds=5)
)
add_plugin(
post.liveblog, 'LiveblogPlugin', language='en', body='plugin 3', publish=True,
post_date=current_date - timedelta(seconds=10)
)
2016-10-08 20:42:35 +00:00
self.assertEqual(list(Liveblog.objects.all().order_by('position').values_list('pk', flat=True)), [1, 2, 3])
2016-09-16 23:26:30 +00:00
plugin.post_date = current_date - timedelta(seconds=20)
plugin.save()
2016-10-08 20:42:35 +00:00
self.assertEqual(list(Liveblog.objects.all().order_by('position').values_list('pk', flat=True)), [2, 3, 1])
2016-09-16 23:26:30 +00:00
2016-06-26 09:54:40 +00:00
except ImportError: # pragma: no cover
pass