Example #1
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())
Example #2
0
    def test_get_votes(self):
        vote_type_aye = VoteType.objects.create(name="aye")
        vote_type_no = VoteType.objects.create(name="no")

        _create_person_vote(self.valid_user, vote_type_aye)
        _create_person_vote(create_sample_usertoken(), vote_type_aye)
        _create_person_vote(create_sample_usertoken(), vote_type_aye)
        _create_person_vote(create_sample_usertoken(), vote_type_no)
        _create_person_vote(create_sample_usertoken(), vote_type_no)

        # @api_key_required
        response = self.client.get(
            reverse(VoteTests.VIEW_NAME, kwargs={"pk": 4837}), )
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

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

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = json.loads(response.content)

        self.assertEqual(data["aye"], 3)
        self.assertEqual(data["no"], 2)
Example #3
0
    def test_delete_vote(self):
        vote_type_aye = VoteType.objects.create(name="aye")
        vote_type_no = VoteType.objects.create(name="no")

        _create_person_vote(self.valid_user, vote_type_aye)
        _create_person_vote(self.valid_user, vote_type_aye, target_id=1423)
        _create_person_vote(create_sample_usertoken(), vote_type_aye)
        _create_person_vote(create_sample_usertoken(), vote_type_aye)
        _create_person_vote(create_sample_usertoken(), vote_type_no)
        _create_person_vote(create_sample_usertoken(), vote_type_no)

        self.assertLengthEquals(Vote.objects.all(), 6)
        self.assertLengthEquals(Vote.objects.filter(user=self.valid_user), 2)

        response = self.client.delete(
            reverse(VoteTests.VIEW_NAME, kwargs={"pk": 1423}),
            content_type="application/json",
            data={
                contract.USER_TOKEN: self.valid_token.hex,
            },
        )

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
        self.assertLengthEquals(Vote.objects.all(), 5)
        self.assertLengthEquals(Vote.objects.filter(user=self.valid_user), 1)

        vote: Vote = Vote.objects.filter(user=self.valid_user).first()
        self.assertEqual(vote.target, self.target_person)
    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))
Example #5
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,
            },
        )
Example #6
0
    def test_account_deletion_also_deletes_votes_by_account(self):
        target = create_sample_usertoken()

        token = create_sample_usertoken()
        another_token = create_sample_usertoken()

        create_sample_vote(target, token, "aye")
        create_sample_vote(target, another_token, "aye")

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

        token.delete()

        self.assertLengthEquals(Vote.objects.all(), 1)
Example #7
0
    def test_comment_unique_per_user_per_object(self):
        user = UserToken.objects.get(username=_TEST_USERNAME)
        Comment.objects.create(
            user=user,
            text=_COMMENT,
            target_type=ContentType.objects.get_for_model(Person),
            target_id=4837,
        )

        Comment.objects.create(
            user=user,
            text="different comment",
            target_type=ContentType.objects.get_for_model(Person),
            target_id=4837,
        )

        # Different user, same target and comment
        Comment.objects.create(
            user=create_sample_usertoken(),
            text=_COMMENT,
            target_type=ContentType.objects.get_for_model(Person),
            target_id=4837,
        )

        # Duplicate of first comment
        with transaction.atomic():
            self.assertRaises(
                IntegrityError,
                lambda: Comment.objects.create(
                    user=user,
                    text=_COMMENT,
                    target_type=ContentType.objects.get_for_model(Person),
                    target_id=4837,
                ),
            )
Example #8
0
    def test_votes_one_vote_per_user_per_target(self):
        """Ensure a user can only vote once for a given target object."""
        vote_type_aye = VoteType.objects.create(name="aye")
        vote_type_no = VoteType.objects.create(name="no")

        Vote.objects.create(
            user=self.valid_user,
            target_type=ContentType.objects.get_for_model(Person),
            target_id=4837,
            vote_type=vote_type_aye,
        )

        # Different user
        Vote.objects.create(
            user=create_sample_usertoken(),
            target_type=ContentType.objects.get_for_model(Person),
            target_id=4837,
            vote_type=vote_type_aye,
        )

        # User duplicate same target
        with transaction.atomic():
            self.assertRaises(
                IntegrityError,
                lambda: Vote.objects.create(
                    user=self.valid_user,
                    target_type=ContentType.objects.get_for_model(Person),
                    target_id=4837,
                    vote_type=vote_type_no,
                ),
            )
    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)
Example #10
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)
Example #11
0
    def setUp(self) -> None:
        self.valid_token = uuid.uuid4()

        self.target_person = Person.objects.create(parliamentdotuk=4837,
                                                   name="Aaron Bell",
                                                   active=True)

        Person.objects.create(parliamentdotuk=1423,
                              name="Boris Johnson",
                              active=True)

        self.valid_user = create_sample_usertoken("VoteTests-username",
                                                  self.valid_token)
Example #12
0
    def setUp(self, *args, **kwargs) -> None:
        self.valid_token = uuid.uuid4()

        Person.objects.create(parliamentdotuk=4837, name="Aaron Bell", active=True)

        create_sample_usertoken(_TEST_USERNAME, token=self.valid_token)