-
-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TestClient lacks of authentication #3
Comments
Yes, I don't have force_authenticate feature implemented. I never thought it's that useful. The TestClient inherits from django-ninja TestClient and it has different ways you can Authenticate a user during testing. What type of authentication are you using in your project? |
Hi! I am using JWTAuthAsync with default settings for now (AccessToken), here is piece of code: ` class NinjaJWTAsync(NinjaJWTDefaultController):
class IsJWTAuthenticatedAsync:
` Here is view example:
` My testing scenario is following: `import pytest from profile.api import ProfileController prefix = "/profile" @pytest.mark.parametrize( |
huge sorry for wrong indentations in comment, hope you’ll understand what i’m talking about |
This is what I could come up with. You need generate an auth token and pass it as Also use the right token for your project. Here I used, from ninja_jwt.tokens import SlidingToken
def get_authentication_header(user, header_key="Authorization"):
token = SlidingToken.for_user(user)
user_credential = {header_key: "Bearer " + str(token)}
return user_credential
prefix = "/profile"
@pytest.mark.parametrize(
"search_str, expected_data",
[
("", []),
( "H", [])
]
)
@pytest.mark.asyncio
@pytest.mark.django_db(transaction=True)
async def test_profile(search_str, expected_data):
result = await profile_client_jwt_authentication.get(
path=f"{prefix}", query=dict(search_str=search_str),
headers=get_authentication_header(user=your_test_user)
)
assert result.json() == expected_data |
I want to hug you friend !!! Not without perversion with my asynchronous JWT, but with your prompt, it all worked out! |
Glad it worked out. |
Hi :) i'm using something whats more like a real life scenario - calling the wanted APIs, so i can test the workflow too in one step. Also i'm using the plain old django test client :) - testing is done with from django.test import Client
import pytest
@pytest.fixture
def api_client():
return Client()
@pytest.fixture
def user_and_access_token(api_client):
user = UserFactory(active=True)
user.set_password("secretpass")
user.save()
token_response = api_client.post(
"/api/token/pair",
data={
"username": user.username,
"password": "secretpass"
},
content_type="application/json",
)
access_token = token_response.json()["access"]
return user, access_token
# use it in tests like this
def test_something(api_client, user_and_access_token):
user, access_token = user_and_access_token
# set access token in header
header = {"HTTP_AUTHORIZATION": f"bearer {access_token}"}
# WHEN
response = api_client.get(url, **header) |
Comparing to DRF API Client, which has force_authenticate method, here I can’t authenticate user in tests. Are there any plans for fixing that?
The text was updated successfully, but these errors were encountered: