示例#1
0
    def test_should_add_rapid_pro_contact(self, rapid_pro_contact):
        first_name = "Jack"
        last_name = "Bob"
        phone = '+8618192235667'
        outcomes = ["YI105 - PCR 1 KEEP CHILDREN AND MOTHERS"]
        districts = ["Kampala"]
        ips = ["KAMPALA DHO, WAKISO DHO"]
        types = ["END_USER", "IMPLEMENTING_PARTNER"]

        contact = self.generate_eums_contact(districts, first_name, ips, last_name, outcomes, phone, types)
        rapid_pro_contact.return_value = self.generate_build_rapid_pro_contact(contact)
        requests.get = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(
            return_value={'results': []}
        )))
        requests.post = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(
            return_value=self.generate_add_or_update_rapid_pro_contact_response(contact))))

        response = ContactService.add_or_update_rapid_pro_contact(contact)

        self.assertEqual(response.status_code, HTTP_200_OK)
        self.assertEqual(len(response.json().get('groups')), 1)
        self.assertEqual(response.json().get('groups')[0], 'EUMS')

        fields = response.json().get("fields")
        self.assertEqual(fields.get('firstname'), first_name)
        self.assertEqual(fields.get('lastname'), last_name)
        self.assertEqual(fields.get('outcomes'), ','.join(outcomes))
        self.assertEqual(fields.get('districts'), ','.join(districts))
        self.assertEqual(fields.get('ips'), ','.join(ips))
        self.assertEqual(fields.get('types'), ','.join(["End-user", "IP"]))

        requests.post.assert_called_once_with(settings.RAPIDPRO_URLS.get('CONTACTS'),
                                              data=json.dumps(ContactService.build_rapid_pro_contact(contact)),
                                              headers=HEADER,
                                              verify=settings.RAPIDPRO_SSL_VERIFY)
示例#2
0
def update_contact(runnable):
    tree_position = getattr(runnable, 'tree_position', Flow.Label.IMPLEMENTING_PARTNER)
    ContactService.update_after_delivery_creation(runnable.contact_person_id,
                                                  tree_position,
                                                  runnable.programme.name,
                                                  runnable.location,
                                                  runnable.consignee.name)
示例#3
0
    def test_should_add_rapid_pro_contact(self):
        requests.get = MagicMock()
        requests.post = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(
            return_value=CONTACT)))
        ContactService.add_or_update_rapid_pro_contact(CONTACT)

        self.assertTrue(requests.post.called)
        self.assertFalse(requests.get.called)
示例#4
0
    def test_should_not_update_contact_when_contact_not_modified(self, update, get):
        get.return_value = CONTACT
        update.return_value = 200

        ContactService.update_after_delivery_creation(CONTACT['_id'], type='End-user',
                                                      outcome='YI105 - PCR 1 KEEP CHILDREN AND MOTHERS',
                                                      ip='WAKISO DHA', district='Wakiso')
        ContactService.get.assert_called_once_with(CONTACT['_id'])
        ContactService.update.assert_not_called(CONTACT)
示例#5
0
    def test_should_add_rapid_pro_contact_called(self, rapid_pro_contact):
        rapid_pro_contact.return_value = self.generate_build_rapid_pro_contact(CONTACT)
        requests.get = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(
            return_value={'results': []}
        )))
        requests.post = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(
            return_value=CONTACT)))
        ContactService.add_or_update_rapid_pro_contact(CONTACT)

        self.assertTrue(requests.post.called)
示例#6
0
    def test_should_update_contact_when_contact_districts_modified(self, update, get):
        get.return_value = CONTACT
        update.return_value = 200

        ContactService.update_after_delivery_creation(CONTACT['_id'], type='End-user',
                                                      outcome='YI105 - PCR 1 KEEP CHILDREN AND MOTHERS',
                                                      ip='WAKISO DHO', district='Abum')

        CONTACT.update({'districts': ['Wakiso', 'Abum']})

        ContactService.get.assert_called_once_with(CONTACT['_id'])
        ContactService.update.assert_called_once_with(CONTACT)
示例#7
0
    def get(self, request, pk=None):
        if pk:
            return Response(status=HTTP_200_OK, data=ContactService.get(pk))

        created_by_user_id = request.GET.get('createdbyuserid')
        if created_by_user_id:
            return Response(status=HTTP_200_OK, data=ContactService.get_by_user_id(created_by_user_id))

        search_field = request.GET.get('searchfield')
        if search_field:
            return Response(status=HTTP_200_OK, data=ContactService.search(search_field))

        return Response(status=HTTP_200_OK, data=ContactService.get_all())
示例#8
0
 def generate_build_rapid_pro_contact(self, contact):
     return {
         'name': 'Jack Bob',
         'groups': [{'name': 'EUMS', 'uuid': '362bcb66-6f2e-4899-b78b-187309bdf636'}],
         'urns': ['tel:%s' % contact.get('phone')],
         'fields': ContactService.build_rapid_pro_contact_fields(contact)
     }
示例#9
0
    def test_should_update_rapid_pro_contact_when_phone_not_modified(self):
        requests.get = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(
            return_value=self.generate_add_or_update_rapid_pro_contact_response(CONTACT))))

        new_contact = copy.deepcopy(CONTACT)
        new_contact.update({
            'prePhone': CONTACT['phone'],
            'phone': CONTACT['phone']
        })
        requests.post = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(
            return_value=new_contact)))

        ContactService.add_or_update_rapid_pro_contact(new_contact)

        self.assertTrue(requests.post.called)
        self.assertFalse(requests.get.called)
示例#10
0
    def test_should_update_rapid_pro_contact_when_phone_not_modified(self, rapid_pro_contact):
        rapid_pro_contact.return_value = self.generate_build_rapid_pro_contact(CONTACT)
        requests.get = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(
            return_value=self.generate_add_or_update_rapid_pro_contact_response(CONTACT))))

        new_contact = copy.deepcopy(CONTACT)
        new_contact.update({
            'prePhone': CONTACT['phone'],
            'phone': CONTACT['phone']
        })
        requests.post = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(
            return_value=new_contact)))

        ContactService.add_or_update_rapid_pro_contact(new_contact)

        self.assertTrue(requests.post.called)
示例#11
0
    def test_should_delete_contact(self):
        requests.delete = MagicMock(return_value=MagicMock(status_code=204))
        response = ContactService.delete(CONTACT.get('_id'))

        requests.delete.assert_called_once_with(url='%s%s' % (settings.CONTACTS_SERVICE_URL, CONTACT['_id']))

        self.assertEqual(response, 204)
示例#12
0
    def test_should_get_rapid_pro_contact(self):
        first_name = "Jack"
        last_name = "Bob"
        phone = '+8618192235667'
        outcomes = ["YI105 - PCR 1 KEEP CHILDREN AND MOTHERS"]
        districts = ["Kampala"]
        ips = ["KAMPALA DHO, WAKISO DHO"]
        types = ["END_USER", "IMPLEMENTING_PARTNER"]

        url_add_rapid_pro_contact = '%s?%s' % (settings.RAPIDPRO_URLS.get('CONTACTS'), urlencode({
            'urns': 'tel:%s' % phone
        }))

        contact = self.generate_eums_contact(districts, first_name, ips, last_name, outcomes, phone, types)
        requests.get = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(
            return_value=self.generate_add_or_update_rapid_pro_contact_response(contact))))
        response = ContactService.get_rapid_pro_contact(phone)

        requests.get.assert_called_once_with(url_add_rapid_pro_contact, headers=HEADER,
                                             verify=settings.RAPIDPRO_SSL_VERIFY)

        self.assertEqual(response.status_code, HTTP_200_OK)
        self.assertEqual(len(response.json().get('groups')), 1)
        self.assertEqual(response.json().get('groups')[0], 'EUMS')

        fields = response.json().get("fields")
        self.assertEqual(fields.get('firstname'), first_name)
        self.assertEqual(fields.get('lastname'), last_name)
        self.assertEqual(fields.get('outcomes'), ','.join(outcomes))
        self.assertEqual(fields.get('districts'), ','.join(districts))
        self.assertEqual(fields.get('ips'), ','.join(ips))
        self.assertEqual(fields.get('types'), ','.join(["End-user", "IP"]))
示例#13
0
    def test_should_delete_contact(self):
        requests.delete = MagicMock(return_value=MagicMock(status_code=204))
        response = ContactService.delete(CONTACT.get('_id'))

        requests.delete.assert_called_once_with(url='%s%s' % (settings.CONTACTS_SERVICE_URL, CONTACT['_id']))

        self.assertEqual(response, 204)
示例#14
0
    def test_should_update_rapid_pro_contact_when_phone_modified(self):
        requests.get = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(
            return_value=self.generate_get_rapid_pro_contact_response(CONTACT))))

        new_contact = copy.deepcopy(CONTACT)
        new_phone = '+8618694029575'
        new_contact.update({
            'prePhone': CONTACT['phone'],
            'phone': new_phone
        })
        requests.post = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(
            return_value=self.generate_add_or_update_rapid_pro_contact_response(CONTACT))))

        logger.info(new_contact)
        ContactService.add_or_update_rapid_pro_contact(new_contact)

        self.assertTrue(requests.post.called)
示例#15
0
    def test_should_update_contact(self):
        requests.put = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(return_value=CONTACT)))
        response = ContactService.update(CONTACT)

        requests.put.assert_called_once_with('%s' % settings.CONTACTS_SERVICE_URL, json.dumps(CONTACT),
                                             headers=HEADER_CONTACT)

        self.assertEqual(response, 200)
示例#16
0
    def test_should_update_contact(self):
        requests.put = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(return_value=CONTACT)))
        response = ContactService.update(CONTACT)

        requests.put.assert_called_once_with('%s' % settings.CONTACTS_SERVICE_URL, json.dumps(CONTACT),
                                             headers=HEADER_CONTACT)

        self.assertEqual(response, 200)
示例#17
0
    def test_should_add_contact(self):
        requests.post = MagicMock(return_value=MagicMock(status_code=201, json=MagicMock(return_value=CONTACT)))
        response = ContactService.add(CONTACT)

        requests.post.assert_called_once_with('%s' % settings.CONTACTS_SERVICE_URL, json.dumps(CONTACT),
                                              headers=HEADER_CONTACT)

        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.json(), CONTACT)
示例#18
0
    def test_should_add_contact(self):
        requests.post = MagicMock(return_value=MagicMock(status_code=201, json=MagicMock(return_value=CONTACT)))
        response = ContactService.add(CONTACT)

        requests.post.assert_called_once_with('%s' % settings.CONTACTS_SERVICE_URL, json.dumps(CONTACT),
                                              headers=HEADER_CONTACT)

        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.json(), CONTACT)
示例#19
0
    def test_should_delete_rapid_pro_contact(self):
        phone = '+8618192235667'
        url_delete_rapid_pro_contact = '%s?%s' % (settings.RAPIDPRO_URLS.get('CONTACTS'), urlencode({
            'urns': 'tel:%s' % phone
        }))
        requests.delete = MagicMock(return_value=MagicMock(status_code=HTTP_204_NO_CONTENT))
        response = ContactService.delete_rapid_pro_contact(phone)

        requests.delete.assert_called_once_with(url_delete_rapid_pro_contact, headers=HEADER,
                                                verify=settings.RAPIDPRO_SSL_VERIFY)
        self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
示例#20
0
    def test_should_delete_rapid_pro_contact(self):
        phone = '+8618192235667'
        url_delete_rapid_pro_contact = '%s?%s' % (settings.RAPIDPRO_URLS.get('CONTACTS'), urlencode({
            'urns': 'tel:%s' % phone
        }))
        requests.delete = MagicMock(return_value=MagicMock(status_code=HTTP_204_NO_CONTENT))
        response = ContactService.delete_rapid_pro_contact(phone)

        requests.delete.assert_called_once_with(url_delete_rapid_pro_contact, headers=HEADER,
                                                verify=settings.RAPIDPRO_SSL_VERIFY)
        self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
示例#21
0
 def generate_get_rapid_pro_contact_response(self, contact):
     return {
         "results": [{
             "uuid":
             "52fa33e7-da32-4703-8feb-aa5114dc1a8b",
             "name":
             "Jack Bob",
             "urns": ["tel:%s" % contact.get('phone')],
             "fields":
             ContactService.build_rapid_pro_contact_fields(contact)
         }]
     }
示例#22
0
 def generate_build_rapid_pro_contact(self, contact):
     return {
         'name':
         'Jack Bob',
         'groups': [{
             'name': 'EUMS',
             'uuid': '362bcb66-6f2e-4899-b78b-187309bdf636'
         }],
         'urns': ['tel:%s' % contact.get('phone')],
         'fields':
         ContactService.build_rapid_pro_contact_fields(contact)
     }
示例#23
0
 def test_should_get_rapid_pro_contact_group(self):
     group_name = 'EUMS'
     requests.get = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(return_value=
         self.generate_rapid_pro_contact_group())))
     url_get_rapid_pro_contact_group = '%s?%s' % (settings.RAPIDPRO_URLS.get('GROUPS'), urlencode({
         'name': '%s' % group_name
     }))
     response = ContactService.get_rapid_pro_contact_group(group_name)
     requests.get.assert_called_once_with(url_get_rapid_pro_contact_group,
                                          headers=HEADER, verify=settings.RAPIDPRO_SSL_VERIFY)
     self.assertEqual(response.get('name'), 'EUMS')
     self.assertEqual(response.get('uuid'), '362bcb66-6f2e-4899-b78b-187309bdf636')
示例#24
0
 def generate_get_rapid_pro_contact_response(self, contact):
     return {
         "results": [
             {
                 "uuid": "52fa33e7-da32-4703-8feb-aa5114dc1a8b",
                 "name": "Jack Bob",
                 "urns": [
                     "tel:%s" % contact.get('phone')
                 ],
                 "fields": ContactService.build_rapid_pro_contact_fields(contact)
             }
         ]
     }
示例#25
0
 def generate_add_or_update_rapid_pro_contact_response(self, contact):
     return {
         "uuid": "e5de51c0-844b-4feb-8023-33b180bdf965",
         "name": "Jack Bob",
         "language": None,
         "group_uuids": ["fbc775f2-03e3-428e-93a2-608d7a7b46dc"],
         "urns": ["tel:%s" % contact.get('phone')],
         "fields": ContactService.build_rapid_pro_contact_fields(contact),
         "blocked": False,
         "failed": False,
         "modified_on": "2016-03-07T09:09:46.429Z",
         "phone": contact.get('phone'),
         "groups": ['EUMS']
     }
示例#26
0
 def generate_add_or_update_rapid_pro_contact_response(self, contact):
     return {
         "uuid": "e5de51c0-844b-4feb-8023-33b180bdf965",
         "name": "Jack Bob",
         "language": None,
         "group_uuids": ["fbc775f2-03e3-428e-93a2-608d7a7b46dc"],
         "urns": ["tel:%s" % contact.get('phone')],
         "fields": ContactService.build_rapid_pro_contact_fields(contact),
         "blocked": False,
         "failed": False,
         "modified_on": "2016-03-07T09:09:46.429Z",
         "phone": contact.get('phone'),
         "groups": ['EUMS']
     }
示例#27
0
 def test_should_get_rapid_pro_contact_group(self):
     group_name = 'EUMS'
     requests.get = MagicMock(return_value=MagicMock(
         status_code=200,
         json=MagicMock(
             return_value=self.generate_rapid_pro_contact_group())))
     url_get_rapid_pro_contact_group = '%s?%s' % (
         settings.RAPIDPRO_URLS.get('GROUPS'),
         urlencode({'name': '%s' % group_name}))
     response = ContactService.get_rapid_pro_contact_group(group_name)
     requests.get.assert_called_once_with(
         url_get_rapid_pro_contact_group,
         headers=HEADER,
         verify=settings.RAPIDPRO_SSL_VERIFY)
     self.assertEqual(response.get('name'), 'EUMS')
     self.assertEqual(response.get('uuid'),
                      '362bcb66-6f2e-4899-b78b-187309bdf636')
示例#28
0
 def test_should_partly_convert_type_label(self):
     pre_types = ['IMPLEMENTING_PARTNER', 'MIDDLE', 'END_USER']
     self.assertEquals(['IP', 'End-user'], ContactService.convert_contact_types(pre_types))
示例#29
0
 def test_should_not_convert_type_label_when_none(self):
     self.assertIsNone(ContactService.convert_contact_types(None))
示例#30
0
 def test_should_not_add_contact_when_rapid_pro_off(self):
     requests.post = MagicMock()
     ContactService.add_or_update_rapid_pro_contact(CONTACT)
     self.assertFalse(requests.post.called)
示例#31
0
 def get_object(self, pk):
     return ContactService.get(pk)
示例#32
0
def set_remote_contact_to_report_item(report_item):
    contact = ContactService.get(report_item.get('contactPersonId', report_item.get('contact_person_id')))
    report_item['contactName'] = '%s %s' % (contact.get('firstName'), contact.get('lastName'))
    report_item['contactPhone'] = contact.get('phone')
    return report_item
示例#33
0
 def test_should_not_convert_type_label_when_none(self):
     self.assertIsNone(ContactService.convert_contact_types(None))
示例#34
0
    def test_should_search_contact_by_phone(self):
        requests.get = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(return_value=CONTACT)))
        response = ContactService.get(CONTACT['phone'])

        self.assertEqual(response, CONTACT)
示例#35
0
def execute_one_time_sync():
    contacts = ContactService.get_all()
    for contact in contacts:
        ContactService.add_or_update_rapid_pro_contact(contact)
示例#36
0
 def test_should_partly_convert_type_label(self):
     pre_types = ['IMPLEMENTING_PARTNER', 'MIDDLE', 'END_USER']
     self.assertEquals(['IP', 'End-user'],
                       ContactService.convert_contact_types(pre_types))
示例#37
0
 def test_should_not_get_contact_when_rapid_pro_off(self):
     phone = '+8618192235667'
     requests.get = MagicMock()
     ContactService.get_rapid_pro_contact(phone)
     self.assertFalse(requests.get.called)
示例#38
0
    def test_should_search_contact_by_phone(self):
        requests.get = MagicMock(return_value=MagicMock(
            status_code=200, json=MagicMock(return_value=CONTACT)))
        response = ContactService.get(CONTACT['phone'])

        self.assertEqual(response, CONTACT)
示例#39
0
    def test_should_get_contact_by_user_id(self):
        requests.get = MagicMock(return_value=MagicMock(
            status_code=200, json=MagicMock(return_value=CONTACT)))
        response = ContactService.get_by_user_id('17')

        self.assertEqual(response, CONTACT)
示例#40
0
    def post(self, request):
        response = ContactService.add(request.data)
        ContactEndpoint.__add_or_update_rapid_pro_contact(request.data)

        return Response(status=response.status_code, data=response.json())
示例#41
0
 def test_should_not_delete_contact_when_rapid_pro_off(self):
     phone = '+8618192235667'
     requests.delete = MagicMock(return_value=MagicMock(
         status_code=HTTP_204_NO_CONTENT))
     ContactService.delete_rapid_pro_contact(phone)
     self.assertFalse(requests.delete.called)
示例#42
0
    def test_should_get_contact_by_user_id(self):
        requests.get = MagicMock(return_value=MagicMock(status_code=200, json=MagicMock(return_value=CONTACT)))
        response = ContactService.get_by_user_id('17')

        self.assertEqual(response, CONTACT)
示例#43
0
 def test_should_not_delete_contact_when_rapid_pro_off(self):
     phone = '+8618192235667'
     requests.delete = MagicMock(return_value=MagicMock(status_code=HTTP_204_NO_CONTENT))
     ContactService.delete_rapid_pro_contact(phone)
     self.assertFalse(requests.delete.called)
示例#44
0
    def delete(self, request, pk):
        current_contact = self.get_object(pk)
        response_code = ContactService.delete(pk)
        ContactEndpoint.__delete_rapid_pro_contact(current_contact.get('phone'))

        return Response(status=response_code)
示例#45
0
 def test_should_not_get_contact_when_rapid_pro_off(self):
     phone = '+8618192235667'
     requests.get = MagicMock()
     ContactService.get_rapid_pro_contact(phone)
     self.assertFalse(requests.get.called)
示例#46
0
 def build_contact(self):
     logger.info('contact person id = %s' % self.contact_person_id)
     return ContactService.get(self.contact_person_id)
示例#47
0
    def put(self, request):
        response_code = ContactService.update(request.data)

        ContactEndpoint.__add_or_update_rapid_pro_contact(request.data)

        return Response(status=response_code)
示例#48
0
 def build_contact(self):
     logger.info('contact person id = %s' % self.contact_person_id)
     return ContactService.get(self.contact_person_id)
示例#49
0
 def test_should_not_add_contact_when_rapid_pro_off(self):
     requests.post = MagicMock()
     ContactService.add_or_update_rapid_pro_contact(CONTACT)
     self.assertFalse(requests.post.called)