def create_comment(data=None, parent=None, target=None, user=None): from django.test import Client import django_comments as comments from django.contrib.sites.models import Site Comment = comments.get_model() body = { 'name': 'user_anonymous_name', 'email': '*****@*****.**', 'comment': 'test_comment', } if data: body.update(data) url = comments.get_form_target() args = [target if target else Site.objects.all()[0]] kwargs = {} if parent is not None: kwargs['parent'] = str(parent.pk) body['parent'] = str(parent.pk) form = comments.get_form()(*args, **kwargs) body.update(form.generate_security_data()) client = Client() if user: client.force_login(user) client.post(url, body, follow=True) return Comment.objects.last()
def _set_up_email_test_objects(self): app = ApplicationFactory(editor=self.editor.editor, partner=self.partner) factory = RequestFactory() request = factory.post(get_form_target()) return app, request
def mptt_comment_form_target(): """ Get the target URL for the comment form. Example:: <form action="{% comment_form_target %}" method="POST"> """ return get_form_target()
def comment_form_target(): """ Get the target URL for the comment form. Example:: <form action="{% comment_form_target %}" method="post"> """ return django_comments.get_form_target()
def _post_comment(self, data=None, parent=None): Comment = django_comments.get_model() body = self.BASE_DATA.copy() if data: body.update(data) url = django_comments.get_form_target() args = [Site.objects.all()[0]] kwargs = {} if parent is not None: kwargs['parent'] = str(parent.pk) body['parent'] = str(parent.pk) form = django_comments.get_form()(*args, **kwargs) body.update(form.generate_security_data()) self.client.post(url, body, follow=True) return Comment.objects.order_by('-id')[0]
def _post_comment(self, data=None, parent=None): Comment = django_comments.get_model() body = self.BASE_DATA.copy() if data: body.update(data) url = django_comments.get_form_target() args = [Site.objects.all()[0]] kwargs = {} if parent is not None: kwargs['parent'] = unicode(parent.pk) body['parent'] = unicode(parent.pk) form = django_comments.get_form()(*args, **kwargs) body.update(form.generate_security_data()) self.client.post(url, body, follow=True) return Comment.objects.order_by('-id')[0]
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelper(self) self.helper.form_action = django_comments.get_form_target() self.helper.layout = Layout( 'honeypot', 'content_type', 'object_pk', 'timestamp', 'security_hash', 'comment', Hidden('next', '{% url "replies:success" %}'), ) self.helper.add_input(Submit('submit', '回复', css_class='btn-sm')) self.fields['comment'].label = ''
def test_contact_us_emails(self, mock_email): factory = RequestFactory() request = factory.post(get_form_target()) request.user = UserFactory() editor = EditorFactory() reply_to = ['*****@*****.**'] cc = ['*****@*****.**'] self.assertEqual(len(mail.outbox), 0) mail_instance = MagicMailBuilder(template_mail_cls=InlineCSSTemplateMail) email = mail_instance.contact_us_email('*****@*****.**', {'editor_wp_username': editor.wp_username, 'body': 'This is a test email'}) email.extra_headers["Reply-To"] = ", ".join(reply_to) email.extra_headers["Cc"] = ", ".join(cc) email.send() self.assertEqual(len(mail.outbox), 1)
def __init__(self, *args, **kwargs): super(CrispyXtdCommentForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_id = 'CF' self.helper.form_class = 'form-horizontal' self.helper.form_method = 'post' self.helper.form_action = django_comments.get_form_target() self.helper.label_class = 'col-lg-3 col-md-3' self.helper.field_class = 'col-lg-8 col-md-8' self.helper.layout = Fieldset( "Your comment", 'content_type', 'object_pk', 'timestamp', 'security_hash', Field('honeypot', wrapper_class="hide"), 'name', 'email', 'url', 'comment', 'followup', 'reply_to' ) send = Submit('submit', 'send', data_name="post") preview = Submit('preview', 'preview', css_class="btn btn-default") preview.field_classes = 'btn btn-default' self.helper.add_input(send) self.helper.add_input(preview)
def test_not_allowed_empty_comment(self): """ 空コメントを投稿できない """ target = PersonaFactory() self.assertTrue(self.client.login(username=self.user, password='******')) url = django_comments.get_form_target() form = KawazCommentForm(target) dict = {field.html_name: field.value() for field in form} r = self.client.post(url, { 'object_pk': dict['object_pk'], 'content_type': dict['content_type'], 'security_hash': dict['security_hash'], 'timestamp': dict['timestamp'], 'comment': '', # 空文字 }) self.assertEqual(r.status_code, 200) # コメントが生成できない comments = Comment.objects.all() self.assertEqual(comments.count(), 0)
def test_post_comment(self): """ コメント投稿用のビューからコメントが投稿できる """ target = PersonaFactory() self.assertTrue(self.client.login(username=self.user, password='******')) url = django_comments.get_form_target() form = KawazCommentForm(target) dict = {field.html_name: field.value() for field in form} dict['comment'] = "Hello" r = self.client.post(url, { 'object_pk': dict['object_pk'], 'content_type': dict['content_type'], 'security_hash': dict['security_hash'], 'timestamp': dict['timestamp'], 'comment': dict['comment'], }) self.assertEqual(r.status_code, 302) # コメントが生成されている comments = Comment.objects.all() self.assertEqual(comments.count(), 1)
def __init__(self, target_object, parent=None, data=None, initial=None, **kwargs): self.parent = parent if initial is None: initial = {} initial.update({'parent': self.parent}) super(MyCommentForm, self).__init__(target_object, data=data, initial=initial, **kwargs) self.helper = FormHelper(self) self.helper.form_action = django_comments.get_form_target() self.helper.layout = Layout( Div( 'honeypot', 'parent', 'content_type', 'object_pk', 'timestamp', 'security_hash', Hidden('next', '{% url "comments:comment_success" %}'), Div( 'name', css_class='col-md-4' ), Div( 'email', css_class='col-md-4' ), Div( 'url', css_class='col-md-4' ), Div( 'comment', css_class='col-md-12' ), css_class='row' ) ) self.helper.add_input(Submit('submit', '发表评论', css_class='btn-sm pull-xs-right')) self.fields['comment'].widget.attrs['rows'] = 6 self.fields['comment'].label = '评论' self.fields['name'].label = '名字' self.fields['url'].label = '网址'
def test_post_comment(self): """ コメント投稿用のビューからコメントが投稿できる """ target = PersonaFactory() self.assertTrue( self.client.login(username=self.user, password='******')) url = django_comments.get_form_target() form = KawazCommentForm(target) dict = {field.html_name: field.value() for field in form} dict['comment'] = "Hello" r = self.client.post( url, { 'object_pk': dict['object_pk'], 'content_type': dict['content_type'], 'security_hash': dict['security_hash'], 'timestamp': dict['timestamp'], 'comment': dict['comment'], }) self.assertEqual(r.status_code, 302) # コメントが生成されている comments = Comment.objects.all() self.assertEqual(comments.count(), 1)
def test_not_allowed_empty_comment(self): """ 空コメントを投稿できない """ target = PersonaFactory() self.assertTrue( self.client.login(username=self.user, password='******')) url = django_comments.get_form_target() form = KawazCommentForm(target) dict = {field.html_name: field.value() for field in form} r = self.client.post( url, { 'object_pk': dict['object_pk'], 'content_type': dict['content_type'], 'security_hash': dict['security_hash'], 'timestamp': dict['timestamp'], 'comment': '', # 空文字 }) self.assertEqual(r.status_code, 200) # コメントが生成できない comments = Comment.objects.all() self.assertEqual(comments.count(), 0)
def testGetFormTarget(self): self.assertEqual(django_comments.get_form_target(), "/post/")
def form_action(self): return get_form_target() # reads get_form_target from COMMENTS_APP