def test_check_connect_facebook(self): graph = get_facebook_graph(access_token='new_user') facebook = FacebookUserConverter(graph) data = facebook.facebook_registration_data() self.assertEqual(data['gender'], 'm') response = self.client.get(reverse('login:index'))
def test_check_connect_facebook(self): graph = get_facebook_graph(access_token='new_user') facebook = FacebookUserConverter(graph) data = facebook.facebook_registration_data() self.assertEqual(data['gender'], 'm') response = self.client.get(reverse('login:index'))
def _register_user(self, facebook, profile_callback=None): ''' Creates a new user and authenticates The registration form handles the registration and validation Other data on the user profile is updates afterwards ''' if not facebook.is_authenticated(): raise ValueError, 'Facebook needs to be authenticated for connect flows' facebook_user_converter = FacebookUserConverter(facebook) from registration.forms import RegistrationFormUniqueEmail #get the backend on new registration systems, or none if we are on an older version backend = get_registration_backend() #get the form used for registration and fall back to uniqueemail version form_class = RegistrationFormUniqueEmail if backend: form_class = backend.get_form_class(request) facebook_data = facebook_user_converter.facebook_registration_data() data = self.request.POST.copy() for k, v in facebook_data.items(): if not data.get(k): data[k] = v if self.request.REQUEST.get('force_registration_hard'): data['email'] = data['email'].replace('@', '+%s@' % randint(0, 100000)) form = form_class(data=data, files=self.request.FILES, initial={'ip': self.request.META['REMOTE_ADDR']}) if not form.is_valid(): error = facebook_exceptions.IncompleteProfileError( 'Facebook data %s gave error %s' % (facebook_data, form.errors)) error.form = form raise error #for new registration systems use the backends methods of saving if backend: new_user = backend.register(request, **form.cleaned_data) else: new_user = form.save(profile_callback=profile_callback) #update some extra data not yet done by the form new_user = self._update_user(new_user, facebook) #IS this the correct way for django 1.3? seems to require the backend attribute for some reason new_user.backend = 'django_facebook.auth_backends.FacebookBackend' auth.login(self.request, new_user) return new_user
def test_long_username(self): request = RequestMock().get('/') request.session = {} request.user = AnonymousUser() graph = get_persistent_graph(request, access_token='long_username') converter = FacebookUserConverter(graph) base_data = converter.facebook_registration_data() action, user = connect_user(self.request, facebook_graph=graph) self.assertEqual(len(base_data['username']), 30) self.assertEqual(len(user.username), 30) self.assertEqual(len(user.first_name), 30) self.assertEqual(len(user.last_name), 30)
def test_long_username(self): request = RequestMock().get('/') request.session = {} request.user = AnonymousUser() graph = get_persistent_graph(request, access_token='long_username') converter = FacebookUserConverter(graph) base_data = converter.facebook_registration_data() action, user = connect_user(self.request, facebook_graph=graph) self.assertEqual(len(base_data['username']), 30) self.assertEqual(len(user.username), 30) self.assertEqual(len(user.first_name), 30) self.assertEqual(len(user.last_name), 30)
def test_gender_matching(self): request = RequestMock().get('/') request.session = {} request.user = AnonymousUser() graph = get_persistent_graph(request, access_token='paul') converter = FacebookUserConverter(graph) base_data = converter.facebook_profile_data() self.assertEqual(base_data['gender'], 'male') data = converter.facebook_registration_data() self.assertEqual(data['gender'], 'm') action, user = connect_user(self.request, facebook_graph=graph) self.assertEqual(user.get_profile().gender, 'm')
def test_gender_matching(self): request = RequestMock().get('/') request.session = {} request.user = AnonymousUser() graph = get_persistent_graph(request, access_token='paul') converter = FacebookUserConverter(graph) base_data = converter.facebook_profile_data() self.assertEqual(base_data['gender'], 'male') data = converter.facebook_registration_data() self.assertEqual(data['gender'], 'm') action, user = connect_user(self.request, facebook_graph=graph) self.assertEqual(user.get_profile().gender, 'm')
def _register_user(self, facebook, profile_callback=None): ''' Creates a new user and authenticates The registration form handles the registration and validation Other data on the user profile is updates afterwards ''' if not facebook.is_authenticated(): raise ValueError, 'Facebook needs to be authenticated for connect flows' facebook_user_converter = FacebookUserConverter(facebook) from registration.forms import RegistrationFormUniqueEmail #get the backend on new registration systems, or none if we are on an older version backend = get_registration_backend() #get the form used for registration and fall back to uniqueemail version form_class = RegistrationFormUniqueEmail if backend: form_class = backend.get_form_class(request) facebook_data = facebook_user_converter.facebook_registration_data() data = self.request.POST.copy() for k, v in facebook_data.items(): if not data.get(k): data[k] = v if self.request.REQUEST.get('force_registration_hard'): data['email'] = data['email'].replace('@', '+%s@' % randint(0, 100000)) form = form_class(data=data, files=self.request.FILES, initial={'ip': self.request.META['REMOTE_ADDR']}) if not form.is_valid(): error = facebook_exceptions.IncompleteProfileError('Facebook data %s gave error %s' % (facebook_data, form.errors)) error.form = form raise error #for new registration systems use the backends methods of saving if backend: new_user = backend.register(request, **form.cleaned_data) else: new_user = form.save(profile_callback=profile_callback) #update some extra data not yet done by the form new_user = self._update_user(new_user, facebook) #IS this the correct way for django 1.3? seems to require the backend attribute for some reason new_user.backend = 'django_facebook.auth_backends.FacebookBackend' auth.login(self.request, new_user) return new_user
def _update_user(self, facebook): ''' Updates the user and his/her profile with the data from facebook ''' user = self.request.user #if you want to add fields to ur user model instead of the profile thats fine #partial support (everything except raw_data and facebook_id is included) facebook_user_converter = FacebookUserConverter(facebook) facebook_data = facebook_user_converter.facebook_registration_data() user_dirty = profile_dirty = False profile = user.get_profile() profile_field_names = [f.name for f in profile._meta.fields] user_field_names = [f.name for f in user._meta.fields] #set the facebook id and make sure we are the only user with this id if facebook_data['facebook_id'] != profile.facebook_id: profile.facebook_id = facebook_data['facebook_id'] profile_dirty = True #like i said, me and only me profile_class = get_profile_class() profile_class.objects.filter( facebook_id=profile.facebook_id).exclude( user__id=user.id).update(facebook_id=None) #update all fields on both user and profile for f in self.facebook_fields: facebook_value = facebook_data.get(f, False) if facebook_value: if f in profile_field_names and not getattr(profile, f, False): setattr(profile, f, facebook_value) profile_dirty = True elif f in user_field_names and not getattr(user, f, False): setattr(user, f, facebook_value) user_dirty = True #write the raw data in case we missed something if hasattr(profile, 'raw_data'): serialized_fb_data = json.dumps( facebook_user_converter.facebook_profile_data()) profile.raw_data = serialized_fb_data profile_dirty = True #save both models if they changed if user_dirty: user.save() if profile_dirty: profile.save() return user
def _update_user(self, facebook): ''' Updates the user and his/her profile with the data from facebook ''' user = self.request.user #if you want to add fields to ur user model instead of the profile thats fine #partial support (everything except raw_data and facebook_id is included) facebook_user_converter = FacebookUserConverter(facebook) facebook_data = facebook_user_converter.facebook_registration_data() user_dirty = profile_dirty = False profile = user.get_profile() profile_field_names = [f.name for f in profile._meta.fields] user_field_names = [f.name for f in user._meta.fields] #set the facebook id and make sure we are the only user with this id if facebook_data['facebook_id'] != profile.facebook_id: profile.facebook_id = facebook_data['facebook_id'] profile_dirty = True #like i said, me and only me profile_class = get_profile_class() profile_class.objects.filter(facebook_id=profile.facebook_id).exclude(user__id=user.id).update(facebook_id=None) #update all fields on both user and profile for f in self.facebook_fields: facebook_value = facebook_data.get(f, False) if facebook_value: if f in profile_field_names and not getattr(profile, f, False): setattr(profile, f, facebook_value) profile_dirty = True elif f in user_field_names and not getattr(user, f, False): setattr(user, f, facebook_value) user_dirty = True #write the raw data in case we missed something if hasattr(profile, 'raw_data'): serialized_fb_data = json.dumps(facebook_user_converter.facebook_profile_data()) profile.raw_data = serialized_fb_data profile_dirty = True #save both models if they changed if user_dirty: user.save() if profile_dirty: profile.save() return user
def test_gender(self): graph = get_facebook_graph(access_token='new_user') facebook = FacebookUserConverter(graph) data = facebook.facebook_registration_data() self.assertEqual(data['gender'], 'm')
def test_gender(self): graph = get_facebook_graph(access_token='new_user') facebook = FacebookUserConverter(graph) data = facebook.facebook_registration_data() self.assertEqual(data['gender'], 'm')