예제 #1
0
 def test_bad_call(self, post, get):
     # a bad HTTP status code causes a PGSheetsHTTPException exception
     post.return_value.status = 501
     post.return_value.content = b''
     c = Client("client_id", "client_secret")
     with self.assertRaises(PGSheetsHTTPException):
         c.getRefreshToken("fake_code")
예제 #2
0
 def test_bad_call(self, post, get):
     # a bad HTTP status code causes a PGSheetsHTTPException exception
     post.return_value.status = 501
     post.return_value.content = b''
     c = Client("client_id", "client_secret")
     with self.assertRaises(PGSheetsHTTPException):
         c.getRefreshToken("fake_code")
예제 #3
0
    def test_get_refresh_token(self, post, get):
        # feed data from:
        # https://developers.google.com/google-apps/spreadsheets/authorize
        # Example url from:
        # https://developers.google.com/identity/protocols/OAuth2InstalledApp
        client_id = ("812741506391-h38jh0j4fv0ce1krdkiq0hfvt6n5amrf.apps."
                     "googleusercontent.com")
        client_secret = "client_secret"
        redirect_uri = "urn:ietf:wg:oauth:2.0:oob"

        # 1) A user gets the permission URL from the getOauthUrl() method

        c = Client(client_id, client_secret)

        expected = (
            "https://accounts.google.com/o/oauth2/auth?"
            "scope={}&"
            "redirect_uri={}&"
            "response_type=code&"
            "client_id={}"
            .format(
                "https%3A//spreadsheets.google.com/feeds",
                redirect_uri,
                client_id
                ))

        self.assertEqual(c.getOauthUrl(), expected)
        self.assertFalse(get.called, "No API calls are made")
        self.assertFalse(post.called, "No API calls are made")

        # 2) The user then supplies the value from Google to getRefreshToken()

        post.return_value.status_code = 200
        post.return_value.content = """
        {
          "access_token":"1/fFAGRNJru1FTz70BzhT3Zg",
          "expires_in":3920,
          "token_type":"Bearer",
          "refresh_token":"1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
        }
        """.encode()

        token = c.getRefreshToken("fake_code")

        self.assertFalse(get.called, "No GET API calls are made")
        self.assertTrue(post.called)
        self.assertEqual(token,
                         "1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI")
예제 #4
0
    def test_get_refresh_token(self, post, get):
        # feed data from:
        # https://developers.google.com/google-apps/spreadsheets/authorize
        # Example url from:
        # https://developers.google.com/identity/protocols/OAuth2InstalledApp
        client_id = ("812741506391-h38jh0j4fv0ce1krdkiq0hfvt6n5amrf.apps."
                     "googleusercontent.com")
        client_secret = "client_secret"
        redirect_uri = "urn:ietf:wg:oauth:2.0:oob"

        # 1) A user gets the permission URL from the getOauthUrl() method

        c = Client(client_id, client_secret)

        expected = ("https://accounts.google.com/o/oauth2/auth?"
                    "scope={}&"
                    "redirect_uri={}&"
                    "response_type=code&"
                    "client_id={}".format(
                        "https%3A//spreadsheets.google.com/feeds",
                        redirect_uri, client_id))

        self.assertEqual(c.getOauthUrl(), expected)
        self.assertFalse(get.called, "No API calls are made")
        self.assertFalse(post.called, "No API calls are made")

        # 2) The user then supplies the value from Google to getRefreshToken()

        post.return_value.status_code = 200
        post.return_value.content = """
        {
          "access_token":"1/fFAGRNJru1FTz70BzhT3Zg",
          "expires_in":3920,
          "token_type":"Bearer",
          "refresh_token":"1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
        }
        """.encode()

        token = c.getRefreshToken("fake_code")

        self.assertFalse(get.called, "No GET API calls are made")
        self.assertTrue(post.called)
        self.assertEqual(token,
                         "1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI")