def test_delete_pending(self):
        DeletionPendingMixin.DELETION_PENDING_PERIOD_HOURS = 1
        now = get_now()

        target = create_sample_usertoken()
        token = create_sample_usertoken()

        pending_comment = create_sample_comment(target, token, "hello pending")
        pending_comment.mark_pending_deletion()
        pending_comment.save()

        expired_comment = create_sample_comment(target, token, "hello expired")
        expired_comment.mark_pending_deletion()
        expired_comment.deletion_requested_at = now - timedelta(hours=3)
        expired_comment.save()

        self.assertLengthEquals(Comment.objects.all(), 2)

        delete_expired_models()

        Comment.objects.get(text="hello pending")

        # Original comment should no longer exist, but an empty placeholder should have been created
        self.assertLengthEquals(Comment.objects.all(), 2)

        # Original has gone
        self.assertRaises(
            Comment.DoesNotExist,
            Comment.objects.get,
            text="hello expired",
        )

        # Placeholder exists
        Comment.objects.get(user=None, text="", **get_target_kwargs(target))
Exemplo n.º 2
0
    def test_get_all_with_content_no_user_token(self):
        create_sample_comment(self.target_person_one, self.valid_user,
                              "first comment")
        create_sample_comment(self.target_person_one, self.valid_user,
                              "second comment")
        create_sample_comment(
            self.target_person_one,
            create_sample_usertoken(username="******"),
            "third comment",
        )
        create_sample_comment(self.target_person_two, self.valid_user,
                              "comment on different target!")

        # Votes for actual target
        create_sample_vote(self.target_person_one, self.valid_user, "aye")
        create_sample_vote(self.target_person_one, create_sample_usertoken(),
                           "aye")
        create_sample_vote(self.target_person_one, create_sample_usertoken(),
                           "no")

        # Votes on different target
        create_sample_vote(self.target_person_two, self.valid_user, "aye")
        create_sample_vote(self.target_person_two, create_sample_usertoken(),
                           "no")
        create_sample_vote(self.target_person_two, create_sample_usertoken(),
                           "no")

        # Disable @api_key_required
        settings.DEBUG = True
        response = self.client.get(
            reverse(
                GetSocialAllTests.VIEW_NAME,
                kwargs={
                    "pk": 4837,
                },
            ))
        settings.DEBUG = False

        self.assertEqual(response.status_code, 200)

        self.assertSocialDataEqual(
            response,
            {
                contract.TITLE:
                "Aaron Bell",
                contract.VOTES: {
                    "aye": 2,
                    "no": 1,
                },
                contract.COMMENTS: [
                    ["first comment", _VALID_USER],
                    ["second comment", _VALID_USER],
                    ["third comment", "another_user"],
                ],
                contract.VOTE_TYPE:
                None,
            },
        )
Exemplo n.º 3
0
    def test_create_placeholder_on_comment_deleted_is_correct(self):
        token = create_sample_usertoken(username="******")
        target = create_sample_usertoken()

        created_on = get_now().replace(year=2019,
                                       month=2,
                                       day=25,
                                       hour=15,
                                       minute=32,
                                       second=1,
                                       microsecond=0)
        now = get_now()

        comment = create_sample_comment(
            target=target,
            user=token,
            text="Hello my name is fred",
            created_on=created_on,
            modified_on=created_on,
        )

        comment.delete()

        placeholder_comment: Comment = Comment.objects.first()
        self.assertIsNone(placeholder_comment.user)
        self.assertEqual(placeholder_comment.text, "")
        self.assertEqual(placeholder_comment.target, target)

        # Ensure modified_on has been set to current timestamp when the comment was deleted
        self.assertNotEqual(created_on, now)
        self.assertEqual(placeholder_comment.created_on, created_on)
        self.assertEqual(placeholder_comment.modified_on.date(), now.date())
Exemplo n.º 4
0
    def test_account_deletion_replaces_comments_with_empty_placeholders(self):
        target = create_sample_usertoken()

        token = create_sample_usertoken()
        another_token = create_sample_usertoken()

        for text in SAMPLE_COMMENT_TEXT:
            create_sample_comment(target, token, text)

        # Different user
        create_sample_comment(target, another_token, SAMPLE_COMMENT_TEXT[0])

        self.assertLengthEquals(Comment.objects.all(), 3)

        token.delete()

        # Comments should be replaced by empty placeholders via signals.on_comment_deleted
        self.assertLengthEquals(Comment.objects.all(), 3)
        self.assertLengthEquals(Comment.objects.filter(user=None), 2)
        self.assertLengthEquals(Comment.objects.filter(text=""), 2)
    def setUp(self):
        dates = create_sample_dates(count=10)

        user1 = create_sample_usertoken()
        user2 = create_sample_usertoken()

        boris = create_sample_person(11, "Boris Johnson")
        keir = create_sample_person(23, "Keir Starmer")
        anna = create_sample_person(37, "Anna McMorrin")

        create_sample_vote(boris, user1, "aye", created_on=dates[0])
        create_sample_vote(keir, user1, "no", created_on=dates[1])
        create_sample_vote(boris, user2, "no", created_on=dates[2])
        create_sample_vote(keir, user2, "aye", created_on=dates[3])

        create_sample_comment(boris, user1, created_on=dates[4])
        create_sample_comment(keir, user2, created_on=dates[5])
        create_sample_comment(keir, user1, created_on=dates[6])

        _create_featured_person(anna)