def test_serialize_reply_message(self): user = baker.make(User, user_type=USER_TYPE.Turker.value) msg = baker.make(Message, content="a comment", chat=user.chat, sender__username="******") reply = baker.make(Message, sender=user, content="xpto", chat=user.chat, reply_to=msg) expected = { "sender_username": user.username, "content": "xpto", "id": reply.id, "accept_reply": False, "sender_is_turker": True, "reply_to": { "sender_username": "******", "content": "a comment", "id": msg.id, }, } serializer = MessageSerializer(instance=reply, context=self.ctx) assert expected == serializer.data
def post(self, request, *args, **kwargs): serializer = self.serializer_class(data=request.data, context={'request': request}) serializer.is_valid(raise_exception=True) message = serializer.save(author=request.user, chat_id=kwargs.get('pk')) return Response( { 'message': MessageSerializer(message, context=self.get_serializer_context()).data }, status=status.HTTP_201_CREATED)
def test_serialize_turker_user_message(self): user = baker.make(User, user_type=USER_TYPE.Turker.value) msg = baker.make(Message, sender=user, content="xpto", chat=user.chat) expected = { "sender_username": user.username, "content": "xpto", "id": msg.id, "reply_to": None, "accept_reply": False, "sender_is_turker": True, } serializer = MessageSerializer(instance=msg, context=self.ctx) assert expected == serializer.data
def test_serialize_message_for_deleted_user(self): user = baker.make(User, user_type=USER_TYPE.Regular.value) msg = baker.make(Message, sender=user, content="xpto", chat=self.chat) user.delete() msg.refresh_from_db() expected = { "sender_username": "******", "content": "xpto", "id": msg.id, "reply_to": None, "accept_reply": False, "sender_is_turker": False, } serializer = MessageSerializer(instance=msg, context=self.ctx) assert expected == serializer.data
def test_get_paginated_messages_data(self): cache_key = f'chat-{self.chat.id}-messages' assert cache.get(cache_key) is None messages = baker.make(Message, chat=self.chat, _quantity=42) response = self.client.get(self.url) data = response.json() expected = MessageSerializer(instance=messages[::-1][:20], context={ "user": self.user }, many=True).data assert 200 == response.status_code assert expected == data["results"] assert 42 == data["count"] assert "next" in data cached_messages = cache.get(cache_key, default=[]) for message in messages: assert message in cached_messages
def test_reply_to_a_message(self): msg = baker.make(Message, chat=self.chat) self.user.user_type = USER_TYPE.Turker.value self.user.save() data = {"content": "new msg", "reply_to": int(msg.id)} response = self.client.post(self.url, data=data) new_msg = Message.objects.latest("id") expected = MessageSerializer( instance=new_msg, context={ "user": self.user }, ).data assert 201 == response.status_code assert "new msg" == new_msg.content assert expected == response.json() assert self.user == new_msg.sender assert self.chat == new_msg.chat assert msg == new_msg.reply_to
def test_add_new_message_on_post(self): cache_key = f'chat-{self.chat.id}-messages' baker.make(Message, chat=self.chat, _quantity=42) # add messages to the chat self.client.get(self.url) # populates cache assert cache.get(cache_key) # ensures cache is populated response = self.client.post(self.url, data={"content": "new msg"}) new_msg = Message.objects.first() expected = MessageSerializer( instance=new_msg, context={ "user": self.user }, ).data assert 201 == response.status_code assert "new msg" == new_msg.content assert expected == response.json() assert self.user == new_msg.sender assert self.chat == new_msg.chat assert new_msg.reply_to is None assert cache.get(cache_key) is None # clears cache after new msg