Skip to content
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

How to use token authentication while testing REST API #43

Closed
junaidurfjd opened this issue Jun 25, 2016 · 7 comments
Closed

How to use token authentication while testing REST API #43

junaidurfjd opened this issue Jun 25, 2016 · 7 comments

Comments

@junaidurfjd
Copy link
Contributor

I am using django rest framework's token authentication in my django project and I need to unit test my ModelViewSets but the default login method of the TestCase class in not doing the job. I'm using drf's TokenAuthentication.

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
    )
}
@frankwiles
Copy link
Member

Hi @Darwesh27,

Yeah the login method only works with Django Session auth, you should just force_authenticate() in your tests using DRF's method for that. Docs for that are here http://www.django-rest-framework.org/api-guide/testing/#forcing-authentication

@eracle
Copy link

eracle commented Dec 13, 2018

I've solved using the credentials methods, here's the gist.

@sahir
Copy link

sahir commented Jul 1, 2020

@eracle gist (Error 404)

@captbilard
Copy link

@eracle link gives error 404. Kindly help with the right link

@eracle
Copy link

eracle commented Nov 17, 2020

sorry guys, it seems desappeared from my gist list

@Olfredos6
Copy link

sorry guys, it seems desappeared from my gist list

Submit a new one

@eracle
Copy link

eracle commented Dec 16, 2020

Unfortunately, I don't have that code anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants