def test_mark_as_pending_due_to_insufficient_previously_approved_comments( self, get_scores): # Index: NOT OK # Membership: NOT OK # Approved comments: NOT OK # Content owner: NOT OK # Auto approve: NOT OK get_scores.return_value = {'user_scores_index': 0} image = Generators.image() author = Generators.user() for i in range(0, 2): comment = NestedCommentsGenerators.comment( author=author, content_type=ContentType.objects.get_for_model(Image), oject_id=image.id, ) comment.pending_moderation = None comment.save() comment = NestedCommentsGenerators.comment( author=author, target=image, ) self.assertTrue(comment.pending_moderation)
def test_do_not_mark_as_pending_due_to_sufficient_previously_approved_comments( self, get_scores): # Index: NOT OK # Membership: NOT OK # Approved comments: OK # Content owner: NOT OK # Auto approve: NOT OK get_scores.return_value = {'user_scores_index': 0} image = Generators.image() author = Generators.user() for i in range(0, 3): comment = NestedCommentsGenerators.comment( author=author, target=image, ) comment.pending_moderation = None comment.save() comment = NestedCommentsGenerators.comment( author=author, target=image, ) self.assertIsNone(comment.pending_moderation)
def test_send_notifications_reply(self, add_story, push_notification, get_scores): get_scores.return_value = {'user_scores_index': 2} image = Generators.image() commenter = Generators.user() commenter2 = Generators.user() comment = NestedCommentsGenerators.comment(author=commenter, target=image) push_notification.assert_called_with(mock.ANY, 'new_comment', mock.ANY) add_story.assert_called_with(comment.author, verb='VERB_COMMENTED_IMAGE', action_object=comment, target=comment.content_object) push_notification.reset_mock() add_story.resetMock() comment = NestedCommentsGenerators.comment(author=commenter2, target=image, parent=comment) calls = [ mock.call(mock.ANY, 'new_comment', mock.ANY), mock.call(mock.ANY, 'new_comment_reply', mock.ANY), ] push_notification.assert_has_calls(calls, any_order=True) add_story.assert_called_with(comment.author, verb='VERB_COMMENTED_IMAGE', action_object=comment, target=comment.content_object)
def test_send_notifications_reply_to_image_owner_with_mention_but_no_notification( self, add_story, push_notification_from_signals, push_notification, get_scores): get_scores.return_value = {'user_scores_index': 2} image = Generators.image() commenter = Generators.user() comment = NestedCommentsGenerators.comment(author=image.user, target=image) with self.assertRaises(AssertionError): push_notification.assert_called_with(mock.ANY, commenter, 'new_comment', mock.ANY) add_story.assert_called_with(comment.author, verb='VERB_COMMENTED_IMAGE', action_object=comment, target=comment.content_object) push_notification.reset_mock() push_notification_from_signals.reset_mock() add_story.resetMock() comment = NestedCommentsGenerators.comment( author=commenter, target=image, parent=comment, text='[quote="%s"]Foo[/quote]' % image.user) with self.assertRaises(AssertionError): push_notification.assert_called_with(mock.ANY, comment.author, 'new_comment', mock.ANY) calls = [ mock.call(mock.ANY, commenter, 'new_comment_reply', mock.ANY), ] push_notification.assert_has_calls(calls, any_order=True) # This is called anyway but will have no effect since there is not NoticeSetting for this user. The goal of this # test is to check that the 'new_comment' notification is sent. push_notification_from_signals.assert_called_with( [image.user], commenter, 'new_comment_mention', mock.ANY) add_story.assert_called_with(comment.author, verb='VERB_COMMENTED_IMAGE', action_object=comment, target=comment.content_object)
def test_can_like_comment_same_user(self, get_scores, is_free): user = Generators.user() comment = NestedCommentsGenerators.comment(author=user) is_free.return_value = False self.assertFalse(UserService(user).can_like(comment))
def test_send_notifications_top_level(self, add_story, push_notification, get_scores): get_scores.return_value = {'user_scores_index': 2} image = Generators.image() commenter = Generators.user() comment = NestedCommentsGenerators.comment(author=commenter, target=image) push_notification.assert_called_with(mock.ANY, commenter, 'new_comment', mock.ANY) with self.assertRaises(AssertionError): push_notification.assert_called_with(mock.ANY, commenter, 'new_comment_reply', mock.ANY) with self.assertRaises(AssertionError): push_notification.assert_called_with(mock.ANY, commenter, 'new_comment_mention', mock.ANY) add_story.assert_called_with(comment.author, verb='VERB_COMMENTED_IMAGE', action_object=comment, target=comment.content_object)
def test_html(self): comment = NestedCommentsGenerators.comment(text='[b]Test comment[/b]') serializer = NestedCommentSerializer(instance=comment) representation = serializer.to_representation(comment) self.assertEqual(comment.text, representation.get('text')) self.assertEqual('<strong>Test comment</strong>', representation.get('html'))
def test_send_notifications_top_level_with_mention_and_notification( self, add_story, push_notification_from_signals, push_notification, get_scores): get_scores.return_value = {'user_scores_index': 2} image = Generators.image() commenter = Generators.user() NoticeSetting.for_user( image.user, NoticeType.objects.get(label='new_comment_mention'), 1) comment = NestedCommentsGenerators.comment( author=commenter, target=image, text='[quote="%s"]Foo[/quote]' % image.user.username) with self.assertRaises(AssertionError): push_notification.assert_called_with(mock.ANY, commenter, 'new_comment', mock.ANY) with self.assertRaises(AssertionError): push_notification.assert_called_with(mock.ANY, commenter, 'new_comment_reply', mock.ANY) push_notification_from_signals.assert_called_with( [image.user], commenter, 'new_comment_mention', mock.ANY) add_story.assert_called_with(comment.author, verb='VERB_COMMENTED_IMAGE', action_object=comment, target=comment.content_object)
def test_non_pending_moderation_does_send_mentions(self, push_notification, get_mentions): user = Generators.user() get_mentions.return_value = [user.get_username()] comment = NestedCommentsGenerators.comment() push_notification.assert_called_with([user], comment.author, 'new_comment_mention', mock.ANY)
def test_can_like_comment_ok(self, get_scores, is_free): get_scores.return_value = {'user_scores_index': 0} is_free.return_value = False user = Generators.user() comment = NestedCommentsGenerators.comment() self.assertTrue(UserService(user).can_like(comment))
def test_send_notifications_reply_with_mention_and_notification( self, add_story, push_notification_from_signals, push_notification, get_scores): get_scores.return_value = {'user_scores_index': 2} image = Generators.image() commenter = Generators.user() replier = Generators.user() NoticeSetting.for_user( commenter, NoticeType.objects.get(label='new_comment_mention'), 1) comment = NestedCommentsGenerators.comment(author=commenter, target=image) push_notification.assert_called_with([image.user], commenter, 'new_comment', mock.ANY) add_story.assert_called_with(comment.author, verb='VERB_COMMENTED_IMAGE', action_object=comment, target=comment.content_object) push_notification.reset_mock() push_notification_from_signals.reset_mock() add_story.resetMock() comment = NestedCommentsGenerators.comment( author=replier, target=image, parent=comment, text='[quote="%s"]Foo[/quote]' % commenter.username) push_notification.assert_has_calls([ mock.call([image.user], replier, 'new_comment', mock.ANY), ], any_order=True) push_notification_from_signals.assert_called_with( [commenter], replier, 'new_comment_mention', mock.ANY) add_story.assert_called_with(comment.author, verb='VERB_COMMENTED_IMAGE', action_object=comment, target=comment.content_object)
def test_pending_moderation_does_not_send_mentions(self, push_notification, get_mentions): user = Generators.user() get_mentions.return_value = [user.get_username()] comment = NestedCommentsGenerators.comment(pending_moderation=True) with self.assertRaises(AssertionError): push_notification.assert_called_with(mock.ANY, comment.author, 'new_comment_mention', mock.ANY)
def test_rejection_api_login_required(self): client = APIClient() image = Generators.image() comment = NestedCommentsGenerators.comment(target=image, pending_moderation=True) response = client.post( reverse('nested_comments:nestedcomments-report-abuse', args=(comment.pk, ))) self.assertEqual(401, response.status_code)
def test_non_pending_moderation_sends_correct_notifications( self, push_notification, send_notifications, send_moderation_required_email): comment = NestedCommentsGenerators.comment() send_moderation_required_email.assert_not_called() send_notifications.assert_called() with self.assertRaises(AssertionError): push_notification.assert_called_with(mock.ANY, comment.author, 'new_comment_mention', mock.ANY)
def test_send_notifications_pending_moderation(self, add_story, push_notification, get_scores): get_scores.return_value = {'user_scores_index': .5} comment = NestedCommentsGenerators.comment() with self.assertRaises(AssertionError): push_notification.assert_called_with(mock.ANY, mock.ANY, 'new_comment', mock.ANY) with self.assertRaises(AssertionError): add_story.assert_called_with( comment.author, verb='VERB_COMMENTED_IMAGE', action_object=comment, target=comment.content_object)
def test_do_not_mark_as_pending_due_to_index(self, get_scores): # Index: OK # Membership: NOT OK # Approved comments: NOT OK # Content owner: NOT OK # Auto approve: NOT OK get_scores.return_value = {'user_scores_index': 10} image = Generators.image() comment = NestedCommentsGenerators.comment(target=image, ) self.assertIsNone(comment.pending_moderation)
def test_approval_api_not_pending_moderation(self): client = APIClient() image = Generators.image() comment = NestedCommentsGenerators.comment(target=image, pending_moderation=False) client.force_authenticate(user=image.user) response = client.post( reverse('nested_comments:nestedcomments-approve', args=(comment.pk, ))) self.assertEqual(400, response.status_code)
def test_send_moderation_required_notification_non_pending_moderation( self, push_notification): image = Generators.image() comment = NestedCommentsGenerators.comment(target=image) push_notification.reset_mock() CommentNotificationsService( comment).send_moderation_required_notification() with self.assertRaises(AssertionError): push_notification.assert_called_with( [image.user], None, 'new_image_comment_moderation', mock.ANY)
def test_do_not_mark_as_pending_due_to_domain(self, get_scores): # Index: NOT OK # Membership: NOT OK # Approved comments: NOT OK # Content owner: NOT OK # Auto approve: OK get_scores.return_value = {'user_scores_index': 0} image = Generators.image() author = Generators.user(email='*****@*****.**') comment = NestedCommentsGenerators.comment(target=image, author=author) self.assertIsNone(comment.pending_moderation)
def test_send_moderation_required_notification_non_image_target( self, push_notification): post = Generators.forum_post() comment = NestedCommentsGenerators.comment(target=post, pending_moderation=True) push_notification.reset_mock() CommentNotificationsService( comment).send_moderation_required_notification() with self.assertRaises(AssertionError): push_notification.assert_called_with( [post.user], None, 'new_image_comment_moderation', mock.ANY)
def test_do_not_mark_as_pending_due_to_index(self, get_scores): # Index: OK # Membership: NOT OK # Approved comments: NOT OK get_scores.return_value = {'user_scores_index': 10} image = Generators.image() comment = NestedCommentsGenerators.comment( content_type=ContentType.objects.get_for_model(Image), oject_id=image.id, ) self.assertIsNone(comment.pending_moderation)
def test_nested_comment_post_save_sends_moderation_required_email( self, send_moderation_required_email, send_notifications, get_mentions): comment = NestedCommentsGenerators.comment() comment.pending_moderation = True nested_comment_post_save(None, comment, True) self.assertTrue(get_mentions.called) self.assertTrue(send_notifications.called) self.assertTrue(send_moderation_required_email.called) comment.pending_moderation = False nested_comment_post_save(None, comment, True) self.assertTrue(send_moderation_required_email.called)
def test_do_not_mark_as_pending_due_to_membership(self, get_scores): # Index: NOT OK # Membership: OK # Approved comments: NOT OK get_scores.return_value = {'user_scores_index': 0} image = Generators.image() author = Generators.user() Generators.premium_subscription(author, "AstroBin Premium 2020+") comment = NestedCommentsGenerators.comment( author=author, content_type=ContentType.objects.get_for_model(Image), oject_id=image.id, ) self.assertIsNone(comment.pending_moderation)
def test_send_notifications_shadowban(self, add_story, push_notification, get_scores): get_scores.return_value = {'user_scores_index': 2} image = Generators.image() shadowbanned_user = Generators.user() image.user.userprofile.shadow_bans.add(shadowbanned_user.userprofile) comment = NestedCommentsGenerators.comment(author=shadowbanned_user, target=image) with self.assertRaises(AssertionError): push_notification.assert_called_with(mock.ANY, comment.author, 'new_comment', mock.ANY) with self.assertRaises(AssertionError): add_story.assert_called_with( comment.author, verb='VERB_COMMENTED_IMAGE', action_object=comment, target=comment.content_object)
def test_approval_api_all_ok(self): client = APIClient() image = Generators.image() comment = NestedCommentsGenerators.comment(target=image, pending_moderation=True) client.force_authenticate(user=image.user) response = client.post( reverse('nested_comments:nestedcomments-approve', args=(comment.pk, ))) self.assertEqual(200, response.status_code) comment = NestedComment.objects.get(pk=comment.pk) self.assertFalse(comment.pending_moderation) self.assertEqual(image.user, comment.moderator)
def test_do_not_mark_as_pending_due_to_membership(self, get_scores): # Index: NOT OK # Membership: OK # Approved comments: NOT OK # Content owner: NOT OK # Auto approve: NOT OK get_scores.return_value = {'user_scores_index': 0} image = Generators.image() author = Generators.user() Generators.premium_subscription(author, "AstroBin Premium 2020+") comment = NestedCommentsGenerators.comment( author=author, target=image, ) self.assertIsNone(comment.pending_moderation)