def test_get_chat_data(self): response = self.client.get(self.url) expected = ChatSerializer(instance=self.chat, context={ "user": self.user }).data assert 200 == response.status_code assert expected == response.json()
def to_representation(self, instance): data = super().to_representation(instance) request = self.context.get("request") data['user'] = UserSerializer(User.objects.get(pk=data['user']), context={ 'request': request }).data data['chat'] = ChatSerializer(Chat.objects.get(pk=data['chat']), context={ 'request': request }).data return data
def post(self, request, *args, **kwargs): serializer = self.serializer_class(data=request.data, context={'request': request}) serializer.is_valid(raise_exception=True) invited = request.data.get('invited', []) chat = serializer.save(creator=request.user, invited=invited) return Response( { 'chat': ChatSerializer(chat, context=self.get_serializer_context()).data }, status=status.HTTP_201_CREATED)
def test_serialize_turker_chat_for_regular_user(self): chat = self.turker.chat expected = { "title": self.turker.username, "info": "", "id": chat.id, "messages_url": reverse("chats_api:chat_messages", args=[chat.id]), "is_collective": False, "open_for_messages": False, } serializer = ChatSerializer(instance=chat, context={"user": self.user}) assert expected == serializer.data
def test_serialize_collective_chat_for_turker_user(self): chat = Chat.objects.get_collective_chat() expected = { "title": "Only Turkers", "info": "", "id": chat.id, "messages_url": reverse("chats_api:chat_messages", args=[chat.id]), "is_collective": True, "open_for_messages": False, } serializer = ChatSerializer(instance=chat, context={"user": self.turker}) assert expected == serializer.data
def test_get_available_chats_for_regular_user(self): [ t.chat for t in baker.make( User, user_type=USER_TYPE.Turker.value, _quantity=5) ] response = self.client.get(self.url) expected = ChatSerializer(instance=Chat.objects.all(), context={ "user": self.user }, many=True).data data = response.json() assert 200 == response.status_code assert expected == data["results"]
def test_get_available_chats_for_turker_user(self): self.user.user_type = USER_TYPE.Turker.value self.user.save() # other turkers baker.make(User, user_type=USER_TYPE.Turker.value, _quantity=5) turker_chat = baker.make(Chat, turker=self.user) collective = Chat.objects.get_collective_chat() expected = ChatSerializer(instance=[collective, turker_chat], context={ "user": self.user }, many=True).data response = self.client.get(self.url) data = response.json() assert 200 == response.status_code assert expected == data["results"]