I have a route and a unit test for it for my Flask application.
I want to mock the auth_func decorator, but i’m a bit lost on how to do this simply.
<code>import pytest
from app import app
@pytest.fixture
def client():
with app.test_client() as client:
yield client
MOCK_TOKEN = "<some token>"
def test_get_stuff(mocker, client):
# set the return value of the token_required decorator to be {"hello": "world"}
mocker.patch('app.routes.stuff.token_required', return_value={"hello": "world"})
# Call the route
response = client.get(
f"/stuff",
headers={"Authorization": f"Bearer {MOCK_TOKEN}"}
)
assert response.status_code == 200
data = response.json
assert data == <some output data in json>
</code>
<code>import pytest
from app import app
@pytest.fixture
def client():
with app.test_client() as client:
yield client
MOCK_TOKEN = "<some token>"
def test_get_stuff(mocker, client):
# set the return value of the token_required decorator to be {"hello": "world"}
mocker.patch('app.routes.stuff.token_required', return_value={"hello": "world"})
# Call the route
response = client.get(
f"/stuff",
headers={"Authorization": f"Bearer {MOCK_TOKEN}"}
)
assert response.status_code == 200
data = response.json
assert data == <some output data in json>
</code>
import pytest
from app import app
@pytest.fixture
def client():
with app.test_client() as client:
yield client
MOCK_TOKEN = "<some token>"
def test_get_stuff(mocker, client):
# set the return value of the token_required decorator to be {"hello": "world"}
mocker.patch('app.routes.stuff.token_required', return_value={"hello": "world"})
# Call the route
response = client.get(
f"/stuff",
headers={"Authorization": f"Bearer {MOCK_TOKEN}"}
)
assert response.status_code == 200
data = response.json
assert data == <some output data in json>
<code>@api.route("/stuff", methods=["GET"])
@auth_func
def get_stuff(**kwargs) -> List[Dict]:
try:
# do stuff
return jsonify(results), 200
except Exception as e:
raise e
</code>
<code>@api.route("/stuff", methods=["GET"])
@auth_func
def get_stuff(**kwargs) -> List[Dict]:
try:
# do stuff
return jsonify(results), 200
except Exception as e:
raise e
</code>
@api.route("/stuff", methods=["GET"])
@auth_func
def get_stuff(**kwargs) -> List[Dict]:
try:
# do stuff
return jsonify(results), 200
except Exception as e:
raise e
I want to mock the token_require decorator, effectively making it like this route below without the @auth_func
. How can I achieve this simply? The mocker.patch doesn’t seem to be working as I thought it would.
<code>@api.route("/stuff", methods=["GET"])
def get_stuff(**kwargs) -> List[Dict]:
try:
# do stuff
return jsonify(results), 200
except Exception as e:
raise e
</code>
<code>@api.route("/stuff", methods=["GET"])
def get_stuff(**kwargs) -> List[Dict]:
try:
# do stuff
return jsonify(results), 200
except Exception as e:
raise e
</code>
@api.route("/stuff", methods=["GET"])
def get_stuff(**kwargs) -> List[Dict]:
try:
# do stuff
return jsonify(results), 200
except Exception as e:
raise e