<s> """<STR_LIT>""" <EOL> from django . core import mail <EOL> from django . test import TestCase <EOL> from django . utils import timezone <EOL> from django . contrib . sites . models import Site <EOL> from django . core . urlresolvers import reverse <EOL> from django . test . utils import override_settings <EOL> from django . contrib . auth . tests . utils import skipIfCustomUser <EOL> import django_comments as comments <EOL> from django_comments . forms import CommentForm <EOL> from django_comments . moderation import moderator as moderator_stack <EOL> from zinnia . models . entry import Entry <EOL> from zinnia . models . author import Author <EOL> from zinnia . managers import PUBLISHED <EOL> from zinnia . moderator import EntryCommentModerator <EOL> from zinnia . signals import connect_discussion_signals <EOL> from zinnia . signals import disconnect_discussion_signals <EOL> from zinnia . signals import disconnect_entry_signals <EOL> @ skipIfCustomUser <EOL> @ override_settings ( <EOL> TEMPLATES = [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> } <EOL> } <EOL> ] <EOL> ) <EOL> class CommentModeratorTestCase ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> disconnect_entry_signals ( ) <EOL> disconnect_discussion_signals ( ) <EOL> self . site = Site . objects . get_current ( ) <EOL> self . author = Author . objects . create ( username = '<STR_LIT>' , <EOL> email = '<STR_LIT>' ) <EOL> params = { '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT:content>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:status>' : PUBLISHED } <EOL> self . entry = Entry . objects . create ( ** params ) <EOL> self . entry . sites . add ( self . site ) <EOL> self . entry . authors . add ( self . author ) <EOL> def test_email ( self ) : <EOL> comment = comments . get_model ( ) . objects . create ( <EOL> comment = '<STR_LIT>' , user = self . author , is_public = True , <EOL> content_object = self . entry , submit_date = timezone . now ( ) , <EOL> site = self . site ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> moderator = EntryCommentModerator ( Entry ) <EOL> moderator . email_reply = False <EOL> moderator . email_authors = False <EOL> moderator . mail_comment_notification_recipients = [ ] <EOL> moderator . email ( comment , self . entry , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> moderator . email_reply = True <EOL> moderator . email_authors = True <EOL> moderator . mail_comment_notification_recipients = [ '<STR_LIT>' ] <EOL> moderator . email ( comment , self . entry , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> def test_do_email_notification ( self ) : <EOL> comment = comments . get_model ( ) . objects . create ( <EOL> comment = '<STR_LIT>' , user = self . author , is_public = True , <EOL> content_object = self . entry , submit_date = timezone . now ( ) , <EOL> site = self . site ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> moderator = EntryCommentModerator ( Entry ) <EOL> moderator . mail_comment_notification_recipients = [ '<STR_LIT>' ] <EOL> moderator . do_email_notification ( comment , self . entry , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> def test_do_email_authors ( self ) : <EOL> comment = comments . get_model ( ) . objects . create ( <EOL> comment = '<STR_LIT>' , user = self . author , is_public = True , <EOL> content_object = self . entry , submit_date = timezone . now ( ) , <EOL> site = self . site ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> moderator = EntryCommentModerator ( Entry ) <EOL> moderator . email_authors = True <EOL> moderator . mail_comment_notification_recipients = [ <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> moderator . do_email_authors ( comment , self . entry , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> moderator . mail_comment_notification_recipients = [ ] <EOL> moderator . do_email_authors ( comment , self . entry , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> def test_do_email_authors_without_email ( self ) : <EOL> """<STR_LIT>""" <EOL> comment = comments . get_model ( ) . objects . create ( <EOL> comment = '<STR_LIT>' , user = self . author , is_public = True , <EOL> content_object = self . entry , submit_date = timezone . now ( ) , <EOL> site = self . site ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> moderator = EntryCommentModerator ( Entry ) <EOL> moderator . email_authors = True <EOL> moderator . mail_comment_notification_recipients = [ ] <EOL> contributor = Author . objects . create ( username = '<STR_LIT>' , <EOL> email = '<STR_LIT>' ) <EOL> self . entry . authors . add ( contributor ) <EOL> moderator . do_email_authors ( comment , self . entry , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> set ( mail . outbox [ <NUM_LIT:0> ] . to ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> mail . outbox = [ ] <EOL> contributor . email = '<STR_LIT>' <EOL> contributor . save ( ) <EOL> moderator . do_email_authors ( comment , self . entry , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( mail . outbox [ <NUM_LIT:0> ] . to , [ '<STR_LIT>' ] ) <EOL> def test_do_email_reply ( self ) : <EOL> comment = comments . get_model ( ) . objects . create ( <EOL> comment = '<STR_LIT>' , user = self . author , is_public = True , <EOL> content_object = self . entry , submit_date = timezone . now ( ) , <EOL> site = self . site ) <EOL> moderator = EntryCommentModerator ( Entry ) <EOL> moderator . email_notification_reply = True <EOL> moderator . mail_comment_notification_recipients = [ <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> moderator . do_email_reply ( comment , self . entry , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> comment = comments . get_model ( ) . objects . create ( <EOL> comment = '<STR_LIT>' , user_email = '<STR_LIT>' , <EOL> content_object = self . entry , is_public = True , <EOL> submit_date = timezone . now ( ) , site = self . site ) <EOL> moderator . do_email_reply ( comment , self . entry , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> comment = comments . get_model ( ) . objects . create ( <EOL> comment = '<STR_LIT>' , user_email = '<STR_LIT>' , <EOL> content_object = self . entry , is_public = True , <EOL> submit_date = timezone . now ( ) , site = self . site ) <EOL> moderator . do_email_reply ( comment , self . entry , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( mail . outbox [ <NUM_LIT:0> ] . bcc , [ '<STR_LIT>' ] ) <EOL> comment = comments . get_model ( ) . objects . create ( <EOL> comment = '<STR_LIT>' , user = self . author , is_public = True , <EOL> content_object = self . entry , submit_date = timezone . now ( ) , <EOL> site = self . site ) <EOL> moderator . do_email_reply ( comment , self . entry , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( <EOL> set ( mail . outbox [ <NUM_LIT:1> ] . bcc ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> def test_moderate ( self ) : <EOL> comment = comments . get_model ( ) . objects . create ( <EOL> comment = '<STR_LIT>' , user = self . author , is_public = True , <EOL> content_object = self . entry , submit_date = timezone . now ( ) , <EOL> site = self . site ) <EOL> moderator = EntryCommentModerator ( Entry ) <EOL> moderator . auto_moderate_comments = True <EOL> moderator . spam_checker_backends = ( ) <EOL> self . assertTrue ( moderator . moderate ( comment , self . entry , '<STR_LIT>' ) ) <EOL> moderator . auto_moderate_comments = False <EOL> self . assertFalse ( moderator . moderate ( comment , self . entry , '<STR_LIT>' ) ) <EOL> moderator . spam_checker_backends = ( <EOL> '<STR_LIT>' , ) <EOL> self . assertTrue ( moderator . moderate ( comment , self . entry , '<STR_LIT>' ) ) <EOL> def test_moderate_comment_on_entry_without_author ( self ) : <EOL> self . entry . authors . clear ( ) <EOL> comment = comments . get_model ( ) . objects . create ( <EOL> comment = '<STR_LIT>' , user = self . author , is_public = True , <EOL> content_object = self . entry , submit_date = timezone . now ( ) , <EOL> site = self . site ) <EOL> moderator = EntryCommentModerator ( Entry ) <EOL> moderator . auto_moderate_comments = False <EOL> moderator . spam_checker_backends = ( <EOL> '<STR_LIT>' , ) <EOL> self . assertTrue ( moderator . moderate ( comment , self . entry , '<STR_LIT>' ) ) <EOL> def test_integrity_error_on_duplicate_spam_comments ( self ) : <EOL> class AllIsSpamModerator ( EntryCommentModerator ) : <EOL> spam_checker_backends = [ <EOL> '<STR_LIT>' ] <EOL> moderator_stack . unregister ( Entry ) <EOL> moderator_stack . register ( Entry , AllIsSpamModerator ) <EOL> datas = { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> f = CommentForm ( self . entry ) <EOL> datas . update ( f . initial ) <EOL> url = reverse ( '<STR_LIT>' ) <EOL> self . assertEqual ( self . entry . comment_count , <NUM_LIT:0> ) <EOL> connect_discussion_signals ( ) <EOL> self . client . post ( url , datas ) <EOL> self . client . post ( url , datas ) <EOL> disconnect_discussion_signals ( ) <EOL> self . assertEqual ( comments . get_model ( ) . objects . count ( ) , <NUM_LIT:1> ) <EOL> entry_reloaded = Entry . objects . get ( pk = self . entry . pk ) <EOL> self . assertEqual ( entry_reloaded . comment_count , <NUM_LIT:0> ) <EOL> def test_comment_count_denormalization ( self ) : <EOL> class AllIsSpamModerator ( EntryCommentModerator ) : <EOL> spam_checker_backends = [ <EOL> '<STR_LIT>' ] <EOL> class NoMailNoSpamModerator ( EntryCommentModerator ) : <EOL> def email ( self , * ka , ** kw ) : <EOL> pass <EOL> def moderate ( self , * ka , ** kw ) : <EOL> return False <EOL> datas = { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> f = CommentForm ( self . entry ) <EOL> datas . update ( f . initial ) <EOL> url = reverse ( '<STR_LIT>' ) <EOL> moderator_stack . unregister ( Entry ) <EOL> moderator_stack . register ( Entry , AllIsSpamModerator ) <EOL> self . assertEqual ( self . entry . comment_count , <NUM_LIT:0> ) <EOL> connect_discussion_signals ( ) <EOL> self . client . post ( url , datas ) <EOL> entry_reloaded = Entry . objects . get ( pk = self . entry . pk ) <EOL> self . assertEqual ( entry_reloaded . comment_count , <NUM_LIT:0> ) <EOL> moderator_stack . unregister ( Entry ) <EOL> moderator_stack . register ( Entry , NoMailNoSpamModerator ) <EOL> datas [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . client . post ( url , datas ) <EOL> disconnect_discussion_signals ( ) <EOL> entry_reloaded = Entry . objects . get ( pk = self . entry . pk ) <EOL> self . assertEqual ( entry_reloaded . comment_count , <NUM_LIT:1> ) </s>
