Пример #1
0
 def test_exception(self, post, get):
     # A bad HTTP code causes a PGSheetsHTTPException
     post.return_value.content = b''
     post.return_value.status = 500
     c = Client("client_id", "client_secret")
     t = Token(c, "refresh_token")
     with self.assertRaises(PGSheetsHTTPException):
         t.getAuthorizationHeader()
     self.assertFalse(get.called)
Пример #2
0
 def test_exception(self, post, get):
     # A bad HTTP code causes a PGSheetsHTTPException
     post.return_value.content = b''
     post.return_value.status = 500
     c = Client("client_id", "client_secret")
     t = Token(c, "refresh_token")
     with self.assertRaises(PGSheetsHTTPException):
         t.getAuthorizationHeader()
     self.assertFalse(get.called)
Пример #3
0
    def test_get_header(self, post, get):
        refresh_token = 'refresh'
        client_id = "client_id"
        client_secret = "client_secret"

        c = Client(client_id, client_secret)
        t = Token(c, refresh_token)

        post.return_value.status_code = 200
        post.return_value.content = b"""
        {
          "access_token":"1/fFBGRNJru1FQd44AzqT3Zg",
          "expires_in":3920,
          "token_type":"Bearer"
        }"""

        h = t.getAuthorizationHeader()

        # https://www.googleapis.com/oauth2/v3/token was called
        pos, kwargs = post.call_args
        self.assertEqual(pos[0], "https://www.googleapis.com/oauth2/v3/token")
        self.assertIn('data', kwargs)
        self.assertEqual(set(kwargs['data'].keys()),
                         {'refresh_token', 'client_id', 'client_secret',
                          'grant_type'})
        self.assertEqual(kwargs['data']['refresh_token'], refresh_token)
        self.assertEqual(kwargs['data']['client_id'], client_id)
        self.assertEqual(kwargs['data']['client_secret'], client_secret)
        self.assertEqual(kwargs['data']['grant_type'], "refresh_token")

        # h is a simple dictionary
        self.assertEqual(h,
                         {'Authorization': 'Bearer 1/fFBGRNJru1FQd44AzqT3Zg'})

        # Another call should get the same header without another API call
        post.reset_mock()
        h = t.getAuthorizationHeader({'test': 1})
        self.assertEqual(h,
                         {'Authorization': 'Bearer 1/fFBGRNJru1FQd44AzqT3Zg',
                          'test': 1})
        self.assertFalse(post.called)

        # Once the timeout has passed we get a new token
        post.status_code = 200
        post.return_value.content = b"""
        {
          "access_token":"new_token",
          "expires_in":3920,
          "token_type":"Bearer"
        }"""
        t._expires = datetime.datetime.utcnow()
        h = t.getAuthorizationHeader()
        self.assertEqual(h, {'Authorization': 'Bearer new_token'})

        # We never make any get requests
        self.assertFalse(get.called)
Пример #4
0
    def test_get_header(self, post, get):
        refresh_token = 'refresh'
        client_id = "client_id"
        client_secret = "client_secret"

        c = Client(client_id, client_secret)
        t = Token(c, refresh_token)

        post.return_value.status_code = 200
        post.return_value.content = b"""
        {
          "access_token":"1/fFBGRNJru1FQd44AzqT3Zg",
          "expires_in":3920,
          "token_type":"Bearer"
        }"""

        h = t.getAuthorizationHeader()

        # https://www.googleapis.com/oauth2/v3/token was called
        pos, kwargs = post.call_args
        self.assertEqual(pos[0], "https://www.googleapis.com/oauth2/v3/token")
        self.assertIn('data', kwargs)
        self.assertEqual(
            set(kwargs['data'].keys()),
            {'refresh_token', 'client_id', 'client_secret', 'grant_type'})
        self.assertEqual(kwargs['data']['refresh_token'], refresh_token)
        self.assertEqual(kwargs['data']['client_id'], client_id)
        self.assertEqual(kwargs['data']['client_secret'], client_secret)
        self.assertEqual(kwargs['data']['grant_type'], "refresh_token")

        # h is a simple dictionary
        self.assertEqual(h,
                         {'Authorization': 'Bearer 1/fFBGRNJru1FQd44AzqT3Zg'})

        # Another call should get the same header without another API call
        post.reset_mock()
        h = t.getAuthorizationHeader({'test': 1})
        self.assertEqual(h, {
            'Authorization': 'Bearer 1/fFBGRNJru1FQd44AzqT3Zg',
            'test': 1
        })
        self.assertFalse(post.called)

        # Once the timeout has passed we get a new token
        post.status_code = 200
        post.return_value.content = b"""
        {
          "access_token":"new_token",
          "expires_in":3920,
          "token_type":"Bearer"
        }"""
        t._expires = datetime.datetime.utcnow()
        h = t.getAuthorizationHeader()
        self.assertEqual(h, {'Authorization': 'Bearer new_token'})

        # We never make any get requests
        self.assertFalse(get.called)