brotherton-erpnext/erpnext/tests/test_exotel.py

69 lines
2.8 KiB
Python
Raw Normal View History

2022-04-02 11:46:56 +00:00
import frappe
from frappe.contacts.doctype.contact.test_contact import create_contact
from frappe.tests.test_api import FrappeAPITestCase
2022-04-02 11:46:56 +00:00
2022-06-15 07:13:45 +00:00
from erpnext.setup.doctype.employee.test_employee import make_employee
2022-04-02 11:46:56 +00:00
class TestExotel(FrappeAPITestCase):
@classmethod
def setUpClass(cls):
cls.CURRENT_DB_CONNECTION = frappe.db
cls.test_employee_name = make_employee(
user="test_employee_exotel@company.com", cell_number="9999999999"
)
frappe.db.set_single_value("Exotel Settings", "enabled", 1)
2022-04-02 11:46:56 +00:00
phones = [{"phone": "+91 9999999991", "is_primary_phone": 0, "is_primary_mobile_no": 1}]
create_contact(name="Test Contact", salutation="Mr", phones=phones)
frappe.db.commit()
2022-04-02 11:46:56 +00:00
def test_for_successful_call(self):
2022-04-04 04:01:15 +00:00
from .exotel_test_data import call_end_data, call_initiation_data
2022-04-02 11:46:56 +00:00
api_method = "handle_incoming_call"
end_call_api_method = "handle_end_call"
self.emulate_api_call_from_exotel(api_method, call_initiation_data)
self.emulate_api_call_from_exotel(end_call_api_method, call_end_data)
call_log = frappe.get_doc("Call Log", call_initiation_data.CallSid)
self.assertEqual(call_log.get("from"), call_initiation_data.CallFrom)
self.assertEqual(call_log.get("to"), call_initiation_data.DialWhomNumber)
self.assertEqual(call_log.get("call_received_by"), self.test_employee_name)
2022-04-02 11:46:56 +00:00
self.assertEqual(call_log.get("status"), "Completed")
def test_for_disconnected_call(self):
from .exotel_test_data import call_disconnected_data
2022-04-04 04:01:15 +00:00
2022-04-02 11:46:56 +00:00
api_method = "handle_missed_call"
self.emulate_api_call_from_exotel(api_method, call_disconnected_data)
call_log = frappe.get_doc("Call Log", call_disconnected_data.CallSid)
self.assertEqual(call_log.get("from"), call_disconnected_data.CallFrom)
self.assertEqual(call_log.get("to"), call_disconnected_data.DialWhomNumber)
self.assertEqual(call_log.get("call_received_by"), self.test_employee_name)
2022-04-02 11:46:56 +00:00
self.assertEqual(call_log.get("status"), "Canceled")
def test_for_call_not_answered(self):
from .exotel_test_data import call_not_answered_data
2022-04-04 04:01:15 +00:00
2022-04-02 11:46:56 +00:00
api_method = "handle_missed_call"
self.emulate_api_call_from_exotel(api_method, call_not_answered_data)
call_log = frappe.get_doc("Call Log", call_not_answered_data.CallSid)
self.assertEqual(call_log.get("from"), call_not_answered_data.CallFrom)
self.assertEqual(call_log.get("to"), call_not_answered_data.DialWhomNumber)
self.assertEqual(call_log.get("call_received_by"), self.test_employee_name)
2022-04-02 11:46:56 +00:00
self.assertEqual(call_log.get("status"), "No Answer")
def emulate_api_call_from_exotel(self, api_method, data):
self.post(
f"/api/method/erpnext.erpnext_integrations.exotel_integration.{api_method}",
data=frappe.as_json(data),
content_type="application/json",
)
# restart db connection to get latest data
frappe.connect()
@classmethod
def tearDownClass(cls):
frappe.db = cls.CURRENT_DB_CONNECTION