Exemple #1
0
    def test_get_access_tokens(self):
        body = {
            "access_token":
            "T9cE5asGnuyYCCqIZFoWjFHvNbvVqHjl",
            "expires_in":
            3600,
            "restricted_to": [],
            "token_type":
            "bearer",
            "refresh_token":
            "J7rxTiWOHMoSC1isKZKBZWizoRXjkQzig5C6jFgCVJ9bUnsUfGMinKBDLZWP9BgR"
        }

        httpretty.register_uri(httpretty.POST,
                               "https://www.box.com/api/oauth2/token",
                               body=json.dumps(body),
                               status=200,
                               content_type='text/json')

        b = boxpython.BoxAuthenticateFlow(self.client_id, self.client_secret)
        auth_code = "@#$@FEWR"
        access_token, refresh_token = b.get_access_tokens(auth_code)

        self.assertEqual(access_token, body['access_token'])
        self.assertEqual(refresh_token, body['refresh_token'])

        query_body = httpretty.last_request().body

        self.assertEqual(
            urlparse.parse_qs(query_body), {
                "client_id": [self.client_id],
                "client_secret": [self.client_secret],
                "grant_type": ["authorization_code"],
                "code": [auth_code],
            })
Exemple #2
0
    def __get_box_data(self, interactive=True):
        self.data_file = "/tmp/test_data.txt"
        self.token_file = "/tmp/test_token.txt"

        if interactive:
            inputresp = raw_input('Use client id and secret'\
                                    ' from file %s? [y,n]' % self.data_file)

        if interactive and inputresp.lower() != 'y':
            client_id = raw_input('Write your client id :')
            client_secret = raw_input('Write your client secret :')
            self.__save_pair(self.data_file, client_id, client_secret)
        else:
            client_id, client_secret = self.__load_pair(self.data_file)

        if interactive:
            inputresp = raw_input('Use access and refresh token' \
                                    ' from file %s? [y,n]' % self.token_file)

        if interactive and inputresp.lower() != 'y':
            boxflow = boxpython.BoxAuthenticateFlow(client_id, client_secret)
            url = boxflow.get_authorization_url()

            import webbrowser
            webbrowser.open(url)
            auth_code = raw_input('Go to %s and write auth code :' % url)

            access_token, refresh_token = boxflow.get_access_tokens(auth_code)

            self.__save_pair(self.token_file, refresh_token, access_token)
        else:
            refresh_token, access_token = self.__load_pair(self.token_file)

        return client_id, client_secret, refresh_token, access_token
Exemple #3
0
    def test_get_authorization_url_redirect(self):
        b = boxpython.BoxAuthenticateFlow(self.client_id, self.client_secret)
        redirect_uri = 'http://toto.com/123?4&a=%201'
        url = b.get_authorization_url(redirect_uri)

        (scheme, netloc, path, params, query,
         fragment) = urlparse.urlparse(url)

        query_dict = urlparse.parse_qs(query)
        decoded_uri = urllib.unquote_plus(query_dict['redirect_uri'][0])
        redirect_uri = urllib.unquote_plus(redirect_uri)
        self.assertEqual(decoded_uri, redirect_uri)
Exemple #4
0
    def test_get_authorization_url(self):
        b = boxpython.BoxAuthenticateFlow(self.client_id, self.client_secret)
        url = b.get_authorization_url()

        (scheme, netloc, path, params, query,
         fragment) = urlparse.urlparse(url)

        expected = ('https', 'www.box.com', '/api/oauth2/authorize', '', '')
        returned = (scheme, netloc, path, params, fragment)
        self.assertEqual(returned, expected)

        query_dict = urlparse.parse_qs(query)
        self.assertEqual(query_dict['client_id'], [self.client_id])
        self.assertEqual(query_dict['response_type'], ['code'])
        self.assertEqual(query_dict['state'], ['authenticated'])
Exemple #5
0
    def test_get_access_tokens_with_json_error(self):
        body = {
            "error": "invalid_grant",
            "error_description": "Invalid user credentials"
        }

        httpretty.register_uri(httpretty.POST,
                               "https://www.box.com/api/oauth2/token",
                               body="{xyz#",
                               status=200,
                               content_type='text/json')

        b = boxpython.BoxAuthenticateFlow(self.client_id, self.client_secret)
        auth_code = "@#$@FEWR"
        with self.assertRaises(boxpython.BoxHttpResponseError):
            access_token, refresh_token = b.get_access_tokens(auth_code)
Exemple #6
0
    def test_get_access_tokens_with_box_error(self):
        body = {
            "error": "invalid_grant",
            "error_description": "Invalid user credentials"
        }

        httpretty.register_uri(httpretty.POST,
                               "https://www.box.com/api/oauth2/token",
                               body=json.dumps(body),
                               status=401,
                               content_type='text/json')

        b = boxpython.BoxAuthenticateFlow(self.client_id, self.client_secret)
        auth_code = "@#$@FEWR"
        with self.assertRaises(boxpython.BoxError) as cm:
            access_token, refresh_token = b.get_access_tokens(auth_code)

        self.assertEqual(cm.exception.status, 401)