I am trying to test my flask-api, but im not being able to do this, because the endpoints require login.
I have a simple code:
import os
import requests
from dotenv import load_dotenv
load_dotenv()
ENDPOINT = os.getenv("ENDPOINT")
### - TESTING SALES ENDPOINT - ###
def test_get_all_sales():
"""Test the sales endpoint, where all sales are getted"""
response = requests.get(f"http://localhost:5000/sales")
print(response)
assert response.status_code == 200
When I ran the tests with 'python -m pytest' (I'm using windows, I think in linux the cmd is different), I receive a forbidden error, because endpoint require login.
I am using flask session, where my frontend send a request to auth/login endpoint and a session in backend is stored and return a cookie to frontend store it. So, my auth works in this way. But how can I test these endpoints? Have a way to login, store a temporary cookie and use these endpoint? Or I need to setup something like a apikey in each endpoint when passed, doesnt require login?
My endpoint is like:
@sales.route('/')
@sales.route("/<filters>")
@login_required # abort and return 403.
def sales_list(filters=None):
sales = Sale.get_all_sales() // Sale is a model class, with crud methods (simple DAO)
if sales:
return jsonify({'message': "sales fetched sucessfully.", 'data': sales}), 200
return jsonify({'message': "sales cant be fetched.", 'data': None}), 400
By the way, i thought this is possible because POSTMAN can set cookies and I can make requests to login_required endpoints. So, I think I can do this in python manually too