def test_notify_followers_dupes(self): # first of all confirm Bob's comment otherwise it doesn't reach DB confirm_comment_url(self.key) # then put in play pull-request-15's assert... # https://github.com/danirus/django-comments-xtd/pull/15 diary = Diary.objects.create(body='Lorem ipsum', allow_comments=True) self.assertEqual(diary.pk, self.article.pk) self.form = django_comments.get_form()(diary) data = { "name": "Charlie", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal eine kleine..." } data.update(self.form.initial) response = post_diary_comment(data, diary_entry=diary) self.assertEqual(response.status_code, 302) self.assertTrue(response.url.startswith('/comments/posted/?c=')) self.key = str( re.search(r'http://.+/confirm/(?P<key>[\S]+)/', self.mock_mailer.call_args[0][1]).group("key")) # 1) confirmation for Bob (sent in `setUp()`) # 2) confirmation for Charlie self.assertEqual(self.mock_mailer.call_count, 2) response = confirm_comment_url(self.key) self.assertEqual(response.status_code, 302) self.assertTrue(response.url.startswith('/comments/cr/')) self.assertEqual(self.mock_mailer.call_count, 2) self.form = django_comments.get_form()(self.article) data = { "name": "Alice", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal iene kleine..." } data.update(self.form.initial) response = post_article_comment(data, article=self.article) self.assertEqual(response.status_code, 302) self.assertTrue(response.url.startswith('/comments/posted/?c=')) self.assertEqual(self.mock_mailer.call_count, 3) self.key = re.search(r'http://.+/confirm/(?P<key>[\S]+)/', self.mock_mailer.call_args[0][1]).group("key") confirm_comment_url(self.key) self.assertEqual(self.mock_mailer.call_count, 4) self.assertTrue( self.mock_mailer.call_args[0][3] == ["*****@*****.**"]) self.assertTrue(self.mock_mailer.call_args[0][1].find( "There is a new comment following up yours.") > -1)
def test_notify_comment_followers(self): # send a couple of comments to the article with followup=True and check # that when the second comment is confirmed a followup notification # email is sent to the user who sent the first comment self.assertEqual(self.mock_mailer.call_count, 1) self.get_confirm_comment_url(self.key) # no comment followers yet: self.assertEqual(self.mock_mailer.call_count, 1) # send 2nd comment self.form = django_comments.get_form()(self.article) data = { "name": "Alice", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal eine kleine..." } data.update(self.form.initial) self.response = self.client.post(reverse("comments-post-comment"), data=data) self.assertEqual(self.mock_mailer.call_count, 2) self.key = re.search(r'http://.+/confirm/(?P<key>[\S]+)', self.mock_mailer.call_args[0][1]).group("key") self.get_confirm_comment_url(self.key) self.assertEqual(self.mock_mailer.call_count, 3) self.assert_(self.mock_mailer.call_args[0][3] == ["*****@*****.**"]) self.assert_(self.mock_mailer.call_args[0][1].find( "There is a new comment following up yours.") > -1)
def test_no_notification_for_same_user_email(self): # test that a follow-up user_email don't get a notification when # sending another email to the thread self.assertEqual(self.mock_mailer.call_count, 1) self.get_confirm_comment_url(self.key) # confirm Bob's comment # no comment followers yet: self.assertEqual(self.mock_mailer.call_count, 1) # send Bob's 2nd comment self.form = django_comments.get_form()(self.article) data = { "name": "Alice", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Bob's comment he shouldn't get notified about" } data.update(self.form.initial) self.response = self.client.post(reverse("comments-post-comment"), data=data) self.assertEqual(self.mock_mailer.call_count, 2) self.key = re.search(r'http://.+/confirm/(?P<key>[\S]+)', self.mock_mailer.call_args[0][1]).group("key") self.get_confirm_comment_url(self.key) self.assertEqual(self.mock_mailer.call_count, 2)
def setUp(self): patcher = patch('django_comments_xtd.views.send_mail') self.mock_mailer = patcher.start() self.article = Article.objects.create(title="October", slug="october", body="What I did on October...") self.form = django_comments.get_form()(self.article)
def setUp(self): patcher = patch('django_comments_xtd.views.send_mail') self.mock_mailer = patcher.start() # Create random string so that it's harder for zlib to compress content = ''.join(random.choice(string.printable) for _ in range(6096)) self.article = Article.objects.create(title="September", slug="september", body="In September..." + content) self.form = django_comments.get_form()(self.article) data = { "name": "Bob", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal iene kleine..." } data.update(self.form.initial) response = post_article_comment(data, self.article) self.assertTrue(self.mock_mailer.call_count == 1) self.key = str( re.search(r'http://.+/confirm/(?P<key>[\S]+)/', self.mock_mailer.call_args[0][1]).group("key")) self.addCleanup(patcher.stop)
def setUp(self): patcher = patch('django_comments_xtd.views.send_mail') self.mock_mailer = patcher.start() self.article = Article.objects.create( title="September", slug="september", body="What I did on September...") self.form = django_comments.get_form()(self.article) data = { "name": "Bob", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal iene kleine..." } data.update(self.form.initial) self.response = self.client.post(reverse("comments-post-comment"), data=data) self.assert_(self.mock_mailer.call_count == 1) self.key = str( re.search(r'http://.+/confirm/(?P<key>[\S]+)', self.mock_mailer.call_args[0][1]).group("key")) self.addCleanup(patcher.stop)
def setUp(self): # Creates an article and send two comments to the article with follow-up # notifications. First comment doesn't have to send any notification. # Second comment has to send one notification (to Bob). self.factory = RequestFactory() patcher = patch('django_comments_xtd.views.send_mail') self.mock_mailer = patcher.start() self.article = Article.objects.create(title="September", slug="september", body="John's September") self.form = django_comments.get_form()(self.article) # Bob sends 1st comment to the article with follow-up data = { "name": "Bob", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Nice September you had..." } data.update(self.form.initial) response = post_article_comment(data, self.article) self.assertEqual(response.status_code, 302) self.assertTrue(response.url.startswith('/comments/posted/?c=')) self.assertTrue(self.mock_mailer.call_count == 1) bobkey = str( re.search(r'http://.+/confirm/(?P<key>[\S]+)/', self.mock_mailer.call_args[0][1]).group("key")) confirm_comment_url(bobkey) # confirm Bob's comment # Alice sends 2nd comment to the article with follow-up data = { "name": "Alice", "email": "*****@*****.**", "followup": True, "reply_to": 1, "level": 1, "order": 1, "comment": "Yeah, great photos" } data.update(self.form.initial) response = post_article_comment(data, self.article) self.assertEqual(response.status_code, 302) self.assertTrue(response.url.startswith('/comments/posted/?c=')) self.assertTrue(self.mock_mailer.call_count == 2) alicekey = str( re.search(r'http://.+/confirm/(?P<key>[\S]+)/', self.mock_mailer.call_args[0][1]).group("key")) confirm_comment_url(alicekey) # confirm Alice's comment # Bob receives a follow-up notification self.assertTrue(self.mock_mailer.call_count == 3) self.bobs_mutekey = str( re.search(r'http://.+/mute/(?P<key>[\S]+)/', self.mock_mailer.call_args[0][1]).group("key")) self.addCleanup(patcher.stop)
def setUp(self): patcher_app1 = patch(send_mail) patcher_app2 = patch('django_comments_xtd.views.send_mail') self.mailer_app1 = patcher_app1.start() self.mailer_app2 = patcher_app2.start() diary_entry = Diary.objects.create(body="What I did on October...", allow_comments=True, publish=datetime.now()) self.form = django_comments.get_form()(diary_entry)
def setUp(self): patcher_app1 = patch(send_mail) patcher_app2 = patch('django_comments_xtd.views.send_mail') self.mailer_app1 = patcher_app1.start() self.mailer_app2 = patcher_app2.start() diary_entry = Diary.objects.create( body="What I did Yesterday...", allow_comments=True, publish=datetime.now() - timedelta(days=5)) self.form = django_comments.get_form()(diary_entry)
def test_notify_followers_dupes(self): # first of all confirm Bob's comment otherwise it doesn't reach DB self.get_confirm_comment_url(self.key) # then put in play pull-request-15's assert... # https://github.com/danirus/django-comments-xtd/pull/15 diary = Diary.objects.create( body='Lorem ipsum', allow_comments=True ) self.assertEqual(diary.pk, self.article.pk) self.form = django_comments.get_form()(diary) data = {"name": "Charlie", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal eine kleine..."} data.update(self.form.initial) self.response = self.client.post(reverse("comments-post-comment"), data=data) self.key = str(re.search(r'http://.+/confirm/(?P<key>[\S]+)', self.mock_mailer.call_args[0][1]).group("key")) # 1) confirmation for Bob (sent in `setUp()`) # 2) confirmation for Charlie self.assertEqual(self.mock_mailer.call_count, 2) self.get_confirm_comment_url(self.key) self.assertEqual(self.mock_mailer.call_count, 2) self.form = django_comments.get_form()(self.article) data = {"name": "Alice", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal iene kleine..."} data.update(self.form.initial) self.response = self.client.post(reverse("comments-post-comment"), data=data) self.assertEqual(self.mock_mailer.call_count, 3) self.key = re.search(r'http://.+/confirm/(?P<key>[\S]+)', self.mock_mailer.call_args[0][1]).group("key") self.get_confirm_comment_url(self.key) self.assertEqual(self.mock_mailer.call_count, 4) self.assert_(self.mock_mailer.call_args[0][3] == ["*****@*****.**"]) self.assert_(self.mock_mailer.call_args[0][1].find( "There is a new comment following up yours.") > -1)
def setUp(self): diary_entry = Diary.objects.create( body="What I did on October...", allow_comments=True, publish=datetime.now()) form = django_comments.get_form()(diary_entry) self.user = User.objects.create_user("bob", "*****@*****.**", "pwd") data = {"name": "Bob", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal eine kleine..."} data.update(form.initial) post_diary_comment(data, diary_entry, auth_user=self.user)
def setUp(self): patcher = patch('django_comments_xtd.views.send_mail') self.mock_mailer = patcher.start() self.article = Article.objects.create( title="October", slug="october", body="What I did on October...") self.form = django_comments.get_form()(self.article) # Remove the following fields on purpose, as we don't know them and # therefore we don't send them when using the web API (unless when) # using the JavaScript plugin, but that is not the case being tested # here. for field_name in ['security_hash', 'timestamp']: self.form.initial.pop(field_name)
def setUp(self): # Create an article and send a comment. Test method will chech headers # to see wheter messages has multiparts or not. patcher = patch('django_comments_xtd.views.send_mail') self.mock_mailer = patcher.start() self.article = Article.objects.create( title="September", slug="september", body="John's September") self.form = django_comments.get_form()(self.article) # Bob sends 1st comment to the article with follow-up self.data = {"name": "Bob", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Nice September you had..."} self.data.update(self.form.initial)
def setUp(self): # Create an article and send a comment. Test method will chech headers # to see wheter messages has multiparts or not. patcher = patch('django_comments_xtd.views.send_mail') self.mock_mailer = patcher.start() self.article = Article.objects.create( title="September", slug="september", body="John's September") self.form = django_comments.get_form()(self.article) # Bob sends 1st comment to the article with follow-up self.data = {"name": "Bob", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Nice September you had..."} self.data.update(self.form.initial)
def setUp(self): diary_entry = Diary.objects.create( body="What I did on October...", allow_comments=True, publish=datetime.now()) self.form = django_comments.get_form()(diary_entry) User.objects.create_user("bob", "*****@*****.**", "pwd") self.client.login(username="******", password="******") data = {"name": "Bob", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal eine kleine..."} data.update(self.form.initial) self.response = self.client.post(reverse("comments-post-comment"), data=data, follow=True)
def test_get_comment_create_data(self): # as it's used in django_comments.views.comments data = {"name": "Daniel", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal iene kleine..."} data.update(self.form.initial) form = django_comments.get_form()(self.article, data) self.assertTrue(self.form.security_errors() == {}) self.assertTrue(self.form.errors == {}) comment = form.get_comment_object() # it does have the new field 'followup' self.assertTrue("followup" in comment)
def test_get_comment_create_data(self): # as it's used in django_comments.views.comments data = {"name": "Daniel", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal iene kleine..."} data.update(self.form.initial) form = django_comments.get_form()(self.article, data) self.assert_(self.form.security_errors() == {}) self.assert_(self.form.errors == {}) comment = form.get_comment_object() # it does have the new field 'followup' self.assert_("followup" in comment)
def setUp(self): patcher = patch('django_comments_xtd.views.send_mail') self.mock_mailer = patcher.start() self.article = Article.objects.create(title="September", slug="september", body="What I did on September...") self.form = django_comments.get_form()(self.article) data = {"name": "Bob", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal iene kleine..."} data.update(self.form.initial) self.response = self.client.post(reverse("comments-post-comment"), data=data) self.assert_(self.mock_mailer.call_count == 1) self.key = str(re.search(r'http://.+/confirm/(?P<key>[\S]+)', self.mock_mailer.call_args[0][1]).group("key")) self.addCleanup(patcher.stop)
def test_get_comment_create_data(self): # as it's used in django_comments.views.comments data = {"name":"Daniel", "email":"*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment":"Es war einmal iene kleine..." } data.update(self.form.initial) form = django_comments.get_form()(self.article, data) self.assert_(self.form.security_errors() == {}) self.assert_(self.form.errors == {}) comment = form.get_comment_object() # it does have the new field 'followup' self.assert_("followup" in comment) # and as long as settings.COMMENTS_XTD_CONFIRM_EMAIL is True # is_public is set to False until receive the user confirmation self.assert_(comment.is_public == False)
def test_no_notification_for_same_user_email(self): # test that a follow-up user_email don't get a notification when # sending another email to the thread self.assertEqual(self.mock_mailer.call_count, 1) self.get_confirm_comment_url(self.key) # confirm Bob's comment # no comment followers yet: self.assertEqual(self.mock_mailer.call_count, 1) # send Bob's 2nd comment self.form = django_comments.get_form()(self.article) data = {"name": "Alice", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Bob's comment he shouldn't get notified about"} data.update(self.form.initial) self.response = self.client.post(reverse("comments-post-comment"), data=data) self.assertEqual(self.mock_mailer.call_count, 2) self.key = re.search(r'http://.+/confirm/(?P<key>[\S]+)', self.mock_mailer.call_args[0][1]).group("key") self.get_confirm_comment_url(self.key) self.assertEqual(self.mock_mailer.call_count, 2)
def setUp(self): # Creates an article and send two comments to the article with follow-up # notifications. First comment doesn't have to send any notification. # Second comment has to send one notification (to Bob). patcher = patch('django_comments_xtd.views.send_mail') self.mock_mailer = patcher.start() self.article = Article.objects.create( title="September", slug="september", body="John's September") self.form = django_comments.get_form()(self.article) # Bob sends 1st comment to the article with follow-up data = {"name": "Bob", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Nice September you had..." } data.update(self.form.initial) response = self.client.post(reverse("comments-post-comment"), data=data) self.assert_(self.mock_mailer.call_count == 1) bobkey = str(re.search(r'http://.+/confirm/(?P<key>[\S]+)', self.mock_mailer.call_args[0][1]).group("key")) self.get_confirm_comment_url(bobkey) # confirm Bob's comment # Alice sends 2nd comment to the article with follow-up data = {"name": "Alice", "email": "*****@*****.**", "followup": True, "reply_to": 1, "level": 1, "order": 1, "comment": "Yeah, great photos" } data.update(self.form.initial) response = self.client.post(reverse("comments-post-comment"), data=data) self.assert_(self.mock_mailer.call_count == 2) alicekey = str(re.search(r'http://.+/confirm/(?P<key>[\S]+)', self.mock_mailer.call_args[0][1]).group("key")) self.get_confirm_comment_url(alicekey) # confirm Alice's comment # Bob receives a follow-up notification self.assert_(self.mock_mailer.call_count == 3) self.bobs_mutekey = str(re.search( r'http://.+/mute/(?P<key>[\S]+)', self.mock_mailer.call_args[0][1]).group("key")) self.addCleanup(patcher.stop)
def test_notify_comment_followers(self): # send a couple of comments to the article with followup=True and check # that when the second comment is confirmed a followup notification # email is sent to the user who sent the first comment self.assertEqual(self.mock_mailer.call_count, 1) self.get_confirm_comment_url(self.key) # no comment followers yet: self.assertEqual(self.mock_mailer.call_count, 1) # send 2nd comment self.form = django_comments.get_form()(self.article) data = {"name":"Alice", "email":"*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment":"Es war einmal eine kleine..." } data.update(self.form.initial) self.response = self.client.post(reverse("comments-post-comment"), data=data) self.assertEqual(self.mock_mailer.call_count, 2) self.key = re.search(r'http://.+/confirm/(?P<key>[\S]+)', self.mock_mailer.call_args[0][1]).group("key") self.get_confirm_comment_url(self.key) self.assertEqual(self.mock_mailer.call_count, 3) self.assert_(self.mock_mailer.call_args[0][3] == ["*****@*****.**"]) self.assert_(self.mock_mailer.call_args[0][1].find("There is a new comment following up yours.") > -1)
def test_get_form(self): # check function django_comments_xtd.get_form retrieves the due class self.assert_(django_comments.get_form() == XtdCommentForm)
def setUp(self): patcher = patch('django_comments_xtd.views.send_mail') self.mock_mailer = patcher.start() self.article = Article.objects.create( title="October", slug="october", body="What I did on October...") self.form = django_comments.get_form()(self.article)
def setUp(self): self.article = Article.objects.create(title="September", slug="september", body="What I did on September...") self.form = django_comments.get_form()(self.article)
def test_followup_preunchecked(self): # Have to update form object to re-initialize 'followup' checkbox # with settings. self.form = django_comments.get_form()(self.article) self.assertEqual(self.form.fields['followup'].initial, True)
def setUp(self): self.article = Article.objects.create( title="September", slug="september", body="What I did on September...") self.form = django_comments.get_form()(self.article)
def test_get_form(self): # check function django_comments_xtd.get_form retrieves the due class self.assert_(django_comments.get_form() == XtdCommentForm)
def test_email_field_is_optional(self): article = Article.objects.create() form = django_comments.get_form()(article) self.assertFalse(form.fields['email'].required)