def test_multi_word_district(self): invalid_messages = { 'look San Francisco harvest': ('San Francisco', 'harvest'), 'look harvest San Francisco': ('San Francisco', 'harvest'), 'look San Francisco plant': ('San Francisco', 'plant'), 'look plant San Francisco': ('San Francisco', 'plant'), 'look Tree House District sell': ('Tree House District', 'sell'), 'look sell Tree House District': ('Tree House District', 'sell'), 'lihat East and West Java panen': ('East and West Java', 'harvest'), 'lihat panen East and West Java': ('East and West Java', 'harvest'), 'lihat tanam Some really stupidly named district': ('Some really stupidly named district', 'plant'), 'lihat Some really stupidly named tanam': ('Some really stupidly named', 'plant'), 'lihat jual Yes it is': ('Yes it is', 'sell'), 'lihat Yes it is jual': ('Yes it is', 'sell') } for body, v in invalid_messages.items(): sms = SmsRequest() sms.body = body cmd = QueryCommand(sms) self.assertTrue(cmd.valid()) self.assertEqual(v[0], cmd.district) self.assertEqual(v[1], cmd.filter)
def test_sell_command_without_amount(self): invalid_messages = ['sell potato', 'jual potato'] for body in invalid_messages: sms = SmsRequest() sms.body = body cmd = SellCommand(sms) self.assertFalse(cmd.valid())
def test_harvest_command_without_amount(self): invalid_messages = ['harvest potato', 'harvest potato'] for body in invalid_messages: sms = SmsRequest() sms.body = body cmd = HarvestCommand(sms) self.assertFalse(cmd.valid())
def _broadcast(self, user, msg, district=None): sms = SmsRequest() sms.body = '' sms.user = user cmd = BroadcastCommand(sms) cmd.district = district cmd.msg = msg return cmd
def _query(self, district, action): sms = SmsRequest(id=SmsRequest.id()) sms.body = "" sms.user = self.user cmd = QueryCommand(sms) cmd.filter = action cmd.district = district return cmd
def _query(self, district, action): sms = SmsRequest(id=SmsRequest.id()) sms.body = '' sms.user = self.user cmd = QueryCommand(sms) cmd.filter = action cmd.district = district return cmd
def incoming_twilio_sms(): sms = SmsRequest(id=SmsRequest.id(), from_number=request.form.get('From'), to_number=request.form.get('To'), body=request.form.get('Body'), twilio_message_id=request.form.get('MessageSid'), from_city=request.form.get('FromCity'), from_state=request.form.get('FromState'), from_zip=request.form.get('FromZip'), from_country=request.form.get('FromCountry'), to_city=request.form.get('ToCity'), to_state=request.form.get('ToState'), to_zip=request.form.get('ToZip'), to_country=request.form.get('ToCountry')) if not sms.valid: app.logger.error(request) abort(400, 'invalid request') # store all sms for auditing sms.put() # load application data associated with the sms user = User.query(User.phone_number == sms.from_number).fetch() if not user: abort( 400, 'The phone number {} does not belong to a user'.format( sms.from_number)) sms.user = user[0] response_twiml = twiml.Response() response_message = None # dispatch sms request try: response_message = dispatcher.dispatch(sms) except (NoRouteError, MultipleRouteError): response_message = _('Unknown command, valid format below:\n' 'PLANT [qty] [type]\n' 'HARVEST [qty] [type]\n' 'SELL [qty] [type]\n' 'BROADCAST [msg]') if response_message: config = Config.query().get() response_twiml.sms(to=sms.from_number, sender=config.twilio_phone_number, msg=response_message) # update sms processed state sms.processed = True sms.ts_processed = datetime.now() sms.put() return str(response_twiml)
def test_harvest_command_with_invalid_command(self): invalid_messages = [ 'sell 20 potato', 'sell potato 20', 'jual 20 potato', 'jual potato 20' ] for body in invalid_messages: sms = SmsRequest() sms.body = body cmd = HarvestCommand(sms) self.assertFalse(cmd.valid())
def test_sell_command_with_invalid_command(self): invalid_messages = [ 'harvest 20 potato', 'harvest potato 20', 'panen 20 potato', 'panen potato 20' ] for body in invalid_messages: sms = SmsRequest() sms.body = body cmd = SellCommand(sms) self.assertFalse(cmd.valid())
def test_harvest_command_without_amount(self): invalid_messages = [ 'harvest potato', 'harvest potato' ] for body in invalid_messages: sms = SmsRequest() sms.body = body cmd = HarvestCommand(sms) self.assertFalse(cmd.valid())
def test_sell_command_without_amount(self): invalid_messages = [ 'sell potato', 'jual potato' ] for body in invalid_messages: sms = SmsRequest() sms.body = body cmd = SellCommand(sms) self.assertFalse(cmd.valid())
def test_ignore_case(self): valid_messages = [ 'SeLL 20 potato', ] for body in valid_messages: sms = SmsRequest() sms.body = body cmd = SellCommand(sms) self.assertTrue(cmd.valid()) self.assertEqual(20, cmd.amount) self.assertEqual('potato', cmd.plant)
def test_harvest_command_should_ignore_cases(self): valid_messages = { 'harvest 20 Potato': 'potato', 'harvest 20 POTATO': 'potato', 'harvest 20 Chinese Broccoli': 'chinese broccoli', 'harvest 20 CHINESE BROCCOLI': 'chinese broccoli' } for body, v in valid_messages.iteritems(): sms = SmsRequest() sms.body = body cmd = HarvestCommand(sms) self.assertEqual(v, cmd.plant)
def test_sell_command_should_ignore_cases(self): valid_messages = { 'sell 20 Potato': 'potato', 'sell 20 POTATO': 'potato', 'sell 20 Chinese Broccoli': 'chinese broccoli', 'sell 20 CHINESE BROCCOLI': 'chinese broccoli' } for body, v in valid_messages.iteritems(): sms = SmsRequest() sms.body = body cmd = SellCommand(sms) self.assertEqual(v, cmd.plant)
def test_sell_command(self): valid_messages = [ 'sell 20 potato', 'sell potato 20', 'jual 20 potato', 'jual potato 20' ] for body in valid_messages: sms = SmsRequest() sms.body = body cmd = SellCommand(sms) self.assertTrue(cmd.valid()) self.assertEqual(20, cmd.amount) self.assertEqual('potato', cmd.plant)
def test_harvest_command(self): valid_messages = [ 'harvest 20 potato', 'harvest potato 20', 'panen 20 potato', 'panen potato 20' ] for body in valid_messages: sms = SmsRequest() sms.body = body cmd = HarvestCommand(sms) self.assertTrue(cmd.valid()) self.assertEqual(20, cmd.amount) self.assertEqual('potato', cmd.plant)
def incoming_twilio_sms(): sms = SmsRequest(id=SmsRequest.id(), from_number=request.form.get('From'), to_number=request.form.get('To'), body=request.form.get('Body'), twilio_message_id=request.form.get('MessageSid'), from_city=request.form.get('FromCity'), from_state=request.form.get('FromState'), from_zip=request.form.get('FromZip'), from_country=request.form.get('FromCountry'), to_city=request.form.get('ToCity'), to_state=request.form.get('ToState'), to_zip=request.form.get('ToZip'), to_country=request.form.get('ToCountry')) if not sms.valid: app.logger.error(request) abort(400, 'invalid request') # store all sms for auditing sms.put() # load application data associated with the sms user = User.query(User.phone_number == sms.from_number).fetch() if not user: abort(400, 'The phone number {} does not belong to a user'.format(sms.from_number)) sms.user = user[0] response_twiml = twiml.Response() response_message = None # dispatch sms request try: response_message = dispatcher.dispatch(sms) except (NoRouteError, MultipleRouteError): response_message = _('Unknown command, valid format below:\n' 'PLANT [qty] [type]\n' 'HARVEST [qty] [type]\n' 'SELL [qty] [type]\n' 'BROADCAST [msg]') if response_message: config = Config.query().get() response_twiml.sms(to=sms.from_number, sender=config.twilio_phone_number, msg=response_message) # update sms processed state sms.processed = True sms.ts_processed = datetime.now() sms.put() return str(response_twiml)
def test_query_command_without_district(self): invalid_messages = [ 'look harvest', 'look plant', 'look sell', 'lihat panen', 'lihat tanam', 'lihat jual' ] for body in invalid_messages: sms = SmsRequest() sms.body = body cmd = QueryCommand(sms) self.assertFalse(cmd.valid())
def incoming_twilio_sms(): sms = SmsRequest( id=SmsRequest.id(), from_number=request.form.get("From"), to_number=request.form.get("To"), body=request.form.get("Body"), twilio_message_id=request.form.get("MessageSid"), from_city=request.form.get("FromCity"), from_state=request.form.get("FromState"), from_zip=request.form.get("FromZip"), from_country=request.form.get("FromCountry"), to_city=request.form.get("ToCity"), to_state=request.form.get("ToState"), to_zip=request.form.get("ToZip"), to_country=request.form.get("ToCountry"), ) if not sms.valid: app.logger.error(request) abort(400, "invalid request") # store all sms for auditing sms.put() # load application data associated with the sms user = User.query(User.phone_number == sms.from_number).fetch() if not user: abort(400, "The phone number {} does not belong to a user".format(sms.from_number)) sms.user = user[0] response_twiml = twiml.Response() response_message = None # dispatch sms request try: response_message = dispatcher.dispatch(sms) except (NoRouteError, MultipleRouteError): response_message = _("Unknown command") if response_message: config = Config.query().get() response_twiml.sms(to=sms.from_number, sender=config.twilio_phone_number, msg=response_message) # update sms processed state sms.processed = True sms.ts_processed = datetime.now() sms.put() return str(response_twiml)
def test_query_command(self): valid_messages = [ { 'filter': ['harvest', 'panen'], 'msg': [ 'look lompoko harvest', 'look harvest lompoko', 'look lompoko', # default harvest 'lihat lompoko panen', 'lihat panen lompoko', 'lihat lompoko' # default harvest ] }, { 'filter': ['plant', 'tanam'], 'msg': [ 'look lompoko plant', 'look plant lompoko', 'lihat lompoko tanam', 'lihat tanam lompoko' ] }, { 'filter': ['sell', 'jual'], 'msg': [ 'look lompoko sell', 'look sell lompoko', 'lihat lompoko jual', 'lihat jual lompoko' ] } ] for valid in valid_messages: for body in valid['msg']: sms = SmsRequest() sms.body = body cmd = QueryCommand(sms) self.assertTrue(cmd.valid()) self.assertTrue(cmd.filter in valid['filter']) self.assertEqual('lompoko', cmd.district)
def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.sms = SmsRequest() self.sms.user = User(role='farmer', phone_number='6072809193', first_name='Kat', district_id='sul123') self.sms.body = ''
def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.sms = SmsRequest() self.sms.user = User(role='farmer', phone_number='6072809193', first_name='Kat', district_id='sul123') self.sms.body = '' Farm(action='harvest', district_id='sul123', crop_name='potato', quantity=10).put() Farm(action='harvest', district_id='sul123', crop_name='carrot', quantity=5).put()
def test_request_should_be_logged_in_datastore(self, mock): mock.dispatch.return_value = None res = self.app.post('/v1/sms/twilio', data={ 'MessageSid': 'sid', 'From': self.user.phone_number, 'To': '+321', 'Body': 'jual' }) self.assertEqual(200, res.status_code) all_sms = SmsRequest.query().fetch() self.assertEqual(1, len(all_sms)) sms = all_sms[0] self.assertEqual('sid', sms.twilio_message_id) self.assertTrue(sms.processed) self.assertEqual(self.user.phone_number, sms.from_number) self.assertEqual('+321', sms.to_number) self.assertEqual('jual', sms.body)
def test_log_bad_request(self, mock): mock.dispatch.side_effect = Exception('can not process sms request ' 'because of bad action or ' 'dispatcher') with self.assertRaises(Exception): self.app.post('/v1/sms/twilio', data={ 'MessageSid': 'sid', 'From': self.user.phone_number, 'To': '+321', 'Body': 'jual' }) all_sms = SmsRequest.query().fetch() self.assertEqual(1, len(all_sms)) sms = all_sms[0] self.assertEqual('sid', sms.twilio_message_id) self.assertFalse(sms.processed) self.assertEqual(self.user.phone_number, sms.from_number) self.assertEqual('+321', sms.to_number) self.assertEqual('jual', sms.body)
def setUp(self): self.sms = SmsRequest()
def _broadcast_cmd(self, body): sms = SmsRequest() sms.user = User() sms.body = body return BroadcastCommand(sms)