Ejemplo n.º 1
0
    def test_refresh(self):
        client = OAuthClient(client_id, client_secret, access_token,
                             refresh_token)

        def server_response(request, uri, headers):
            return (200, headers, json.dumps(data))

        hp.register_uri(hp.POST,
                        client.TOKEN_ENDPOINT_URI,
                        body=server_response)

        data = {
            'access_token': 'newaccesstoken',
            'refresh_token': 'newrefreshtoken',
        }
        self.assertNotEqual(client.access_token, data['access_token'])
        self.assertNotEqual(client.refresh_token, data['refresh_token'])
        client.refresh()
        self.assertEqual(client.access_token, data['access_token'])
        self.assertEqual(client.refresh_token, data['refresh_token'])

        def server_response(request, uri, headers):
            return (400, headers, '')

        hp.reset()
        hp.register_uri(hp.POST,
                        client.TOKEN_ENDPOINT_URI,
                        body=server_response)

        client.access_token = access_token
        client.refresh_token = refresh_token
        with self.assertRaises(TokenRefreshError):
            client.refresh()
        self.assertEqual(client.access_token, access_token)
        self.assertEqual(client.refresh_token, refresh_token)
Ejemplo n.º 2
0
  def test_refresh(self):
    client = OAuthClient(client_id, client_secret, access_token, refresh_token)

    def server_response(request, uri, headers):
      return (200, headers, json.dumps(data))
    hp.register_uri(hp.POST, client.TOKEN_ENDPOINT_URI, body=server_response)

    data = {
        'access_token': 'newaccesstoken',
        'refresh_token': 'newrefreshtoken',
      }
    self.assertNotEqual(client.access_token, data['access_token'])
    self.assertNotEqual(client.refresh_token, data['refresh_token'])
    client.refresh()
    self.assertEqual(client.access_token, data['access_token'])
    self.assertEqual(client.refresh_token, data['refresh_token'])

    def server_response(request, uri, headers):
      return (400, headers, '')
    hp.reset()
    hp.register_uri(hp.POST, client.TOKEN_ENDPOINT_URI, body=server_response)

    client.access_token = access_token
    client.refresh_token = refresh_token
    with self.assertRaises(TokenRefreshError):
      client.refresh()
    self.assertEqual(client.access_token, access_token)
    self.assertEqual(client.refresh_token, refresh_token)
Ejemplo n.º 3
0
    def test_token_endpoint_uri_used_instead_of_default(self):
        # Requests to the default BASE_API_URI will noticeably fail by raising an
        # AssertionError. Requests to the new URL will respond HTTP 200.
        new_token_endpoint_uri = 'http://example.com/oauth/token'

        # If any error is raised by the server, the test suite will never exit when
        # using Python 3. This strange technique is used to raise the errors
        # outside of the mocked server environment.
        errors_in_server = []

        def server_response(request, uri, headers):
            parsed_uri = urlparse(uri)
            parsed_reference = urlparse(new_token_endpoint_uri)
            try:
                self.assertEqual(parsed_uri.scheme, parsed_reference.scheme)
                self.assertEqual(parsed_uri.netloc, parsed_reference.netloc)
                self.assertEqual(parsed_uri.path, parsed_reference.path)
            except AssertionError as e:
                errors_in_server.append(e)
            response = {
                'access_token': 'newaccesstoken',
                'refresh_token': 'newrefreshtoken',
            }
            return (200, headers, json.dumps(response))

        hp.register_uri(hp.POST,
                        OAuthClient.TOKEN_ENDPOINT_URI,
                        body=server_response)
        hp.register_uri(hp.POST, new_token_endpoint_uri, body=server_response)

        client = OAuthClient(client_id, client_secret, access_token,
                             refresh_token)
        with self.assertRaises(AssertionError):
            client.refresh()
            if errors_in_server: raise errors_in_server.pop()

        client2 = OAuthClient(client_id,
                              client_secret,
                              access_token,
                              refresh_token,
                              token_endpoint_uri=new_token_endpoint_uri)
        self.assertTrue(client2.refresh())
Ejemplo n.º 4
0
  def test_token_endpoint_uri_used_instead_of_default(self):
    # Requests to the default BASE_API_URI will noticeably fail by raising an
    # AssertionError. Requests to the new URL will respond HTTP 200.
    new_token_endpoint_uri = 'http://example.com/oauth/token'

    # If any error is raised by the server, the test suite will never exit when
    # using Python 3. This strange technique is used to raise the errors
    # outside of the mocked server environment.
    errors_in_server = []

    def server_response(request, uri, headers):
      parsed_uri = urlparse(uri)
      parsed_reference = urlparse(new_token_endpoint_uri)
      try:
        self.assertEqual(parsed_uri.scheme, parsed_reference.scheme)
        self.assertEqual(parsed_uri.netloc, parsed_reference.netloc)
        self.assertEqual(parsed_uri.path, parsed_reference.path)
      except AssertionError as e:
        errors_in_server.append(e)
      response = {
          'access_token': 'newaccesstoken',
          'refresh_token': 'newrefreshtoken',
        }
      return (200, headers, json.dumps(response))
    hp.register_uri(hp.POST, OAuthClient.TOKEN_ENDPOINT_URI, body=server_response)
    hp.register_uri(hp.POST, new_token_endpoint_uri, body=server_response)

    client = OAuthClient(client_id, client_secret, access_token, refresh_token)
    with self.assertRaises(AssertionError):
      client.refresh()
      if errors_in_server: raise errors_in_server.pop()

    client2 = OAuthClient(
        client_id,
        client_secret,
        access_token,
        refresh_token,
        token_endpoint_uri=new_token_endpoint_uri)
    self.assertTrue(client2.refresh())