Example #1
0
    def test_create(self, mock_create_broadcast):
        mock_create_broadcast.return_value = TembaBroadcast.create(contacts=[self.contact1.uuid])

        # test from contact
        msg = Message.create_for_contact(self.unicef, self.contact1, "Hello", self.room1)
        self.assertEqual(msg.org, self.unicef)
        self.assertEqual(msg.contact, self.contact1)
        self.assertEqual(msg.user, None)
        self.assertEqual(msg.text, "Hello")
        self.assertEqual(msg.room, self.room1)
        self.assertEqual(msg.status, STATUS_SENT)
        self.assertIsNotNone(msg.time)
        self.assertFalse(msg.is_user_message())

        self.assertEqual(msg.as_json(), dict(id=msg.id, sender=self.contact1.as_participant_json(), text="Hello",
                                             room_id=self.room1.id, time=msg.time, status='S'))

        # test from user
        msg = Message.create_for_user(self.unicef, self.user1, "Goodbye", self.room1)
        self.assertEqual(msg.org, self.unicef)
        self.assertEqual(msg.contact, None)
        self.assertEqual(msg.user, self.user1)
        self.assertEqual(msg.text, "Goodbye")
        self.assertEqual(msg.room, self.room1)
        self.assertEqual(msg.status, STATUS_PENDING)
        self.assertIsNotNone(msg.time)
        self.assertTrue(msg.is_user_message())

        self.assertEqual(msg.as_json(), dict(id=msg.id, sender=self.user1.profile.as_participant_json(), text="Goodbye",
                                             room_id=self.room1.id, time=msg.time, status='P'))

        # async task will have sent the message
        self.assertEqual(Message.objects.get(pk=msg.pk).status, STATUS_SENT)
Example #2
0
    def post(self, request, *args, **kwargs):
        entity = kwargs['entity'].lower()
        action = kwargs['action'].lower()
        org = request.org

        token = request.REQUEST.get('token', None)
        if org.get_secret_token() != token:
            return HttpResponseForbidden(
                "Secret token not provided or incorrect")

        if entity == 'message' and action == 'new':
            contact_uuid = request.REQUEST.get('contact', None)
            text = request.REQUEST.get('text', None)
            group_uuid = request.REQUEST.get('group', None)

            if not (contact_uuid and text and group_uuid):
                return HttpResponseBadRequest(
                    "Missing contact, text or group parameter")

            room = self._get_or_create_room(org, group_uuid)
            contact = self._get_or_create_contact(org, room, contact_uuid)

            Message.create_for_contact(org, contact, text, room)

            # TODO handle contact.room vs room mismatch

        elif entity == 'contact' and action == 'new':
            contact_uuid = request.REQUEST.get('contact', None)
            group_uuid = request.REQUEST.get('group', None)

            if not (contact_uuid and group_uuid):
                return HttpResponseBadRequest(
                    "Missing contact or group parameter")

            room = self._get_or_create_room(org, group_uuid)
            self._get_or_create_contact(org, room, contact_uuid)

        elif entity == 'contact' and action == 'del':
            contact_uuid = request.REQUEST.get('contact', None)

            if not contact_uuid:
                return HttpResponseBadRequest(
                    "Missing contact or group parameter")

            self._del_contact(org, contact_uuid)

        return JsonResponse({})
Example #3
0
    def post(self, request, *args, **kwargs):
        entity = kwargs['entity'].lower()
        action = kwargs['action'].lower()
        org = request.org

        token = request.REQUEST.get('token', None)
        if org.get_secret_token() != token:
            return HttpResponseForbidden("Secret token not provided or incorrect")

        if entity == 'message' and action == 'new':
            contact_uuid = request.REQUEST.get('contact', None)
            text = request.REQUEST.get('text', None)
            group_uuid = request.REQUEST.get('group', None)

            if not (contact_uuid and text and group_uuid):
                return HttpResponseBadRequest("Missing contact, text or group parameter")

            room = self._get_or_create_room(org, group_uuid)
            contact = self._get_or_create_contact(org, room, contact_uuid)

            Message.create_for_contact(org, contact, text, room)

            # TODO handle contact.room vs room mismatch

        elif entity == 'contact' and action == 'new':
            contact_uuid = request.REQUEST.get('contact', None)
            group_uuid = request.REQUEST.get('group', None)

            if not (contact_uuid and group_uuid):
                return HttpResponseBadRequest("Missing contact or group parameter")

            room = self._get_or_create_room(org, group_uuid)
            self._get_or_create_contact(org, room, contact_uuid)

        elif entity == 'contact' and action == 'del':
            contact_uuid = request.REQUEST.get('contact', None)

            if not contact_uuid:
                return HttpResponseBadRequest("Missing contact or group parameter")

            self._del_contact(org, contact_uuid)

        return JsonResponse({})
Example #4
0
    def get_context_data(self, **kwargs):
        context = super(ChatView, self).get_context_data(**kwargs)
        allowed_rooms = self.request.user.get_rooms(self.request.org).order_by('name')

        if 'room' in self.kwargs:
            try:
                initial_room = allowed_rooms.get(pk=self.kwargs['room'])
            except Room.DoesNotExist:
                raise PermissionDenied()
        else:
            initial_room = allowed_rooms.first()

        msg_text_chars = MESSAGE_MAX_LEN - len(Message.get_user_prefix(self.request.user))

        context['rooms'] = allowed_rooms
        context['initial_room'] = initial_room
        context['msg_text_chars'] = msg_text_chars
        return context
Example #5
0
    def get_context_data(self, **kwargs):
        context = super(ChatView, self).get_context_data(**kwargs)
        allowed_rooms = self.request.user.get_rooms(
            self.request.org).order_by('name')

        if 'room' in self.kwargs:
            try:
                initial_room = allowed_rooms.get(pk=self.kwargs['room'])
            except Room.DoesNotExist:
                raise PermissionDenied()
        else:
            initial_room = allowed_rooms.first()

        msg_text_chars = MESSAGE_MAX_LEN - len(
            Message.get_user_prefix(self.request.user))

        context['rooms'] = allowed_rooms
        context['initial_room'] = initial_room
        context['msg_text_chars'] = msg_text_chars
        return context
Example #6
0
 def test_get_user_prefix(self):
     self.assertEqual(Message.get_user_prefix(self.superuser), '')
     self.assertEqual(Message.get_user_prefix(self.user1), 'sammy: ')