Example #1
0
 def test_get_token_success(self, now):
     http = request_mock(http_client.OK, "application/json", json.dumps({"access_token": "a", "expires_in": 100}))
     token, expiry = _metadata.get_token(http=http)
     self.assertEqual(token, "a")
     self.assertEqual(expiry, datetime.datetime.min + datetime.timedelta(seconds=100))
     # Verify mocks.
     now.assert_called_once_with()
     self.assertEqual(http.requests, 1)
     self.assertEqual(http.uri, EXPECTED_URL + "/token")
     self.assertEqual(http.method, "GET")
     self.assertIsNone(http.body)
     self.assertEqual(http.headers, _metadata.METADATA_HEADERS)
Example #2
0
 def test_get_token_success(self, now):
     http_request = request_mock(
         http_client.OK, 'application/json',
         json.dumps({
             'access_token': 'a',
             'expires_in': 100
         }))
     token, expiry = _metadata.get_token(http_request=http_request)
     self.assertEqual(token, 'a')
     self.assertEqual(
         expiry, datetime.datetime.min + datetime.timedelta(seconds=100))
     http_request.assert_called_once_with(EXPECTED_URL + '/token',
                                          **EXPECTED_KWARGS)
     now.assert_called_once_with()
 def test_get_token_success(self, now):
     http_request = request_mock(
         http_client.OK,
         'application/json',
         json.dumps({'access_token': 'a', 'expires_in': 100})
     )
     token, expiry = _metadata.get_token(http_request=http_request)
     self.assertEqual(token, 'a')
     self.assertEqual(
         expiry, datetime.datetime.min + datetime.timedelta(seconds=100))
     http_request.assert_called_once_with(
         EXPECTED_URL + '/token',
         **EXPECTED_KWARGS
     )
     now.assert_called_once_with()
Example #4
0
    def _refresh(self, http):
        """Refreshes the access token.

        Skip all the storage hoops and just refresh using the API.

        Args:
            http: an object to be used to make HTTP requests.

        Raises:
            HttpAccessTokenRefreshError: When the refresh fails.
        """
        try:
            self._retrieve_info(http)
            self.access_token, self.token_expiry = _metadata.get_token(
                http, service_account=self.service_account_email)
        except http_client.HTTPException as err:
            raise client.HttpAccessTokenRefreshError(str(err))
Example #5
0
    def _refresh(self, http):
        """Refreshes the access token.

        Skip all the storage hoops and just refresh using the API.

        Args:
            http: an object to be used to make HTTP requests.

        Raises:
            HttpAccessTokenRefreshError: When the refresh fails.
        """
        try:
            self._retrieve_info(http)
            self.access_token, self.token_expiry = _metadata.get_token(
                http, service_account=self.service_account_email)
        except http_client.HTTPException as err:
            raise client.HttpAccessTokenRefreshError(str(err))
Example #6
0
 def test_get_token_success(self, now):
     http = request_mock(
         http_client.OK, 'application/json',
         json.dumps({
             'access_token': 'a',
             'expires_in': 100
         }))
     token, expiry = _metadata.get_token(http=http)
     self.assertEqual(token, 'a')
     self.assertEqual(
         expiry, datetime.datetime.min + datetime.timedelta(seconds=100))
     # Verify mocks.
     now.assert_called_once_with()
     self.assertEqual(http.requests, 1)
     self.assertEqual(http.uri, EXPECTED_URL + '/token')
     self.assertEqual(http.method, 'GET')
     self.assertIsNone(http.body)
     self.assertEqual(http.headers, _metadata.METADATA_HEADERS)
Example #7
0
    def _refresh(self, http_request):
        """Refreshes the access_token.

        Skip all the storage hoops and just refresh using the API.

        Args:
            http_request: callable, a callable that matches the method
                          signature of httplib2.Http.request, used to make
                          the refresh request.

        Raises:
            HttpAccessTokenRefreshError: When the refresh fails.
        """
        try:
            self.access_token, self.token_expiry = _metadata.get_token(
                http_request=http_request)
        except httplib2.HttpLib2Error as e:
            raise HttpAccessTokenRefreshError(str(e))
Example #8
0
    def _refresh(self, http_request):
        """Refreshes the access_token.

        Skip all the storage hoops and just refresh using the API.

        Args:
            http_request: callable, a callable that matches the method
                          signature of httplib2.Http.request, used to make
                          the refresh request.

        Raises:
            HttpAccessTokenRefreshError: When the refresh fails.
        """
        try:
            self._retrieve_info(http_request)
            self.access_token, self.token_expiry = _metadata.get_token(
                http_request, service_account=self.service_account_email)
        except http_client.HTTPException as err:
            raise client.HttpAccessTokenRefreshError(str(err))
Example #9
0
    def _refresh(self, http_request):
        """Refreshes the access_token.

        Skip all the storage hoops and just refresh using the API.

        Args:
            http_request: callable, a callable that matches the method
                          signature of httplib2.Http.request, used to make
                          the refresh request.

        Raises:
            HttpAccessTokenRefreshError: When the refresh fails.
        """
        try:
            self._retrieve_info(http_request)
            self.access_token, self.token_expiry = _metadata.get_token(
                http_request, service_account=self.service_account_email)
        except httplib2.HttpLib2Error as e:
            raise HttpAccessTokenRefreshError(str(e))