コード例 #1
0
class TestTokenRefreshing(unittest.TestCase):
    def setUp(self):
        self.wrapper = TokenRefreshClient(token='token')

    @responses.activate
    def test_not_token_refresh_ready_client_call_raises_not_implemented(self):
        no_refresh_client = TesterClient()

        responses.add_callback(
            responses.POST,
            no_refresh_client.test().data,
            callback=lambda *a, **k: (401, {}, ''),
            content_type='application/json',
        )

        with self.assertRaises(NotImplementedError):
            no_refresh_client.test().post(refresh_auth=True)

    @responses.activate
    def test_token_expired_and_no_refresh_flag(self):
        responses.add(responses.POST,
                      self.wrapper.test().data,
                      body='{"error": "Token expired"}',
                      status=401,
                      content_type='application/json')
        with self.assertRaises(ClientError) as context:
            response = self.wrapper.test().post()

    @responses.activate
    def test_token_expired_with_active_refresh_flag(self):
        self.first_call = True

        def request_callback(request):
            if self.first_call:
                self.first_call = False
                return (401, {
                    'content_type': 'application/json'
                }, json.dumps('{"error": "Token expired"}'))
            else:
                self.first_call = None
                return (201, {'content_type': 'application/json'}, '')

        responses.add_callback(
            responses.POST,
            self.wrapper.test().data,
            callback=request_callback,
            content_type='application/json',
        )

        response = self.wrapper.test().post(refresh_auth=True)

        # refresh_authentication method should be able to update api_params
        self.assertEqual(response._api_params['token'], 'new_token')
コード例 #2
0
class TestTokenRefreshing(unittest.TestCase):

    def setUp(self):
        self.wrapper = TokenRefreshClient(token='token', refresh_token_by_default=True)

    @responses.activate
    def test_not_token_refresh_client_propagates_client_error(self):
        no_refresh_client = TesterClient()

        responses.add_callback(
            responses.POST, no_refresh_client.test().data,
            callback=lambda *a, **k: (401, {}, ''),
            content_type='application/json',
        )

        with self.assertRaises(ClientError):
            no_refresh_client.test().post()

    @responses.activate
    def test_disable_token_refreshing(self):
        responses.add_callback(
            responses.POST, self.wrapper.test().data,
            callback=lambda *a, **k: (401, {}, ''),
            content_type='application/json',
        )

        with self.assertRaises(ClientError):
            self.wrapper.test().post(refresh_token=False)

    @responses.activate
    def test_token_expired_automatically_refresh_authentication(self):
        self.first_call = True

        def request_callback(request):
            if self.first_call:
                self.first_call = False
                return (401, {'content_type': 'application/json'}, json.dumps('{"error": "Token expired"}'))
            else:
                self.first_call = None
                return (201, {'content_type': 'application/json'}, '')

        responses.add_callback(
            responses.POST, self.wrapper.test().data,
            callback=request_callback,
            content_type='application/json',
        )

        response = self.wrapper.test().post()

        # refresh_authentication method should be able to update api_params
        self.assertEqual(response._api_params['token'], 'new_token')
コード例 #3
0
class TestTokenRefreshing(unittest.TestCase):
    def setUp(self):
        self.wrapper = TokenRefreshClient(token="token")

    @responses.activate
    def test_not_token_refresh_ready_client_call_raises_not_implemented(self):
        no_refresh_client = TesterClient()

        responses.add_callback(
            responses.POST,
            no_refresh_client.test().data,
            callback=lambda *a, **k: (401, {}, ""),
            content_type="application/json",
        )

        with self.assertRaises(NotImplementedError):
            no_refresh_client.test().post(refresh_auth=True)

    @responses.activate
    def test_token_expired_and_no_refresh_flag(self):
        responses.add(
            responses.POST,
            self.wrapper.test().data,
            body='{"error": "Token expired"}',
            status=401,
            content_type="application/json",
        )
        with self.assertRaises(ClientError) as context:
            response = self.wrapper.test().post()

    @responses.activate
    def test_token_expired_with_active_refresh_flag(self):
        self.first_call = True

        def request_callback(request):
            if self.first_call:
                self.first_call = False
                return (401, {"content_type": "application/json"}, json.dumps('{"error": "Token expired"}'))
            else:
                self.first_call = None
                return (201, {"content_type": "application/json"}, "")

        responses.add_callback(
            responses.POST, self.wrapper.test().data, callback=request_callback, content_type="application/json"
        )

        response = self.wrapper.test().post(refresh_auth=True)

        # refresh_authentication method should be able to update api_params
        self.assertEqual(response._api_params["token"], "new_token")
コード例 #4
0
 def setUp(self):
     self.wrapper = TokenRefreshClient(token='token',
                                       refresh_token_by_default=True)
コード例 #5
0
 def setUp(self):
     self.wrapper = TokenRefreshClient(token="token")
コード例 #6
0
 def setUp(self):
     self.wrapper = TokenRefreshClient(token='token')