def test_save__removes_removed_recipients(self): serializer = ContentSerializer( instance=self.limited_content_with_recipients, partial=True, context={"request": Mock(user=self.user)}, data={ "recipients": [self.user_recipient.handle, self.user_recipient2.handle], "visibility": Visibility.LIMITED.value, }, ) serializer.is_valid(True) content = serializer.save() actual = set(content.limited_visibilities.all().order_by("id")) self.assertSetEqual(actual, {self.user_recipient, self.user_recipient2}) # Remove one recipient serializer = ContentSerializer( instance=content, partial=True, context={"request": Mock(user=self.user)}, data={ "recipients": [self.user_recipient.handle], "visibility": Visibility.LIMITED.value }, ) serializer.is_valid(True) content = serializer.save() self.assertSetEqual(set(content.limited_visibilities.all()), {self.user_recipient})
def test_validate_recipients__fails_bad_recipients(self): serializer = ContentSerializer( instance=self.limited_content, partial=True, context={"request": Mock(user=self.user)}, data={ "recipients": [f"{self.user_recipient.handle}+bad_ext", f"{self.user_recipient2.handle}+bad_ext"], "visibility": Visibility.LIMITED.value }, ) with pytest.raises(serializers.ValidationError, match=r".*Not all recipients could be found\..*"): serializer.is_valid(True)
def test_validate_recipients__recipients_ignored_if_not_limited(self): serializer = ContentSerializer( instance=self.limited_content, partial=True, context={"request": Mock(user=self.user)}, data={ "recipients": f"{self.user_recipient.handle},{self.user_recipient2.handle}", "visibility": Visibility.PUBLIC.value }, ) serializer.is_valid(True) self.assertEquals(serializer.validated_data["recipients"], set())
def test_save__collects_recipients(self): serializer = ContentSerializer( instance=self.limited_content, partial=True, context={"request": Mock(user=self.user)}, data={ "recipients": [self.user_recipient.handle, self.user_recipient2.handle], "visibility": Visibility.LIMITED.value }, ) serializer.is_valid() content = serializer.save() self.assertEquals(set(content.limited_visibilities.all()), {self.user_recipient, self.user_recipient2})
def test_validate_recipients__recipients_and_include_following_cant_both_be_empty(self): serializer = ContentSerializer( instance=self.limited_content, partial=True, context={"request": Mock(user=self.user)}, data={ "recipients": "", "include_following": False, "visibility": Visibility.LIMITED.value }, ) with pytest.raises( serializers.ValidationError, match=r".*Recipients cannot be empty if not including followed users\..*" ): serializer.is_valid(True)
def test_update_doesnt_allow_changing_parent(self): serializer = ContentSerializer( instance=self.reply, partial=True, context={"request": Mock(user=self.user)}, data={ "parent": self.content.id, }, ) self.assertTrue(serializer.is_valid()) serializer = ContentSerializer( instance=self.reply, partial=True, context={"request": Mock(user=self.user)}, data={ "parent": self.content2.id, }, ) self.assertFalse(serializer.is_valid())
def test_update_doesnt_allow_changing_parent(self): serializer = ContentSerializer( instance=self.reply, partial=True, context={"request": Mock(user=self.user)}, data={ "parent": self.content.id, }, ) self.assertTrue(serializer.is_valid()) serializer = ContentSerializer( instance=self.reply, partial=True, context={"request": Mock(user=self.user)}, data={ "parent": self.content2.id, }, ) self.assertFalse(serializer.is_valid())
def test_create_without_visibility(self): serializer = ContentSerializer( context={"request": Mock(user=self.user)}, data={ "text": "Without visibility", }) self.assertFalse(serializer.is_valid())
def test_save__collects_recipients__reply_copies_from_parent(self): serializer = ContentSerializer( instance=self.reply_parent_with_recipients, partial=True, context={"request": Mock(user=self.user)}, data={ "recipients": [self.user_recipient.handle, self.user_recipient2.handle], "visibility": Visibility.LIMITED.value }, ) serializer.is_valid(True) content = serializer.save() actual = set(content.limited_visibilities.all().order_by("id")) self.assertSetEqual({self.user_recipient, self.user_recipient2}, actual)
def test_create_without_parent(self): serializer = ContentSerializer(context={"request": Mock(user=self.user)}, data={ "text": "Without parent", "visibility": "public", }) self.assertTrue(serializer.is_valid()) serializer.save(author=self.user.profile) content = Content.objects.order_by("id").last() self.assertEqual(content.text, "Without parent")
def test_create_without_parent(self): serializer = ContentSerializer(context={"request": Mock(user=self.user)}, data={ "text": "Without parent", "visibility": "public", }) self.assertTrue(serializer.is_valid()) serializer.save(author=self.user.profile) content = Content.objects.order_by("id").last() self.assertEqual(content.text, "Without parent")
def test_create_with_parent__visibility_does_not_match_parent(self): serializer = ContentSerializer( context={"request": Mock(user=self.user)}, data={ "text": "With parent visibility mismatch", "visibility": "public", "parent": self.limited_content.id, }) self.assertFalse(serializer.is_valid())
def test_create_with_parent__user_cannot_see_parent(self): serializer = ContentSerializer( context={"request": Mock(user=self.user)}, data={ "text": "With parent cannot seeee it", "visibility": "limited", "parent": self.limited_content.id, }) self.assertFalse(serializer.is_valid())
def test_validate_recipients(self): serializer = ContentSerializer( instance=self.limited_content, partial=True, context={"request": Mock(user=self.user)}, data={ "recipients": [self.user_recipient.handle, self.user_recipient2.handle], "visibility": Visibility.LIMITED.value }, ) self.assertTrue(serializer.is_valid()) self.assertEquals(serializer.validated_data["recipients"], {self.user_recipient.handle, self.user_recipient2.handle})
def test_create_without_visibility__reply(self): serializer = ContentSerializer(context={"request": Mock(user=self.user)}, data={ "text": "Without visibility", "parent": self.limited_content2.id, }) self.assertTrue(serializer.is_valid()) serializer.save(author=self.profile) content = Content.objects.order_by("id").last() self.assertEqual(content.text, "Without visibility") # Inherits from parent self.assertEqual(content.visibility, Visibility.LIMITED) self.assertEqual(content.parent, self.limited_content2)
def test_save__collects_recipients__include_following(self): user = UserWithContactFactory() content = LimitedContentFactory(author=user.profile) serializer = ContentSerializer( instance=content, partial=True, context={"request": Mock(user=user)}, data={ "recipients": [self.user_recipient.handle, self.user_recipient2.handle], "visibility": Visibility.LIMITED.value, "include_following": True, }) serializer.is_valid(True) content = serializer.save() actual = set(content.limited_visibilities.all().order_by("id")) expected = set( list(user.profile.following.all().order_by("id")) + [self.user_recipient, self.user_recipient2] ) self.assertSetEqual(actual, expected)
def test_create_without_visibility__reply(self): serializer = ContentSerializer(context={"request": Mock(user=self.user)}, data={ "text": "Without visibility", "parent": self.limited_content2.id, }) self.assertTrue(serializer.is_valid()) serializer.save(author=self.profile) content = Content.objects.order_by("id").last() self.assertEqual(content.text, "Without visibility") # Inherits from parent self.assertEqual(content.visibility, Visibility.LIMITED) self.assertEqual(content.parent, self.limited_content2) self.assertEqual(content.root_parent, self.limited_content2)
def test_create_without_visibility(self): serializer = ContentSerializer(context={"request": Mock(user=self.user)}, data={ "text": "Without visibility", }) self.assertFalse(serializer.is_valid())
def test_create_with_parent__visibility_does_not_match_parent(self): serializer = ContentSerializer(context={"request": Mock(user=self.user)}, data={ "text": "With parent visibility mismatch", "visibility": "public", "parent": self.limited_content.id, }) self.assertFalse(serializer.is_valid())
def test_create_with_parent__user_cannot_see_parent(self): serializer = ContentSerializer(context={"request": Mock(user=self.user)}, data={ "text": "With parent cannot seeee it", "visibility": "limited", "parent": self.limited_content.id, }) self.assertFalse(serializer.is_valid())