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_visibility(self): serializer = ContentSerializer( context={"request": Mock(user=self.user)}, data={ "text": "Without visibility", }) self.assertFalse(serializer.is_valid())
def get_json_context(self): context = { "currentBrowsingProfileId": getattr(getattr(self.request.user, "profile", None), "id", None), "isUserAuthenticated": bool(self.request.user.is_authenticated), "isReply": self.is_reply, "federate": self.object.federate, "includeFollowing": self.object.include_following, "pinned": self.object.pinned, "showPreview": self.object.show_preview, "visibility": self.object.visibility.value } parent_id = getattr(getattr(self.object, "parent"), "id", None) if parent_id is not None: context["parent"] = parent_id serialized = ContentSerializer(self.object, context={ "request": self.request }).data context["recipients"] = serialized.get("recipients", []) context["text"] = serialized.get("text", "") return context
def test_serializes_through(self): serializer = ContentSerializer(self.content) self.assertEqual(serializer.data["through"], self.content.id) serializer = ContentSerializer( self.content, context={"throughs": { self.content.id: 666 }}) self.assertEqual(serializer.data["through"], 666)
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__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__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_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_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_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_serialize__recipients_list_empty_for_not_owners(self): limited_content = LimitedContentWithRecipientsFactory(author=self.user.profile) serializer = ContentSerializer(limited_content, context={"request": Mock(user=self.user)}) expected_recipients_list = set( limited_content.limited_visibilities.all().order_by("id").values_list("handle", flat=True) ) self.assertEqual(expected_recipients_list, set(serializer.data["recipients"])) serializer = ContentSerializer(limited_content, context={"request": Mock(user=PublicUserFactory())}) self.assertEqual(set(serializer.data["recipients"]), set())
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(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_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_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_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_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_tags_with_tag(self): tag = TagFactory(name="yolo") self.content.tags.clear() self.content.tags.add(tag) serializer = ContentSerializer( self.content, context={"request": Mock(user=self.user)}) self.assertEqual(serializer.data["tags"], ["yolo"])
def get(self, request, **kwargs): qs, throughs = self.get_content() serializer = ContentSerializer(qs, many=True, context={ "throughs": throughs, "request": request }) return Response(serializer.data)
def test_serializes_author(self): serializer = ContentSerializer(self.content) data = serializer.data["author"] self.assertEqual(data["guid"], self.remote_profile.guid) self.assertEqual(data["handle"], self.remote_profile.handle) self.assertEqual(data["home_url"], self.remote_profile.home_url) self.assertEqual(data["id"], self.remote_profile.id) self.assertEqual(data["image_url_small"], self.remote_profile.image_url_small) self.assertEqual(data["is_local"], self.remote_profile.is_local) self.assertEqual(data["name"], self.remote_profile.name) self.assertEqual(data["url"], self.remote_profile.url)
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_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 collect_data(self): # User serializer = UserSerializer(instance=self.user, context=self.context) self.data['user'] = serializer.data # Profile serializer = ProfileSerializer(instance=self.user.profile, context=self.context) self.data['profile'] = serializer.data # Followed profiles self.data['following'] = [] for follow in self.user.profile.following.all(): serializer = LimitedProfileSerializer(instance=follow, context=self.context) self.data['following'].append(serializer.data) # Content self.data['content'] = [] content_qs = Content.objects.filter( author=self.user.profile).order_by('created') for content in content_qs: serializer = ContentSerializer(instance=content, context=self.context) self.data['content'].append(serializer.data)
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_user_has_shared_true_if_shared(self): self.content.share(self.profile) serializer = ContentSerializer(context={"request": Mock(user=self.user)}) self.assertTrue(serializer.get_user_has_shared(self.content))
def test_user_is_author_false_if_not_author(self): serializer = ContentSerializer( context={"request": Mock(user=self.user)}) self.assertFalse(serializer.get_user_is_author(self.content))
def test_user_is_author_true_if_author(self): serializer = ContentSerializer(context={"request": Mock(user=self.user)}) self.assertTrue(serializer.get_user_is_author(self.user_content))
def test_user_has_shared_false_if_not_shared(self): serializer = ContentSerializer( context={"request": Mock(user=self.user)}) self.assertFalse(serializer.get_user_has_shared(self.content))
def test_user_has_shared_false_if_no_request(self): serializer = ContentSerializer() self.assertFalse(serializer.get_user_has_shared(None))
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_user_has_shared_false_if_no_request(self): serializer = ContentSerializer() self.assertFalse(serializer.get_user_has_shared(None))
def test_user_is_author_false_if_not_authenticated(self): serializer = ContentSerializer( context={"request": Mock(user=AnonymousUser())}) self.assertFalse(serializer.get_user_is_author(None))
def test_user_has_shared_false_if_anonymous_user(self): serializer = ContentSerializer(context={"request": Mock(user=AnonymousUser())}) self.assertFalse(serializer.get_user_has_shared(None))
def test_user_is_author_true_if_author(self): serializer = ContentSerializer( context={"request": Mock(user=self.user)}) self.assertTrue(serializer.get_user_is_author(self.user_content))
def test_user_has_shared_false_if_not_shared(self): serializer = ContentSerializer(context={"request": Mock(user=self.user)}) self.assertFalse(serializer.get_user_has_shared(self.content))
def test_user_has_shared_false_if_anonymous_user(self): serializer = ContentSerializer( context={"request": Mock(user=AnonymousUser())}) self.assertFalse(serializer.get_user_has_shared(None))
def test_user_following_author_true_if_following(self): self.profile.following.add(self.remote_profile) serializer = ContentSerializer(context={"request": Mock(user=self.user)}) self.assertTrue(serializer.get_user_following_author(self.content))
def test_user_has_shared_true_if_shared(self): self.content.share(self.profile) serializer = ContentSerializer( context={"request": Mock(user=self.user)}) self.assertTrue(serializer.get_user_has_shared(self.content))
def test_user_is_author_false_if_no_request(self): serializer = ContentSerializer() self.assertFalse(serializer.get_user_is_author(None))
def get_serialized_content(self): serializer = ContentSerializer(instance=self.object, context={"request": self.request}) return serializer.data
def test_user_is_author_false_if_not_authenticated(self): serializer = ContentSerializer(context={"request": Mock(user=AnonymousUser())}) self.assertFalse(serializer.get_user_is_author(None))
def get(self, request, **kwargs): content = self.get_content() serializer = ContentSerializer(content, many=True) return Response(serializer.data)
def test_user_is_author_false_if_not_author(self): serializer = ContentSerializer(context={"request": Mock(user=self.user)}) self.assertFalse(serializer.get_user_is_author(self.content))