本文整理汇总了Python中vumi.tests.utils.MockHttpServer.stop方法的典型用法代码示例。如果您正苦于以下问题:Python MockHttpServer.stop方法的具体用法?Python MockHttpServer.stop怎么用?Python MockHttpServer.stop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类vumi.tests.utils.MockHttpServer
的用法示例。
在下文中一共展示了MockHttpServer.stop方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestYoPaymentHandler
# 需要导入模块: from vumi.tests.utils import MockHttpServer [as 别名]
# 或者: from vumi.tests.utils.MockHttpServer import stop [as 别名]
class TestYoPaymentHandler(VumiTestCase):
@inlineCallbacks
def setUp(self):
self.eh_helper = self.add_helper(EventHandlerHelper())
yield self.eh_helper.setup_event_dispatcher(
'afropinions', YoPaymentHandler, {
'poll_manager_prefix': 'vumigo.',
'username': 'username',
'password': 'password',
'url': None,
'method': 'POST',
'amount': 1000,
'reason': 'foo',
})
self.eh_helper.track_event('survey_completed', 'afropinions')
@inlineCallbacks
def test_hitting_url(self):
msisdn = u'+2345'
message_id = u'message-id'
event = self.eh_helper.make_event('survey_completed', {
'from_addr': msisdn,
'message_id': message_id,
'transport_type': 'ussd',
'participant': {'interactions': 2},
})
self.mock_server = MockHttpServer()
yield self.mock_server.start()
self.eh_helper.get_handler('afropinions').url = self.mock_server.url
yield self.eh_helper.dispatch_event(event)
received_request = yield self.mock_server.queue.get()
self.assertEqual(received_request.args['msisdn'][0], msisdn)
self.assertEqual(received_request.args['amount'][0], '2000')
self.assertEqual(received_request.args['reason'][0], 'foo')
headers = received_request.requestHeaders
self.assertEqual(headers.getRawHeaders('Content-Type'),
['application/x-www-form-urlencoded'])
self.assertEqual(headers.getRawHeaders('Authorization'),
['Basic %s' % (base64.b64encode('username:password'),)])
yield self.mock_server.stop()
def test_auth_headers(self):
handler = self.eh_helper.get_handler('afropinions')
auth = handler.get_auth_headers('username', 'password')
credentials = base64.b64encode('username:password')
self.assertEqual(auth, {
'Authorization': 'Basic %s' % (credentials.strip(),)
})
示例2: TestTransport
# 需要导入模块: from vumi.tests.utils import MockHttpServer [as 别名]
# 或者: from vumi.tests.utils.MockHttpServer import stop [as 别名]
class TestTransport(TestCase):
@inlineCallbacks
def setUp(self):
DelayedCall.debug = True
self.ok_transport_calls = DeferredQueue()
self.mock_service = MockHttpServer(self.handle_request)
yield self.mock_service.start()
config = {
'transport_name': 'test_ok_transport',
'transport_type': 'ok',
'ussd_string_prefix': '',
'web_path': "foo",
'web_port': 0,
'url': self.mock_service.url,
'username': 'testuser',
'password': 'testpass',
}
self.worker = get_stubbed_worker(OkTransport, config)
self.broker = self.worker._amqp_client.broker
yield self.worker.startWorker()
self.worker_url = self.worker.get_transport_url()
@inlineCallbacks
def tearDown(self):
yield self.worker.stopWorker()
yield self.mock_service.stop()
def handle_request(self, request):
self.ok_transport_calls.put(request)
return ''
@inlineCallbacks
def test_health(self):
result = yield http_request(self.worker_url + "health", "",
method='GET')
self.assertEqual(json.loads(result), {
'pending_requests': 0
})
@inlineCallbacks
def test_inbound(self):
d = http_request(self.worker_url + "foo", '', method='GET')
msg, = yield self.broker.wait_messages("vumi",
"test_ok_transport.inbound", 1)
payload = msg.payload
tum = TransportUserMessage(**payload)
rep = tum.reply("OK")
self.broker.publish_message("vumi", "test_ok_transport.outbound",
rep)
response = yield d
self.assertEqual(response, 'OK')
示例3: AirtelBfTransportTestCase
# 需要导入模块: from vumi.tests.utils import MockHttpServer [as 别名]
# 或者: from vumi.tests.utils.MockHttpServer import stop [as 别名]
class AirtelBfTransportTestCase(TransportTestCase):
transport_name = "airtel"
transport_type = "sms"
transport_class = AirtelBfHttpTransport
send_path = "/sendsms/index"
send_port = 9999
@inlineCallbacks
def setUp(self):
yield super(AirtelBfTransportTestCase, self).setUp()
self.airtel_sms_calls = DeferredQueue()
self.mock_airtel_sms = MockHttpServer(self.handle_request)
yield self.mock_airtel_sms.start()
self.config = {
"transport_name": self.transport_name,
"receive_path": "sendsms",
"receive_port": 9998,
"outbound_url": self.mock_airtel_sms.url,
"default_shortcode": "3411",
"login": "texttochange",
"password": "password",
}
self.transport = yield self.get_transport(self.config)
self.transport_url = self.transport.get_transport_url()
@inlineCallbacks
def tearDown(self):
yield self.mock_airtel_sms.stop()
yield super(AirtelBfTransportTestCase, self).tearDown()
def handle_request(self, request):
self.airtel_sms_calls.put(request)
return ""
@inlineCallbacks
def test_sending_sms(self):
yield self.dispatch(self.mkmsg_out())
req = yield self.airtel_sms_calls.get()
self.assertEqual(req.path, "/")
self.assertEqual(req.method, "GET")
self.assertEqual(
{
"REQUESTTYPE": ["SMSSubmitReq"],
"ORIGIN_ADDR": ["9292"],
"MOBILENO": ["+41791234567"],
"MESSAGE": ["hello world"],
"USERNAME": ["texttochange"],
"PASSWORD": ["password"],
"TYPE": ["0"],
},
req.args,
)
[smsg] = self.get_dispatched_events()
self.assertEqual(self.mkmsg_ack(user_message_id="1", sent_message_id="1"), smsg)
@inlineCallbacks
def test_sending_sms_complex(self):
yield self.dispatch(
self.mkmsg_out(
from_addr="+2261", content=u"setoffre #A#BELG=10/tete+300000/pu# envoy\xe9 depuis SIMAgriMobile"
)
)
req = yield self.airtel_sms_calls.get()
self.assertEqual(req.path, "/")
self.assertEqual(req.method, "GET")
self.assertEqual("setoffre #A#BELG=10/tete+300000/pu# envoyé depuis SIMAgriMobile", req.args["MESSAGE"][0])
[smsg] = self.get_dispatched_events()
self.assertEqual(self.mkmsg_ack(user_message_id="1", sent_message_id="1"), smsg)
def mkurl_raw(self, **params):
return "%s%s?%s" % (self.transport_url, self.config["receive_path"], urlencode(params))
def mkurl(self, content, from_addr, **kw):
params = {"message": content, "msisdn": from_addr}
params.update(kw)
return self.mkurl_raw(**params)
@inlineCallbacks
def test_receiving_sms(self):
url = self.mkurl("Hello envoy\xc3\xa9", "+2261")
response = yield http_request_full(url, method="GET")
[smsg] = self.get_dispatched_messages()
self.assertEqual(response.code, http.OK)
self.assertEqual(u"Hello envoy\xe9", smsg["content"])
self.assertEqual("3411", smsg["to_addr"])
self.assertEqual("+2261", smsg["from_addr"])
@inlineCallbacks
def test_receiving_sms_accent(self):
url = self.mkurl("Hello envoyé", "+2261")
response = yield http_request_full(url, method="GET")
[smsg] = self.get_dispatched_messages()
self.assertEqual(response.code, http.OK)
self.assertEqual(u"Hello envoy\xe9", smsg["content"])
#.........这里部分代码省略.........
示例4: TestIntegratTransport
# 需要导入模块: from vumi.tests.utils import MockHttpServer [as 别名]
# 或者: from vumi.tests.utils.MockHttpServer import stop [as 别名]
class TestIntegratTransport(TestCase):
timeout = 5
@inlineCallbacks
def setUp(self):
self.integrat_calls = DeferredQueue()
self.mock_integrat = MockHttpServer(self.handle_request)
yield self.mock_integrat.start()
config = {
'transport_name': 'testgrat',
'web_path': "foo",
'web_port': "0",
'url': self.mock_integrat.url,
'username': 'testuser',
'password': 'testpass',
}
self.worker = get_stubbed_worker(IntegratTransport, config)
self.broker = self.worker._amqp_client.broker
yield self.worker.startWorker()
addr = self.worker.web_resource.getHost()
self.worker_url = "http://%s:%s/" % (addr.host, addr.port)
@inlineCallbacks
def tearDown(self):
yield self.worker.stopWorker()
yield self.mock_integrat.stop()
def handle_request(self, request):
self.integrat_calls.put(request)
return ''
@inlineCallbacks
def test_health(self):
result = yield http_request(self.worker_url + "health", "",
method='GET')
self.assertEqual(result, "OK")
@inlineCallbacks
def test_outbound(self):
msg = TransportUserMessage(to_addr="12345", from_addr="56789",
transport_name="testgrat",
transport_type="ussd",
transport_metadata={
'session_id': "sess123",
},
)
self.broker.publish_message("vumi", "testgrat.outbound", msg)
req = yield self.integrat_calls.get()
self.assertEqual(req.path, '/')
self.assertEqual(req.method, 'POST')
self.assertEqual(req.getHeader('content-type'),
'text/xml; charset=utf-8')
self.assertEqual(req.content.getvalue(),
'<Message><Version Version="1.0" />'
'<Request Flags="0" SessionID="sess123"'
' Type="USSReply">'
'<UserID Orientation="TR">testuser</UserID>'
'<Password>testpass</Password>'
'<USSText Type="TEXT" />'
'</Request></Message>')
@inlineCallbacks
def test_inbound(self):
xml = XML_TEMPLATE % {
'ussd_type': 'Request',
'sid': 'sess1234',
'network_sid': "netsid12345",
'msisdn': '27345',
'connstr': '*120*99#',
'text': 'foobar',
}
yield http_request(self.worker_url + "foo", xml, method='GET')
msg, = yield self.broker.wait_messages("vumi", "testgrat.inbound", 1)
payload = msg.payload
self.assertEqual(payload['transport_name'], "testgrat")
self.assertEqual(payload['transport_type'], "ussd")
self.assertEqual(payload['transport_metadata'],
{"session_id": "sess1234"})
self.assertEqual(payload['session_event'],
TransportUserMessage.SESSION_RESUME)
self.assertEqual(payload['from_addr'], '27345')
self.assertEqual(payload['to_addr'], '*120*99#')
self.assertEqual(payload['content'], 'foobar')
@inlineCallbacks
def test_inbound_non_ascii(self):
xml = (XML_TEMPLATE % {
'ussd_type': 'Request',
'sid': 'sess1234',
'network_sid': "netsid12345",
'msisdn': '27345',
'connstr': '*120*99#',
'text': u'öæł',
}).encode("utf-8")
yield http_request(self.worker_url + "foo", xml, method='GET')
msg, = yield self.broker.wait_messages("vumi", "testgrat.inbound", 1)
payload = msg.payload
self.assertEqual(payload['content'], u'öæł')
示例5: TestPermissiveCellulantSmsTransport
# 需要导入模块: from vumi.tests.utils import MockHttpServer [as 别名]
# 或者: from vumi.tests.utils.MockHttpServer import stop [as 别名]
class TestPermissiveCellulantSmsTransport(VumiTestCase):
@inlineCallbacks
def setUp(self):
self.cellulant_sms_calls = DeferredQueue()
self.mock_cellulant_sms = MockHttpServer(self.handle_request)
yield self.mock_cellulant_sms.start()
self.config = {
'web_path': "foo",
'web_port': 0,
'credentials': {
'2371234567': {
'username': 'user',
'password': 'pass',
},
'9292': {
'username': 'other-user',
'password': 'other-pass',
}
},
'outbound_url': self.mock_cellulant_sms.url,
'validation_mode': 'permissive',
}
self.tx_helper = self.add_helper(
TransportHelper(CellulantSmsTransport))
self.transport = yield self.tx_helper.get_transport(self.config)
self.transport_url = self.transport.get_transport_url()
def handle_request(self, request):
self.cellulant_sms_calls.put(request)
return ''
def mkurl(self, content, from_addr="2371234567", **kw):
params = {
'SOURCEADDR': from_addr,
'DESTADDR': '12345',
'MESSAGE': content,
'ID': '1234567',
}
params.update(kw)
return self.mkurl_raw(**params)
def mkurl_raw(self, **params):
return '%s%s?%s' % (
self.transport_url,
self.config['web_path'],
urlencode(params)
)
@inlineCallbacks
def tearDown(self):
yield self.mock_cellulant_sms.stop()
yield super(TestPermissiveCellulantSmsTransport, self).tearDown()
@inlineCallbacks
def test_bad_parameter_in_permissive_mode(self):
url = self.mkurl('hello', foo='bar')
response = yield http_request_full(url, '', method='GET')
[msg] = self.tx_helper.get_dispatched_inbound()
self.assertEqual(200, response.code)
self.assertEqual(json.loads(response.delivered_body),
{'message_id': msg['message_id']})
@inlineCallbacks
def test_missing_parameters(self):
url = self.mkurl_raw(ID='12345678', DESTADDR='12345', MESSAGE='hello')
response = yield http_request_full(url, '', method='GET')
self.assertEqual(400, response.code)
self.assertEqual(json.loads(response.delivered_body),
{'missing_parameter': ['SOURCEADDR']})
@inlineCallbacks
def test_ignored_parameters(self):
url = self.mkurl('hello', channelID='a', keyword='b', CHANNELID='c',
serviceID='d', SERVICEID='e', unsub='f')
response = yield http_request(url, '', method='GET')
[msg] = self.tx_helper.get_dispatched_inbound()
self.assertEqual(msg['content'], "hello")
self.assertEqual(json.loads(response),
{'message_id': msg['message_id']})
示例6: TestAcksCellulantSmsTransport
# 需要导入模块: from vumi.tests.utils import MockHttpServer [as 别名]
# 或者: from vumi.tests.utils.MockHttpServer import stop [as 别名]
class TestAcksCellulantSmsTransport(VumiTestCase):
@inlineCallbacks
def setUp(self):
self.cellulant_sms_calls = DeferredQueue()
self.mock_cellulant_sms = MockHttpServer(self.handle_request)
self._mock_response = ''
yield self.mock_cellulant_sms.start()
self.config = {
'web_path': "foo",
'web_port': 0,
'credentials': {
'2371234567': {
'username': 'user',
'password': 'pass',
},
'9292': {
'username': 'other-user',
'password': 'other-pass',
}
},
'outbound_url': self.mock_cellulant_sms.url,
'validation_mode': 'permissive',
}
self.tx_helper = self.add_helper(
TransportHelper(CellulantSmsTransport))
self.transport = yield self.tx_helper.get_transport(self.config)
self.transport_url = self.transport.get_transport_url()
@inlineCallbacks
def tearDown(self):
yield self.mock_cellulant_sms.stop()
yield super(TestAcksCellulantSmsTransport, self).tearDown()
def mock_response(self, response):
self._mock_response = response
def handle_request(self, request):
self.cellulant_sms_calls.put(request)
return self._mock_response
@inlineCallbacks
def mock_event(self, msg, nr_events):
self.mock_response(msg)
yield self.tx_helper.make_dispatch_outbound(
"foo", to_addr='2371234567', message_id='id_%s' % (msg,))
yield self.cellulant_sms_calls.get()
events = yield self.tx_helper.wait_for_dispatched_events(nr_events)
returnValue(events)
@inlineCallbacks
def test_nack_param_error_E0(self):
[nack] = yield self.mock_event('E0', 1)
self.assertEqual(nack['event_type'], 'nack')
self.assertEqual(nack['user_message_id'], 'id_E0')
self.assertEqual(nack['nack_reason'],
self.transport.KNOWN_ERROR_RESPONSE_CODES['E0'])
@inlineCallbacks
def test_nack_login_error_E1(self):
[nack] = yield self.mock_event('E1', 1)
self.assertEqual(nack['event_type'], 'nack')
self.assertEqual(nack['user_message_id'], 'id_E1')
self.assertEqual(nack['nack_reason'],
self.transport.KNOWN_ERROR_RESPONSE_CODES['E1'])
@inlineCallbacks
def test_nack_credits_error_E2(self):
[nack] = yield self.mock_event('E2', 1)
self.assertEqual(nack['event_type'], 'nack')
self.assertEqual(nack['user_message_id'], 'id_E2')
self.assertEqual(nack['nack_reason'],
self.transport.KNOWN_ERROR_RESPONSE_CODES['E2'])
@inlineCallbacks
def test_nack_delivery_failed_1005(self):
[nack] = yield self.mock_event('1005', 1)
self.assertEqual(nack['event_type'], 'nack')
self.assertEqual(nack['user_message_id'], 'id_1005')
self.assertEqual(nack['nack_reason'],
self.transport.KNOWN_ERROR_RESPONSE_CODES['1005'])
@inlineCallbacks
def test_unknown_response(self):
[nack] = yield self.mock_event('something_unexpected', 1)
self.assertEqual(nack['event_type'], 'nack')
self.assertEqual(nack['user_message_id'], 'id_something_unexpected')
self.assertEqual(nack['nack_reason'],
'Unknown response code: something_unexpected')
@inlineCallbacks
def test_ack_success(self):
[event] = yield self.mock_event('1', 1)
self.assertEqual(event['event_type'], 'ack')
self.assertEqual(event['user_message_id'], 'id_1')
示例7: TestCellulantSmsTransport
# 需要导入模块: from vumi.tests.utils import MockHttpServer [as 别名]
# 或者: from vumi.tests.utils.MockHttpServer import stop [as 别名]
class TestCellulantSmsTransport(VumiTestCase):
@inlineCallbacks
def setUp(self):
self.cellulant_sms_calls = DeferredQueue()
self.mock_cellulant_sms = MockHttpServer(self.handle_request)
yield self.mock_cellulant_sms.start()
self.config = {
'web_path': "foo",
'web_port': 0,
'credentials': {
'2371234567': {
'username': 'user',
'password': 'pass',
},
'9292': {
'username': 'other-user',
'password': 'other-pass',
}
},
'outbound_url': self.mock_cellulant_sms.url,
}
self.tx_helper = self.add_helper(
TransportHelper(CellulantSmsTransport))
self.transport = yield self.tx_helper.get_transport(self.config)
self.transport_url = self.transport.get_transport_url()
@inlineCallbacks
def tearDown(self):
yield self.mock_cellulant_sms.stop()
yield super(TestCellulantSmsTransport, self).tearDown()
def handle_request(self, request):
self.cellulant_sms_calls.put(request)
return ''
def mkurl(self, content, from_addr="2371234567", **kw):
params = {
'SOURCEADDR': from_addr,
'DESTADDR': '12345',
'MESSAGE': content,
'ID': '1234567',
}
params.update(kw)
return self.mkurl_raw(**params)
def mkurl_raw(self, **params):
return '%s%s?%s' % (
self.transport_url,
self.config['web_path'],
urlencode(params)
)
@inlineCallbacks
def test_health(self):
result = yield http_request(
self.transport_url + "health", "", method='GET')
self.assertEqual(json.loads(result), {'pending_requests': 0})
@inlineCallbacks
def test_inbound(self):
url = self.mkurl('hello')
response = yield http_request(url, '', method='GET')
[msg] = self.tx_helper.get_dispatched_inbound()
self.assertEqual(msg['transport_name'], self.tx_helper.transport_name)
self.assertEqual(msg['to_addr'], "12345")
self.assertEqual(msg['from_addr'], "2371234567")
self.assertEqual(msg['content'], "hello")
self.assertEqual(json.loads(response),
{'message_id': msg['message_id']})
@inlineCallbacks
def test_outbound(self):
yield self.tx_helper.make_dispatch_outbound(
"hello world", to_addr="2371234567")
req = yield self.cellulant_sms_calls.get()
self.assertEqual(req.path, '/')
self.assertEqual(req.method, 'GET')
self.assertEqual({
'username': ['other-user'],
'password': ['other-pass'],
'source': ['9292'],
'destination': ['2371234567'],
'message': ['hello world'],
}, req.args)
@inlineCallbacks
def test_outbound_creds_selection(self):
yield self.tx_helper.make_dispatch_outbound(
"hello world", to_addr="2371234567", from_addr='2371234567')
req = yield self.cellulant_sms_calls.get()
self.assertEqual(req.path, '/')
self.assertEqual(req.method, 'GET')
self.assertEqual({
'username': ['user'],
'password': ['pass'],
'source': ['2371234567'],
'destination': ['2371234567'],
'message': ['hello world'],
#.........这里部分代码省略.........
示例8: TestMTechKenyaTransport
# 需要导入模块: from vumi.tests.utils import MockHttpServer [as 别名]
# 或者: from vumi.tests.utils.MockHttpServer import stop [as 别名]
class TestMTechKenyaTransport(VumiTestCase):
transport_class = MTechKenyaTransport
@inlineCallbacks
def setUp(self):
self.cellulant_sms_calls = DeferredQueue()
self.mock_mtech_sms = MockHttpServer(self.handle_request)
yield self.mock_mtech_sms.start()
self.valid_creds = {
'mt_username': 'testuser',
'mt_password': 'testpass',
}
self.config = {
'web_path': "foo",
'web_port': 0,
'outbound_url': self.mock_mtech_sms.url,
}
self.config.update(self.valid_creds)
self.tx_helper = self.add_helper(
TransportHelper(self.transport_class, mobile_addr='2371234567'))
self.transport = yield self.tx_helper.get_transport(self.config)
self.transport_url = self.transport.get_transport_url()
@inlineCallbacks
def tearDown(self):
yield self.mock_mtech_sms.stop()
yield super(TestMTechKenyaTransport, self).tearDown()
def handle_request(self, request):
if request.args.get('user') != [self.valid_creds['mt_username']]:
request.setResponseCode(401)
elif request.args.get('MSISDN') != ['2371234567']:
request.setResponseCode(403)
self.cellulant_sms_calls.put(request)
return ''
def mkurl(self, content, from_addr="2371234567", **kw):
params = {
'shortCode': '12345',
'MSISDN': from_addr,
'MESSAGE': content,
'messageID': '1234567',
}
params.update(kw)
return self.mkurl_raw(**params)
def mkurl_raw(self, **params):
return '%s%s?%s' % (
self.transport_url,
self.config['web_path'],
urlencode(params)
)
@inlineCallbacks
def test_health(self):
result = yield http_request(
self.transport_url + "health", "", method='GET')
self.assertEqual(json.loads(result), {'pending_requests': 0})
@inlineCallbacks
def test_inbound(self):
url = self.mkurl('hello')
response = yield http_request(url, '', method='POST')
[msg] = self.tx_helper.get_dispatched_inbound()
self.assertEqual(msg['transport_name'], self.tx_helper.transport_name)
self.assertEqual(msg['to_addr'], "12345")
self.assertEqual(msg['from_addr'], "2371234567")
self.assertEqual(msg['content'], "hello")
self.assertEqual(json.loads(response),
{'message_id': msg['message_id']})
@inlineCallbacks
def test_handle_non_ascii_input(self):
url = self.mkurl(u"öæł".encode("utf-8"))
response = yield http_request(url, '', method='POST')
[msg] = self.tx_helper.get_dispatched_inbound()
self.assertEqual(msg['transport_name'], self.tx_helper.transport_name)
self.assertEqual(msg['to_addr'], "12345")
self.assertEqual(msg['from_addr'], "2371234567")
self.assertEqual(msg['content'], u"öæł")
self.assertEqual(json.loads(response),
{'message_id': msg['message_id']})
@inlineCallbacks
def test_bad_parameter(self):
url = self.mkurl('hello', foo='bar')
response = yield http_request_full(url, '', method='POST')
self.assertEqual(400, response.code)
self.assertEqual(json.loads(response.delivered_body),
{'unexpected_parameter': ['foo']})
@inlineCallbacks
def test_outbound(self):
msg = yield self.tx_helper.make_dispatch_outbound("hi")
req = yield self.cellulant_sms_calls.get()
self.assertEqual(req.path, '/')
self.assertEqual(req.method, 'POST')
self.assertEqual({
#.........这里部分代码省略.........
示例9: AirtelBfTransportTestCase
# 需要导入模块: from vumi.tests.utils import MockHttpServer [as 别名]
# 或者: from vumi.tests.utils.MockHttpServer import stop [as 别名]
class AirtelBfTransportTestCase(TransportTestCase):
transport_name = 'airtel'
transport_type = 'sms'
transport_class = AirtelBfHttpTransport
send_path = '/sendsms/index'
send_port = 9999
@inlineCallbacks
def setUp(self):
yield super(AirtelBfTransportTestCase, self).setUp()
self.airtel_sms_calls = DeferredQueue()
self.mock_airtel_sms = MockHttpServer(self.handle_request)
yield self.mock_airtel_sms.start()
self.config = {
'transport_name': self.transport_name,
'receive_path': 'sendsms',
'receive_port': 9998,
'outbound_url': self.mock_airtel_sms.url,
'default_shortcode': '3411',
'login': 'texttochange',
'password': 'password'
}
self.transport = yield self.get_transport(self.config)
self.transport_url = self.transport.get_transport_url()
@inlineCallbacks
def tearDown(self):
yield self.mock_airtel_sms.stop()
yield super(AirtelBfTransportTestCase, self).tearDown()
def handle_request(self, request):
self.airtel_sms_calls.put(request)
return ''
@inlineCallbacks
def test_sending_sms(self):
yield self.dispatch(self.mkmsg_out())
req = yield self.airtel_sms_calls.get()
self.assertEqual(req.path, '/')
self.assertEqual(req.method, 'GET')
self.assertEqual({
'DA': ['9292'],
'SOA': ['+41791234567'],
'content': ['hello world'],
'u': ['texttochange'],
'p': ['password']
}, req.args)
[smsg] = self.get_dispatched_events()
self.assertEqual(
self.mkmsg_ack(user_message_id='1',
sent_message_id='1'),
smsg)
@inlineCallbacks
def test_sending_sms_complex(self):
yield self.dispatch(self.mkmsg_out(
from_addr="+2261", content=u'setoffre #A#BELG=10/tete+300000/pu# envoy\xe9 depuis SIMAgriMobile'))
req = yield self.airtel_sms_calls.get()
self.assertEqual(req.path, '/')
self.assertEqual(req.method, 'GET')
self.assertEqual(
'setoffre #A#BELG=10/tete+300000/pu# envoyé depuis SIMAgriMobile',
req.args['content'][0])
[smsg] = self.get_dispatched_events()
self.assertEqual(
self.mkmsg_ack(user_message_id='1',
sent_message_id='1'),
smsg)
def mkurl_raw(self, **params):
return '%s%s?%s' % (
self.transport_url,
self.config['receive_path'],
urlencode(params))
def mkurl(self, content, from_addr, **kw):
params = {
'message': content,
'msisdn': from_addr}
params.update(kw)
return self.mkurl_raw(**params)
@inlineCallbacks
def test_receiving_sms(self):
url = self.mkurl('Hello envoy\xc3\xa9', '+2261')
response = yield http_request_full(url, method='GET')
[smsg] = self.get_dispatched_messages()
self.assertEqual(response.code, http.OK)
self.assertEqual(u'Hello envoy\xe9', smsg['content'])
self.assertEqual('3411', smsg['to_addr'])
self.assertEqual('+2261', smsg['from_addr'])
@inlineCallbacks
#.........这里部分代码省略.........
示例10: CmYoTransportTestCase
# 需要导入模块: from vumi.tests.utils import MockHttpServer [as 别名]
# 或者: from vumi.tests.utils.MockHttpServer import stop [as 别名]
class CmYoTransportTestCase(TransportTestCase):
transport_name = 'cm'
transport_type = 'sms'
transport_class = CmYoTransport
send_path = '/sendsms'
send_port = 9999
yo_incomming_template = ('%s%s?sender=0041791234567&'
'code=%s&message=%s')
@inlineCallbacks
def setUp(self):
DelayedCall.debug = True
yield super(CmYoTransportTestCase, self).setUp()
self.cmyo_sms_calls = DeferredQueue()
self.mock_cmyo_sms = MockHttpServer(self.handle_request)
yield self.mock_cmyo_sms.start()
self.config = {
'transport_name': self.transport_name,
'login': 'login',
'password': 'password',
'default_origin': "+313455",
'customer_id': '3454',
'receive_path': 'yo',
'receive_port': 9998,
'outbound_url': self.mock_cmyo_sms.url,
}
self.transport = yield self.get_transport(self.config)
self.transport_url = self.transport.get_transport_url()
self.today = datetime.utcnow().date()
@inlineCallbacks
def tearDown(self):
yield self.mock_cmyo_sms.stop()
yield super(CmYoTransportTestCase, self).tearDown()
def mkmsg_fail(self, user_message_id='1',
failure_level='', failure_code=0,
failure_reason='', transport_metadata={}):
if transport_metadata is None:
transport_metadata = {}
return TransportEvent(
event_id=RegexMatcher(r'^[0-9a-fA-F]{32}$'),
event_type='delivery_report',
delivery_status='failed',
failure_level=failure_level,
failure_code=failure_code,
failure_reason=failure_reason,
user_message_id=user_message_id,
timestamp=UTCNearNow(),
transport_name=self.transport_name,
transport_metadata=transport_metadata)
def mkmsg_in(self, content='Hello World',
from_addr='41791234567',
session_event=TransportUserMessage.SESSION_NONE,
message_id='abc', transport_type=None,
transport_metadata=None):
if transport_type is None:
transport_type = self.transport_type
if transport_metadata is None:
transport_metadata = {}
return TransportUserMessage(
from_addr='+41791234567',
to_addr='9292',
group=None,
message_id=message_id,
transport_name=self.transport_name,
transport_type=transport_type,
transport_metadata=transport_metadata,
content=content,
session_event=session_event,
timestamp=UTCNearNow())
#def make_resource_worker(self, msg, code=http.OK, send_id=None):
#w = get_stubbed_worker(TestResourceWorker, {})
#w.set_resources([
#(self.send_path, TestResource, (msg, code, send_id))])
#self._workers.append(w)
#return w.startWorker()
def handle_request(self, request):
self.cmyo_sms_calls.put(request)
return ''
@inlineCallbacks
def test_sending_sms(self):
msg = self.mkmsg_out(to_addr='+41791234567', content=u'Message envoy\xe9')
yield self.dispatch(msg)
req = yield self.cmyo_sms_calls.get()
self.assertEqual(req.path, '/')
self.assertEqual(req.method, 'POST')
[smsg] = self.get_dispatched_events()
self.assertEqual('1', smsg['user_message_id'])
self.assertEqual('1', smsg['sent_message_id'])
#.........这里部分代码省略.........
示例11: TestAppositTransport
# 需要导入模块: from vumi.tests.utils import MockHttpServer [as 别名]
# 或者: from vumi.tests.utils.MockHttpServer import stop [as 别名]
class TestAppositTransport(VumiTestCase):
@inlineCallbacks
def setUp(self):
self.mock_server = MockHttpServer(self.handle_inbound_request)
self.outbound_requests = DeferredQueue()
self.mock_server_response = ''
self.mock_server_response_code = http.OK
yield self.mock_server.start()
config = {
'web_path': 'api/v1/apposit/sms',
'web_port': 0,
'credentials': {
'8123': {
'username': 'root',
'password': 'toor',
'service_id': 'service-id-1',
},
'8124': {
'username': 'admin',
'password': 'nimda',
'service_id': 'service-id-2',
}
},
'outbound_url': self.mock_server.url,
}
self.tx_helper = self.add_helper(
TransportHelper(
AppositTransport, transport_addr='8123',
mobile_addr='251911223344'))
self.transport = yield self.tx_helper.get_transport(config)
self.transport_url = self.transport.get_transport_url()
self.web_path = config['web_path']
@inlineCallbacks
def tearDown(self):
yield self.mock_server.stop()
yield super(TestAppositTransport, self).tearDown()
def send_full_inbound_request(self, **params):
return http_request_full(
'%s%s' % (self.transport_url, self.web_path),
data=urlencode(params),
method='POST',
headers={'Content-Type': self.transport.CONTENT_TYPE})
def send_inbound_request(self, **kwargs):
params = {
'from': '251911223344',
'to': '8123',
'channel': 'SMS',
'content': 'never odd or even',
'isTest': 'true',
}
params.update(kwargs)
return self.send_full_inbound_request(**params)
def handle_inbound_request(self, request):
self.outbound_requests.put(request)
request.setResponseCode(self.mock_server_response_code)
return self.mock_server_response
def set_mock_server_response(self, code=http.OK, body=''):
self.mock_server_response_code = code
self.mock_server_response = body
def assert_outbound_request(self, request, **kwargs):
expected_args = {
'username': 'root',
'password': 'toor',
'serviceId': 'service-id-1',
'fromAddress': '8123',
'toAddress': '251911223344',
'content': 'so many dynamos',
'channel': 'SMS',
}
expected_args.update(kwargs)
self.assertEqual(request.path, '/')
self.assertEqual(request.method, 'POST')
self.assertEqual(dict((k, [v]) for k, v in expected_args.iteritems()),
request.args)
self.assertEqual(request.getHeader('Content-Type'),
self.transport.CONTENT_TYPE)
def assert_message_fields(self, msg, **kwargs):
fields = {
'transport_name': self.tx_helper.transport_name,
'transport_type': 'sms',
'from_addr': '251911223344',
'to_addr': '8123',
'content': 'so many dynamos',
'provider': 'apposit',
'transport_metadata': {'apposit': {'isTest': 'true'}},
}
fields.update(kwargs)
for field_name, expected_value in fields.iteritems():
self.assertEqual(msg[field_name], expected_value)
#.........这里部分代码省略.........
示例12: SimagriTransportTestCase
# 需要导入模块: from vumi.tests.utils import MockHttpServer [as 别名]
# 或者: from vumi.tests.utils.MockHttpServer import stop [as 别名]
class SimagriTransportTestCase(TransportTestCase):
transport_name = 'simagri'
transport_type = 'sms'
transport_class = SimagriHttpTransport
send_path = '/sendsms/index'
send_port = 9999
@inlineCallbacks
def setUp(self):
yield super(SimagriTransportTestCase, self).setUp()
self.simagri_sms_calls = DeferredQueue()
self.mock_simagri_sms = MockHttpServer(self.handle_request)
yield self.mock_simagri_sms.start()
self.config = {
'transport_name': self.transport_name,
'receive_path': 'sendsms',
'receive_port': 9998,
'outbound_url': self.mock_simagri_sms.url
}
self.transport = yield self.get_transport(self.config)
self.transport_url = self.transport.get_transport_url()
@inlineCallbacks
def tearDown(self):
yield self.mock_simagri_sms.stop()
yield super(SimagriTransportTestCase, self).tearDown()
def handle_request(self, request):
self.simagri_sms_calls.put(request)
return ''
@inlineCallbacks
def test_sending_sms(self):
yield self.dispatch(self.mkmsg_out(from_addr="+2261"))
req = yield self.simagri_sms_calls.get()
self.assertEqual(req.path, '/')
self.assertEqual(req.method, 'GET')
self.assertEqual({
'from_addr': ['+2261'],
'message': ['hello world'],
}, req.args)
[smsg] = self.get_dispatched_events()
self.assertEqual(self.mkmsg_ack(user_message_id='1',
sent_message_id='1'),
smsg)
@inlineCallbacks
def test_sending_sms_complex(self):
yield self.dispatch(self.mkmsg_out(
from_addr="+2261", content=u'setoffre #A#BELG=10/tete+300000/pu# envoy\xe9 depuis SIMAgriMobile'))
req = yield self.simagri_sms_calls.get()
self.assertEqual(req.path, '/')
self.assertEqual(req.method, 'GET')
self.assertEqual({
'from_addr': ['+2261'],
'message': ['setoffre #A#BELG=10/tete+300000/pu# envoy\xc3\xa9 depuis SIMAgriMobile'],
}, req.args)
[smsg] = self.get_dispatched_events()
self.assertEqual(self.mkmsg_ack(user_message_id='1',
sent_message_id='1'),
smsg)
def mkurl_raw(self, **params):
return '%s%s?%s' % (
self.transport_url,
self.config['receive_path'],
urlencode(params))
def mkurl(self, content, to_addr, from_addr, **kw):
params = {
'message': content,
'to_addr': to_addr,
'from_addr': from_addr,
}
params.update(kw)
return self.mkurl_raw(**params)
@inlineCallbacks
def test_receiving_sms(self):
url = self.mkurl('Hello envoy\xc3\xa9', '+2261', '2323')
response = yield http_request_full(url, method='GET')
[smsg] = self.get_dispatched_messages()
self.assertEqual(response.code, http.OK)
self.assertEqual(u'Hello envoy\xe9', smsg['content'])
self.assertEqual('+2261', smsg['to_addr'])
self.assertEqual('2323', smsg['from_addr'])
@inlineCallbacks
def test_receiving_sms_accent(self):
url = self.mkurl("Mr Zonga Salif veut vendre 11 t Maïs Blanc à 14909,09/t, Total HT 163999,99 contact:+22666486073 l'offre expire dans 11 jrs", '+2261', '2323')
response = yield http_request_full(url, method='GET')
[smsg] = self.get_dispatched_messages()
self.assertEqual(response.code, http.OK)
self.assertEqual(u"Mr Zonga Salif veut vendre 11 t Ma\xefs Blanc \xe0 14909,09/t, Total HT 163999,99 contact:+22666486073 l'offre expire dans 11 jrs", smsg['content'])
#.........这里部分代码省略.........