-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Comments
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:
I might be wrong though. |
@Jefffrey, have you checked the tests included in the lib? They test the pipeline functionality, it even adds custom pipelines functions to it. |
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. |
Did the SocialClient get dropped in the move from django-social-auth to python-social-auth: |
+1 |
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. |
+1 |
I mock the 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 |
@BraisGabin I haven't had any luck following that example; when I run it, the Would you mind fleshing out the example a little more so I can see how you're doing this? |
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") |
@BraisGabin thanks, @coriolinus if this is still a relevant issue, please open an issue in the corresponding component at python-social-auth. |
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.
The text was updated successfully, but these errors were encountered: