Exemple #1
0
 def setUp(self):
     self.factory = RequestFactory()
     self.session_middleware = SessionMiddleware()
     self.django_auth_middleware = AuthenticationMiddleware()
     self.auth_middleware = FacebookAuthenticationMiddleware()
Exemple #2
0
class FacebookAuthenticationMiddlewareTests(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.session_middleware = SessionMiddleware()
        self.django_auth_middleware = AuthenticationMiddleware()
        self.auth_middleware = FacebookAuthenticationMiddleware()

    def request(self, url='/fb', locale='en-US'):
        request = self.factory.get(url)
        self.session_middleware.process_request(request)
        self.django_auth_middleware.process_request(request)
        request.META['HTTP_ACCEPT_LANGUAGE'] = locale
        return request

    @patch('affiliates.facebook.middleware.activate_locale')
    def test_no_session(self, activate_locale):
        """
        If there is no authenticated session, the user attribute should contain
        an anonymous user.
        """
        request = self.request()
        with patch('affiliates.facebook.middleware.Prefixer') as Prefixer:
            Prefixer.return_value.get_language.return_value = 'en-US'
            self.auth_middleware.process_request(request)
            Prefixer.assert_called_with(request)
        eq_(request.user.is_anonymous(), True)

        # Check that the locale from the request was activated.
        activate_locale.assert_called_once_with(ANY, 'en-US')

    @patch('affiliates.facebook.middleware.activate_locale')
    def test_no_matching_user(self, activate_locale):
        """
        If an invalid user id is given in the session, the user attribute should
        contain an anonymous user.
        """
        request = self.request()
        request.session[SESSION_KEY] = 9999
        with patch('affiliates.facebook.middleware.Prefixer') as Prefixer:
            Prefixer.return_value.get_language.return_value = 'es'
            self.auth_middleware.process_request(request)
            Prefixer.assert_called_with(request)
        eq_(request.user.is_anonymous(), True)

        # Check that the locale from the request was activated.
        activate_locale.assert_called_once_with(ANY, 'es')

    @patch('affiliates.facebook.middleware.activate_locale')
    def test_user_found(self, activate_locale):
        """
        If there is an authenticated session with an existing user, the
        specified user should be available via the user attribute.
        """
        request = self.request()
        user = FacebookUserFactory.create(locale='fr')
        request.session[SESSION_KEY] = user.id
        self.auth_middleware.process_request(request)
        eq_(request.user, user)

        # Check that the locale from the user object was activated.
        activate_locale.assert_called_once_with(ANY, 'fr')

    @patch('affiliates.facebook.middleware.activate_locale')
    def test_user_non_fb_app(self, activate_locale):
        """
        If there is an authenticated session with an existing user outside of
        the app, the specified user should not be a FacebookUser.
        """
        request = self.request(url='/', locale='pt-BR')
        user = FacebookUserFactory.create()
        request.session[SESSION_KEY] = user.id

        self.auth_middleware.process_request(request)
        ok_(not request.user == user)
        eq_(request.user.is_anonymous(), True)

        # Check that the FB locale stuff was never called.
        eq_(activate_locale.called, False)