def spoof_incoming_obj(self, message, connection=None): if connection is None: connection = Connection.objects.all()[0] incomingmessage = IncomingMessage(connection, message) incomingmessage.db_message = Message.objects.create(direction='I', connection=Connection.objects.all()[0], text=message) return incomingmessage
def spoof_incoming_obj(self, message, connection=None): if connection is None: connection = Connection.objects.all()[0] incomingmessage = IncomingMessage(connection, message) incomingmessage.db_message = Message.objects.create( direction='I', connection=connection, text=message) return incomingmessage
def get_rapid_message(number, text, backend=None): from rapidsms.messages import IncomingMessage db_msg = get_message(number, text) msg = IncomingMessage(db_msg.connection, text) msg.db_message = db_msg return msg
def tick(self, session): """ Invoked periodically for each live session to check how long since we sent the last question, and decide to resend it or give up the whole thing. """ timeout = conf.TIMEOUT idle_time = datetime.datetime.now() - session.last_modified if idle_time >= datetime.timedelta(seconds=timeout): # feed a dummy message to the handler msg = IncomingMessage(connection=session.connection, text="TimeOut") self.router.incoming(msg) msg.flush_responses() # make sure response goes out
def test_no_handlers(self): """If app has no (relevant) handlers, it should return nothing.""" app = HandlersApp(self.router) app.handlers = [] msg = IncomingMessage(self.connection, 'hello world') retVal = app.handle(msg) self.assertEqual(retVal, None) self.assertEqual(len(msg.responses), 0)
def test(cls, text, identity=None): """ Test this handler by dispatching an IncomingMessage containing ``text``, as sent by ``identity`` via a mock backend. Return a list containing the ``text`` property of each response, in the order which they were sent.:: >>> class AlwaysHandler(BaseHandler): ... ... @classmethod ... def dispatch(cls, router, msg): ... msg.respond("xxx") ... msg.respond("yyy") ... return True >>> AlwaysHandler.test('anything') ['xxx', 'yyy'] Return False if the handler ignored the message (ie, the ``dispatch`` method returned False or None). >>> class NeverHandler(BaseHandler): ... pass >>> NeverHandler.test('anything') False This is intended to test the handler in complete isolation. To test the interaction between multiple apps and/or handlers, see the rapidsms.tests.scripted module. """ # avoid setting the default identity to "mock" in the signature, # to avoid exposing it in the public API. it's not important. if identity is None: identity = "mock" # models can't be loaded until the django ORM is ready. from rapidsms.models import Backend, Connection from rapidsms.messages import IncomingMessage # spawn a mock backend for each handler, to allow multiple tests # to interact with one another without overlapping. if not hasattr(cls, "_mock_backend"): cls._mock_backend = Backend.objects.create( name="mock_%d" % id(cls)) conn, created = Connection.objects.get_or_create( backend=cls._mock_backend, identity=identity) msg = IncomingMessage( connection=conn, text=text) accepted = cls.dispatch(None, msg) return [m['text'] for m in msg.responses]\ if accepted else False
def test_handle(self): """App should call upon its handlers to respond to the message.""" app = HandlersApp(self.router) app.handlers = [EchoKeywordHandler] msg = IncomingMessage(self.connection, 'hello world') retVal = app.handle(msg) self.assertTrue(retVal) self.assertEqual(len(msg.responses), 1) self.assertEqual(msg.responses[0]['text'], 'world')
def _check_dispatch(self, text, correct_response): msg = IncomingMessage(self.connection, text) retVal = EchoKeywordHandler.dispatch(self.router, msg) if correct_response is not None: self.assertTrue(retVal) self.assertEqual(len(msg.responses), 1) self.assertEqual(msg.responses[0]['text'], correct_response) else: self.assertFalse(retVal) self.assertEqual(len(msg.responses), 0)
def test_no_pattern(self): """Handler should not operate if it does not have a pattern.""" pattern = getattr(AdditionPatternHandler, 'pattern') delattr(AdditionPatternHandler, 'pattern') try: with self.assertRaises(HandlerError): msg = IncomingMessage(self.connection, '1 plus 2') AdditionPatternHandler.dispatch(self.router, msg) finally: setattr(AdditionPatternHandler, 'pattern', pattern)
def test_no_keyword(self): """Handler should raise an exception if there is no keyword.""" keyword = getattr(EchoKeywordHandler, 'keyword') delattr(EchoKeywordHandler, 'keyword') try: with self.assertRaises(HandlerError): msg = IncomingMessage(self.connection, 'hello') EchoKeywordHandler.dispatch(self.router, msg) finally: setattr(EchoKeywordHandler, 'keyword', keyword)
def _test_handle(self, text, correct_response): msg = IncomingMessage(self.connection, text) retVal = PingHandler.dispatch(self.connection, msg) if correct_response is not None: self.assertTrue(retVal) self.assertEqual(len(msg.responses), 1) self.assertEqual(msg.responses[0]['text'], correct_response) else: self.assertFalse(retVal) self.assertEqual(len(msg.responses), 0)
def incoming(backend_name, identity, text): backend = settings.INSTALLED_BACKENDS.get(backend_name, {}) if "HANDLER" in backend: module = try_import(backend['HANDLER']) if module: module.incoming(backend_name, identity, text) else: backend, _ = Backend.objects.get_or_create(name=backend_name) connection, _ = backend.connection_set.get_or_create(identity=identity) message = IncomingMessage(connection, text, datetime.datetime.now()) router = Router() response = router.incoming(message)
def receive(text, connection, fields=None): """ Creates an incoming message and passes it to the router for processing. """ from rapidsms.router import get_router from rapidsms.messages import IncomingMessage router = get_router()() router.start() message = IncomingMessage(connection, text, datetime.datetime.now(), fields=fields) router.receive_incoming(message) router.stop() return message
def receive(text, connection, **kwargs): """ Creates an incoming message and passes it to the router for processing. :param text: text message :param connection: RapidSMS :py:class:`~rapidsms.models.Connection` object :param kwargs: Extra kwargs to pass to :py:class:`~rapidsms.messages.incoming.IncomingMessage` constructor :returns: :py:class:`~rapidsms.messages.incoming.IncomingMessage` object constructed by router. A returned message object does not indicate that router processing has finished or even started, as this depends on the router defined in :setting:`RAPIDSMS_ROUTER`. :rtype: :py:class:`~rapidsms.messages.incoming.IncomingMessage` """ router = get_router() message = IncomingMessage(connection, text) router.handle_incoming(connection.backend, connection.identity, text) return message
def create_incoming_message(self, incoming_response): incoming_message = IncomingMessage(self.connection, incoming_response) incoming_message.db_message = Message.objects.create(direction='I', connection=self.connection, text=incoming_response) return incoming_message
def run(self, backend_name, identity, text): backend, _ = Backend.objects.get_or_create(name=backend_name) connection, _ = backend.connection_set.get_or_create(identity=identity) message = IncomingMessage(connection, text, datetime.datetime.now()) router = Router() response = router.incoming(message)
def get_incoming_message(connection, message): incoming_message = IncomingMessage(connection, message) incoming_message.db_message = Message.objects.create(direction='I', connection=connection, text=message) return incoming_message
def test_dispatch(self): """BaseHandler dispatch should always return False.""" msg = IncomingMessage(self.connection, 'hello') retVal = BaseHandler.dispatch(self.router, msg) self.assertFalse(retVal) self.assertEqual(len(msg.responses), 0)