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 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 validate_user_credentials(self, *args, **kw): user_id = self.request.get('user_id') secret = self.request.get('secret') if SFUser.validate_user(user_id, secret): handler_method(self, *args, **kw) else: self.bail_with_message(None, {'status': False, 'message': 'Authentication failed'}, 403) return
def test_user_by_user_id_and_fb_token(self): '''Get user by id and fb_token''' self.assertEqual(self.user.key(), SFUser.user_by_user_id_and_fb_token(self.user_id, self.fb_token).key()) self.assertEqual(self.user.key(), SFUser.user_by_user_id_and_fb_token(self.user_id, self.fb_token).key()) self.assertNone(SFUser.user_by_user_id_and_fb_token(self.user_id, 'a' + self.fb_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))
def test_create_secret(self): '''Creating a secret''' secret1 = SFUser.create_secret() self.assertNotNone(secret1) secret2 = SFUser.create_secret() self.assertNotEqual(secret1, secret2)
def test_validate_user(self): '''User validation''' self.assertTrue(SFUser.validate_user(self.user_id, self.secret)) self.assertTrue(SFUser.validate_user(self.user_id, self.secret)) self.assertFalse(SFUser.validate_user(self.user_id, "a" + self.secret))
def setUp(self): super(TestSFUser, self).setUp() self.user = SFUser.create(self.user_id, self.fb_token, self.secret)