def test_repr(self): """ ``repr(Namespace)`` produces self-explanatory human-readable output. """ self.assertEqual('<Namespace uri=None prefix=None>', repr(Namespace(None))) self.assertEqual("<Namespace uri='http://example.com' prefix=None>", repr(Namespace('http://example.com'))) self.assertEqual("<Namespace uri='http://example.com' prefix='ex'>", repr(Namespace('http://example.com', 'ex')))
def test_namespaced(self): """ Tags that are `QualifiedName` instances or use Clark notation produce namespaced XML elements. """ E = ElementMaker() self.assertEqual('<ns0:tag xmlns:ns0="http://example.com" />', tostring(E('{http://example.com}tag'))) self.assertEqual( '<ns0:tag xmlns:ns0="http://example.com" />', tostring(QualifiedName('http://example.com', 'tag')())) ns = Namespace('http://example.com', 'ex') self.assertEqual('<ex:tag xmlns:ex="http://example.com" />', tostring(ns.tag()))
def test_local_name(self): """ `Namespace.__getattr__` produces local `QualifiedName` instances if `Namespace.__uri` is `None`. """ ns = Namespace(None) self.assertEqual(QualifiedName('foo'), ns.foo)
def test_namespaced_attributes(self): """ XML attributes that are `QualifiedName` instances or use Clark notation produce namespaced XML element attributes. """ ns = Namespace('http://example.com', 'ex') attrib = {ns.key: 'value'} self.assertEqual( {'{http://example.com}tag': { '@{http://example.com}key': 'value' }}, element_to_dict(ns.tag(attrib))) attrib = {'{http://example.com}key': 'value'} self.assertEqual( {'{http://example.com}tag': { '@{http://example.com}key': 'value' }}, element_to_dict(ns.tag(attrib)))
def test_namespaced_attributes(self): """ XML attributes that are `QualifiedName` instances or use Clark notation produce namespaced XML element attributes. """ ns = Namespace('http://example.com', 'ex') attrib = {ns.key: 'value'} self.assertEqual( {'{http://example.com}tag': { '@{http://example.com}key': 'value'}}, element_to_dict(ns.tag(attrib))) attrib = {'{http://example.com}key': 'value'} self.assertEqual( {'{http://example.com}tag': { '@{http://example.com}key': 'value'}}, element_to_dict(ns.tag(attrib)))
def test_namespaced(self): """ Tags that are `QualifiedName` instances or use Clark notation produce namespaced XML elements. """ E = ElementMaker() self.assertEqual( '<ns0:tag xmlns:ns0="http://example.com" />', tostring(E('{http://example.com}tag'))) self.assertEqual( '<ns0:tag xmlns:ns0="http://example.com" />', tostring(QualifiedName('http://example.com', 'tag')())) ns = Namespace('http://example.com', 'ex') self.assertEqual( '<ex:tag xmlns:ex="http://example.com" />', tostring(ns.tag()))
def test_qualified_name(self): """ `Namespace.__getattr__` produces qualified `QualifiedName` instances if `Namespace.__uri` is not `None`. """ uri = 'http://example.com' ns = Namespace(uri) self.assertEqual(QualifiedName(uri, 'foo'), ns.foo)
def test_namespaced(self): """ `element_to_dict` supports namespaced element names and namespaced attributes. """ ns = Namespace('http://example.com', 'ex') self.assertEqual( {str(ns.root): { 'child': [ {'@' + str(ns.attr): 'value'}, {'@attr2': 'value2', '#text': 'hello'}, 'world']}}, element_to_dict( ns.root( L.child({ns.attr: 'value'}), L.child('hello', attr2='value2'), L.child('world'))))
def test_namespaced(self): """ `element_to_dict` supports namespaced element names and namespaced attributes. """ ns = Namespace('http://example.com', 'ex') self.assertEqual( { str(ns.root): { 'child': [{ '@' + str(ns.attr): 'value' }, { '@attr2': 'value2', '#text': 'hello' }, 'world'] } }, element_to_dict( ns.root(L.child({ns.attr: 'value'}), L.child('hello', attr2='value2'), L.child('world'))))
def test_equality(self): """ Two `Namespace` instances created with the same values compare equal to one another. """ self.assertEqual(Namespace('http://example.com'), Namespace('http://example.com')) self.assertEqual(Namespace('http://example.com', 'ex'), Namespace('http://example.com', 'ex')) self.assertNotEqual(Namespace('http://example.com'), Namespace('http://example.com', 'ex')) self.assertNotEqual(Namespace('http://example.com/'), Namespace('http://example.com'))
def test_str(self): """ ``str(Namespace)`` produces the Namespace URI. """ uri = 'http://example.com' self.assertEqual(uri, str(Namespace(uri)))
from twisted.internet.defer import maybeDeferred, fail from twisted.python import log from twisted.python.constants import Values, ValueConstant from twisted.web import http from twisted.web.resource import Resource from twisted.web.server import NOT_DONE_YET from vumi.transports.parlayx.client import PARLAYX_COMMON_NS from vumi.transports.parlayx.soaputil import ( soap_envelope, unwrap_soap_envelope, soap_fault, SoapFault) from vumi.transports.parlayx.xmlutil import ( Namespace, elemfind, gettext, split_qualified, parse_document, tostring) from vumi.utils import normalize_msisdn NOTIFICATION_NS = Namespace( 'http://www.csapi.org/schema/parlayx/sms/notification/v2_2/local', 'loc') def normalize_address(address): """ Normalize a ParlayX address. """ if address.startswith('tel:'): address = address[4:] return normalize_msisdn(address) class DeliveryStatus(Values): """ ParlayX `DeliveryStatus` enumeration type. """
# -*- test-case-name: vumi.transports.parlayx.tests.test_client -*- import hashlib import uuid from collections import namedtuple from datetime import datetime from vumi.transports.parlayx.soaputil import perform_soap_request, SoapFault from vumi.transports.parlayx.xmlutil import (gettext, gettextall, Namespace, LocalNamespace as L) PARLAYX_COMMON_NS = Namespace( 'http://www.csapi.org/schema/parlayx/common/v2_1', 'parlayx_common') SEND_NS = Namespace('http://www.csapi.org/schema/parlayx/sms/send/v2_2/local', 'send') NOTIFICATION_MANAGER_NS = Namespace( 'http://www.csapi.org/schema/parlayx/sms/notification_manager/v2_3/local', 'nm') PARLAYX_HEAD_NS = Namespace('http://www.huawei.com.cn/schema/common/v2_1', 'parlayx_head') def format_address(msisdn): """ Format a normalized MSISDN as a URI that ParlayX will accept. """ if not msisdn.startswith('+'): raise ValueError('Only international format addresses are supported') return 'tel:' + msisdn[1:] def format_timestamp(when):
# -*- test-case-name: vumi.transports.parlayx.tests.test_soaputil -*- """ Utility functions for performing and processing SOAP requests, constructing SOAP responses and parsing and constructing SOAP faults. """ from twisted.web import http from twisted.python import log from vumi.utils import http_request_full from vumi.transports.parlayx.xmlutil import (Namespace, LocalNamespace, elemfind, gettext, fromstring, tostring) SOAP_ENV = Namespace('http://schemas.xmlsoap.org/soap/envelope/', 'soapenv') def perform_soap_request(uri, action, body, header=None, expected_faults=None, http_request_full=http_request_full): """ Perform a SOAP request. If the remote server responds with an HTTP 500 status, then it is assumed that the body contains a SOAP fault, which is then parsed and a `SoapFault` exception raised. :param uri: SOAP endpoint URI. :param action: SOAP action.