pytest can't seem to access Redis session store #72
-
Hi, Thank you for this library. I'm learning FastAPI and I find this library very helpful. I have used its Redis session store without any problem in my application proper. However, my integration test using What have I missed please? Content of import pytest
from fastapi.testclient import TestClient
from . import test_main
@pytest.fixture(scope='module')
def app():
return test_main.app
@pytest.fixture(scope='module')
def test_client(app):
return TestClient(app) Following is the def __login_page_context(inc_not_login_msg=False):
context = {"title": "FastAPI Login"}
if inc_not_login_msg:
context.update({"message": "This session has not been logged in before"})
return context
@router.post("/logout", response_class=HTMLResponse)
async def logout(request: Request):
context = __login_page_context(
inc_not_login_msg=request.session.get("access_token")==None)
request.session.clear()
return templates.TemplateResponse(request=request, name="auth/login.html", context=context) When the test gets to this endpoint, The integration test is not able to clear the session content from Redis. While the application is able to. That is, the application works as expected. The call below has no effect during test, the corresponding Redis session does not get cleared: request.session.clear() And this is my test method: @pytest.mark.auth_integration
def test_integration_valid_login(test_client):
"""
Test /auth/token path with a valid credential.
"""
try:
login_data = {
"username": "[email protected]",
"password": "password"
}
response = test_client.post(
"/auth/token",
data=login_data,
headers={ 'Content-Type': 'application/x-www-form-urlencoded'}
)
assert response != None
assert response.status_code == HTTPStatus.OK.value
status = response.json()
assert status['access_token'] == '[email protected]'
assert status['token_type'] == 'bearer'
print("\ncookie session -> ", test_client.cookies.get('session'))
finally:
# Logout. Clean up server sessions.
test_client.post('/auth/logout')
And Thank you and best regards, ...behai. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
With the helps of ChatGPT, I have the solution for it: @pytest.mark.behai_only
def test_integration_valid_login(test_client):
"""
Test /auth/token path with a valid credential.
"""
try:
login_data = {
'username': '[email protected]',
'password': 'password'
}
response = test_client.post(
'/auth/token',
data=login_data,
headers={'Content-Type': 'application/x-www-form-urlencoded'}
)
assert response != None
assert response.status_code == HTTPStatus.OK.value
status = response.json()
assert status['access_token'] == '[email protected]'
assert status['token_type'] == 'bearer'
session_cookie = response.cookies.get('session')
finally:
test_client.cookies = {'session': session_cookie}
test_client.post('/auth/logout') |
Beta Was this translation helpful? Give feedback.
-
I don't know how your application is configured, but check if you have Refer to read me for details. |
Beta Was this translation helpful? Give feedback.
With the helps of ChatGPT, I have the solution for it: