def test_parallel_register(self): ''' Adding some testing for the case when one person tries to register multiple times in the same second ''' graph = get_facebook_graph(access_token='short_username') FacebookUserConverter(graph) action, user = connect_user(self.request, facebook_graph=graph) self.assertEqual(action, CONNECT_ACTIONS.REGISTER) self.request.user.is_authenticated = lambda: False with patch('django_facebook.connect.authenticate') as patched: return_sequence = [user, None] def side(*args, **kwargs): value = return_sequence.pop() return value patched.side_effect = side with patch('django_facebook.connect._register_user' ) as patched_register: patched_register.side_effect = facebook_exceptions.AlreadyRegistered( 'testing parallel registers') action, user = connect_user(self.request, facebook_graph=graph) self.assertEqual(action, CONNECT_ACTIONS.LOGIN)
def _register_user(request, facebook, profile_callback=None, remove_old_connections=False): ''' Creates a new user and authenticates The registration form handles the registration and validation Other data on the user profile is updates afterwards if remove_old_connections = True we will disconnect old profiles from their facebook flow ''' if not facebook.is_authenticated(): raise ValueError( 'Facebook needs to be authenticated for connect flows') # get the backend on new registration systems, or none # if we are on an older version backend = get_registration_backend() logger.info('running backend %s for registration', backend) # gets the form class specified in FACEBOOK_REGISTRATION_FORM form_class = get_form_class(backend, request) facebook_data = facebook.facebook_registration_data() data = request.POST.copy() for k, v in facebook_data.items(): if not data.get(k): data[k] = v if remove_old_connections: _remove_old_connections(facebook_data['facebook_id']) if request.REQUEST.get('force_registration_hard'): data['email'] = data['email'].replace( '@', '+test%s@' % randint(0, 1000000000)) form = form_class(data=data, files=request.FILES, initial={'ip': request.META['REMOTE_ADDR']}) if not form.is_valid(): # show errors in sentry form_errors = form.errors error = facebook_exceptions.IncompleteProfileError( 'Facebook signup incomplete') error.form = form raise error try: # for new registration systems use the backends methods of saving new_user = None if backend: new_user = backend.register(request, form=form, **form.cleaned_data) # fall back to the form approach if new_user is None: raise ValueError( 'new_user is None, note that backward compatability for the older versions of django registration has been dropped.') except IntegrityError, e: # this happens when users click multiple times, the first request registers # the second one raises an error raise facebook_exceptions.AlreadyRegistered(e)
def clean_email(self): """ Validate that the supplied email address is unique for the site. """ if get_user_model().objects.filter( email__iexact=self.cleaned_data['email']): user = get_user_model().objects.get( email__iexact=self.cleaned_data['email']) request = get_request() my_login(request, user) print("email already in use, logged in!") raise facebook_exceptions.AlreadyRegistered( _("This email address is already in use. Please supply a different email address." )) #raise forms.ValidationError(_( # "This email address is already in use. Please supply a different email address.")) return self.cleaned_data['email']
def _register_user(request, facebook, profile_callback=None, remove_old_connections=False): ''' Creates a new user and authenticates The registration form handles the registration and validation Other data on the user profile is updates afterwards if remove_old_connections = True we will disconnect old profiles from their facebook flow ''' if not facebook.is_authenticated(): raise ValueError( 'Facebook needs to be authenticated for connect flows') # get the backend on new registration systems, or none # if we are on an older version backend = get_registration_backend() logger.info('running backend %s for registration', backend) # gets the form class specified in FACEBOOK_REGISTRATION_FORM form_class = get_form_class(backend, request) print("0") facebook_data = facebook.facebook_registration_data() print("1") data = request.POST.copy() print("2") for k, v in facebook_data.items(): if not data.get(k): data[k] = v print("3") if remove_old_connections: _remove_old_connections(facebook_data['facebook_id']) print("4") if request.REQUEST.get('force_registration_hard'): data['email'] = data['email'].replace( '@', '+test%s@' % randint(0, 1000000000)) print("debug facebook data ", facebook_data, " data ", data) try: form = form_class(data=data, files=request.FILES, initial={'ip': request.META['REMOTE_ADDR']}) if not form.is_valid(): # show errors in sentry form_errors = form.errors error = facebook_exceptions.IncompleteProfileError( 'Facebook signup incomplete') error.form = form raise error except facebook_exceptions.AlreadyRegistered: print("re raised!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") # TODO raise facebook_exceptions.AlreadyRegistered(_("This email address is already in use. Please supply a different email address.")) print("debug33", data["facebook_id"]) print("debug34", form.cleaned_data) try: # for new registration systems use the backends methods of saving new_user = None if backend: new_user = backend.register(request, form=form, **form.cleaned_data) # fall back to the form approach if new_user is None: raise ValueError( 'new_user is None, note that backward compatability for the older versions of django registration has been dropped.') except IntegrityError as e: # this happens when users click multiple times, the first request registers # the second one raises an error raise facebook_exceptions.AlreadyRegistered(e) # update some extra data not yet done by the form print("debu4", new_user.facebook_id) new_user = _update_user(new_user, facebook) print("debu5", new_user.facebook_id) new_user.facebook_id = data["facebook_id"] new_user.raw_data = data new_user.save() signals.facebook_user_registered.send(sender=get_user_model(), user=new_user, facebook_data=facebook_data, request=request, converter=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(request, new_user) return new_user