@async_test_timeout(timeout=30)
  def testFacebookRegistration(self):
    self._validate = False

    # Get one facebook test user by querying facebook.
    fu = facebook_utils.FacebookUtils()
    users = fu.QueryFacebookTestUsers(limit=1)
    assert len(users) == 1, users

    def _VerifyAccountStatus(cookie, results):
      u = results['user']
      dev = results['device']
      ident = results['identity']
      self.assertEqual(ident.user_id, u.user_id)
      self.assertTrue(u.name)
      self.assertTrue(u.given_name)
      self.assertTrue(u.family_name)
      self.assertIsNotNone(u.webapp_dev_id)
      [self.assertEqual(getattr(dev, k), v) for k, v in self._mobile_device_dict.items()]

      # Keep querying until notifications are found.
      while True:
        response_dict = self._SendRequest('query_notifications', cookie, {})
        if len(response_dict['notifications']) > 2:
          break
        time.sleep(0.100)

      self.assertEqual(response_dict['notifications'][1]['name'], 'register friend')
      notification = response_dict['notifications'][2]
      self.assertEqual(notification['name'], 'fetch_contacts')
      sort_key = Contact.CreateSortKey(None, notification['timestamp'])
      self.assertEqual(notification['invalidate']['contacts']['start_key'], sort_key)
      self.stop()

    def _VerifyResponse(response):
      """Verify successful registration. Query the identity and
      contacts and verify against the actual test data in facebook.
      """
      self.assertEqual(response.code, 200)
      cookie = self._tester.GetCookieFromResponse(response)
      user_dict = self._tester.DecodeUserCookie(cookie)
      response_dict = json.loads(response.body)

      self.assertTrue('user_id' in user_dict)
      self.assertTrue('device_id' in user_dict)
      self.assertEqual(user_dict['device_id'], response_dict['device_id'])

      with util.DictBarrier(partial(_VerifyAccountStatus, cookie)) as b:
        identity_key = 'FacebookGraph:%s' % users[0]['id']
        Identity.Query(self._client, hash_key=identity_key, col_names=None,
                       callback=b.Callback('identity'))
        User.Query(self._client, hash_key=user_dict['user_id'], col_names=None,
                   callback=b.Callback('user'))
        Device.Query(self._client, hash_key=user_dict['user_id'], range_key=user_dict['device_id'],
                     col_names=None, callback=b.Callback('device'))

    url = self.get_url('/link/facebook') + '?' + \
          urllib.urlencode({'access_token': users[0]['access_token']})
    self.http_client.fetch(url, method='POST',
                           headers={'Content-Type': 'application/json',
                                    'X-Xsrftoken': 'fake_xsrf',
                                    'Cookie': 'user=%s;_xsrf=fake_xsrf' % self._cookie},
                           body=json.dumps(auth_test._CreateRegisterRequest(self._mobile_device_dict)),
                           callback=_VerifyResponse)
Test end-end Facebook registration scenario using a test Facebook
dataset/ETHPy150Open viewfinderco/viewfinder/backend/www/test/auth_facebook_test.py/AuthFacebookTestCase.testFacebookRegistration