def test_user_by_id_memcache(self): '''Get user by id should memcache the answer''' user_id = "memcache_ubi_user_id" user = SFUser.create(user_id, "fb_token", "secret") self.assertNone(memcache.get(USER_KEY % user_id)) self.assertEqual(user.key(), SFUser.user_by_id(user_id).key()) self.assertEqual(user.key(), memcache.get(USER_KEY % user_id).key())
def get(self, user_id): from google.appengine.api.urlfetch import fetch token = self.request.get('fb_token') user = SFUser.user_by_user_id_and_fb_token(user_id, token) if user is not None: self.respond({'secret': user.secret, 'status': True}) else: try: response = fetch('https://graph.facebook.com/me?access_token=%s' % token) me_data = JSONDecoder().decode(response.content) if me_data.get('id', None) is not None: if me_data.get('id') == user_id: user = SFUser.user_by_id(user_id) if user is None: user = SFUser.create(user_id, token, SFUser.create_secret()) else: user.fb_token = token user.put() self.respond({'secret': user.secret, 'status': True}) else: raise FBUserTokenError, "User id mismatch" else: self.bail_with_message(None, {'status': False, 'message': 'Facebook API said: %s' % response.content}, response.status_code) except Exception, err: self.bail_with_message(err, {'status': False, 'message': 'Error while validating Facebook token'})
def test_user_by_id(self): '''Get user by id''' self.assertEqual(self.user.key(), SFUser.user_by_id(self.user_id).key()) self.assertNone(SFUser.user_by_id("a" + self.user_id))