I am new to python and flask. I wanted to create unit tests for an api written. We have used jwt
for authentication.
To unit test, I don't want the flow to enter @jwt_required
decorator. In addition to that I have chained a few other decorators for that method.
class A():
@jwt_required()
@mandatory_fields_check
@unlock_and_lock()
def get(self, address, name):
..
..
..
return jsonify(
{"payload": data,
"message": "data received successfully"}), 200
Unit test I am trying to write
def test_get():
a_obj = A()
a_obj.get("address123", 'xyz')
When I run above test using py.test, I am getting Runtime error
def _find_app():
top = _app_ctx_stack.top
if top is None:
raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.
E
E This typically means that you attempted to use functionality that needed
E to interface with the current application object in some way. To solve
E this, set up an application context with app.app_context(). See the
E documentation for more information.
Below are my objectives:
I don't want flow to enter decorators logic
Jwt decorator is asking for context. However my intention is to unit test this method as a normal class method without any flask features.
How can I mock objects created inside the method under test?