def test_send_message(self): '''Sending a message should place the message on the queue for the channel''' properties = self.create_channel_properties() config = yield self.create_channel_config() redis = yield self.get_redis() channel = Channel(redis, config, properties, 'test-channel') yield channel.save() yield channel.start(self.service) resp = yield self.post('/channels/test-channel/messages/', { 'to': '+1234', 'content': 'foo', 'from': None}) yield self.assert_response( resp, http.OK, 'message sent', { 'to': '+1234', 'channel_id': 'test-channel', 'from': None, 'reply_to': None, 'channel_data': {}, 'content': 'foo', }, ignore=['timestamp', 'message_id']) [message] = self.get_dispatched_messages('test-channel.outbound') message_id = (yield resp.json())['result']['message_id'] self.assertEqual(message['message_id'], message_id) event_url = yield self.api.outbounds.load_event_url( 'test-channel', message['message_id']) self.assertEqual(event_url, None)
def create_channel(self, service, redis, transport_class=None, properties=default_channel_properties, id=None, config=None, plugins=[]): '''Creates and starts, and saves a channel, with a TelnetServerTransport transport''' self.patch(junebug.logging_service, 'LogFile', DummyLogFile) if transport_class is None: transport_class = 'vumi.transports.telnet.TelnetServerTransport' properties = deepcopy(properties) logpath = self.mktemp() if config is None: config = yield self.create_channel_config( channels={properties['type']: transport_class}, logging_path=logpath) channel = Channel(redis, config, properties, id=id, plugins=plugins) yield channel.start(self.service) properties['config']['transport_name'] = channel.id yield channel.save() self.addCleanup(channel.stop) returnValue(channel)
def test_send_message_event_url(self): '''Sending a message with a specified event url should store the event url for sending events in the future''' properties = self.create_channel_properties() config = yield self.create_channel_config() redis = yield self.get_redis() channel = Channel(redis, config, properties, 'test-channel') yield channel.save() yield channel.start(self.service) resp = yield self.post('/channels/test-channel/messages/', { 'to': '+1234', 'content': 'foo', 'from': None, 'event_url': 'http://test.org'}) yield self.assert_response( resp, http.OK, 'message sent', { 'to': '+1234', 'channel_id': 'test-channel', 'from': None, 'reply_to': None, 'channel_data': {}, 'content': 'foo', }, ignore=['timestamp', 'message_id']) event_url = yield self.api.outbounds.load_event_url( 'test-channel', (yield resp.json())['result']['message_id']) self.assertEqual(event_url, 'http://test.org')
def test_send_message(self): '''Sending a message should place the message on the queue for the channel''' properties = self.create_channel_properties() config = yield self.create_channel_config() redis = yield self.get_redis() channel = Channel(redis, config, properties, id='test-channel') yield channel.save() yield channel.start(self.service) resp = yield self.post('/channels/test-channel/messages/', { 'to': '+1234', 'content': 'foo', 'from': None }) yield self.assert_response(resp, http.OK, 'message sent', { 'to': '+1234', 'channel_id': 'test-channel', 'from': None, 'reply_to': None, 'channel_data': {}, 'content': 'foo', }, ignore=['timestamp', 'message_id']) [message] = self.get_dispatched_messages('test-channel.outbound') message_id = (yield resp.json())['result']['message_id'] self.assertEqual(message['message_id'], message_id) event_url = yield self.api.outbounds.load_event_url( 'test-channel', message['message_id']) self.assertEqual(event_url, None)
def test_delete_channel(self): config = yield self.create_channel_config() properties = self.create_channel_properties() channel = Channel(self.redis, config, properties, id='test-channel') yield channel.save() yield channel.start(self.service) self.assertTrue('test-channel' in self.service.namedServices) properties = yield self.redis.get('test-channel:properties') self.assertNotEqual(properties, None) resp = yield self.delete('/channels/test-channel') yield self.assert_response(resp, http.OK, 'channel deleted', {}) self.assertFalse('test-channel' in self.service.namedServices) properties = yield self.redis.get('test-channel:properties') self.assertEqual(properties, None) resp = yield self.delete('/channels/test-channel') yield self.assert_response( resp, http.NOT_FOUND, 'channel not found', {'errors': [{ 'message': '', 'type': 'ChannelNotFound', }]}) self.assertFalse('test-channel' in self.service.namedServices) properties = yield self.redis.get('test-channel:properties') self.assertEqual(properties, None)
def test_send_message_event_url(self): '''Sending a message with a specified event url should store the event url for sending events in the future''' properties = self.create_channel_properties() config = yield self.create_channel_config() redis = yield self.get_redis() channel = Channel(redis, config, properties, id='test-channel') yield channel.save() yield channel.start(self.service) resp = yield self.post( '/channels/test-channel/messages/', { 'to': '+1234', 'content': 'foo', 'from': None, 'event_url': 'http://test.org' }) yield self.assert_response(resp, http.OK, 'message sent', { 'to': '+1234', 'channel_id': 'test-channel', 'from': None, 'reply_to': None, 'channel_data': {}, 'content': 'foo', }, ignore=['timestamp', 'message_id']) event_url = yield self.api.outbounds.load_event_url( 'test-channel', (yield resp.json())['result']['message_id']) self.assertEqual(event_url, 'http://test.org')
def test_send_message_equal_character_limit(self): '''If the content length is equal to the character limit, no errors should be returned''' content = 'Equal to the character limit.' properties = self.create_channel_properties( character_limit=len(content)) config = yield self.create_channel_config() redis = yield self.get_redis() channel = Channel(redis, config, properties, id='test-channel') yield channel.save() yield channel.start(self.service) resp = yield self.post('/channels/test-channel/messages/', { 'to': '+1234', 'content': content, 'from': None }) yield self.assert_response(resp, http.OK, 'message sent', { 'to': '+1234', 'channel_id': 'test-channel', 'from': None, 'reply_to': None, 'channel_data': {}, 'content': content, }, ignore=['timestamp', 'message_id'])
def test_send_message_over_character_limit(self): '''If the content length is over the character limit, an error should be returned''' properties = self.create_channel_properties(character_limit=10) config = yield self.create_channel_config() redis = yield self.get_redis() channel = Channel(redis, config, properties, id='test-channel') yield channel.save() yield channel.start(self.service) resp = yield self.post('/channels/test-channel/messages/', { 'to': '+1234', 'content': 'Over the character limit.', 'from': None }) yield self.assert_response( resp, http.BAD_REQUEST, 'message too long', { 'errors': [{ 'message': "Message content u'Over the character limit.' " "is of length 25, which is greater than the character " "limit of 10", 'type': 'MessageTooLong', }], })
def test_delete_channel(self): config = yield self.create_channel_config() properties = self.create_channel_properties() channel = Channel(self.redis, config, properties, 'test-channel') yield channel.save() yield channel.start(self.service) self.assertTrue('test-channel' in self.service.namedServices) properties = yield self.redis.get('test-channel:properties') self.assertNotEqual(properties, None) resp = yield self.delete('/channels/test-channel') yield self.assert_response(resp, http.OK, 'channel deleted', {}) self.assertFalse('test-channel' in self.service.namedServices) properties = yield self.redis.get('test-channel:properties') self.assertEqual(properties, None) resp = yield self.delete('/channels/test-channel') yield self.assert_response( resp, http.NOT_FOUND, 'channel not found', { 'errors': [{ 'message': '', 'type': 'ChannelNotFound', }] }) self.assertFalse('test-channel' in self.service.namedServices) properties = yield self.redis.get('test-channel:properties') self.assertEqual(properties, None)
def create_channel( self, service, redis, transport_class=None, properties=default_channel_properties, id=None, config=None, plugins=[]): '''Creates and starts, and saves a channel, with a TelnetServerTransport transport''' self.patch(junebug.logging_service, 'LogFile', DummyLogFile) if transport_class is None: transport_class = 'vumi.transports.telnet.TelnetServerTransport' properties = deepcopy(properties) logpath = self.mktemp() if config is None: config = yield self.create_channel_config( channels={ properties['type']: transport_class }, logging_path=logpath) channel = Channel( redis, config, properties, id=id, plugins=plugins) yield channel.start(self.service) properties['config']['transport_name'] = channel.id yield channel.save() self.addCleanup(channel.stop) returnValue(channel)
def create_channel(self, request, body): '''Create a channel''' channel = Channel( self.redis, self.config, body) yield channel.save() yield channel.start(self.service) returnValue(response( request, 'channel created', (yield channel.status())))
def create_channel(self, request, body): '''Create a channel''' channel = Channel( self.redis, self.config, body, self.plugins) yield channel.start(self.service) yield channel.save() returnValue(response( request, 'channel created', (yield channel.status()), code=http.CREATED))
def create_channel(self, request, body): '''Create a channel''' if not (body.get('mo_url') or body.get('amqp_queue')): raise ApiUsageError( 'One or both of "mo_url" and "amqp_queue" must be specified') channel = Channel(self.redis, self.config, body, self.plugins) yield channel.start(self.service) yield channel.save() returnValue( response(request, 'channel created', (yield channel.status())))
def create_channel(self, request, body): '''Create a channel''' if not (body.get('mo_url') or body.get('amqp_queue')): raise ApiUsageError( 'One or both of "mo_url" and "amqp_queue" must be specified') channel = Channel( self.redis, self.config, body, self.plugins) yield channel.save() yield channel.start(self.service) returnValue(response( request, 'channel created', (yield channel.status())))
def test_get_channel(self): properties = self.create_channel_properties() config = yield self.create_channel_config() redis = yield self.get_redis() channel = Channel(redis, config, properties, u'test-channel') yield channel.save() yield channel.start(self.service) resp = yield self.get('/channels/test-channel') yield self.assert_response( resp, http.OK, 'channel found', conjoin(properties, { 'status': {}, 'id': 'test-channel', }))
def create_channel( self, service, redis, transport_class, properties=default_channel_properties, id=None): '''Creates and starts, and saves a channel, with a TelnetServerTransport transport''' properties = deepcopy(properties) config = yield self.create_channel_config() channel = Channel( redis, config, properties, id=id) properties['config']['transport_name'] = channel.id yield channel.start(self.service) yield channel.save() self.addCleanup(channel.stop) returnValue(channel)
def test_get_channel(self): properties = self.create_channel_properties() config = yield self.create_channel_config() redis = yield self.get_redis() channel = Channel(redis, config, properties, id=u'test-channel') yield channel.save() yield channel.start(self.service) resp = yield self.get('/channels/test-channel') yield self.assert_response( resp, http.OK, 'channel found', conjoin(properties, { 'status': self.generate_status(), 'id': 'test-channel', }))
def test_restart_channel(self): config = yield self.create_channel_config() properties = self.create_channel_properties() channel = Channel(self.redis, config, properties, id='test-channel') yield channel.save() yield channel.start(self.service) actions = self.record_channel_methods('start', 'stop') resp = yield self.post('/channels/test-channel/restart', None) yield self.assert_response(resp, http.OK, 'channel restarted', {}) self.assertEqual(actions, [ ('stop', u'test-channel'), ('start', u'test-channel'), ])
def test_send_message_message_rate(self): '''Sending a message should increment the message rate counter''' clock = yield self.patch_message_rate_clock() channel = Channel( (yield self.get_redis()), (yield self.create_channel_config()), self.create_channel_properties(), id='test-channel') yield channel.save() yield channel.start(self.service) yield self.post('/channels/test-channel/messages/', { 'to': '+1234', 'content': 'foo', 'from': None}) clock.advance(channel.config.metric_window) rate = yield self.api.message_rate.get_messages_per_second( 'test-channel', 'outbound', channel.config.metric_window) self.assertEqual(rate, 1.0 / channel.config.metric_window)
def test_modify_channel_config_change(self): redis = yield self.get_redis() properties = self.create_channel_properties() config = yield self.create_channel_config() channel = Channel(redis, config, properties, 'test-channel') yield channel.save() yield channel.start(self.service) properties['config']['name'] = 'bar' resp = yield self.post('/channels/test-channel', properties) yield self.assert_response( resp, http.OK, 'channel updated', conjoin(properties, { 'status': {}, 'id': 'test-channel', }))
def test_modify_channel_config_change(self): redis = yield self.get_redis() properties = self.create_channel_properties() config = yield self.create_channel_config() channel = Channel(redis, config, properties, id='test-channel') yield channel.save() yield channel.start(self.service) properties['config']['name'] = 'bar' resp = yield self.post('/channels/test-channel', properties) yield self.assert_response( resp, http.OK, 'channel updated', conjoin(properties, { 'status': self.generate_status(), 'id': 'test-channel', }))
def test_modify_channel_no_config_change(self): properties = self.create_channel_properties() config = yield self.create_channel_config() redis = yield self.get_redis() channel = Channel(redis, config, properties, 'test-channel') yield channel.save() yield channel.start(self.service) resp = yield self.post( '/channels/test-channel', {'metadata': {'foo': 'bar'}}) yield self.assert_response( resp, http.OK, 'channel updated', conjoin(properties, { 'status': {}, 'id': 'test-channel', 'metadata': {'foo': 'bar'}, }))
def test_send_message_message_rate(self): '''Sending a message should increment the message rate counter''' clock = yield self.patch_message_rate_clock() channel = Channel((yield self.get_redis()), (yield self.create_channel_config()), self.create_channel_properties(), id='test-channel') yield channel.save() yield channel.start(self.service) yield self.post('/channels/test-channel/messages/', { 'to': '+1234', 'content': 'foo', 'from': None }) clock.advance(channel.config.metric_window) rate = yield self.api.message_rate.get_messages_per_second( 'test-channel', 'outbound', channel.config.metric_window) self.assertEqual(rate, 1.0 / channel.config.metric_window)
def test_send_message_under_character_limit(self): '''If the content length is under the character limit, no errors should be returned''' properties = self.create_channel_properties(character_limit=100) config = yield self.create_channel_config() redis = yield self.get_redis() channel = Channel(redis, config, properties, id='test-channel') yield channel.save() yield channel.start(self.service) resp = yield self.post('/channels/test-channel/messages/', { 'to': '+1234', 'content': 'Under the character limit.', 'from': None}) yield self.assert_response( resp, http.OK, 'message sent', { 'to': '+1234', 'channel_id': 'test-channel', 'from': None, 'reply_to': None, 'channel_data': {}, 'content': 'Under the character limit.', }, ignore=['timestamp', 'message_id'])
def test_send_message_over_character_limit(self): '''If the content length is over the character limit, an error should be returned''' properties = self.create_channel_properties(character_limit=10) config = yield self.create_channel_config() redis = yield self.get_redis() channel = Channel(redis, config, properties, id='test-channel') yield channel.save() yield channel.start(self.service) resp = yield self.post('/channels/test-channel/messages/', { 'to': '+1234', 'content': 'Over the character limit.', 'from': None}) yield self.assert_response( resp, http.BAD_REQUEST, 'message too long', { 'errors': [{ 'message': "Message content u'Over the character limit.' " "is of length 25, which is greater than the character " "limit of 10", 'type': 'MessageTooLong', }], })
def test_send_message_reply(self): '''Sending a reply message should fetch the relevant inbound message, use it to construct a reply message, and place the reply message on the queue for the channel''' channel = Channel( redis_manager=(yield self.get_redis()), config=(yield self.create_channel_config()), properties=self.create_channel_properties(), id='test-channel') yield channel.save() yield channel.start(self.service) in_msg = TransportUserMessage( from_addr='+2789', to_addr='+1234', transport_name='test-channel', transport_type='_', transport_metadata={'foo': 'bar'}) yield self.api.inbounds.store_vumi_message('test-channel', in_msg) expected = in_msg.reply(content='testcontent') expected = api_from_message(expected) resp = yield self.post('/channels/test-channel/messages/', { 'reply_to': in_msg['message_id'], 'content': 'testcontent', }) yield self.assert_response( resp, http.OK, 'message sent', omit(expected, 'timestamp', 'message_id'), ignore=['timestamp', 'message_id']) [message] = self.get_dispatched_messages('test-channel.outbound') message_id = (yield resp.json())['result']['message_id'] self.assertEqual(message['message_id'], message_id)
def test_modify_channel_no_config_change(self): properties = self.create_channel_properties() config = yield self.create_channel_config() redis = yield self.get_redis() channel = Channel(redis, config, properties, id='test-channel') yield channel.save() yield channel.start(self.service) resp = yield self.post('/channels/test-channel', {'metadata': { 'foo': 'bar' }}) yield self.assert_response( resp, http.OK, 'channel updated', conjoin( properties, { 'status': self.generate_status(), 'id': 'test-channel', 'metadata': { 'foo': 'bar' }, }))
def test_send_message_reply(self): '''Sending a reply message should fetch the relevant inbound message, use it to construct a reply message, and place the reply message on the queue for the channel''' channel = Channel(redis_manager=(yield self.get_redis()), config=(yield self.create_channel_config()), properties=self.create_channel_properties(), id='test-channel') yield channel.save() yield channel.start(self.service) in_msg = TransportUserMessage(from_addr='+2789', to_addr='+1234', transport_name='test-channel', transport_type='_', transport_metadata={'foo': 'bar'}) yield self.api.inbounds.store_vumi_message('test-channel', in_msg) expected = in_msg.reply(content='testcontent') expected = api_from_message(expected) resp = yield self.post('/channels/test-channel/messages/', { 'reply_to': in_msg['message_id'], 'content': 'testcontent', }) yield self.assert_response(resp, http.OK, 'message sent', omit(expected, 'timestamp', 'message_id'), ignore=['timestamp', 'message_id']) [message] = self.get_dispatched_messages('test-channel.outbound') message_id = (yield resp.json())['result']['message_id'] self.assertEqual(message['message_id'], message_id)