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

        send_url = reverse('msgs.message_send')

        # try to send as superuser who doesn't have a chat profile
        self.login(self.superuser)
        response = self.url_post('unicef', send_url, dict(room=self.room1.id, text="Hello"))
        self.assertEqual(response.status_code, 403)

        # send as admin user
        self.login(self.admin)
        response = self.url_post('unicef', send_url, dict(room=self.room1.id, text="Hello 1"))
        self.assertEqual(response.status_code, 200)
        mock_create_broadcast.assert_called_with("richard: Hello 1", groups=[self.room1.uuid])

        msg = Message.objects.get(text="Hello 1")
        self.assertEqual(msg.room, self.room1)
        self.assertEqual(msg.user, self.admin)

        # send as regular user
        self.login(self.user1)
        response = self.url_post('unicef', send_url, dict(room=self.room1.id, text="Hello 2"))
        self.assertEqual(response.status_code, 200)
        mock_create_broadcast.assert_called_with("sammy: Hello 2", groups=[self.room1.uuid])

        msg = Message.objects.get(text="Hello 2")
        self.assertEqual(msg.room, self.room1)
        self.assertEqual(msg.user, self.user1)

        # try to send to room that user doesn't have access to
        self.url_post('unicef', send_url, dict(room=self.room3.id, text="Hello 3"))
        self.assertFalse(Message.objects.filter(text="Hello 3").exists())
示例#2
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)
示例#3
0
    def test_create(self, mock_create_broadcast):
        mock_create_broadcast.return_value = Broadcast.create()
        now = timezone.now()

        # create non-regional issue with 3 responses (1 complete, 1 partial, 1 empty)
        issue1 = Issue.objects.create(poll=self.poll1,
                                      region=None,
                                      conducted_on=timezone.now())

        Response.objects.create(flow_run_id=123,
                                issue=issue1,
                                contact=self.contact1,
                                created_on=now,
                                updated_on=now,
                                status=RESPONSE_COMPLETE)
        Response.objects.create(flow_run_id=234,
                                issue=issue1,
                                contact=self.contact2,
                                created_on=now,
                                updated_on=now,
                                status=RESPONSE_PARTIAL)
        Response.objects.create(flow_run_id=345,
                                issue=issue1,
                                contact=self.contact4,
                                created_on=now,
                                updated_on=now,
                                status=RESPONSE_EMPTY)

        msg1 = Message.create(self.unicef, self.admin, "Test #1", issue1,
                              COHORT_ALL, None)
        self.assertEqual(msg1.org, self.unicef)
        self.assertEqual(msg1.sent_by, self.admin)
        self.assertIsNotNone(msg1.sent_on)
        self.assertEqual(msg1.text, "Test #1")
        self.assertEqual(msg1.issue, issue1)
        self.assertEqual(msg1.cohort, COHORT_ALL)
        self.assertEqual(msg1.region, None)
        self.assertEqual(list(msg1.recipients.order_by('pk')),
                         [self.contact1, self.contact2, self.contact4])
        self.assertEqual(unicode(msg1), "Test #1")

        self.assertEqual(msg1.as_json(), dict(id=msg1.pk, recipients=3))

        msg2 = Message.create(self.unicef, self.admin, "Test #1", issue1,
                              COHORT_RESPONDENTS, None)
        self.assertEqual(msg2.cohort, COHORT_RESPONDENTS)
        self.assertEqual(msg2.region, None)
        self.assertEqual(list(msg2.recipients.order_by('pk')), [self.contact1])

        msg3 = Message.create(self.unicef, self.admin, "Test #1", issue1,
                              COHORT_NONRESPONDENTS, None)
        self.assertEqual(msg3.cohort, COHORT_NONRESPONDENTS)
        self.assertEqual(msg3.region, None)
        self.assertEqual(list(msg3.recipients.order_by('pk')),
                         [self.contact2, self.contact4])

        msg4 = Message.create(self.unicef, self.admin, "Test #1", issue1,
                              COHORT_ALL, self.region1)
        self.assertEqual(msg4.cohort, COHORT_ALL)
        self.assertEqual(msg4.region, self.region1)
        self.assertEqual(list(msg4.recipients.order_by('pk')),
                         [self.contact1, self.contact2])