def config(): update = request.args.get('update') config = Config() existing_configs = Config.query().fetch() if existing_configs: config = existing_configs[0] if update == 'true': admin_username = request.args.get('admin_username') admin_apikey = request.args.get('admin_apikey') twilio_account_sid = request.args.get('twilio_account_sid') twilio_auth_token = request.args.get('twilio_auth_token') twilio_phone_number = request.args.get('twilio_phone_number') if admin_username: config.admin_username = admin_username if admin_apikey: config.admin_apikey = admin_apikey if twilio_account_sid: config.twilio_account_sid = twilio_account_sid if twilio_auth_token: config.twilio_auth_token = twilio_auth_token if twilio_phone_number: config.twilio_phone_number = twilio_phone_number config.put() return config.toJson()
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 inner(*args, **kwargs): config = Config.query().fetch()[0] auth = request.authorization if not auth \ or auth.username != config.admin_username \ or auth.password != config.admin_apikey: abort(400, "unauthorized access") return func(*args, **kwargs)
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 setUp(self): self.app = app.test_client() self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() ndb.get_context().clear_cache() Config(admin_username='******', admin_apikey='key').put()
def setUp(self): self.ADMIN = 'admin' self.APIKEY = '123456789' self.app = app.test_client() self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.district = District(id='district_id', name='Sulawesi', slug='sulawesi') self.district.put() self.config = Config(admin_username=self.ADMIN, admin_apikey=self.APIKEY) self.config.put() ndb.get_context().clear_cache()
def setUp(self): self.app = app.test_client() self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() Config(id='test', twilio_account_sid='SID', twilio_auth_token='TOKEN', twilio_phone_number='+321').put() self.resource = Messages('foo', ('SID', 'TOKEN'))
def broadcast_sms(): config = Config.query().get() data = request.form.get("task") d = json.loads(data) phone_numbers = d["phone_numbers"] message = d["message"] client = TwilioRestClient(config.twilio_account_sid, config.twilio_auth_token) for phone_number in phone_numbers: client.messages.create(body=message, to=phone_number, from_=config.twilio_phone_number) return {"farmers_sent": len(phone_numbers), "body": message}
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 setUp(self): self.app = app.test_client() self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() ndb.get_context().clear_cache() self.user = User(role='farmer', phone_number='+123', first_name='name') self.user.put() Config(id='test', twilio_phone_number='+321').put()
def test_update_config(self): res = self.config(update='true', admin_username='******', admin_apikey='123', twilio_account_sid='sid', twilio_auth_token='token', twilio_phone_number='123') data = json.loads(res.data) self.assertEqual(200, res.status_code) self.assertEqual('kat', data['admin_username']) self.assertEqual('123', data['admin_apikey']) self.assertEqual('sid', data['twilio_account_sid']) self.assertEqual('token', data['twilio_auth_token']) self.assertEqual('123', data['twilio_phone_number']) self.assertEqual(1, len(Config.query().fetch()))
def setUp(self): self.app = app.test_client() self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.testbed.init_taskqueue_stub(root_path='.') self.user = User(role='district_leader', phone_number='6072809193', first_name='Kat', district_id='sum123') self.user.put() District(id='sum123', name='Sumatra', slug='sumatra').put() Config(id='test', twilio_phone_number='+321').put()
def broadcast_sms(): config = Config.query().get() data = request.form.get('task') d = json.loads(data) phone_numbers = d['phone_numbers'] message = d['message'] client = TwilioRestClient( config.twilio_account_sid, config.twilio_auth_token) for phone_number in phone_numbers: client.messages.create( body=message, to=phone_number, from_=config.twilio_phone_number) return { 'farmers_sent': len(phone_numbers), 'body': message }
class UserTest(unittest.TestCase): def setUp(self): self.ADMIN = 'admin' self.APIKEY = '123456789' self.app = app.test_client() self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.district = District(id='district_id', name='Sulawesi', slug='sulawesi') self.district.put() self.config = Config(admin_username=self.ADMIN, admin_apikey=self.APIKEY) self.config.put() ndb.get_context().clear_cache() def tearDown(self): self.testbed.deactivate() def headers(self, username=None, apikey=None): username = username or self.ADMIN apikey = apikey or self.APIKEY return { 'Authorization': 'Basic ' + b64encode("{}:{}".format(username, apikey)) } def insert(self, **kwargs): headers = self.headers() return self.app.post('/v1/users', data=kwargs, headers=headers) def retrieve(self, user_id): headers = self.headers() return self.app.get('/v1/users/{}'.format(user_id), headers=headers) def fetch(self, **kwargs): headers = self.headers() return self.app.get('/v1/users', query_string=kwargs, headers=headers) def insert_without_auth(self, **kwargs): return self.app.post('/v1/users', data=kwargs) def retrieve_without_auth(self, user_id): return self.app.get('/v1/users/{}'.format(user_id)) def fetch_without_auth(self, **kwargs): return self.app.get('/v1/users', query_string=kwargs) def insert_with_invalid_admin(self, **kwargs): headers = self.headers(username='******') return self.app.post('/v1/users', data=kwargs, headers=headers) def retrieve_with_invalid_admin(self, user_id): headers = self.headers(username='******') return self.app.get('/v1/users/{}'.format(user_id), headers=headers) def fetch_with_invalid_admin(self, **kwargs): headers = self.headers(username='******') return self.app.get('/v1/users', query_string=kwargs, headers=headers) def insert_with_invalid_apikey(self, **kwargs): headers = self.headers('admin', '663377') return self.app.post('/v1/users', data=kwargs, headers=headers) def retrieve_with_invalid_apikey(self, user_id): headers = self.headers('admin', '663377') return self.app.get('/v1/users/{}'.format(user_id), headers=headers) def fetch_with_invalid_apikey(self, **kwargs): headers = self.headers('admin', '663377') return self.app.get('/v1/users', query_string=kwargs, headers=headers) def test_insert_user(self): res = self.insert(role='farmer', phone_number='1234567', first_name='Kat', last_name='Leigh', district_id=self.district.key.id()) data = json.loads(res.data) self.assertEqual('farmer', data['role']) self.assertEqual('1234567', data['phone_number']) self.assertEqual('Kat', data['first_name']) self.assertEqual('Leigh', data['last_name']) self.assertEqual('district_id', data['district_id']) self.assertEqual(1, len(User.query().fetch())) def test_insert_without_district(self): res = self.insert(role='farmer', phone_number='1234567', first_name='Kat', last_name='Leigh') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('district_id is required', data['error']) def test_insert_with_non_existent_district(self): res = self.insert(role='farmer', phone_number='1234567', first_name='Kat', last_name='Leigh', district_id='Dsomerandomid') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('Dsomerandomid is an invalid district_id', data['error']) def test_insert_without_optional_param(self): res = self.insert(role='hutan_biru', phone_number='1234567', first_name='Kat') data = json.loads(res.data) self.assertEqual('hutan_biru', data['role']) self.assertEqual('1234567', data['phone_number']) self.assertEqual('Kat', data['first_name']) self.assertEqual(None, data['last_name']) self.assertEqual(1, len(User.query().fetch())) def test_insert_without_role(self): res = self.insert(phone_number='1234567', first_name='Kat') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('role is required', data['error']) self.assertEqual(0, len(User.query().fetch())) def test_insert_without_phone(self): res = self.insert(role='farmer', first_name='Kat') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('phone_number is required', data['error']) self.assertEqual(0, len(User.query().fetch())) def test_insert_without_name(self): res = self.insert(role='farmer', phone_number='1234567') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('first_name is required', data['error']) self.assertEqual(0, len(User.query().fetch())) def test_insert_with_undefined_roles(self): res = self.insert(role='teacher', phone_number='1234567', first_name='Kat') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertTrue('teacher is an invalid role' in data['error']) self.assertEqual(0, len(User.query().fetch())) def test_with_different_roles(self): self.insert(role='farmer', phone_number='1234567', first_name='Kat', last_name='Leigh', district_id=self.district.key.id()) self.insert(role='hutan_biru', phone_number='1234567', first_name='Kat', last_name='Leigh') self.assertEqual(2, len(User.query().fetch())) def test_insert_and_retrieve(self): req = self.insert(role='hutan_biru', phone_number='321', first_name='Kat', last_name='Leigh') data = json.loads(req.data) get = self.retrieve(data['id']) res = json.loads(get.data) self.assertEqual(data['id'], res['id']) self.assertEqual(data['role'], res['role']) self.assertEqual(data['phone_number'], res['phone_number']) self.assertEqual(data['first_name'], res['first_name']) self.assertEqual(data['last_name'], res['last_name']) self.assertEqual(data['ts_created'], res['ts_created']) self.assertEqual(data['ts_updated'], res['ts_updated']) self.assertEqual(1, len(User.query().fetch())) def test_insert_and_retrieve_without_last_name(self): req = self.insert(role='hutan_biru', phone_number='321', first_name='Kat') data = json.loads(req.data) get = self.retrieve(data['id']) res = json.loads(get.data) self.assertEqual(data['id'], res['id']) self.assertEqual(data['role'], res['role']) self.assertEqual(data['phone_number'], res['phone_number']) self.assertEqual(data['first_name'], res['first_name']) self.assertEqual(None, res['last_name']) self.assertEqual(None, data['last_name']) self.assertEqual(data['ts_created'], res['ts_created']) self.assertEqual(data['ts_updated'], res['ts_updated']) self.assertEqual(1, len(User.query().fetch())) def test_retrieve_empty_db(self): res = self.retrieve('123') data = json.loads(res.data) self.assertEqual(404, res.status_code) self.assertEqual('The uri "/v1/users/123" was not found', data['error']) def test_fetch_empty_db(self): res = self.fetch() data = json.loads(res.data) self.assertEqual(200, res.status_code) self.assertEqual(0, len(data['users'])) def test_fetch_with_phone_number(self): self.insert(role='farmer', phone_number='123', first_name='Erika', district_id=self.district.key.id()) self.insert(role='hutan_biru', phone_number='321', first_name='Kat') res = self.fetch(phone_number='123') data = json.loads(res.data) r = data['users'] self.assertEqual(200, res.status_code) self.assertEqual(1, len(r)) self.assertEqual('123', r[0]['phone_number']) self.assertEqual('Erika', r[0]['first_name']) self.assertEqual('farmer', r[0]['role']) def test_fetch_without_phone_number(self): self.insert(role='hutan_biru', phone_number='321', first_name='Kat') self.insert(role='farmer', phone_number='123', first_name='Erika', district_id=self.district.key.id()) res = self.fetch() data = json.loads(res.data) r = data['users'] self.assertEqual(200, res.status_code) self.assertEqual(2, len(r)) self.assertEqual('123', r[0]['phone_number']) self.assertEqual('Erika', r[0]['first_name']) self.assertEqual('farmer', r[0]['role']) self.assertEqual('district_id', r[0]['district_id']) self.assertEqual('321', r[1]['phone_number']) self.assertEqual('Kat', r[1]['first_name']) self.assertEqual('hutan_biru', r[1]['role']) def test_insert_without_auth(self): res = self.insert_without_auth(role='farmer', phone_number='1234567', first_name='Kat', last_name='Leigh', district_id=self.district.key.id()) data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', data['error']) self.assertEqual(0, len(User.query().fetch())) def test_retrieve_without_auth(self): req = self.insert(role='hutan_biru', phone_number='321', first_name='Kat', last_name='Leigh') data = json.loads(req.data) res = self.retrieve_without_auth(data['id']) r = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', r['error']) def test_fetch_without_auth(self): self.insert(role='farmer', phone_number='123', first_name='Erika', district_id=self.district.key.id()) res = self.fetch_without_auth(phone_number='123') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', data['error']) def test_insert_with_invalid_admin(self): res = self.insert_with_invalid_admin( role='farmer', phone_number='1234567', first_name='Kat', district_id=self.district.key.id()) data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', data['error']) self.assertEqual(0, len(User.query().fetch())) def test_retrieve_with_invalid_admin(self): req = self.insert(role='hutan_biru', phone_number='321', first_name='Kat', last_name='Leigh') data = json.loads(req.data) res = self.retrieve_with_invalid_admin(data['id']) r = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', r['error']) def test_fetch_with_invalid_admin(self): self.insert(role='farmer', phone_number='123', first_name='Erika', district_id=self.district.key.id()) res = self.fetch_with_invalid_admin(phone_number='123') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', data['error']) def test_insert_with_invalid_apikey(self): res = self.insert_with_invalid_apikey( role='farmer', phone_number='1234567', first_name='Kat', district_id=self.district.key.id()) data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', data['error']) self.assertEqual(0, len(User.query().fetch())) def test_retrieve_with_invalid_apikey(self): req = self.insert(role='hutan_biru', phone_number='321', first_name='Kat', last_name='Leigh') data = json.loads(req.data) res = self.retrieve_with_invalid_apikey(data['id']) r = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', r['error']) def test_fetch_with_invalid_apikey(self): self.insert(role='farmer', phone_number='123', first_name='Erika', district_id=self.district.key.id()) res = self.fetch_with_invalid_apikey(phone_number='123') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', data['error'])
class UserTest(unittest.TestCase): def setUp(self): self.ADMIN = 'admin' self.APIKEY = '123456789' self.app = app.test_client() self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.district = District(id='district_id', name='Sulawesi', slug='sulawesi') self.district.put() self.config = Config(admin_username=self.ADMIN, admin_apikey=self.APIKEY) self.config.put() ndb.get_context().clear_cache() def tearDown(self): self.testbed.deactivate() def headers(self, username=None, apikey=None): username = username or self.ADMIN apikey = apikey or self.APIKEY return { 'Authorization': 'Basic ' + b64encode("{}:{}".format(username, apikey)) } def insert(self, **kwargs): headers = self.headers() return self.app.post('/v1/users', data=kwargs, headers=headers) def retrieve(self, user_id): headers = self.headers() return self.app.get('/v1/users/{}'.format(user_id), headers=headers) def fetch(self, **kwargs): headers = self.headers() return self.app.get('/v1/users', query_string=kwargs, headers=headers) def insert_without_auth(self, **kwargs): return self.app.post('/v1/users', data=kwargs) def retrieve_without_auth(self, user_id): return self.app.get('/v1/users/{}'.format(user_id)) def fetch_without_auth(self, **kwargs): return self.app.get('/v1/users', query_string=kwargs) def insert_with_invalid_admin(self, **kwargs): headers = self.headers(username='******') return self.app.post('/v1/users', data=kwargs, headers=headers) def retrieve_with_invalid_admin(self, user_id): headers = self.headers(username='******') return self.app.get('/v1/users/{}'.format(user_id), headers=headers) def fetch_with_invalid_admin(self, **kwargs): headers = self.headers(username='******') return self.app.get('/v1/users', query_string=kwargs, headers=headers) def insert_with_invalid_apikey(self, **kwargs): headers = self.headers('admin', '663377') return self.app.post('/v1/users', data=kwargs, headers=headers) def retrieve_with_invalid_apikey(self, user_id): headers = self.headers('admin', '663377') return self.app.get('/v1/users/{}'.format(user_id), headers=headers) def fetch_with_invalid_apikey(self, **kwargs): headers = self.headers('admin', '663377') return self.app.get('/v1/users', query_string=kwargs, headers=headers) def test_insert_user(self): res = self.insert(role='farmer', phone_number='1234567', first_name='Kat', last_name='Leigh', district_id=self.district.key.id()) data = json.loads(res.data) self.assertEqual('farmer', data['role']) self.assertEqual('1234567', data['phone_number']) self.assertEqual('Kat', data['first_name']) self.assertEqual('Leigh', data['last_name']) self.assertEqual('district_id', data['district_id']) self.assertEqual(1, len(User.query().fetch())) def test_insert_without_district(self): res = self.insert(role='farmer', phone_number='1234567', first_name='Kat', last_name='Leigh') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('district_id is required', data['error']) def test_insert_with_non_existent_district(self): res = self.insert(role='farmer', phone_number='1234567', first_name='Kat', last_name='Leigh', district_id='Dsomerandomid') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('Dsomerandomid is an invalid district_id', data['error']) def test_insert_without_optional_param(self): res = self.insert(role='hutan_biru', phone_number='1234567', first_name='Kat') data = json.loads(res.data) self.assertEqual('hutan_biru', data['role']) self.assertEqual('1234567', data['phone_number']) self.assertEqual('Kat', data['first_name']) self.assertEqual(None, data['last_name']) self.assertEqual(1, len(User.query().fetch())) def test_insert_without_role(self): res = self.insert(phone_number='1234567', first_name='Kat') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('role is required', data['error']) self.assertEqual(0, len(User.query().fetch())) def test_insert_without_phone(self): res = self.insert(role='farmer', first_name='Kat') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('phone_number is required', data['error']) self.assertEqual(0, len(User.query().fetch())) def test_insert_without_name(self): res = self.insert(role='farmer', phone_number='1234567') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('first_name is required', data['error']) self.assertEqual(0, len(User.query().fetch())) def test_insert_with_undefined_roles(self): res = self.insert(role='teacher', phone_number='1234567', first_name='Kat') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertTrue('teacher is an invalid role' in data['error']) self.assertEqual(0, len(User.query().fetch())) def test_with_different_roles(self): self.insert(role='farmer', phone_number='1234567', first_name='Kat', last_name='Leigh', district_id=self.district.key.id()) self.insert(role='hutan_biru', phone_number='1234567', first_name='Kat', last_name='Leigh') self.assertEqual(2, len(User.query().fetch())) def test_insert_and_retrieve(self): req = self.insert(role='hutan_biru', phone_number='321', first_name='Kat', last_name='Leigh') data = json.loads(req.data) get = self.retrieve(data['id']) res = json.loads(get.data) self.assertEqual(data['id'], res['id']) self.assertEqual(data['role'], res['role']) self.assertEqual(data['phone_number'], res['phone_number']) self.assertEqual(data['first_name'], res['first_name']) self.assertEqual(data['last_name'], res['last_name']) self.assertEqual(data['ts_created'], res['ts_created']) self.assertEqual(data['ts_updated'], res['ts_updated']) self.assertEqual(1, len(User.query().fetch())) def test_insert_and_retrieve_without_last_name(self): req = self.insert(role='hutan_biru', phone_number='321', first_name='Kat') data = json.loads(req.data) get = self.retrieve(data['id']) res = json.loads(get.data) self.assertEqual(data['id'], res['id']) self.assertEqual(data['role'], res['role']) self.assertEqual(data['phone_number'], res['phone_number']) self.assertEqual(data['first_name'], res['first_name']) self.assertEqual(None, res['last_name']) self.assertEqual(None, data['last_name']) self.assertEqual(data['ts_created'], res['ts_created']) self.assertEqual(data['ts_updated'], res['ts_updated']) self.assertEqual(1, len(User.query().fetch())) def test_retrieve_empty_db(self): res = self.retrieve('123') data = json.loads(res.data) self.assertEqual(404, res.status_code) self.assertEqual('The uri "/v1/users/123" was not found', data['error']) def test_fetch_empty_db(self): res = self.fetch() data = json.loads(res.data) self.assertEqual(200, res.status_code) self.assertEqual(0, len(data['users'])) def test_fetch_with_phone_number(self): self.insert(role='farmer', phone_number='123', first_name='Erika', district_id=self.district.key.id()) self.insert(role='hutan_biru', phone_number='321', first_name='Kat') res = self.fetch(phone_number='123') data = json.loads(res.data) r = data['users'] self.assertEqual(200, res.status_code) self.assertEqual(1, len(r)) self.assertEqual('123', r[0]['phone_number']) self.assertEqual('Erika', r[0]['first_name']) self.assertEqual('farmer', r[0]['role']) def test_fetch_without_phone_number(self): self.insert(role='hutan_biru', phone_number='321', first_name='Kat') self.insert(role='farmer', phone_number='123', first_name='Erika', district_id=self.district.key.id()) res = self.fetch() data = json.loads(res.data) r = data['users'] self.assertEqual(200, res.status_code) self.assertEqual(2, len(r)) self.assertEqual('123', r[0]['phone_number']) self.assertEqual('Erika', r[0]['first_name']) self.assertEqual('farmer', r[0]['role']) self.assertEqual('district_id', r[0]['district_id']) self.assertEqual('321', r[1]['phone_number']) self.assertEqual('Kat', r[1]['first_name']) self.assertEqual('hutan_biru', r[1]['role']) def test_insert_without_auth(self): res = self.insert_without_auth(role='farmer', phone_number='1234567', first_name='Kat', last_name='Leigh', district_id=self.district.key.id()) data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', data['error']) self.assertEqual(0, len(User.query().fetch())) def test_retrieve_without_auth(self): req = self.insert(role='hutan_biru', phone_number='321', first_name='Kat', last_name='Leigh') data = json.loads(req.data) res = self.retrieve_without_auth(data['id']) r = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', r['error']) def test_fetch_without_auth(self): self.insert(role='farmer', phone_number='123', first_name='Erika', district_id=self.district.key.id()) res = self.fetch_without_auth(phone_number='123') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', data['error']) def test_insert_with_invalid_admin(self): res = self.insert_with_invalid_admin(role='farmer', phone_number='1234567', first_name='Kat', district_id=self.district.key.id()) data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', data['error']) self.assertEqual(0, len(User.query().fetch())) def test_retrieve_with_invalid_admin(self): req = self.insert(role='hutan_biru', phone_number='321', first_name='Kat', last_name='Leigh') data = json.loads(req.data) res = self.retrieve_with_invalid_admin(data['id']) r = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', r['error']) def test_fetch_with_invalid_admin(self): self.insert(role='farmer', phone_number='123', first_name='Erika', district_id=self.district.key.id()) res = self.fetch_with_invalid_admin(phone_number='123') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', data['error']) def test_insert_with_invalid_apikey(self): res = self.insert_with_invalid_apikey(role='farmer', phone_number='1234567', first_name='Kat', district_id=self.district.key.id()) data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', data['error']) self.assertEqual(0, len(User.query().fetch())) def test_retrieve_with_invalid_apikey(self): req = self.insert(role='hutan_biru', phone_number='321', first_name='Kat', last_name='Leigh') data = json.loads(req.data) res = self.retrieve_with_invalid_apikey(data['id']) r = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', r['error']) def test_fetch_with_invalid_apikey(self): self.insert(role='farmer', phone_number='123', first_name='Erika', district_id=self.district.key.id()) res = self.fetch_with_invalid_apikey(phone_number='123') data = json.loads(res.data) self.assertEqual(400, res.status_code) self.assertEqual('unauthorized access', data['error'])