blob: 1b4299c0bb54788dabf1a2746d1a4258aff0b409 [file] [log] [blame]
Subin Tome57e7bb2022-04-02 17:16:56 +05301import frappe
Subin Tome57e7bb2022-04-02 17:16:56 +05302from frappe.contacts.doctype.contact.test_contact import create_contact
Suraj Shetty40d33b52022-04-04 11:15:29 +05303from frappe.tests.test_api import FrappeAPITestCase
Subin Tome57e7bb2022-04-02 17:16:56 +05304
Rucha Mahabal3fe2ef62022-06-15 12:43:45 +05305from erpnext.setup.doctype.employee.test_employee import make_employee
Subin Tome57e7bb2022-04-02 17:16:56 +05306
7
Suraj Shetty40d33b52022-04-04 11:15:29 +05308class 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 Shettyff41b8d2022-04-13 20:12:08 +053015 frappe.db.set_value("Exotel Settings", "Exotel Settings", "enabled", 1)
Subin Tome57e7bb2022-04-02 17:16:56 +053016 phones = [{"phone": "+91 9999999991", "is_primary_phone": 0, "is_primary_mobile_no": 1}]
Suraj Shetty40d33b52022-04-04 11:15:29 +053017 create_contact(name="Test Contact", salutation="Mr", phones=phones)
18 frappe.db.commit()
Subin Tome57e7bb2022-04-02 17:16:56 +053019
20 def test_for_successful_call(self):
Suraj Shettyf4b85732022-04-04 09:31:15 +053021 from .exotel_test_data import call_end_data, call_initiation_data
22
Subin Tome57e7bb2022-04-02 17:16:56 +053023 api_method = "handle_incoming_call"
24 end_call_api_method = "handle_end_call"
Subin Tome57e7bb2022-04-02 17:16:56 +053025
Suraj Shetty40d33b52022-04-04 11:15:29 +053026 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 Shetty0e9ebad2022-04-04 07:23:08 +053028 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 Shetty40d33b52022-04-04 11:15:29 +053032 self.assertEqual(call_log.get("call_received_by"), self.test_employee_name)
Subin Tome57e7bb2022-04-02 17:16:56 +053033 self.assertEqual(call_log.get("status"), "Completed")
34
35 def test_for_disconnected_call(self):
Suraj Shetty0e9ebad2022-04-04 07:23:08 +053036 from .exotel_test_data import call_disconnected_data
Suraj Shettyf4b85732022-04-04 09:31:15 +053037
Subin Tome57e7bb2022-04-02 17:16:56 +053038 api_method = "handle_missed_call"
Suraj Shetty40d33b52022-04-04 11:15:29 +053039 self.emulate_api_call_from_exotel(api_method, call_disconnected_data)
Suraj Shetty0e9ebad2022-04-04 07:23:08 +053040 call_log = frappe.get_doc("Call Log", call_disconnected_data.CallSid)
Suraj Shetty0e9ebad2022-04-04 07:23:08 +053041 self.assertEqual(call_log.get("from"), call_disconnected_data.CallFrom)
42 self.assertEqual(call_log.get("to"), call_disconnected_data.DialWhomNumber)
Suraj Shetty40d33b52022-04-04 11:15:29 +053043 self.assertEqual(call_log.get("call_received_by"), self.test_employee_name)
Subin Tome57e7bb2022-04-02 17:16:56 +053044 self.assertEqual(call_log.get("status"), "Canceled")
45
46 def test_for_call_not_answered(self):
Suraj Shetty0e9ebad2022-04-04 07:23:08 +053047 from .exotel_test_data import call_not_answered_data
Suraj Shettyf4b85732022-04-04 09:31:15 +053048
Subin Tome57e7bb2022-04-02 17:16:56 +053049 api_method = "handle_missed_call"
Suraj Shetty40d33b52022-04-04 11:15:29 +053050 self.emulate_api_call_from_exotel(api_method, call_not_answered_data)
Suraj Shetty0e9ebad2022-04-04 07:23:08 +053051 call_log = frappe.get_doc("Call Log", call_not_answered_data.CallSid)
Suraj Shetty0e9ebad2022-04-04 07:23:08 +053052 self.assertEqual(call_log.get("from"), call_not_answered_data.CallFrom)
53 self.assertEqual(call_log.get("to"), call_not_answered_data.DialWhomNumber)
Suraj Shetty40d33b52022-04-04 11:15:29 +053054 self.assertEqual(call_log.get("call_received_by"), self.test_employee_name)
Subin Tome57e7bb2022-04-02 17:16:56 +053055 self.assertEqual(call_log.get("status"), "No Answer")
56
Suraj Shetty40d33b52022-04-04 11:15:29 +053057 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",
62 as_tuple=True,
63 )
64 # restart db connection to get latest data
65 frappe.connect()
Subin Tome57e7bb2022-04-02 17:16:56 +053066
Suraj Shetty40d33b52022-04-04 11:15:29 +053067 @classmethod
68 def tearDownClass(cls):
69 frappe.db = cls.CURRENT_DB_CONNECTION