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

Unit Test Django Client Login? #81

Closed
meconlin opened this issue Nov 10, 2013 · 6 comments
Closed

Unit Test Django Client Login? #81

meconlin opened this issue Nov 10, 2013 · 6 comments

Comments

@meconlin
Copy link

How can I unit test authentication on my Django app using python-social-auth?

I attempted this:

    self.c = Client()
    self.u = User.objects.create(username="testuser", password="password", is_staff=True, is_active=True, is_superuser=True)
    self.u.save()
    self.auth = UserSocialAuth(user=self.u, provider="Facebook")
    self.auth.save()
    self.c.login(username=self.u.username, password=self.u.password) 

but this call to login returns False and the user is not authenticated

@omab
Copy link
Owner

omab commented Nov 11, 2013

I don't see any social login there, just a UserSocialAuth instance being saved that's not used at all.

The problem with testing social authentication is the dependency with the third-parties that provider that auth for you, that's slow, prone to error and any user looking to run the tests need to register an application and define keys/secret for the desired backends. That's why tests on PSA are written using HTTPPretty to mock the server responses and focus on functionality testing instead.

@meconlin
Copy link
Author

Sorry if I was not clear omab, I was not trying to test oauth in any meaningful way. I was simply trying to get an authenticated django.test.Client so that I can unit test REST endpoints. My mistake was thinking that it mattered how that Client got authenticated, for unit testing the tasteypie endpoints oauth really doesnt need to come into play at all.

this worked for me:

        self.user = User.objects.create(username='testuser', password='12345', is_active=True, is_staff=True, is_superuser=True) 
        self.user.set_password('hello') 
        self.user.save() 
        self.user = authenticate(username='testuser', password='hello') 
        login = self.c.login(username='testuser', password='hello') 
        self.assertTrue(login)

@ChillarAnand
Copy link

Thank you @meconlin I had the same problem. Your script helped me a lot!

@rustoceans
Copy link

Thanks man!

@gonvaled
Copy link

gonvaled commented Nov 4, 2015

@omab what if I really want to make sure that the integration of python social auth with my django project is working? I do not need to test that the social endpoints are working, but at least I would like to know that my site is doing right the request/reply cycle. How to go about this? Is there any example available?

@idchlife
Copy link

idchlife commented Jun 2, 2016

@gonvaled +1

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