Created
September 1, 2024 08:35
-
-
Save cyrenity/ace37e1acb72e7132fce28662a69dc8e to your computer and use it in GitHub Desktop.
Django FreeSWITCH API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Visit following link to view API speficications! | |
https://app.swaggerhub.com/apis/cyrenity/FooSWITCH/v1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DestinationTestCase(APITestCase): | |
def setUp(self): | |
# Create a user using User model | |
self.user = get_user_model().objects.create_user(email='[email protected]', password='foo') | |
self.destination_data = { | |
'destination_number': '17189984009', | |
'destination_data': '1001 XML example.lynx.com', | |
} | |
self.destination_data_update = { | |
'destination_number': '7189984009', | |
'destination_data': '1002 XML example.lynx.com', | |
} | |
# authenticate api using credentials for user created above; before sending any API request | |
jwt_url = reverse('token_obtain_pair') | |
jwt_response = self.client.post(jwt_url, {'email': '[email protected]', 'password': 'foo'}, format='json') | |
token = jwt_response.data['access'] | |
refresh_token = jwt_response.data['refresh'] | |
# append Token with every request in tests | |
self.client.credentials(HTTP_AUTHORIZATION=f'Bearer {token}') | |
def test_01_create_destination(self): | |
""" | |
Test destination creation | |
""" | |
destiantion_url = reverse('dest-list') | |
response = self.client.post(destiantion_url, self.destination_data, format='json') | |
# Check if destination created successfully | |
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | |
# Check in database | |
self.assertEqual(Destinations.objects.count(), 1) | |
destination_dialplan_uuid = response.json()['dialplan_uuid'] | |
# print(Dialplans.objects.get(dialplan_uuid=response.json()['dialplan_uuid']).dialplan_xml) | |
self.assertIn('1001 XML example.lynx.com', Dialplans.objects.get(dialplan_uuid=destination_dialplan_uuid).dialplan_xml) | |
def test_02_update_destination(self): | |
""" | |
Test updating destination | |
""" | |
response = self.client.post(reverse('dest-list'), self.destination_data, format='json') | |
# Check if destination created successfully | |
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | |
# Update destination | |
response = self.client.patch( | |
reverse('dest-detail', args=[response.json()['destination_uuid']]), | |
self.destination_data_update, | |
format='json' | |
) | |
self.assertEqual(response.status_code, status.HTTP_200_OK) | |
# print(Dialplans.objects.get(dialplan_uuid=response.json()['dialplan_uuid']).dialplan_xml) | |
self.assertIn('1002 XML example.lynx.com', Dialplans.objects.get(dialplan_uuid=response.json()['dialplan_uuid']).dialplan_xml) | |
def test_03_delete_destination(self): | |
""" | |
Test deleting destination | |
""" | |
response = self.client.post(reverse('dest-list'), self.destination_data, format='json') | |
# Check if destination created successfully | |
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | |
# Delete destination | |
response = self.client.delete( | |
reverse('dest-detail', args=[response.json()['destination_uuid']]), | |
format='json' | |
) | |
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) | |
self.assertEqual(Destinations.objects.count(), 0) | |
self.assertEqual(Dialplans.objects.filter(app_uuid=Destinations.app_uuid).count(), 0) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DialplansTest(TestCase): | |
def test_01_create_destination_dialplan(self): | |
user = LynxUser.objects.create(email='[email protected]', password='carrot123!') | |
user.is_acitve = True | |
dom = Domains.objects.create( | |
domain_name="test.com.pk", | |
owner=user | |
) | |
destination = Destinations.objects.create( | |
domain_uuid = dom, | |
destination_type = 'inbound', | |
destination_number = '7189984009', | |
destination_condition_field = 'destination_number', | |
destination_context = 'public', | |
destination_app = 'transfer', | |
destination_data = '1000 XML public', | |
destination_enabled = "True" | |
) | |
self.assertIn('1000 XML public', Dialplans.objects.get(dialplan_uuid=destination.dialplan_uuid.dialplan_uuid).dialplan_xml) | |
def test_02_load_xml_and_gen_dialplan(self): | |
user = LynxUser.objects.create(email='[email protected]', password='carrot123!') | |
user.is_acitve = True | |
dom = Domains.objects.create( | |
domain_name="test.com.pk", | |
owner=user | |
) | |
s = StaticFilesStorage(location='switch') | |
fslist = list(get_files(s, location='templates/domain-dialplan')) | |
generate_domain_dialplan.send(sender=__file__, domain=dom) | |
dialp = Dialplans.objects.all() | |
self.assertEqual(dialp.count(), len(fslist)) | |
def test_03_gen_dialplan_from_XMLDialplan_obj(self): | |
user = LynxUser.objects.create(email='[email protected]', password='carrot123!') | |
dom = Domains.objects.create( | |
domain_name="test.com.pk", | |
owner=user | |
) | |
dp = XMLDialplan ( | |
name = "test-dialplan", | |
uuid = uuid.uuid4(), | |
context="public", | |
domain_uuid=dom, | |
number='[ext]', | |
condition_groups=[ | |
ConditionGroup( | |
group=0, | |
conditions=[ | |
Condition( | |
field="${user_exists}", | |
expression=f"false", | |
order=191, | |
), | |
Condition( | |
field="destination_number", | |
expression=f"{12344444}", | |
order=100, | |
), | |
], | |
antiactions=[ | |
AntiAction("transfer", "1001 XML Data", 40), | |
], | |
actions=[ | |
Action("log", f"CRIT Just a simple log message!", 30), | |
Action("set", f"error=Just a simple log message!", 30, "true"), | |
Action("transfer", "1000 XML Data", 40), | |
], | |
), | |
ConditionGroup( | |
group=1, | |
conditions=[ | |
Condition( | |
field="${user_exists}", | |
expression=f"false", | |
order=191, | |
), | |
Condition( | |
field="wday", | |
expression=f"1-5", | |
order=100, | |
), | |
Condition( | |
field="date-time", | |
expression=f"1-5", | |
order=99, | |
), | |
], | |
actions=[ | |
Action("log", f"CRIT Just a simple log message!", 30), | |
Action("transfer", "1000 XML Data", 40), | |
], | |
antiactions=[ | |
AntiAction("transfer", "24324 XML Data", 40), | |
], | |
), | |
], | |
) | |
dp.gen_dialplan() | |
dialp = Dialplans.objects.all() | |
self.assertIn('1000 XML Data', dialp[0].dialplan_xml) | |
self.assertIn('1001 XML Data', dialp[0].dialplan_xml) | |
self.assertIn('24324 XML Data', dialp[0].dialplan_xml) | |
def test_04_gen_time_condition(self): | |
user = LynxUser.objects.create(email='[email protected]', password='carrot123!') | |
dom = Domains.objects.create( | |
domain_name="test.com.pk", | |
owner=user | |
) | |
time_conditions = [('mday', 1), ('year', '2021-2024')] | |
tc = TimeCondition( | |
"TimeCondition-0", | |
3333, | |
('transfer', '1908 XML m.foo.com'), | |
('transfer', '*98 XML m.foo.com'), | |
time_conditions, dom | |
) | |
expected_output = '<condition field="destination_number" expression="^3333$" break="on-false"/>\n\t' | |
expected_output += '<condition mday="1" year="2021-2024" break="on-true">\n\t\t' | |
expected_output += '<action application="transfer" data="1908 XML m.foo.com"/>\n\t</condition>\n\t' | |
expected_output += '<condition field="destination_number" expression="^3333$" break="on-false">\n\t\t' | |
expected_output += '<action application="transfer" data="*98 XML m.foo.com"/>\n\t</condition>' | |
dialplan_xml = tc.gen_dialplan().dialplan_xml | |
self.assertIn(expected_output, dialplan_xml) | |
def create_ring_group(self): | |
user = LynxUser.objects.create(email='[email protected]', password='carrot123!') | |
user.is_acitve = True | |
dom = Domains.objects.create( | |
domain_name="test.com.pk", | |
owner=user | |
) | |
ring_group = RingGroups.objects.create( | |
domain_uuid = dom, | |
ring_group_name = 'Ring-Group-1', | |
ring_group_extension = '1234', | |
ring_group_greeting = 'say:', | |
ring_group_context = 'm.foo.com', | |
ring_group_call_timeout = 30, | |
ring_group_strategy = 'simultaneous', | |
ring_group_timeout_app = 'transfer', | |
ring_group_timeout_data = '1908 XML m.foo.com', | |
ring_group_ringback = '${us-ring}', | |
ring_group_enabled = 'true', | |
) | |
ring_group_member = RingGroupDestinations( | |
ring_group_uuid = ring_group, | |
destination_number = '1908', | |
destination_delay = 0, | |
destination_timeout = 30 | |
) | |
self.assertEqual(ring_group.ring_group_name, 'Ring-Group-1') | |
self.assertEqual(ring_group.dialplan_uuid.dialplan_number, '1234') | |
self.assertEqual(RingGroups.objects.all().count(), 1) | |
ring_group.dialplan_uuid.delete() | |
self.assertEqual(RingGroups.objects.all().count(), 0) | |
def create_ivr_menu(self): | |
user = LynxUser.objects.create(email='[email protected]', password='carrot123!') | |
user.is_acitve = True | |
dom = Domains.objects.create( | |
domain_name="test.com.pk", | |
owner=user | |
) | |
ivr_menu = IvrMenus.objects.create( | |
domain_uuid = dom, | |
ivr_menu_name = 'IVR-Menu-1', | |
ivr_menu_extension = '2056', | |
ivr_menu_greet_long = 'say:', | |
ivr_menu_greet_short = 'say:', | |
ivr_menu_context = 'm.foo.com', | |
ivr_menu_exit_app = 'transfer', | |
ivr_menu_exit_data = '1908 XML m.foo.com', | |
ivr_menu_ringback = '${us-ring}', | |
ivr_menu_enabled = 'true', | |
) | |
ivr_menu_option = IvrMenuOptions.objects.create( | |
ivr_menu_uuid = ivr_menu, | |
ivr_menu_option_digits = 1, | |
ivr_menu_option_action = 'transfer', | |
ivr_menu_option_param = '1908 XML m.foo.com', | |
ivr_menu_option_description = 'Default Option' | |
) | |
self.assertEqual(ivr_menu.ivr_menu_name, 'IVR-Menu-1') | |
self.assertEqual(IvrMenus.objects.all().count(), 1) | |
dialplan = ivr_menu.dialplan_uuid | |
dialplan.delete() | |
self.assertEqual(IvrMenus.objects.all().count(), 0) | |
def create_fax_server(self): | |
user = LynxUser.objects.create(email='[email protected]', password='carrot123!') | |
user.is_acitve = True | |
dom = Domains.objects.create( | |
domain_name="test.com.pk", | |
owner=user | |
) | |
fax_server = Fax.objects.create( | |
domain_uuid = dom, | |
fax_extension = '2055', | |
fax_name = 'FAX-Server-1', | |
fax_description = 'This is a test fax object', | |
fax_email = '[email protected]', | |
accountcode = 'bitvizor.fooswitch.com' | |
) | |
self.assertEqual(fax_server.fax_name, 'FAX-Server-1') | |
self.assertEqual(Fax.objects.all().count(), 1) | |
dialplan = fax_server.dialplan_uuid | |
dialplan.delete() | |
self.assertEqual(Fax.objects.all().count(), 0) | |
def create_user(self): | |
user = LynxUser.objects.create(email='[email protected]', password='carrot123!') | |
user_activation.send(sender=self.__class__, instance=user, domain="primary") | |
# from .management.commands.prepare_system import Command | |
# dom = Command().handle() | |
user1 = LynxUser.objects.create(email='[email protected]', password='carrot123!') | |
user_activation.send(sender=self.__class__, instance=user1, domain="primary") | |
print(user.profile.domain.get_extens()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment