Пример #1
0
    def test_new_message(self, mock_get_contact, mock_get_group):
        url = reverse('api.temba_handler', kwargs=dict(entity='message', action='new'))

        # GET is not allowed
        response = self.url_get('unicef', '%s?%s' % (url, 'contact=C-001&text=Hello%20World&group=G-001&token=1234567890'))
        self.assertEqual(response.status_code, 405)

        # bad request if you forget a parameter
        response = self.url_post('unicef', '%s?%s' % (url, 'text=Hello%20World&group=G-001&token=1234567890'))
        self.assertEqual(response.status_code, 400)

        # make a valid request for new message from an existing contact to an existing group
        response = self.url_post('unicef', '%s?%s' % (url, 'contact=C-001&text=Hello%20World&group=G-001&token=1234567890'))
        self.assertEqual(response.status_code, 200)

        # check new message created
        msg = Message.objects.get(text="Hello World")
        self.assertEqual(msg.contact, self.contact1)
        self.assertEqual(msg.room, self.room1)

        # try with new room/group that must be fetched
        mock_get_group.return_value = TembaGroup.create(uuid='G-007', name="New group", size=2)

        response = self.url_post('unicef', '%s?%s' % (url, 'contact=C-001&text=Hello%20Again&group=G-007&token=1234567890'))
        self.assertEqual(response.status_code, 200)
        new_room = Room.objects.get(uuid='G-007', name="New group")

        # check new message created
        msg = Message.objects.get(text="Hello Again")
        self.assertEqual(msg.contact, self.contact1)
        self.assertEqual(msg.room, new_room)

        # try with new contact and new room/group that must be fetched
        mock_get_group.return_value = TembaGroup.create(uuid='G-008', name="Newest group", size=2)
        mock_get_contact.return_value = TembaContact.create(uuid='C-007', name="Ken", urns=['tel:234'],
                                                            groups=['G-008'], fields=dict(chat_name="ken"),
                                                            language='eng', modified_on=timezone.now())

        response = self.url_post('unicef', '%s?%s' % (url, 'contact=001-007&text=Goodbye&group=001-008&token=1234567890'))
        self.assertEqual(response.status_code, 200)
        new_contact = Contact.objects.get(uuid='C-007')
        new_room = Room.objects.get(uuid='G-008', name="Newest group")

        # check new message created
        msg = Message.objects.get(text="Goodbye")
        self.assertEqual(msg.contact, new_contact)
        self.assertEqual(msg.room, new_room)
Пример #2
0
    def test_new_contact(self, mock_get_contact, mock_get_group):
        url = reverse('api.temba_handler', kwargs=dict(entity='contact', action='new'))

        # GET is not allowed
        response = self.url_get('unicef', '%s?%s' % (url, 'contact=C-007&group=G-001&token=1234567890'))
        self.assertEqual(response.status_code, 405)

        # forbidden response if you don't include secret token
        response = self.url_post('unicef', '%s?%s' % (url, 'contact=C-007&group=G-001'))
        self.assertEqual(response.status_code, 403)

        # bad request if you forget a parameter
        response = self.url_post('unicef', '%s?%s' % (url, 'group=G-001&token=1234567890'))
        self.assertEqual(response.status_code, 400)

        # make a valid request for new contact in an existing group
        mock_get_contact.return_value = TembaContact.create(uuid='C-007', name="Jan", urns=['tel:123'],
                                                            groups=['G-001'], fields=dict(chat_name="jan"),
                                                            language='eng', modified_on=timezone.now())

        response = self.url_post('unicef', '%s?%s' % (url, 'contact=C-007&group=G-001&token=1234567890'))
        self.assertEqual(response.status_code, 200)

        # check new contact created
        contact = Contact.objects.get(uuid='C-007')
        self.assertEqual(contact.full_name, "Jan")
        self.assertEqual(contact.chat_name, "jan")
        self.assertEqual(contact.urn, 'tel:123')
        self.assertEqual(contact.room, Room.objects.get(uuid='G-001'))

        # try with new room/group that must be fetched
        mock_get_contact.return_value = TembaContact.create(uuid='C-008', name="Ken", urns=['tel:234'],
                                                            groups=['G-007'], fields=dict(chat_name="ken"),
                                                            language='eng', modified_on=timezone.now())
        mock_get_group.return_value = TembaGroup.create(uuid='G-007', name="New group", size=2)

        response = self.url_post('unicef', '%s?%s' % (url, 'contact=C-008&group=G-007&token=1234567890'))
        self.assertEqual(response.status_code, 200)
        new_room = Room.objects.get(uuid='G-007', name="New group")

        # check new contact and room created
        contact = Contact.objects.get(uuid='C-008')
        self.assertEqual(contact.full_name, "Ken")
        self.assertEqual(contact.chat_name, "ken")
        self.assertEqual(contact.urn, 'tel:234')
        self.assertEqual(contact.room, new_room)

        # check re-activating an inactive room
        new_room.is_active = False
        new_room.save()

        response = self.url_post('unicef', '%s?%s' % (url, 'contact=C-008&group=G-007&token=1234567890'))
        self.assertEqual(response.status_code, 200)
        Room.objects.get(uuid='G-007', name="New group", is_active=True)

        # repeating a request shouldn't create duplicates
        response = self.url_post('unicef', '%s?%s' % (url, 'contact=C-008&group=G-007&token=1234567890'))
        self.assertEqual(response.status_code, 200)
Пример #3
0
    def setUp(self):
        super(TracProTest, self).setUp()
        self.org = factories.Org()

        self.temba_groups = {
            '1': TembaGroup.create(uuid="1", name="Uganda"),
            '2': TembaGroup.create(uuid="2", name="Kampala"),
            '3': TembaGroup.create(uuid="3", name="Entebbe"),
            '4': TembaGroup.create(uuid="4", name="Makerere"),
        }

        self.uganda = factories.Region(
            org=self.org, uuid='1', name="Uganda")
        self.kampala = factories.Region(
            org=self.org, uuid='2', name="Kampala", parent=self.uganda)
        self.entebbe = factories.Region(
            org=self.org, uuid='3', name="Entebbe", parent=self.uganda)
        self.makerere = factories.Region(
            org=self.org, uuid='4', name="makerere", parent=self.kampala)
        self.inactive = factories.Region(
            org=self.org, uuid='5', name="inactive", parent=self.kampala,
            is_active=False)
Пример #4
0
    def test_update_room_groups(self, mock_get_contacts, mock_get_groups):
        mock_get_groups.return_value = [TembaGroup.create(uuid='G-007', name="New group", size=2)]
        mock_get_contacts.return_value = [
            TembaContact.create(uuid='C-007', name="Jan", urns=['tel:123'], groups=['G-007'],
                                fields=dict(chat_name="jan"), language='eng', modified_on=timezone.now()),
            TembaContact.create(uuid='C-008', name="Ken", urns=['tel:234'], groups=['G-007'],
                                fields=dict(chat_name="ken"), language='eng', modified_on=timezone.now())
        ]

        # select one new group
        Room.update_room_groups(self.unicef, ['G-007'])
        self.assertEqual(self.unicef.rooms.filter(is_active=True).count(), 1)
        self.assertEqual(self.unicef.rooms.filter(is_active=False).count(), 3)  # existing de-activated

        new_room = Room.objects.get(uuid='G-007')
        self.assertEqual(new_room.name, "New group")
        self.assertTrue(new_room.is_active)

        # check contact changes
        self.assertEqual(self.unicef.contacts.filter(is_active=True).count(), 2)
        self.assertEqual(self.unicef.contacts.filter(is_active=False).count(), 5)  # existing de-activated

        jan = Contact.objects.get(uuid='C-007')
        self.assertEqual(jan.full_name, "Jan")
        self.assertEqual(jan.chat_name, "jan")
        self.assertEqual(jan.urn, 'tel:123')
        self.assertEqual(jan.room, new_room)
        self.assertTrue(jan.is_active)

        # change group and contacts on chatpro side
        Room.objects.filter(name="New group").update(name="Huh?", is_active=False)
        jan.full_name = "Janet"
        jan.save()
        Contact.objects.filter(full_name="Ken").update(is_active=False)

        # re-select new group
        Room.update_room_groups(self.unicef, ['G-007'])

        # local changes should be overwritten
        self.assertEqual(self.unicef.rooms.get(is_active=True).name, 'New group')
        self.assertEqual(self.unicef.contacts.filter(is_active=True).count(), 2)
        Contact.objects.get(full_name="Jan", is_active=True)
Пример #5
0
    def test_sync_create_new(self, mock_get_contacts, mock_get_groups):
        """Create a new group if the UUID hasn't been seen before."""
        self.temba_groups['6'] = TembaGroup.create(uuid='6', name="New")
        mock_get_groups.return_value = self.temba_groups.values()
        uuids = ['1', '2', '3', '4', '6']
        models.Region.sync_with_temba(self.org, uuids)
        self.refresh_regions()

        new_region = models.Region.objects.get(uuid='6')
        self.assertEqual(set(models.Region.get_all(self.org)), set([
            self.uganda,
            self.kampala,
            self.makerere,
            self.entebbe,
            new_region,
        ]))
        self.assertEqual(new_region.name, "New")
        self.assertIsNone(new_region.parent, None)
        self.assertEqual(mock_get_groups.call_count, 1)
        self.assertEqual(mock_get_contacts.call_count, 1)
Пример #6
0
    def test_new_contact(self, mock_get_contact, mock_get_group):
        url = reverse('api.temba_handler',
                      kwargs=dict(entity='contact', action='new'))

        # GET is not allowed
        response = self.url_get(
            'unicef',
            '%s?%s' % (url, 'contact=C-007&group=G-001&token=1234567890'))
        self.assertEqual(response.status_code, 405)

        # forbidden response if you don't include secret token
        response = self.url_post('unicef',
                                 '%s?%s' % (url, 'contact=C-007&group=G-001'))
        self.assertEqual(response.status_code, 403)

        # bad request if you forget a parameter
        response = self.url_post(
            'unicef', '%s?%s' % (url, 'group=G-001&token=1234567890'))
        self.assertEqual(response.status_code, 400)

        # make a valid request for new contact in an existing group
        mock_get_contact.return_value = TembaContact.create(
            uuid='C-007',
            name="Jan",
            urns=['tel:123'],
            groups=['G-001'],
            fields=dict(chat_name="jan"),
            language='eng',
            modified_on=timezone.now())

        response = self.url_post(
            'unicef',
            '%s?%s' % (url, 'contact=C-007&group=G-001&token=1234567890'))
        self.assertEqual(response.status_code, 200)

        # check new contact created
        contact = Contact.objects.get(uuid='C-007')
        self.assertEqual(contact.full_name, "Jan")
        self.assertEqual(contact.chat_name, "jan")
        self.assertEqual(contact.urn, 'tel:123')
        self.assertEqual(contact.room, Room.objects.get(uuid='G-001'))

        # try with new room/group that must be fetched
        mock_get_contact.return_value = TembaContact.create(
            uuid='C-008',
            name="Ken",
            urns=['tel:234'],
            groups=['G-007'],
            fields=dict(chat_name="ken"),
            language='eng',
            modified_on=timezone.now())
        mock_get_group.return_value = TembaGroup.create(uuid='G-007',
                                                        name="New group",
                                                        size=2)

        response = self.url_post(
            'unicef',
            '%s?%s' % (url, 'contact=C-008&group=G-007&token=1234567890'))
        self.assertEqual(response.status_code, 200)
        new_room = Room.objects.get(uuid='G-007', name="New group")

        # check new contact and room created
        contact = Contact.objects.get(uuid='C-008')
        self.assertEqual(contact.full_name, "Ken")
        self.assertEqual(contact.chat_name, "ken")
        self.assertEqual(contact.urn, 'tel:234')
        self.assertEqual(contact.room, new_room)

        # check re-activating an inactive room
        new_room.is_active = False
        new_room.save()

        response = self.url_post(
            'unicef',
            '%s?%s' % (url, 'contact=C-008&group=G-007&token=1234567890'))
        self.assertEqual(response.status_code, 200)
        Room.objects.get(uuid='G-007', name="New group", is_active=True)

        # repeating a request shouldn't create duplicates
        response = self.url_post(
            'unicef',
            '%s?%s' % (url, 'contact=C-008&group=G-007&token=1234567890'))
        self.assertEqual(response.status_code, 200)
Пример #7
0
    def test_new_message(self, mock_get_contact, mock_get_group):
        url = reverse('api.temba_handler',
                      kwargs=dict(entity='message', action='new'))

        # GET is not allowed
        response = self.url_get(
            'unicef', '%s?%s' %
            (url,
             'contact=C-001&text=Hello%20World&group=G-001&token=1234567890'))
        self.assertEqual(response.status_code, 405)

        # bad request if you forget a parameter
        response = self.url_post(
            'unicef',
            '%s?%s' % (url, 'text=Hello%20World&group=G-001&token=1234567890'))
        self.assertEqual(response.status_code, 400)

        # make a valid request for new message from an existing contact to an existing group
        response = self.url_post(
            'unicef', '%s?%s' %
            (url,
             'contact=C-001&text=Hello%20World&group=G-001&token=1234567890'))
        self.assertEqual(response.status_code, 200)

        # check new message created
        msg = Message.objects.get(text="Hello World")
        self.assertEqual(msg.contact, self.contact1)
        self.assertEqual(msg.room, self.room1)

        # try with new room/group that must be fetched
        mock_get_group.return_value = TembaGroup.create(uuid='G-007',
                                                        name="New group",
                                                        size=2)

        response = self.url_post(
            'unicef', '%s?%s' %
            (url,
             'contact=C-001&text=Hello%20Again&group=G-007&token=1234567890'))
        self.assertEqual(response.status_code, 200)
        new_room = Room.objects.get(uuid='G-007', name="New group")

        # check new message created
        msg = Message.objects.get(text="Hello Again")
        self.assertEqual(msg.contact, self.contact1)
        self.assertEqual(msg.room, new_room)

        # try with new contact and new room/group that must be fetched
        mock_get_group.return_value = TembaGroup.create(uuid='G-008',
                                                        name="Newest group",
                                                        size=2)
        mock_get_contact.return_value = TembaContact.create(
            uuid='C-007',
            name="Ken",
            urns=['tel:234'],
            groups=['G-008'],
            fields=dict(chat_name="ken"),
            language='eng',
            modified_on=timezone.now())

        response = self.url_post(
            'unicef', '%s?%s' %
            (url,
             'contact=001-007&text=Goodbye&group=001-008&token=1234567890'))
        self.assertEqual(response.status_code, 200)
        new_contact = Contact.objects.get(uuid='C-007')
        new_room = Room.objects.get(uuid='G-008', name="Newest group")

        # check new message created
        msg = Message.objects.get(text="Goodbye")
        self.assertEqual(msg.contact, new_contact)
        self.assertEqual(msg.room, new_room)
Пример #8
0
    def test_update_room_groups(self, mock_get_contacts, mock_get_groups):
        mock_get_groups.return_value = [
            TembaGroup.create(uuid='G-007', name="New group", size=2)
        ]
        mock_get_contacts.return_value = [
            TembaContact.create(uuid='C-007',
                                name="Jan",
                                urns=['tel:123'],
                                groups=['G-007'],
                                fields=dict(chat_name="jan"),
                                language='eng',
                                modified_on=timezone.now()),
            TembaContact.create(uuid='C-008',
                                name="Ken",
                                urns=['tel:234'],
                                groups=['G-007'],
                                fields=dict(chat_name="ken"),
                                language='eng',
                                modified_on=timezone.now())
        ]

        # select one new group
        Room.update_room_groups(self.unicef, ['G-007'])
        self.assertEqual(self.unicef.rooms.filter(is_active=True).count(), 1)
        self.assertEqual(self.unicef.rooms.filter(is_active=False).count(),
                         3)  # existing de-activated

        new_room = Room.objects.get(uuid='G-007')
        self.assertEqual(new_room.name, "New group")
        self.assertTrue(new_room.is_active)

        # check contact changes
        self.assertEqual(
            self.unicef.contacts.filter(is_active=True).count(), 2)
        self.assertEqual(
            self.unicef.contacts.filter(is_active=False).count(),
            5)  # existing de-activated

        jan = Contact.objects.get(uuid='C-007')
        self.assertEqual(jan.full_name, "Jan")
        self.assertEqual(jan.chat_name, "jan")
        self.assertEqual(jan.urn, 'tel:123')
        self.assertEqual(jan.room, new_room)
        self.assertTrue(jan.is_active)

        # change group and contacts on chatpro side
        Room.objects.filter(name="New group").update(name="Huh?",
                                                     is_active=False)
        jan.full_name = "Janet"
        jan.save()
        Contact.objects.filter(full_name="Ken").update(is_active=False)

        # re-select new group
        Room.update_room_groups(self.unicef, ['G-007'])

        # local changes should be overwritten
        self.assertEqual(
            self.unicef.rooms.get(is_active=True).name, 'New group')
        self.assertEqual(
            self.unicef.contacts.filter(is_active=True).count(), 2)
        Contact.objects.get(full_name="Jan", is_active=True)
Пример #9
0
    def test_sync_with_groups(self, mock_get_contacts, mock_get_groups):
        mock_get_groups.return_value = [
            TembaGroup.create(uuid='G-101', name="New region", size=2),
            TembaGroup.create(uuid='G-102', name="Other region", size=1)
        ]
        mock_get_contacts.return_value = [
            TembaContact.create(uuid='C-101',
                                name="Jan",
                                urns=['tel:123'],
                                groups=['G-101', 'G-005'],
                                fields=dict(chat_name="jan"),
                                language='eng',
                                modified_on=timezone.now()),
            TembaContact.create(uuid='C-102',
                                name="Ken",
                                urns=['tel:234'],
                                groups=['G-101', 'G-006'],
                                fields=dict(chat_name="ken"),
                                language='eng',
                                modified_on=timezone.now())
        ]

        # select one new group
        Region.sync_with_groups(self.unicef, ['G-101'])
        self.assertEqual(self.unicef.regions.filter(is_active=True).count(), 1)
        self.assertEqual(
            self.unicef.regions.filter(is_active=False).count(),
            3)  # existing de-activated

        new_region = Region.objects.get(uuid='G-101')
        self.assertEqual(new_region.name, "New region")
        self.assertTrue(new_region.is_active)

        # check contact changes
        self.assertEqual(
            self.unicef.contacts.filter(is_active=True).count(), 2)
        self.assertEqual(
            self.unicef.contacts.filter(is_active=False).count(),
            5)  # existing de-activated

        jan = Contact.objects.get(uuid='C-101')
        self.assertEqual(jan.name, "Jan")
        self.assertEqual(jan.urn, 'tel:123')
        self.assertEqual(jan.region, new_region)
        self.assertTrue(jan.is_active)

        # change group and contacts on chatpro side
        Region.objects.filter(name="New region").update(name="Huh?",
                                                        is_active=False)
        jan.name = "Janet"
        jan.save()
        Contact.objects.filter(name="Ken").update(is_active=False)

        # re-select new group
        Region.sync_with_groups(self.unicef, ['G-101'])

        # local changes should be overwritten
        self.assertEqual(
            self.unicef.regions.get(is_active=True).name, 'New region')
        self.assertEqual(
            self.unicef.contacts.filter(is_active=True).count(), 2)
        Contact.objects.get(name="Jan", is_active=True)