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)
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)
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)
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)
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)
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)
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())
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) }
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)
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)
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)
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"]))
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)
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)
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)
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)
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) }] }
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) }
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')
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) } ] }
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'] }
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')
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))
def test_should_not_convert_type_label_when_none(self): self.assertIsNone(ContactService.convert_contact_types(None))
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)
def get_object(self, pk): return ContactService.get(pk)
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
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)
def execute_one_time_sync(): contacts = ContactService.get_all() for contact in contacts: ContactService.add_or_update_rapid_pro_contact(contact)
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)
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)
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)
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())
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)
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)
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)
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)
def build_contact(self): logger.info('contact person id = %s' % self.contact_person_id) return ContactService.get(self.contact_person_id)
def put(self, request): response_code = ContactService.update(request.data) ContactEndpoint.__add_or_update_rapid_pro_contact(request.data) return Response(status=response_code)