コード例 #1
0
    def test_send_custom_decoder(self):
        def test_callable():
            pass

        def decode_form_encoded(query):
            return dict([pair.split('=') for pair in query.split('&')])

        req = oauth2.AccessTokenRequest(authenticator=test_callable,
                                        grant=test_callable,
                                        endpoint='test_endpoint')

        self._mox.StubOutWithMock(req, 'build_url_request')
        self._mox.StubOutWithMock(oauth2, 'urlopen')

        resp_mock = self._create_file_mock()

        req.build_url_request().AndReturn('test return value')
        oauth2.urlopen('test return value').AndReturn(resp_mock)
        resp_mock.read().AndReturn('access_token=test_access_token&token_type=test_token_type&refresh_token=test_refresh_token')

        self._mox.ReplayAll()
        token = req.send(response_decoder=decode_form_encoded)
        self._mox.VerifyAll()

        self.assertEquals('test_access_token', token.access_token)
        self.assertEquals('test_token_type', token.token_type)
        self.assertEquals('test_refresh_token', token.refresh_token)

        self._mox.UnsetStubs()
コード例 #2
0
    def test_send_error_response_non_string_error_code(self):
        from urllib2 import HTTPError

        def test_callable():
            pass

        req = oauth2.AccessTokenRequest(authenticator=test_callable,
                                        grant=test_callable,
                                        endpoint='test_endpoint')

        self._mox.StubOutWithMock(req, 'build_url_request')
        self._mox.StubOutWithMock(oauth2, 'urlopen')

        # HTTPError.read cannot be directly mocked, so we need to stub it in
        def read(self):
            return '{"error": {"type": "OAuthException", "message": "Error validating verification code."}}'

        HTTPError.read = read
        http_error = HTTPError('test return value', 400, 'Bad Request', {}, None)

        req.build_url_request().AndReturn('test return value')
        oauth2.urlopen('test return value').AndRaise(http_error)

        self._mox.ReplayAll()
        try:
            req.send()
        except oauth2.AccessTokenRequestError as e:
            self.assertEquals("{u'message': u'Error validating verification code.', u'type': u'OAuthException'}", e.error_code)
        except Exception as ex:
            self.fail('Expected exception oauth2.AccessTokenRequestError not raised. Got error %s' % ex)
        finally:
            self._mox.VerifyAll()
            self._mox.UnsetStubs()
            del HTTPError.read
コード例 #3
0
    def test_send_default_response_decoder(self):
        def test_callable():
            pass
        req = oauth2.AccessTokenRequest(authenticator=test_callable,
                                        grant=test_callable,
                                        endpoint='test_endpoint')

        self._mox.StubOutWithMock(req, 'build_url_request')
        self._mox.StubOutWithMock(oauth2, 'urlopen')

        resp_mock = self._create_file_mock()

        req.build_url_request().AndReturn('test return value')
        oauth2.urlopen('test return value').AndReturn(resp_mock)
        resp_mock.read().AndReturn('{"access_token": "test_access_token",\
                                     "token_type": "test_token_type",\
                                     "expires_in": "3600",\
                                     "refresh_token": "test_refresh_token"}')

        self._mox.ReplayAll()
        token = req.send()
        self._mox.VerifyAll()

        self.assertEquals('test_access_token', token.access_token)
        self.assertEquals('test_token_type', token.token_type)
        self.assertEquals('test_refresh_token', token.refresh_token)

        # "expires" will be a datetime object representing the current
        # date/time plus the number of seconds the access token is good for.
        # We need to account for time consumed by the script, so we give 60
        # seconds leeway
        from datetime import datetime, timedelta
        expected = datetime.now() + timedelta(seconds=3600)
        delta = expected - token.expires
        self.assertTrue(0 == delta.days)
        self.assertTrue(60 > delta.seconds)

        self._mox.UnsetStubs()
コード例 #4
0
    def test_send_error_response(self):
        from urllib2 import HTTPError

        def test_callable():
            pass

        req = oauth2.AccessTokenRequest(authenticator=test_callable,
                                        grant=test_callable,
                                        endpoint='test_endpoint')

        self._mox.StubOutWithMock(req, 'build_url_request')
        self._mox.StubOutWithMock(oauth2, 'urlopen')

        # HTTPError.read cannot be directly mocked, so we need to stub it in
        def read(self):
            return '{"error": "invalid_request", "error_description": "error description", "error_uri": "http://www.example.com/error"}'

        HTTPError.read = read
        http_error = HTTPError('test return value', 400, 'Bad Request', {}, None)

        req.build_url_request().AndReturn('test return value')
        oauth2.urlopen('test return value').AndRaise(http_error)

        self._mox.ReplayAll()
        try:
            req.send()
        except oauth2.AccessTokenRequestError as e:
            self.assertEquals('invalid_request', e.error_code)
            self.assertEquals('error description', e.error_description)
            self.assertEquals('http://www.example.com/error', e.error_uri)
        except Exception as ex:
            self.fail('Expected exception oauth2.AccessTokenRequestError not raised. Got error %s' % ex)
        finally:
            self._mox.VerifyAll()
            self._mox.UnsetStubs()
            del HTTPError.read