def setUpTestData(cls): super().setUpTestData() cls.content = ContentFactory(visibility=Visibility.PUBLIC) cls.reply = ContentFactory(parent=cls.content, visibility=Visibility.PUBLIC) cls.share = ContentFactory(share_of=cls.content, visibility=Visibility.PUBLIC)
def test_reply_is_updated_from_entity(self): author = ProfileFactory(handle=self.comment.handle) ContentFactory(guid=self.comment.guid, author=author) process_entity_comment(self.comment, author) content = Content.objects.get(guid=self.comment.guid, parent=self.content) self.assertEqual(content.text, self.comment.raw_content) # Don't allow updating if the author is different invalid_entity = base.Comment(guid=self.comment.guid, raw_content="barfoo", handle="*****@*****.**", target_guid=self.content.guid) process_entity_comment(invalid_entity, ProfileFactory(handle=invalid_entity.handle)) content.refresh_from_db() self.assertEqual(content.text, self.comment.raw_content) self.assertEqual(content.author, author) # Don't allow changing parent invalid_entity = base.Comment(guid=self.comment.guid, raw_content="barfoo", handle="*****@*****.**", target_guid=ContentFactory().guid) process_entity_comment(invalid_entity, author) content.refresh_from_db() self.assertEqual(content.text, self.comment.raw_content) self.assertEqual(content.author, author)
def setUpTestData(cls): super().setUpTestData() author = UserFactory() Profile.objects.filter(id=author.profile.id).update(rsa_private_key=get_dummy_private_key().exportKey()) cls.limited_content = ContentFactory(visibility=Visibility.LIMITED, author=author.profile) cls.public_content = ContentFactory(visibility=Visibility.PUBLIC, author=author.profile) cls.remote_content = ContentFactory(visibility=Visibility.PUBLIC)
def test_content_save_calls_streamconsumer_group_send__public_share_with_followers( self, mock_async): mock_send = Mock() mock_async.return_value = mock_send self.profile.following.add(self.remote_profile) other_user = PublicUserFactory() other_profile = ProfileFactory() content = ContentFactory(author=self.remote_profile) share = ContentFactory(content_type=ContentType.SHARE, share_of=content, author=other_profile) with patch("socialhome.users.models.User.recently_active", new_callable=mock.PropertyMock, return_value=True): update_streams_with_content(share) data = { "type": "notification", "payload": { "event": "new", "id": content.id } } calls = [ call(f"streams_profile_all__{share.author.id}__{self.user.id}", data), call(f"streams_profile_all__{share.author.id}__{other_user.id}", data), call(f"streams_followed__{self.user.id}", data), ] mock_send.assert_has_calls(calls, any_order=True) self.assertEqual(mock_send.call_count, 3)
def setUpTestData(cls): super().setUpTestData() author = UserFactory() cls.limited_content = ContentFactory(visibility=Visibility.LIMITED, author=author.profile) cls.public_content = ContentFactory(visibility=Visibility.PUBLIC, author=author.profile)
def setUpTestData(cls): super().setUpTestData() cls.user = UserFactory() cls.content = ContentFactory() cls.content2 = ContentFactory() cls.child_content = ContentFactory(parent=cls.content) cls.tag = TagFactory()
def setUpTestData(cls): super().setUpTestData() cls.user = UserFactory() Profile.objects.filter(user__username=cls.user.username).update(visibility=Visibility.PUBLIC) cls.user_content = ContentFactory(author=cls.user.profile, visibility=Visibility.PUBLIC) cls.profile = ProfileFactory(visibility=Visibility.PUBLIC) cls.profile_content = ContentFactory(author=cls.profile, visibility=Visibility.PUBLIC)
def test_content_saved_in_correct_order(self): profile = ProfileFactory(guid="1234") pinned_content_1 = ContentFactory(pinned=True, text="foobar", author=profile) pinned_content_2 = ContentFactory(pinned=True, text="foobar", author=profile) pinned_content_3 = ContentFactory(pinned=True, text="foobar", author=profile) self.assertEqual([pinned_content_1.order, pinned_content_2.order, pinned_content_3.order], [1, 2, 3])
def setUpTestData(cls): super().setUpTestData() cls.user = UserFactory() cls.user2 = UserFactory() cls.content = ContentFactory(visibility=Visibility.PUBLIC) cls.other_content = ContentFactory(visibility=Visibility.PUBLIC) cls.user.profile.following.add(cls.content.author)
def setUpTestData(cls): super().setUpTestData() cls.create_local_and_remote_user() cls.user2 = AnonymousUser() cls.local_user = UserFactory() cls.public_content = ContentFactory( visibility=Visibility.PUBLIC, text="**Foobar**", author=cls.profile, ) cls.site_content = ContentFactory(visibility=Visibility.SITE, text="_Foobar_") cls.limited_content = ContentFactory(visibility=Visibility.LIMITED) cls.self_content = ContentFactory(visibility=Visibility.SELF) cls.remote_content = ContentFactory( visibility=Visibility.PUBLIC, remote_created=make_aware(datetime.datetime(2015, 1, 1)), author=cls.remote_profile, ) cls.ids = [ cls.public_content.id, cls.site_content.id, cls.limited_content.id, cls.self_content.id ] cls.set = { cls.public_content, cls.site_content, cls.limited_content, cls.self_content }
def _get_request_view_and_content(self, create_content=True, anonymous_user=False): request = self.client.get("/") request.site = get_current_site(request) if anonymous_user: request.user = AnonymousUser() profile = self.profile else: request.user = self.user profile = self.user.profile contents = [] if create_content: contents.extend([ ContentFactory(author=profile, order=3, pinned=True), ContentFactory(author=profile, order=2, pinned=True), ContentFactory(author=profile, order=1, pinned=True), ]) Content.objects.filter(id=contents[0].id).update(order=3) Content.objects.filter(id=contents[1].id).update(order=2) Content.objects.filter(id=contents[2].id).update(order=1) view = ProfileDetailView(request=request, kwargs={"guid": profile.guid}) view.object = profile view.target_profile = profile return request, view, contents, profile
def test_local_content_retraction_gets_sent(self, mock_send): user = UserFactory() content = ContentFactory(author=user.profile) self.assertTrue(content.local) mock_send.reset_mock() content.delete() mock_send.assert_called_once_with(content, content.author_id)
def test_reply_is_updated_from_entity(self): author = ProfileFactory(fid=self.comment.actor_id) ContentFactory(fid=self.comment.id, author=author) process_entity_comment(self.comment, author) content = Content.objects.get(fid=self.comment.id, parent=self.content) self.assertEqual(content.text, self.comment.raw_content) # Don't allow updating if the author is different invalid_entity = base.Comment( id=self.comment.id, raw_content="barfoo", actor_id="https://example.com/notthesameperson", target_id=self.content.fid, ) process_entity_comment(invalid_entity, ProfileFactory(fid=invalid_entity.actor_id)) content.refresh_from_db() self.assertEqual(content.text, self.comment.raw_content) self.assertEqual(content.author, author) # Don't allow changing parent invalid_entity = base.Comment( id=self.comment.id, raw_content="barfoo", actor_id="https://example.com/notthesameperson", target_id=ContentFactory().fid, ) process_entity_comment(invalid_entity, author) content.refresh_from_db() self.assertEqual(content.text, self.comment.raw_content) self.assertEqual(content.author, author)
def setUpTestData(cls): super().setUpTestData() cls.content = ContentFactory(visibility=Visibility.LIMITED) cls.public_content = ContentFactory(visibility=Visibility.PUBLIC) cls.receiving_user = UserFactory() cls.receiving_profile = cls.receiving_user.profile cls.profile = ProfileFactory()
def setUpTestData(cls): super().setUpTestData() cls.public_content = ContentFactory(pinned=True) cls.public_tag_content = ContentFactory(text="#foobar") cls.limited_content = LimitedContentFactory() cls.tag = Tag.objects.get(name="foobar") cls.site_content = SiteContentFactory(pinned=True) cls.site_tag_content = SiteContentFactory(text="#foobar") cls.self_user = UserFactory() cls.self_content = SelfContentFactory(author=cls.self_user.profile, pinned=True) cls.self_tag_content = SelfContentFactory(author=cls.self_user.profile, text="#foobar") cls.other_user = UserFactory() cls.anonymous_user = AnonymousUser() cls.other_user.profile.following.add(cls.public_content.author, cls.self_user.profile) cls.public_reply = PublicContentFactory(parent=cls.public_content) cls.limited_reply = LimitedContentFactory(parent=cls.limited_content) cls.self_reply = SelfContentFactory(parent=cls.self_content) cls.site_reply = SiteContentFactory(parent=cls.site_content) cls.public_share = PublicContentFactory(share_of=cls.public_content, author=cls.self_user.profile) cls.limited_share = LimitedContentFactory(share_of=cls.limited_content, author=cls.self_user.profile) cls.self_share = SelfContentFactory(share_of=cls.self_content, author=cls.self_user.profile) cls.site_share = SiteContentFactory(share_of=cls.site_content, author=cls.self_user.profile) cls.public_share_reply = PublicContentFactory(parent=cls.public_share) cls.share_limited_reply = LimitedContentFactory( parent=cls.limited_share) cls.share_self_reply = SelfContentFactory(parent=cls.self_share) cls.share_site_reply = SiteContentFactory(parent=cls.site_share)
def test_save_sort_order_skips_non_qs_contents(self): request, view, contents, profile = self._get_request_view_and_content() other_user = UserFactory() other_content = ContentFactory(author=other_user.profile, pinned=True) Content.objects.filter(id=other_content.id).update(order=100) view._save_sort_order([other_content.id]) other_content.refresh_from_db() assert other_content.order == 100
def test_local_content_retraction_gets_sent(self, mock_send): user = UserFactory() content = ContentFactory(author=user.profile) self.assertTrue(content.local) mock_send.reset_mock() content_id = content.id content.delete() mock_send.assert_called_once_with(send_content_retraction, content, content.author_id)
def setUpTestData(cls): super().setUpTestData() cls.content = ContentFactory(visibility=Visibility.PUBLIC) cls.site = ContentFactory(visibility=Visibility.SITE) cls.selff = ContentFactory(visibility=Visibility.SELF) cls.limited = ContentFactory(visibility=Visibility.LIMITED) cls.user = UserFactory() cls.client = Client()
def setUpTestData(cls): super().setUpTestData() cls.limited_content = ContentFactory(visibility=Visibility.LIMITED) author = UserFactory() author.profile.rsa_private_key = get_dummy_private_key().exportKey() author.profile.save() cls.public_content = ContentFactory(visibility=Visibility.PUBLIC, author=author.profile) cls.profile = author.profile
def test_calls_update_streams_with_content(self, mock_update): # Calls on create content = ContentFactory() mock_update.assert_called_once_with(content) mock_update.reset_mock() # Does not call on update content.text = "update!" content.save() self.assertFalse(mock_update.called)
def test_get_context_data_does_not_contain_content_for_other_users(self): request, view, contents, profile = self._get_request_view_and_content(create_content=False) user = UserFactory() ContentFactory(author=user.profile, pinned=True) user = UserFactory() ContentFactory(author=user.profile, pinned=True) view.content_list = view._get_contents_queryset() context = view.get_context_data() assert len(context["content_list"]) == 0
def test_contents_queryset_returns_all_for_self(self): request, view, contents, profile = self._get_request_view_and_content(create_content=False) site = ContentFactory(author=profile, visibility=Visibility.SITE, pinned=True) selff = ContentFactory(author=profile, visibility=Visibility.SELF, pinned=True) limited = ContentFactory(author=profile, visibility=Visibility.LIMITED, pinned=True) public = ContentFactory(author=profile, visibility=Visibility.PUBLIC, pinned=True) qs = view._get_contents_queryset() assert qs.count() == 4 assert set(qs) == {public, site, selff, limited}
def test_contents_queryset_returns_public_only_for_unauthenticated(self): request, view, contents, profile = self._get_request_view_and_content(create_content=False) ContentFactory(author=profile, visibility=Visibility.SITE, pinned=True) ContentFactory(author=profile, visibility=Visibility.SELF, pinned=True) ContentFactory(author=profile, visibility=Visibility.LIMITED, pinned=True) public = ContentFactory(author=profile, visibility=Visibility.PUBLIC, pinned=True) request.user = AnonymousUser() qs = view._get_contents_queryset() assert qs.count() == 1 assert qs.first() == public
def setUpTestData(cls): super().setUpTestData() cls.local_user = UserFactory() cls.content = ContentFactory(visibility=Visibility.PUBLIC, author=cls.local_user.profile) cls.remote_content = ContentFactory(visibility=Visibility.PUBLIC) cls.site = ContentFactory(visibility=Visibility.SITE, author=cls.local_user.profile) cls.selff = ContentFactory(visibility=Visibility.SELF, author=cls.local_user.profile) cls.limited = ContentFactory(visibility=Visibility.LIMITED) cls.user = UserFactory() cls.client = Client()
def test_contents_queryset_returns_public_or_site_only_for_authenticated(self): request, view, contents, profile = self._get_request_view_and_content(create_content=False) site = ContentFactory(author=profile, visibility=Visibility.SITE, pinned=True) ContentFactory(author=profile, visibility=Visibility.SELF, pinned=True) ContentFactory(author=profile, visibility=Visibility.LIMITED, pinned=True) public = ContentFactory(author=profile, visibility=Visibility.PUBLIC, pinned=True) request.user = User.objects.get(username="******") qs = view._get_contents_queryset() assert qs.count() == 2 assert set(qs) == {public, site}
def test_contains_only_public_content(self): content = ContentFactory(text="#tag public", visibility=Visibility.PUBLIC) site = ContentFactory(text="#tag site", visibility=Visibility.SITE) selff = ContentFactory(text="#tag self", visibility=Visibility.SELF) limited = ContentFactory(text="#tag limited", visibility=Visibility.LIMITED) response = self.client.get(reverse("streams:tag", kwargs={"name": "tag"})) assert content.rendered in str(response.content) assert site.rendered not in str(response.content) assert selff.rendered not in str(response.content) assert limited.rendered not in str(response.content)
def test_get_context_data_does_not_contain_content_for_other_users( self, admin_client, rf): request, view, contents, profile = self._get_request_view_and_content( rf, create_content=False) user = UserFactory() ContentFactory(author=user.profile, pinned=True) user = UserFactory() ContentFactory(author=user.profile, pinned=True) context = view.get_context_data() assert len(context["contents"]) == 0
def test_save_sort_order_skips_non_qs_contents(self, admin_client, rf): request, view, contents, profile = self._get_request_view_and_content( rf) other_user = UserFactory() other_content = ContentFactory(author=other_user.profile, order=100, pinned=True) view._save_sort_order([other_content.id]) other_content.refresh_from_db() assert other_content.order == 100
def setUpTestData(cls): super().setUpTestData() author = UserFactory() author.profile.rsa_private_key = get_dummy_private_key().exportKey() author.profile.save() cls.public_content = ContentFactory(visibility=Visibility.PUBLIC, author=author.profile) cls.remote_reply = ContentFactory(parent=cls.public_content, author=ProfileFactory()) cls.reply = ContentFactory(parent=cls.public_content)
def test_contains_only_public_content(self, client): content = ContentFactory(visibility=Visibility.PUBLIC) site = ContentFactory(visibility=Visibility.SITE) selff = ContentFactory(visibility=Visibility.SELF) limited = ContentFactory(visibility=Visibility.LIMITED) response = client.get(reverse("streams:public")) assert content.text in str(response.content) assert site.text not in str(response.content) assert selff.text not in str(response.content) assert limited.text not in str(response.content)
def test_content_save_calls_streamconsumer_group_send(self, mock_consumer): mock_consumer.group_send = Mock() # Public post no tags or followers content = ContentFactory() data = json.dumps({"event": "new", "id": content.id}) calls = [ call("streams_public", data), call("streams_profile__%s" % content.author.id, data), call("streams_profile_all__%s" % content.author.id, data), ] mock_consumer.group_send.assert_has_calls(calls, any_order=True) mock_consumer.group_send.reset_mock() # Private post with tags content = ContentFactory(visibility=Visibility.LIMITED, text="#foobar #barfoo") data = json.dumps({"event": "new", "id": content.id}) calls = [ call("streams_tag__%s_foobar" % Tag.objects.get(name="foobar").id, data), call("streams_tag__%s_barfoo" % Tag.objects.get(name="barfoo").id, data), call("streams_profile__%s" % content.author.id, data), call("streams_profile_all__%s" % content.author.id, data), ] mock_consumer.group_send.assert_has_calls(calls, any_order=True) mock_consumer.group_send.reset_mock() # Public post with followers follower = UserFactory() follower2 = UserFactory() profile = ProfileFactory() follower.profile.following.add(content.author) follower2.profile.following.add(content.author) profile.following.add(content.author) content = ContentFactory(author=content.author) data = json.dumps({"event": "new", "id": content.id}) calls = [ call("streams_public", data), call("streams_profile__%s" % content.author.id, data), call("streams_profile_all__%s" % content.author.id, data), call("streams_followed__%s" % follower.username, data), call("streams_followed__%s" % follower2.username, data), ] mock_consumer.group_send.assert_has_calls(calls, any_order=True) mock_consumer.group_send.reset_mock() # Replies reply = ContentFactory(parent=content) data = json.dumps({"event": "new", "id": reply.id}) mock_consumer.group_send.assert_called_once_with( "streams_content__%s" % content.channel_group_name, data) mock_consumer.group_send.reset_mock() # Update shouldn't cause a group send content.text = "foo" content.save() self.assertFalse(mock_consumer.group_send.called)
def test_content_save_calls_streamconsumer_group_send(self, mock_consumer): mock_consumer.group_send = Mock() # Public post no tags or followers content = ContentFactory() data = json.dumps({"event": "new", "id": content.id}) calls = [ call("streams_public", data), call("streams_profile__%s" % content.author.id, data), call("streams_profile_all__%s" % content.author.id, data), ] mock_consumer.group_send.assert_has_calls(calls, any_order=True) mock_consumer.group_send.reset_mock() # Private post with tags content = ContentFactory(visibility=Visibility.LIMITED, text="#foobar #barfoo") data = json.dumps({"event": "new", "id": content.id}) calls = [ call("streams_tag__%s_foobar" % Tag.objects.get(name="foobar").id, data), call("streams_tag__%s_barfoo" % Tag.objects.get(name="barfoo").id, data), call("streams_profile__%s" % content.author.id, data), call("streams_profile_all__%s" % content.author.id, data), ] mock_consumer.group_send.assert_has_calls(calls, any_order=True) mock_consumer.group_send.reset_mock() # Public post with followers follower = UserFactory() follower2 = UserFactory() profile = ProfileFactory() follower.profile.following.add(content.author) follower2.profile.following.add(content.author) profile.following.add(content.author) content = ContentFactory(author=content.author) data = json.dumps({"event": "new", "id": content.id}) calls = [ call("streams_public", data), call("streams_profile__%s" % content.author.id, data), call("streams_profile_all__%s" % content.author.id, data), call("streams_followed__%s" % follower.username, data), call("streams_followed__%s" % follower2.username, data), ] mock_consumer.group_send.assert_has_calls(calls, any_order=True) mock_consumer.group_send.reset_mock() # Replies reply = ContentFactory(parent=content) data = json.dumps({"event": "new", "id": reply.id}) mock_consumer.group_send.assert_called_once_with("streams_content__%s" % content.channel_group_name, data) mock_consumer.group_send.reset_mock() # Update shouldn't cause a group send content.text = "foo" content.save() self.assertFalse(mock_consumer.group_send.called)
def test_non_local_content_retraction_does_not_get_sent(self, mock_send): content = ContentFactory() content.delete() self.assertTrue(mock_send.called is False)
def test_exception_calls_logger(self, mock_logger, mock_send): user = UserFactory() content = ContentFactory(author=user.profile) content.delete() self.assertTrue(mock_logger.called)
def test_render_content_called(self): content = ContentFactory() content.render = Mock() content.save() content.render.assert_called_once_with()
def test_render_content_exception_logger_called(self, logger): content = ContentFactory() content.render = Mock(side_effect=Exception) content.save() self.assertTrue(logger.called)