Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 1 | import os |
| 2 | import time |
| 3 | import unittest |
| 4 | |
| 5 | import frappe |
| 6 | import requests |
| 7 | from frappe.contacts.doctype.contact.test_contact import create_contact |
| 8 | |
| 9 | from erpnext.hr.doctype.employee.test_employee import make_employee |
| 10 | |
| 11 | |
| 12 | class TestExotel(unittest.TestCase): |
| 13 | def setUp(self): |
| 14 | make_employee("test_employee_exotel@company.com", cell_number="9999999999") |
| 15 | phones = [{"phone": "+91 9999999991", "is_primary_phone": 0, "is_primary_mobile_no": 1}] |
| 16 | create_contact("Test Contact", "Mr", phones=phones) |
| 17 | |
| 18 | def test_for_successful_call(self): |
Suraj Shetty | f4b8573 | 2022-04-04 09:31:15 +0530 | [diff] [blame^] | 19 | from .exotel_test_data import call_end_data, call_initiation_data |
| 20 | |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 21 | api_method = "handle_incoming_call" |
| 22 | end_call_api_method = "handle_end_call" |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 23 | |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 24 | emulate_api_call(call_initiation_data, api_method) |
| 25 | emulate_api_call(call_end_data, end_call_api_method) |
Suraj Shetty | f4b8573 | 2022-04-04 09:31:15 +0530 | [diff] [blame^] | 26 | |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 27 | call_log = frappe.get_doc("Call Log", call_initiation_data.CallSid) |
| 28 | |
| 29 | self.assertEqual(call_log.get("from"), call_initiation_data.CallFrom) |
| 30 | self.assertEqual(call_log.get("to"), call_initiation_data.DialWhomNumber) |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 31 | self.assertEqual(call_log.get("call_received_by"), "EMP-00001") |
| 32 | self.assertEqual(call_log.get("status"), "Completed") |
| 33 | |
| 34 | def test_for_disconnected_call(self): |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 35 | from .exotel_test_data import call_disconnected_data |
Suraj Shetty | f4b8573 | 2022-04-04 09:31:15 +0530 | [diff] [blame^] | 36 | |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 37 | api_method = "handle_missed_call" |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 38 | emulate_api_call(call_disconnected_data, api_method) |
| 39 | call_log = frappe.get_doc("Call Log", call_disconnected_data.CallSid) |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 40 | |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 41 | self.assertEqual(call_log.get("from"), call_disconnected_data.CallFrom) |
| 42 | self.assertEqual(call_log.get("to"), call_disconnected_data.DialWhomNumber) |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 43 | self.assertEqual(call_log.get("call_received_by"), "EMP-00001") |
| 44 | self.assertEqual(call_log.get("status"), "Canceled") |
| 45 | |
| 46 | def test_for_call_not_answered(self): |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 47 | from .exotel_test_data import call_not_answered_data |
Suraj Shetty | f4b8573 | 2022-04-04 09:31:15 +0530 | [diff] [blame^] | 48 | |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 49 | api_method = "handle_missed_call" |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 50 | emulate_api_call(call_not_answered_data, api_method) |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 51 | |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 52 | call_log = frappe.get_doc("Call Log", call_not_answered_data.CallSid) |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 53 | |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 54 | self.assertEqual(call_log.get("from"), call_not_answered_data.CallFrom) |
| 55 | self.assertEqual(call_log.get("to"), call_not_answered_data.DialWhomNumber) |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 56 | self.assertEqual(call_log.get("call_received_by"), "EMP-00001") |
| 57 | self.assertEqual(call_log.get("status"), "No Answer") |
| 58 | |
| 59 | def tearDown(self): |
| 60 | frappe.db.rollback() |
| 61 | |
| 62 | |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 63 | def emulate_api_call(data, api_method): |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 64 | # Build URL |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 65 | url = get_exotel_handler_endpoint(api_method) |
| 66 | res = requests.post(url=url, data=frappe.as_json(data)) |
| 67 | res.raise_for_status() |
| 68 | time.sleep(1) |
| 69 | |
Suraj Shetty | f4b8573 | 2022-04-04 09:31:15 +0530 | [diff] [blame^] | 70 | |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 71 | def get_exotel_handler_endpoint(method): |
| 72 | site = "localhost" if os.environ.get("CI") else frappe.local.site |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 73 | port = frappe.get_site_config().webserver_port or "8000" |
Suraj Shetty | f4b8573 | 2022-04-04 09:31:15 +0530 | [diff] [blame^] | 74 | return f"http://{site}:{port}/api/method/erpnext.erpnext_integrations.exotel_integration.{method}" |