Exemple #1
0
    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)
Exemple #2
0
 def test_double_username(self):
     '''
     This used to give an error with duplicate usernames
     with different capitalization
     '''
     facebook = get_facebook_graph(access_token='short_username')
     action, user = connect_user(self.request, facebook_graph=facebook)
     user.username = '******'
     user.save()
     self.request.user = AnonymousUser()
     facebook = get_facebook_graph(access_token='same_username')
     action, new_user = connect_user(self.request, facebook_graph=facebook)
     self.assertNotEqual(user.username, new_user.username)
     self.assertNotEqual(user.id, new_user.id)
Exemple #3
0
    def test_fb_update_required(self):
        def pre_update(sender, user, profile, facebook_data, **kwargs):
            user.pre_update_signal = True

        Profile = get_profile_model()
        user_model = get_user_model()
        signals.facebook_pre_update.connect(pre_update, sender=user_model)
        facebook = get_facebook_graph(access_token='tschellenbach')

        facebook_settings.FACEBOOK_FORCE_PROFILE_UPDATE_ON_LOGIN = True
        action, user = connect_user(self.request, facebook_graph=facebook)
        self.assertEqual(action, CONNECT_ACTIONS.LOGIN)
        self.assertTrue(hasattr(user, 'pre_update_signal'))

        facebook_settings.FACEBOOK_FORCE_PROFILE_UPDATE_ON_LOGIN = False
        action, user = connect_user(self.request, facebook_graph=facebook)
        self.assertEqual(action, CONNECT_ACTIONS.LOGIN)
        self.assertFalse(hasattr(user, 'pre_update_signal'))
Exemple #4
0
    def test_persistent_graph(self):
        request = RequestMock().get('/')
        request.session = {}
        request.user = AnonymousUser()

        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)
Exemple #5
0
 def test_full_connect(self):
     # going for a register, connect and login
     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)
     # and now we do a login, not a connect
     action, user = connect_user(self.request, facebook_graph=graph)
     self.assertEqual(action, CONNECT_ACTIONS.LOGIN)
     self.request.GET._mutable = True
     self.request.GET['connect_facebook'] = 1
     action, user = connect_user(self.request,
                                 facebook_graph=graph,
                                 connect_facebook=True)
     self.assertEqual(action, CONNECT_ACTIONS.CONNECT)
     self.request.user = AnonymousUser()
     action, user = connect_user(self.request,
                                 facebook_graph=graph,
                                 connect_facebook=True)
     self.assertEqual(action, CONNECT_ACTIONS.LOGIN)
Exemple #6
0
 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)
Exemple #7
0
    def test_update_access_token(self):
        request = RequestMock().get('/')
        request.session = {}
        request.user = AnonymousUser()
        graph = get_persistent_graph(request, access_token='paul')
        action, user = connect_user(self.request, facebook_graph=graph)
        first_user_id = user.id

        # new token required should start out as False
        profile = try_get_profile(user)
        new_token_required = get_user_attribute(user, profile,
                                                'new_token_required')
        self.assertEqual(new_token_required, False)

        # we manually set it to true
        update_user_attributes(user,
                               profile,
                               dict(new_token_required=True),
                               save=True)
        if profile:
            profile = get_profile_model().objects.get(id=profile.id)
        user = get_user_model().objects.get(id=user.id)
        new_token_required = get_user_attribute(user, profile,
                                                'new_token_required')
        self.assertEqual(new_token_required, True)

        # another update should however set it back to False
        request.facebook = None
        graph = get_facebook_graph(request, access_token='paul2')
        logger.info('and the token is %s', graph.access_token)
        action, user = connect_user(self.request, facebook_graph=graph)
        user = get_user_model().objects.get(id=user.id)
        self.assertEqual(user.id, first_user_id)
        if profile:
            profile = get_profile_model().objects.get(id=profile.id)
        user = get_user_model().objects.get(id=user.id)
        new_token_required = get_user_attribute(user, profile,
                                                'new_token_required')
        self.assertEqual(new_token_required, False)
Exemple #8
0
 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)
     profile = try_get_profile(user)
     gender = get_user_attribute(user, profile, 'gender')
     self.assertEqual(gender, 'm')
Exemple #9
0
def _connect(request, graph):
    '''
    Handles the view logic around connect user
    - (if authenticated) connect the user
    - login
    - register

    We are already covered by the facebook_required_lazy decorator
    So we know we either have a graph and permissions, or the user denied
    the oAuth dialog
    '''
    backend = get_registration_backend()
    context = RequestContext(request)
    connect_facebook = to_bool(request.REQUEST.get('connect_facebook'))

    logger.info('trying to connect using Facebook')
    if graph:
        logger.info('found a graph object')
        converter = get_instance_for('user_conversion', graph)
        authenticated = converter.is_authenticated()
        # Defensive programming :)
        if not authenticated:
            raise ValueError('didnt expect this flow')

        logger.info('Facebook is authenticated')
        facebook_data = converter.facebook_profile_data()
        # either, login register or connect the user
        try:
            action, user = connect_user(request,
                                        connect_facebook=connect_facebook)
            logger.info('Django facebook performed action: %s', action)
        except facebook_exceptions.IncompleteProfileError, e:
            # show them a registration form to add additional data
            warning_format = u'Incomplete profile data encountered with error %s'
            warn_message = warning_format % unicode(e)
            send_warning(warn_message, e=e, facebook_data=facebook_data)

            context['facebook_mode'] = True
            context['form'] = e.form
            return render_to_response(
                facebook_settings.FACEBOOK_REGISTRATION_TEMPLATE,
                context_instance=context,
            )
        except facebook_exceptions.AlreadyConnectedError, e:
            user_ids = [u.get_user_id() for u in e.users]
            ids_string = ','.join(map(str, user_ids))
            additional_params = dict(already_connected=ids_string)
            return backend.post_error(request, additional_params)
Exemple #10
0
    def test_registration_form(self):
        '''
        Django_facebook should use user supplied registration form if given
        '''
        from django.conf import settings
        if settings.MODE == 'userena':
            return

        test_form = 'django_facebook.test_utils.forms.SignupForm'
        old_setting = facebook_settings.FACEBOOK_REGISTRATION_FORM
        facebook_settings.FACEBOOK_REGISTRATION_FORM = test_form
        try:
            facebook = get_facebook_graph(access_token='short_username')
            action, user = connect_user(self.request, facebook_graph=facebook)
            # The test form always sets username to test form
            self.assertEqual(user.username, 'Test form')
        finally:
            facebook_settings.FACEBOOK_REGISTRATION_FORM = old_setting
Exemple #11
0
    def test_auth_backend(self):
        # the auth backend
        backend = FacebookBackend()
        facebook = get_facebook_graph(access_token='new_user')
        action, user = connect_user(self.request, facebook_graph=facebook)
        facebook_email = user.email
        profile = try_get_profile(user)
        user_or_profile = get_instance_for_attribute(user, profile,
                                                     'facebook_id')
        facebook_id = user_or_profile.facebook_id
        auth_user = backend.authenticate(facebook_email=facebook_email)
        logger.info('%s %s %s', auth_user.email, user.email, facebook_email)
        self.assertEqual(auth_user, user)

        auth_user = backend.authenticate(facebook_id=facebook_id)
        self.assertEqual(auth_user, user)

        auth_user = backend.authenticate(facebook_id=facebook_id,
                                         facebook_email=facebook_email)
        self.assertEqual(auth_user, user)

        auth_user = backend.authenticate()
        self.assertIsNone(auth_user)
Exemple #12
0
 def test_short_username(self):
     facebook = get_facebook_graph(access_token='short_username')
     action, user = connect_user(self.request, facebook_graph=facebook)
     self.assertTrue(len(user.username) > 4)
     self.assertEqual(action, CONNECT_ACTIONS.REGISTER)
Exemple #13
0
 def test_new_user(self):
     facebook = get_facebook_graph(access_token='new_user')
     action, user = connect_user(self.request, facebook_graph=facebook)
Exemple #14
0
 def test_current_user(self):
     facebook = get_facebook_graph(access_token='tschellenbach')
     action, user = connect_user(self.request, facebook_graph=facebook)
     self.assertEqual(action, CONNECT_ACTIONS.LOGIN)
Exemple #15
0
 def test_utf8(self):
     graph = get_facebook_graph(access_token='unicode_string')
     facebook = FacebookUserConverter(graph)
     action, user = connect_user(self.request, facebook_graph=graph)