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 test properly #414

Closed
shoooe opened this issue Oct 10, 2014 · 11 comments
Closed

How to test properly #414

shoooe opened this issue Oct 10, 2014 · 11 comments

Comments

@shoooe
Copy link

shoooe commented Oct 10, 2014

I've tried to look for a way to test the pipeline or generally use a properly logged user, but I can't seem to find a solution. I've already read this issue and this issue but both "solutions" just avoid the problem entirely (for example by using the default authentication system instead).

I've extended the functionality of the pipeline, both disconnect and connect pipeline. I've also added a step that analyzes the extra data a tries to take out a verified email if it can. All of this needs to be testes.

To test the pipeline, AFAIKS, the suggested method is to just test each custom function per se, and I can agree with that. But how can I have a properly logged in user without defining a password for such a user? Some decisions the system makes are based on whether the user has or not a password in association with the account, and I need to be able to test the parts in which the user only has a social account. How can I do that?

Also it might be useful if there was a chapter in the documentation regarding how to test with a custom Client.

Thanks for the attention.

@shoooe
Copy link
Author

shoooe commented Oct 16, 2014

Now I've come in contact with another problem. I have this piece of logic where I say: "A user can remove connections only when the user has at least 2 accounts". Fairly simple. Problem is that I don't know how I can create a dummy user connection per social account.

In my opinion this shows that there should be some testing tool that:

  • allows for fast creation of dummy connections
  • simulates the authentication to these dummy connections

I might be wrong though.

@omab
Copy link
Owner

omab commented Oct 23, 2014

@Jefffrey, have you checked the tests included in the lib? They test the pipeline functionality, it even adds custom pipelines functions to it.

@shoooe
Copy link
Author

shoooe commented Oct 23, 2014

I will, but I think there should be a chapter in the documentation regarding this. Maybe listing all the tools python-social-auth gives you for testing.

@chrismcginlay
Copy link

Did the SocialClient get dropped in the move from django-social-auth to python-social-auth:
http://django-social-auth.readthedocs.org/en/latest/testing_tools.html
I'm trying to figure out the best way to do functional testing with selenium for sequences of user operations that will require social sign ins. Wondered if something like SocialClient might have helped

@glowka
Copy link

glowka commented Apr 9, 2015

+1

@kreativitea
Copy link

I'd very much like the SocialClient from django-social-auth back. I'm doing integration testing with individual endpoints, and I'd like the auth rejected flow to be part of those tests.

@lcardito
Copy link

+1

@BraisGabin
Copy link

BraisGabin commented Jun 21, 2016

I mock the user_data function in the backends to test with different responses. An example to mock Facebook:

from social.backends import facebook
from requests.exceptions import HTTPError
from requests.models import Response

def get_facebook_info_mock(self, access_token, *args, **kwargs):
    info = {
        'user_1': {
            'id': '00001',
            'name': 'Foo Bar',
            'email': 'foo@bar.com',
        },
        # ...
    }
    try:
        return info[access_token]
    except KeyError:
        response = Response()
        response.status_code = 400
        raise HTTPError('Error', response=response)


class FacebookTestCase(APITestCase):
    def setUp(self):
        facebook.FacebookOAuth2.user_data = get_facebook_info_mock

    # the tests

@coriolinus
Copy link

@BraisGabin I haven't had any luck following that example; when I run it, the get_facebook_info_mock function is bypassed in favor of the genuine user_data function. My best guess is that my test case is in the wrong scope when I substitute in the replacement function.

Would you mind fleshing out the example a little more so I can see how you're doing this?

@omab omab closed this as completed Dec 27, 2016
@BraisGabin
Copy link

BraisGabin commented Dec 27, 2016

I hope this can help @coriolinus

from social.backends import facebook
from requests.exceptions import HTTPError
from requests.models import Response
from rest_framework.test import APITestCase

def get_facebook_info_mock(self, access_token, *args, **kwargs):
    info = {
        'user_1': {
            'id': '00001',
            'name': 'Foo Bar',
            'email': 'foo@bar.com',
        },
        # ...
    }
    try:
        return info[access_token]
    except KeyError:
        response = Response()
        response.status_code = 400
        raise HTTPError('Error', response=response)


class FacebookTestCase(APITestCase):
    def setUp(self):
        facebook.FacebookOAuth2.user_data = get_facebook_info_mock

    def testFacebook(self):
       d = {'grant_type': 'convert_token', 'client_id': 'foo', 'client_secret': 'bar', 'backend': 'facebook', 'token': 'user_1'}
        response = self.client.post('/oauth/convert-token/', d)
        self.assertEqual(response.status_code, 200, response.data)
        user = User.objects.all().latest('date_joined')
        self.assertEqual(user.email, "foo@bar.com")

@omab
Copy link
Owner

omab commented Dec 27, 2016

@BraisGabin thanks, @coriolinus if this is still a relevant issue, please open an issue in the corresponding component at python-social-auth.

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

8 participants