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_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_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_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_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_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_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)