Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preprocessing functions for test client #133

Open
wbrackenbury opened this issue Apr 23, 2019 · 0 comments
Open

Preprocessing functions for test client #133

wbrackenbury opened this issue Apr 23, 2019 · 0 comments

Comments

@wbrackenbury
Copy link

Is there a reason that the Flask application preprocess_request() methods aren't currently called in the TestCase _pre_setup() method?

Use case: I want to test whether OAuth2 authorization is occurring correctly, and I want to be able to examine the state of the OAuth2 session in a test case. For the Google OAuth2Blueprint, however, this is loaded as follows (found in Flask-Dance, /contrib/google.py):

    google_bp = OAuth2ConsumerBlueprint("google", __name__,
        client_id=client_id,
        client_secret=client_secret,
        scope=scope,
        base_url="https://www.googleapis.com/",
        authorization_url="https://accounts.google.com/o/oauth2/auth",
        token_url="https://accounts.google.com/o/oauth2/token",
        auto_refresh_url=auto_refresh_url,
        redirect_url=redirect_url,
        redirect_to=redirect_to,
        login_url=login_url,
        authorized_url=authorized_url,
        authorization_url_params=authorization_url_params,
        session_class=session_class,
        backend=backend,
    )
    @google_bp.before_app_request
    def set_applocal_session():
        ctx = stack.top
        ctx.google_oauth = google_bp.session

...

google = LocalProxy(partial(_lookup_app_object, "google_oauth"))

This registers a function in the main Flask application that loads in the OAuth session upon each new request.

With a regular client, this yields no issues, as seen in the Flask application method (found in Flask, app.py)

    def full_dispatch_request(self):
        """Dispatches the request and on top of that performs request
        pre and postprocessing as well as HTTP exception catching and
        error handling.

        .. versionadded:: 0.7
        """
        self.try_trigger_before_first_request_functions()
        try:
            request_started.send(self)
            rv = self.preprocess_request()
            if rv is None:
                rv = self.dispatch_request()
        except Exception as e:
            rv = self.handle_user_exception(e)
        return self.finalize_request(rv)

As the self.preprocess_request() line loads the session into the context.

_pre_setup() currently reads, however:

    def _pre_setup(self):
        self.app = self.create_app()

        self._orig_response_class = self.app.response_class
        self.app.response_class = _make_test_response(self.app.response_class)

        self.client = self.app.test_client()

        self._ctx = self.app.test_request_context()
        self._ctx.push()

Which yields a RequestContext object, but does not wrap it with preprocessing.

Therefore, within a test case, if you try and print the imported session, you receive the error:

   File "/usr/local/lib/python3.5/site-packages/werkzeug/local.py", line 348, in __getattr__
     return getattr(self._get_current_object(), name)
   File "/usr/local/lib/python3.5/site-packages/werkzeug/local.py", line 307, in _get_current_object
     return self.__local()
   File "/usr/local/lib/python3.5/site-packages/flask/globals.py", line 45, in _lookup_app_object
     return getattr(top, name)
 AttributeError: 'AppContext' object has no attribute 'google_oauth'

However, if you save the application as an instance variable, and manually call the function preprocess_request(), it succeeds.

I'm new at this, so I'm not sure of the wider implications of this, or whether this is a correct solution. In general, though, would calling preprocess_request() in _pre_setup() potentially induce negative side effects?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant