mardi 1 septembre 2015

How do I mock Flask operations from a @before_request decorator?

I am working on a Flask application and trying to add unit tests. There is a function decorated with @before_request which processes authentication headers and sets properties on the "request" object. I want to unit test a method decorated as "@app.route('/status')", but I do not see how to mock the request headers and the processing of the @before_request function. Could anyone suggest how to accomplish this?

EXAMPLE:

@before_request
def process_auth_headers():
    # Process auth headers and set properties on "request" object

@app.route("/status")
def status():
    # Check "request" object properties for auth results

And the test code:

import logging
import unittest
import os
from ccbackendvsphere.api.v3 import app

__author__ = 'dphillips'

logger = logging.getLogger(__name__)

class TestFlaskApp(unittest.TestCase):

    def setUp(self):
        app.config['TESTING'] = True
        app.config['WTF_CSRF_ENABLED'] = False
        app.confg['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db')
        self.flask_app = app.test_client()

    def test_unauthenticated_status_request(self):
        rv = self.flask_app.get("/status")

if __name__ == '__main__':
    unittest.main()

Aucun commentaire:

Enregistrer un commentaire