def test_serialization_with_share_newsfeed_item(self): """ A NewsfeedItem that is a share should have the item it shares be serialized as well""" nw_item = NewsfeedItem.objects.create( author=self.auth_user, type=NW_ITEM_TEXT_POST, content={'content': 'Hello I like turtles'}) share_item = NewsfeedItem.objects.create_share_post( author=self.auth_user, shared_item=nw_item) expected_data = { 'id': share_item.id, 'author': UserSerializer().to_representation(instance=self.auth_user), 'type': share_item.type, 'comments': NewsfeedItemCommentSerializer(many=True).to_representation( share_item.comments.all()), 'content': share_item.content, 'shared_item': NewsfeedItemSerializer().to_representation( instance=nw_item), # this is attached 'is_private': share_item.is_private, 'created_at': share_item.created_at.isoformat().replace('+00:00', 'Z'), 'updated_at': share_item.updated_at.isoformat().replace('+00:00', 'Z'), 'like_count': 0 } received_data = NewsfeedItemSerializer(instance=share_item).data self.assertEqual(received_data, expected_data)
def test_serializer_with_user_variable(self): # If we pass a User to the to_representation call, a 'user_has_liked' field should appear nw_item = NewsfeedItem.objects.create( author=self.auth_user, type=NW_ITEM_TEXT_POST, content={'content': 'Hello I like turtles'}) NewsfeedItemComment.objects.create(author=self.auth_user, content='name', newsfeed_item=nw_item) NewsfeedItemComment.objects.create(author=self.auth_user, content='name', newsfeed_item=nw_item) NewsfeedItemComment.objects.create(author=self.auth_user, content='Drop the top', newsfeed_item=nw_item) nw_like = NewsfeedItemLike.objects.create(author=self.auth_user, newsfeed_item=nw_item) serializer = NewsfeedItemSerializer() expected_data = { 'id': nw_item.id, 'author': UserSerializer().to_representation(instance=self.auth_user), 'type': nw_item.type, 'comments': NewsfeedItemCommentSerializer(many=True).to_representation( nw_item.comments.all()), 'content': nw_item.content, 'is_private': nw_item.is_private, 'created_at': nw_item.created_at.isoformat().replace('+00:00', 'Z'), 'updated_at': nw_item.updated_at.isoformat().replace('+00:00', 'Z'), 'like_count': 1, 'user_has_liked': True } received_data = serializer.to_representation(instance=nw_item, user=self.auth_user) self.assertEqual(received_data, expected_data) nw_like.delete() nw_item.refresh_from_db() received_data = serializer.to_representation(instance=nw_item, user=self.auth_user) # test to see if the user_has_liked will be changed as appropriate self.assertFalse(received_data['user_has_liked'])
def test_list_serialization(self): """ This tests our custom ListSerializer class and assures it attaches the user_has_liked variable """ nw_item = NewsfeedItem.objects.create( author=self.auth_user, type=NW_ITEM_TEXT_POST, content={'content': 'Hello I like turtles'}) NewsfeedItemComment.objects.create(author=self.auth_user, content='name', newsfeed_item=nw_item) NewsfeedItemComment.objects.create(author=self.auth_user, content='name', newsfeed_item=nw_item) NewsfeedItemComment.objects.create(author=self.auth_user, content='Drop the top', newsfeed_item=nw_item) NewsfeedItemLike.objects.create(author=self.auth_user, newsfeed_item=nw_item) serializer = NewsfeedItemSerializer(many=True) nw_item_repr = { 'id': nw_item.id, 'author': UserSerializer().to_representation(instance=self.auth_user), 'type': nw_item.type, 'comments': NewsfeedItemCommentSerializer(many=True).to_representation( nw_item.comments.all()), 'content': nw_item.content, 'is_private': nw_item.is_private, 'created_at': nw_item.created_at.isoformat().replace('+00:00', 'Z'), 'updated_at': nw_item.updated_at.isoformat().replace('+00:00', 'Z'), 'like_count': 1, 'user_has_liked': True } expected_data = [nw_item_repr, nw_item_repr] received_data = serializer.to_representation(data=[nw_item, nw_item], user=self.auth_user) self.assertEqual(expected_data, received_data)
def test_returns_serialized_nw_item(self): response = self.client.get(self.nw_item_us2_1.get_absolute_url(), HTTP_AUTHORIZATION=self.auth_token) self.assertEqual(response.status_code, 200) self.assertEqual(response.data, NewsfeedItemSerializer(self.nw_item_us2_1).data)
def test_pagination_works(self): self.user5 = User.objects.create(username='******', password='******', email='*****@*****.**', score=123, role=self.base_role) self.auth_user.follow(self.user5) # Create 2 more than what will be shown in the first page and query for the second page first_two_items = [ NewsfeedItem.objects.create(author=self.user5, type=NW_ITEM_TEXT_POST, content={'content': 'Hi'}), NewsfeedItem.objects.create(author=self.user5, type=NW_ITEM_TEXT_POST, content={'content': 'Hi'}) ] for i in range(NEWSFEED_ITEMS_PER_PAGE): NewsfeedItem.objects.create(author=self.user5, type=NW_ITEM_TEXT_POST, content={'content': 'Hi'}) expected_items = NewsfeedItemSerializer(many=True)\ .to_representation(reversed(first_two_items), user=self.auth_user) response = self.client.get('/social/feed?page=2', HTTP_AUTHORIZATION=self.auth_token) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['items'], expected_items)
def get(self, request, *args, **kwargs): try: page = int(request.GET.get('page', 1)) - 1 except ValueError: page = 0 serializer = NewsfeedItemSerializer(many=True) start_offset = page * NEWSFEED_ITEMS_PER_PAGE # TODO: For NewsfeedItems which are SubmissionLinks, validate that the current user can see them otherwise dont show them :) nw_items: [NewsfeedItem] = request.user.fetch_newsfeed( start_offset=start_offset, end_limit=start_offset + NEWSFEED_ITEMS_PER_PAGE) return Response(data={ 'items': serializer.to_representation(nw_items, user=request.user) })
def test_should_see_all_items_including_his(self): self.auth_user.follow(self.user2) newest_item = NewsfeedItem.objects.create(author=self.auth_user, type=NW_ITEM_TEXT_POST, content={'content': 'Hi'}, created_at=datetime.now() + timedelta(days=1)) expected_items = NewsfeedItemSerializer(many=True)\ .to_representation([newest_item, self.nw_item_us2_2, self.nw_item_us2_1], user=self.auth_user) response = self.client.get('/social/feed', HTTP_AUTHORIZATION=self.auth_token) self.assertEqual(response.status_code, 200) self.assertEqual(expected_items, response.data['items'])
def test_serialiation(self): nw_item = NewsfeedItem.objects.create( author=self.auth_user, type=NW_ITEM_TEXT_POST, content={'content': 'Hello I like turtles'}) NewsfeedItemComment.objects.create(author=self.auth_user, content='name', newsfeed_item=nw_item) NewsfeedItemComment.objects.create(author=self.auth_user, content='name', newsfeed_item=nw_item) NewsfeedItemComment.objects.create(author=self.auth_user, content='Drop the top', newsfeed_item=nw_item) NewsfeedItemLike.objects.create(author=self.auth_user, newsfeed_item=nw_item) serializer = NewsfeedItemSerializer(instance=nw_item) expected_data = { 'id': nw_item.id, 'author': UserSerializer().to_representation(instance=self.auth_user), 'type': nw_item.type, 'comments': NewsfeedItemCommentSerializer(many=True).to_representation( nw_item.comments.all()), 'content': nw_item.content, 'is_private': nw_item.is_private, 'created_at': nw_item.created_at.isoformat().replace('+00:00', 'Z'), 'updated_at': nw_item.updated_at.isoformat().replace('+00:00', 'Z'), 'like_count': 1 } received_data = serializer.data self.assertEqual(received_data, expected_data)