예제 #1
0
 def testMissingAccessToken(self):
   """ERROR: Test error on missing facebook access token."""
   self.assertRaisesHttpError(400,
                              auth_test._SendAuthRequest,
                              self._tester,
                              self.get_url('/register/facebook'),
                              'POST',
                              request_dict=auth_test._CreateRegisterRequest(self._mobile_device_dict))
예제 #2
0
 def testMissingRefreshToken(self):
   """ERROR: Test error on missing Google refresh token."""
   self.assertRaisesHttpError(400,
                              auth_test._SendAuthRequest,
                              self._tester,
                              self.get_url('/register/google'),
                              'POST',
                              request_dict=auth_test._CreateRegisterRequest(self._mobile_device_dict))
예제 #3
0
 def testMissingRefreshToken(self):
     """ERROR: Test error on missing Google refresh token."""
     self.assertRaisesHttpError(
         400,
         auth_test._SendAuthRequest,
         self._tester,
         self.get_url('/register/google'),
         'POST',
         request_dict=auth_test._CreateRegisterRequest(
             self._mobile_device_dict))
예제 #4
0
 def testMissingAccessToken(self):
     """ERROR: Test error on missing facebook access token."""
     self.assertRaisesHttpError(
         400,
         auth_test._SendAuthRequest,
         self._tester,
         self.get_url('/register/facebook'),
         'POST',
         request_dict=auth_test._CreateRegisterRequest(
             self._mobile_device_dict))
예제 #5
0
  def testAuthenticationFailed(self):
    """ERROR: Fail Facebook authentication (which returns None user_dict)."""
    with mock.patch('tornado.httpclient.AsyncHTTPClient', MockAsyncHTTPClient()) as mock_client:
      mock_client.map(r'https://graph.facebook.com/me\?',
                      lambda request: httpclient.HTTPResponse(request, 400))

      url = self.get_url('/register/facebook?access_token=access_token')
      self.assertRaisesHttpError(401,
                                 auth_test._SendAuthRequest,
                                 self._tester,
                                 url,
                                 'POST',
                                 user_cookie=self._cookie,
                                 request_dict=auth_test._CreateRegisterRequest(self._mobile_device_dict))
예제 #6
0
    def testAuthenticationFailed(self):
        """ERROR: Fail Facebook authentication (which returns None user_dict)."""
        with mock.patch('tornado.httpclient.AsyncHTTPClient',
                        MockAsyncHTTPClient()) as mock_client:
            mock_client.map(
                r'https://graph.facebook.com/me\?',
                lambda request: httpclient.HTTPResponse(request, 400))

            url = self.get_url('/register/facebook?access_token=access_token')
            self.assertRaisesHttpError(
                401,
                auth_test._SendAuthRequest,
                self._tester,
                url,
                'POST',
                user_cookie=self._cookie,
                request_dict=auth_test._CreateRegisterRequest(
                    self._mobile_device_dict))
예제 #7
0
    def testFacebookRegistration(self):
        """Test end-end Facebook registration scenario using a test Facebook
    account.
    """
        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)
예제 #8
0
  def testFacebookRegistration(self):
    """Test end-end Facebook registration scenario using a test Facebook
    account.
    """
    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)