예제 #1
0
 def test_request_user(self, update_user_info):
     """
     After a user is logged in, the user attribute should be set on the
     request object.
     """
     request = self.request()
     user = FacebookUserFactory()
     login(request, user)
     eq_(request.user, user)
예제 #2
0
 def test_old_user_task_scheduled(self, update_task, update_method):
     """
     If the user logging in isn't new, use the asynchronous task to update
     their info instead of the normal method.
     """
     request = self.request()
     user = FacebookUserFactory()
     login(request, user)
     update_task.delay.assert_called_once_with(user.id)
     ok_(not update_method.called)
예제 #3
0
    def test_new_session_key(self, update_user_info):
        """
        If there is an existing, unauthenticated session, change the session
        key on the request.
        """
        request = self.request()
        old_key = request.session.session_key
        user = FacebookUserFactory()

        login(request, user)
        ok_(request.session.session_key != old_key)
예제 #4
0
    def test_last_login_attribute(self, mock_datetime, update_user_info):
        """
        During the login process, the last_login attribute on the user must be
        set to the current datetime.
        """
        mock_datetime.now.return_value = datetime(2012, 1, 1)
        request = self.request()
        user = FacebookUserFactory.create(last_login=datetime(2000, 1, 1))
        login(request, user)

        user = refresh_model(user)
        eq_(user.last_login, datetime(2012, 1, 1))
예제 #5
0
    def test_flush_session(self, update_user_info):
        """
        If a previous login session is found and logging in as a different user,
        flush the previous session.
        """
        request = self.request()
        user1 = FacebookUserFactory()
        login(request, user1)

        request.session["somedata"] = 1
        user2 = FacebookUserFactory()
        login(request, user2)
        ok_(not "somedata" in request.session)
예제 #6
0
def load_app(request):
    """
    Create or authenticate the Facebook user and direct them to the correct
    area of the app upon their entry.
    """
    # Temporary measure to handle when Facebook does a GET to the main URL when
    # a logged-out user views the app. In the future we should show a promo
    # page instead.
    if request.method != "POST":
        return request_authorization(request)

    signed_request = request.POST.get("signed_request", None)
    if signed_request is None:
        # App wasn't loaded within a canvas, redirect to the home page.
        return redirect("home")

    decoded_request = decode_signed_request(signed_request, settings.FACEBOOK_APP_SECRET)
    if decoded_request is None:
        return redirect("home")

    # If user is using Safari, we need to apply the cookie workaround.
    useragent = request.META.get("HTTP_USER_AGENT", "")
    using_safari = "Safari" in useragent and not "Chrome" in useragent
    workaround_applied = SAFARI_WORKAROUND_KEY in request.COOKIES
    if using_safari and not workaround_applied:
        return fb_redirect(request, absolutify(reverse("facebook.safari_workaround")))

    user, created = FacebookUser.objects.get_or_create_user_from_decoded_request(decoded_request)
    if user is None:
        # User has yet to authorize the app, offer authorization.
        return request_authorization(request)

    # Attach country data to the user object. This can only be retrieved from
    # the decoded request, so we add it here and login saves it.
    user.country = decoded_request["user"].get("country", user.country)

    # User has been authed, let's log them in.
    login(request, user)

    # Normally the FacebookAuthenticationMiddleware activates the locale for
    # the user, but since it does not run for this view, we need to activate it
    # manually.
    activate_locale(request, user.locale)

    return banner_list(request)
예제 #7
0
    def test_delayed_task_overwritten(self, update_user_info):
        """
        Regression test: If DEV is true, the delayed task will execute
        immediately. But because the task does not alter the user object, if the
        old user object is saved these changes will be overwritten.
        """
        request = self.request()
        user = FacebookUserFactory.create(first_name="Unchanged")

        def alter_user(user):
            user.first_name = "Changed"
            user.save()

        update_user_info.side_effect = alter_user

        login(request, user)
        user = refresh_model(user)
        eq_(user.first_name, "Changed")
예제 #8
0
def load_app(request):
    """
    Create or authenticate the Facebook user and direct them to the correct
    area of the app upon their entry.
    """
    signed_request = request.POST.get('signed_request', None)
    if signed_request is None:
        # App wasn't loaded within a canvas, redirect to the home page.
        return redirect('home')

    decoded_request = decode_signed_request(signed_request,
                                            settings.FACEBOOK_APP_SECRET)
    if decoded_request is None:
        return redirect('home')

    # If user is using Safari, we need to apply the cookie workaround.
    useragent = request.META.get('HTTP_USER_AGENT', '')
    using_safari = 'Safari' in useragent and not 'Chrome' in useragent
    workaround_applied = SAFARI_WORKAROUND_KEY in request.COOKIES
    if using_safari and not workaround_applied:
        return fb_redirect(request,
                           absolutify(reverse('facebook.safari_workaround')),
                           top_window=True)

    user, created = (FacebookUser.objects.
            get_or_create_user_from_decoded_request(decoded_request))
    if user is None:
        # User has yet to authorize the app, redirect to the pre-auth promo.
        return fb_redirect(request,
                           absolutify(reverse('facebook.pre_auth_promo')))

    # Attach country data to the user object. This can only be retrieved from
    # the decoded request, so we add it here and login saves it.
    user.country = decoded_request['user'].get('country', user.country)

    # User has been authed, let's log them in.
    login(request, user)

    return fb_redirect(request, absolutify(reverse('facebook.banner_list')))
예제 #9
0
def load_app(request):
    """
    Create or authenticate the Facebook user and direct them to the correct
    area of the app upon their entry.
    """
    signed_request = request.POST.get('signed_request', None)
    if signed_request is None:
        # App wasn't loaded within a canvas, redirect to the home page.
        return redirect('home')

    decoded_request = decode_signed_request(signed_request,
                                            settings.FACEBOOK_APP_SECRET)
    if decoded_request is None:
        return redirect('home')

    # If user is using Safari, we need to apply the cookie workaround.
    useragent = request.META.get('HTTP_USER_AGENT', '')
    using_safari = 'Safari' in useragent and not 'Chrome' in useragent
    workaround_applied = SAFARI_WORKAROUND_KEY in request.COOKIES
    if using_safari and not workaround_applied:
        return fb_redirect(request,
                           absolutify(reverse('facebook.safari_workaround')),
                           top_window=True)

    user, created = (FacebookUser.objects.
                     get_or_create_user_from_decoded_request(decoded_request))
    if user is None:
        # User has yet to authorize the app, redirect to the pre-auth promo.
        return fb_redirect(request,
                           absolutify(reverse('facebook.pre_auth_promo')))

    # Attach country data to the user object. This can only be retrieved from
    # the decoded request, so we add it here and login saves it.
    user.country = decoded_request['user'].get('country', user.country)

    # User has been authed, let's log them in.
    login(request, user)

    return fb_redirect(request, absolutify(reverse('facebook.banner_list')))
예제 #10
0
 def test_new_user_update_user_info_called(self, update_user_info):
     """Ensure that update_user_info is called on a successful login."""
     request = self.request()
     user = FacebookUserFactory(last_login=None)
     login(request, user)
     update_user_info.assert_called_once_with(user)