blob: a5dc7dd2118b9ab55e48897f116865a249214399 [file] [log] [blame]
Subin Tome57e7bb2022-04-02 17:16:56 +05301import os
2import time
3import unittest
4
5import frappe
6import requests
7from frappe.contacts.doctype.contact.test_contact import create_contact
8
9from erpnext.hr.doctype.employee.test_employee import make_employee
10
11
12class 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 Shettyf4b85732022-04-04 09:31:15 +053019 from .exotel_test_data import call_end_data, call_initiation_data
20
Subin Tome57e7bb2022-04-02 17:16:56 +053021 api_method = "handle_incoming_call"
22 end_call_api_method = "handle_end_call"
Subin Tome57e7bb2022-04-02 17:16:56 +053023
Suraj Shetty0e9ebad2022-04-04 07:23:08 +053024 emulate_api_call(call_initiation_data, api_method)
25 emulate_api_call(call_end_data, end_call_api_method)
Suraj Shettyf4b85732022-04-04 09:31:15 +053026
Suraj Shetty0e9ebad2022-04-04 07:23:08 +053027 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 Tome57e7bb2022-04-02 17:16:56 +053031 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 Shetty0e9ebad2022-04-04 07:23:08 +053035 from .exotel_test_data import call_disconnected_data
Suraj Shettyf4b85732022-04-04 09:31:15 +053036
Subin Tome57e7bb2022-04-02 17:16:56 +053037 api_method = "handle_missed_call"
Suraj Shetty0e9ebad2022-04-04 07:23:08 +053038 emulate_api_call(call_disconnected_data, api_method)
39 call_log = frappe.get_doc("Call Log", call_disconnected_data.CallSid)
Subin Tome57e7bb2022-04-02 17:16:56 +053040
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)
Subin Tome57e7bb2022-04-02 17:16:56 +053043 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 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 Shetty0e9ebad2022-04-04 07:23:08 +053050 emulate_api_call(call_not_answered_data, api_method)
Subin Tome57e7bb2022-04-02 17:16:56 +053051
Suraj Shetty0e9ebad2022-04-04 07:23:08 +053052 call_log = frappe.get_doc("Call Log", call_not_answered_data.CallSid)
Subin Tome57e7bb2022-04-02 17:16:56 +053053
Suraj Shetty0e9ebad2022-04-04 07:23:08 +053054 self.assertEqual(call_log.get("from"), call_not_answered_data.CallFrom)
55 self.assertEqual(call_log.get("to"), call_not_answered_data.DialWhomNumber)
Subin Tome57e7bb2022-04-02 17:16:56 +053056 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 Shetty0e9ebad2022-04-04 07:23:08 +053063def emulate_api_call(data, api_method):
Subin Tome57e7bb2022-04-02 17:16:56 +053064 # Build URL
Suraj Shetty0e9ebad2022-04-04 07:23:08 +053065 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 Shettyf4b85732022-04-04 09:31:15 +053070
Suraj Shetty0e9ebad2022-04-04 07:23:08 +053071def get_exotel_handler_endpoint(method):
72 site = "localhost" if os.environ.get("CI") else frappe.local.site
Subin Tome57e7bb2022-04-02 17:16:56 +053073 port = frappe.get_site_config().webserver_port or "8000"
Suraj Shettyf4b85732022-04-04 09:31:15 +053074 return f"http://{site}:{port}/api/method/erpnext.erpnext_integrations.exotel_integration.{method}"