def test_validate_user_memcache(self): '''Validation of a user should store answer in memcache''' user_id = "memcache_validate_user " secret = "memcahce_validate_secret" SFUser.create(user_id, "fb_token", secret) self.assertNone(memcache.get(SECRET_KEY % (user_id, secret))) self.assertTrue(SFUser.validate_user(user_id, secret)) self.assertEqual(memcache.get(SECRET_KEY % (user_id, secret)), 1)
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_user_id_and_fb_token_memcache(self): '''Get user by id and fb_token should memcache the answer''' user_id = "memcache_ubyift_user_id" fb_token = "memcache_ubyift_fb_token" user = SFUser.create(user_id, fb_token, "secret") self.assertNone(memcache.get(TOKEN_KEY % (user_id, fb_token))) self.assertEqual(user.key(), SFUser.user_by_user_id_and_fb_token(user_id, fb_token).key()) self.assertEqual(user.key(), memcache.get(TOKEN_KEY % (user_id, fb_token)).key())
def setUp(self): super(TestSFUser, self).setUp() self.user = SFUser.create(self.user_id, self.fb_token, self.secret)