Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 1 | import frappe |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 2 | from frappe.contacts.doctype.contact.test_contact import create_contact |
Suraj Shetty | 40d33b5 | 2022-04-04 11:15:29 +0530 | [diff] [blame] | 3 | from frappe.tests.test_api import FrappeAPITestCase |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 4 | |
Rucha Mahabal | 3fe2ef6 | 2022-06-15 12:43:45 +0530 | [diff] [blame] | 5 | from erpnext.setup.doctype.employee.test_employee import make_employee |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 6 | |
| 7 | |
Suraj Shetty | 40d33b5 | 2022-04-04 11:15:29 +0530 | [diff] [blame] | 8 | class TestExotel(FrappeAPITestCase): |
| 9 | @classmethod |
| 10 | def setUpClass(cls): |
| 11 | cls.CURRENT_DB_CONNECTION = frappe.db |
| 12 | cls.test_employee_name = make_employee( |
| 13 | user="test_employee_exotel@company.com", cell_number="9999999999" |
| 14 | ) |
Suraj Shetty | ff41b8d | 2022-04-13 20:12:08 +0530 | [diff] [blame] | 15 | frappe.db.set_value("Exotel Settings", "Exotel Settings", "enabled", 1) |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 16 | phones = [{"phone": "+91 9999999991", "is_primary_phone": 0, "is_primary_mobile_no": 1}] |
Suraj Shetty | 40d33b5 | 2022-04-04 11:15:29 +0530 | [diff] [blame] | 17 | create_contact(name="Test Contact", salutation="Mr", phones=phones) |
| 18 | frappe.db.commit() |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 19 | |
| 20 | def test_for_successful_call(self): |
Suraj Shetty | f4b8573 | 2022-04-04 09:31:15 +0530 | [diff] [blame] | 21 | from .exotel_test_data import call_end_data, call_initiation_data |
| 22 | |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 23 | api_method = "handle_incoming_call" |
| 24 | end_call_api_method = "handle_end_call" |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 25 | |
Suraj Shetty | 40d33b5 | 2022-04-04 11:15:29 +0530 | [diff] [blame] | 26 | self.emulate_api_call_from_exotel(api_method, call_initiation_data) |
| 27 | self.emulate_api_call_from_exotel(end_call_api_method, call_end_data) |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 28 | call_log = frappe.get_doc("Call Log", call_initiation_data.CallSid) |
| 29 | |
| 30 | self.assertEqual(call_log.get("from"), call_initiation_data.CallFrom) |
| 31 | self.assertEqual(call_log.get("to"), call_initiation_data.DialWhomNumber) |
Suraj Shetty | 40d33b5 | 2022-04-04 11:15:29 +0530 | [diff] [blame] | 32 | self.assertEqual(call_log.get("call_received_by"), self.test_employee_name) |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 33 | self.assertEqual(call_log.get("status"), "Completed") |
| 34 | |
| 35 | def test_for_disconnected_call(self): |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 36 | from .exotel_test_data import call_disconnected_data |
Suraj Shetty | f4b8573 | 2022-04-04 09:31:15 +0530 | [diff] [blame] | 37 | |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 38 | api_method = "handle_missed_call" |
Suraj Shetty | 40d33b5 | 2022-04-04 11:15:29 +0530 | [diff] [blame] | 39 | self.emulate_api_call_from_exotel(api_method, call_disconnected_data) |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 40 | call_log = frappe.get_doc("Call Log", call_disconnected_data.CallSid) |
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) |
Suraj Shetty | 40d33b5 | 2022-04-04 11:15:29 +0530 | [diff] [blame] | 43 | self.assertEqual(call_log.get("call_received_by"), self.test_employee_name) |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 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 | 40d33b5 | 2022-04-04 11:15:29 +0530 | [diff] [blame] | 50 | self.emulate_api_call_from_exotel(api_method, call_not_answered_data) |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 51 | call_log = frappe.get_doc("Call Log", call_not_answered_data.CallSid) |
Suraj Shetty | 0e9ebad | 2022-04-04 07:23:08 +0530 | [diff] [blame] | 52 | self.assertEqual(call_log.get("from"), call_not_answered_data.CallFrom) |
| 53 | self.assertEqual(call_log.get("to"), call_not_answered_data.DialWhomNumber) |
Suraj Shetty | 40d33b5 | 2022-04-04 11:15:29 +0530 | [diff] [blame] | 54 | self.assertEqual(call_log.get("call_received_by"), self.test_employee_name) |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 55 | self.assertEqual(call_log.get("status"), "No Answer") |
| 56 | |
Suraj Shetty | 40d33b5 | 2022-04-04 11:15:29 +0530 | [diff] [blame] | 57 | def emulate_api_call_from_exotel(self, api_method, data): |
| 58 | self.post( |
| 59 | f"/api/method/erpnext.erpnext_integrations.exotel_integration.{api_method}", |
| 60 | data=frappe.as_json(data), |
| 61 | content_type="application/json", |
Suraj Shetty | 40d33b5 | 2022-04-04 11:15:29 +0530 | [diff] [blame] | 62 | ) |
| 63 | # restart db connection to get latest data |
| 64 | frappe.connect() |
Subin Tom | e57e7bb | 2022-04-02 17:16:56 +0530 | [diff] [blame] | 65 | |
Suraj Shetty | 40d33b5 | 2022-04-04 11:15:29 +0530 | [diff] [blame] | 66 | @classmethod |
| 67 | def tearDownClass(cls): |
| 68 | frappe.db = cls.CURRENT_DB_CONNECTION |