def test_answer_access_denied(self):
        from py3oauth2.errors import AccessDenied

        req = RefreshTokenRequest()
        req.update({
            'grant_type': 'refresh_token',
            'refresh_token': 'unknown_refresh_token',
        })

        provider = BlindAuthorizationProvider(self.store)
        with self.assertRaises(AccessDenied):
            req.answer(provider, self.owner)
    def test_answer_invalid_scope_1(self):
        from py3oauth2.errors import InvalidScope

        provider = BlindAuthorizationProvider(self.store)

        req = RefreshTokenRequest()
        req.update({
            'grant_type': 'refresh_token',
            'refresh_token': self.access_token.get_refresh_token(),
            'scope': 'view write admin',
        })
        with self.assertRaises(InvalidScope):
            req.answer(provider, self.owner)
    def test_answer_store_raises_error_exception(self):
        from py3oauth2.errors import AccessDenied
        req = RefreshTokenRequest()
        req.update({
            'grant_type': 'refresh_token',
            'refresh_token': self.access_token.get_refresh_token(),
        })

        self.store.issue_access_token = mock.Mock(side_effect=AccessDenied())
        provider = BlindAuthorizationProvider(self.store)

        try:
            req.answer(provider, self.owner)
        except AccessDenied as why:
            self.assertIs(why.request, req)
        else:
            self.fail()
    def test_answer_unauthorized_client(self):
        from py3oauth2.errors import UnauthorizedClient
        from py3oauth2.provider import AuthorizationProvider

        req = RefreshTokenRequest()
        req.update({
            'grant_type': 'refresh_token',
            'refresh_token': self.access_token.get_refresh_token(),
        })

        provider = AuthorizationProvider(self.store)
        with contextlib.ExitStack() as stack:
            stack.enter_context(mock.patch.object(provider, 'authorize_client',
                                                  return_value=False))
            stack.enter_context(self.assertRaises(UnauthorizedClient))

            req.answer(provider, self.owner)
    def test_answer(self):
        req = RefreshTokenRequest()
        req.update({
            'grant_type': 'refresh_token',
            'refresh_token': self.access_token.get_refresh_token(),
        })

        provider = BlindAuthorizationProvider(self.store)
        resp = req.answer(provider, self.owner)

        self.assertIsInstance(resp, req.response)

        token = self.store.get_access_token(resp.access_token)
        self.assertIsNotNone(token)
        self.assertEqual(resp.token_type, token.get_type())
        self.assertEqual(resp.expires_in, token.get_expires_in())
        self.assertEqual(provider.normalize_scope(resp.scope),
                         token.get_scope())