def test_server_login():
    api = BroadworksAPI(**BASIC_API_PARAMS)
    assert api is not None
    assert api.__class__.__name__ == "BroadworksAPI"
    assert api.authenticated is False
    response = api.command("SystemSoftwareVersionGetRequest")
    assert api.authenticated is True
    assert response is not None
    assert response.type_ == "SystemSoftwareVersionGetResponse"
def test_server_login_fail():
    params = BASIC_API_PARAMS
    params["password"] = "******"
    api = BroadworksAPI(**params)
    assert api is not None
    assert api.__class__.__name__ == "BroadworksAPI"
    assert api.authenticated is False
    with pytest.raises(OCIErrorResponse) as excinfo:
        api.command("SystemSoftwareVersionGetRequest")
    assert "Invalid password" in str(excinfo.value)
    assert api.authenticated is False
def main():
    parser = argparse.ArgumentParser(
        description="Parse Broadworks OCI-P schema, build interface code", )
    parser.add_argument("--host", type=str, required=True)
    parser.add_argument("--port", type=int, default=2208)
    parser.add_argument("--username", type=str, required=True)
    parser.add_argument("--password", type=str, required=True)
    args = parser.parse_args()

    api = BroadworksAPI(
        host=args.host,
        port=args.port,
        username=args.username,
        password=args.password,
    )
    result = api.command("ServiceProviderGetListRequest")
    for thing in result.service_provider_table:
        print(thing)
def test_user_assigned_services_get_list_response():
    # Unfortunately I can't use the basic function for this due to differing object locations
    xml = (
        b'<?xml version="1.0" encoding="ISO-8859-2"?>'
        b'<BroadsoftDocument protocol="OCI" xmlns="C" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
        b'<sessionId xmlns="">00000000-1111-2222-3333-444444444444</sessionId>'
        b'<command echo="" xsi:type="UserAssignedServicesGetListResponse" xmlns="">'
        b"<groupServiceEntry>"
        b"<serviceName>Music On Hold</serviceName>"
        b"<isActive>true</isActive>"
        b"</groupServiceEntry>"
        b"<userServiceEntry>"
        b"<serviceName>Anonymous Call Rejection</serviceName>"
        b"<isActive>false</isActive>"
        b"</userServiceEntry>"
        b"<userServiceEntry>"
        b"<serviceName>Three-Way Call</serviceName>"
        b"<isActive>true</isActive>"
        b"</userServiceEntry>"
        b"</command>"
        b"</BroadsoftDocument>")

    api = BroadworksAPI(**BASIC_API_PARAMS)
    generated = api.decode_xml(xml)
    assert generated.type_ == "UserAssignedServicesGetListResponse"

    assert (generated.group_service_entry[0].to_dict() ==
            api.get_type_object(  # noqa: W503
                "AssignedGroupServicesEntry",
                service_name="Music On Hold",
                is_active=True,
            ).to_dict())

    assert (generated.user_service_entry[0].to_dict() ==
            api.get_type_object(  # noqa: W503
                "AssignedUserServicesEntry",
                service_name="Anonymous Call Rejection",
                is_active=False,
            ).to_dict())

    assert (generated.user_service_entry[1].to_dict() ==
            api.get_type_object(  # noqa: W503
                "AssignedUserServicesEntry",
                service_name="Three-Way Call",
                is_active=True,
            ).to_dict())
Exemple #5
0
    def listen_for_traffic(self):
        while True:
            self.socket.listen(5)
            connection, address = self.socket.accept()
            (host, port) = address
            self.logger.info(f"Connection from host={host} port={port}")
            api = BroadworksAPI(**BASIC_API_PARAMS, logger=self.logger)

            while True:
                content = b""
                while True:
                    readable, writable, exceptional = select.select(
                        [connection],
                        [],
                        [connection],
                        5,
                    )
                    if readable:  # there is only one thing in the set...
                        try:
                            content += connection.recv(4096)
                        except ConnectionResetError:
                            self.logger.debug("Connection got reset")
                            connection.close()
                            return
                        # look for the end of document marker (we ignore line ends)
                        splits = content.partition(b"</BroadsoftDocument>")
                        if len(splits[1]) > 0:
                            break
                    elif exceptional:
                        self.logger.debug("Connection likely terminated")
                        connection.close()
                        return
                    elif not readable and not writable and not exceptional:
                        self.logger.debug("Read timeout")
                        connection.close()
                        return
                self.logger.debug(f"RECV: {str(content)}")
                self.process_command(connection, content, api)
Exemple #6
0
"""Tests for `broadworks_ocip` package."""
from collections import namedtuple

import pytest  # noqa: F401
from lxml import etree

from broadworks_ocip import BroadworksAPI

BASIC_API_PARAMS = {
    "host": "localhost",
    "username": "******",
    "password": "******",
    "session_id": "00000000-1111-2222-3333-444444444444",
}

api = BroadworksAPI(**BASIC_API_PARAMS)


def canon_xml(inxml):
    """
    we XML canonicalise/pretty print this to prevent whitespace/quote ordering
    issues and to make any diffs easier to read.
    Returned XML is as a decoded string - prettier for diffs
    """
    return etree.tostring(etree.fromstring(inxml), pretty_print=True).decode()


def check_command_xml(wanted, cmd):
    """Create a Broadworks XML command fragment from the arguments"""
    generated = cmd.build_xml_()
    assert canon_xml(generated) == canon_xml(wanted)
def make_command_from_xml(xml, command, serialised):
    """Create a Broadworks XML command framgment from the argumenta"""
    api = BroadworksAPI(**BASIC_API_PARAMS)
    generated = api.decode_xml(xml)
    assert generated.type_ == command
    assert generated.to_dict() == serialised