コード例 #1
0
class RegisterTests(TestCase):
    fixtures = ['registered_users']

    def setUp(self):
        self.factory = SessionRequestFactory()

    def _register(self, data={}, email=None):
        default_data = {'display_name': 'TestUser', 'agreement': True}
        default_data.update(data)

        with self.activate('en-US'):
            request = self.factory.post(reverse('home'), data=default_data)

            # Inject fake verification into session
            if email:
                request.session['browserid_verification'] = {'email': email}

        response = register(request, RegisterForm(default_data))

        return {'request': request, 'response': response}

    def test_invalid_form(self):
        """Return None if the form isn't valid."""
        result = self._register({'agreement': False}, email='*****@*****.**')
        eq_(result['response'], None)

    def test_no_verification(self):
        """Return None if the session verification doesn't exist."""
        result = self._register()
        eq_(result['response'], None)

    @mock_browserid('*****@*****.**')
    def test_registered(self):
        """
        Return redirect and do not register if the user is already registered.
        """
        result = self._register(email='*****@*****.**')
        eq_(result['response'].status_code, 302)
        eq_(User.objects.filter(email='*****@*****.**').count(), 1)

    @mock_browserid('*****@*****.**')
    def test_new_user(self):
        """Return redirect and register if user is new."""
        result = self._register(email='*****@*****.**')
        eq_(result['response'].status_code, 302)
        ok_('_auth_user_id' in result['request'].session,
            'New user was not logged in.')

    @mock_browserid('*****@*****.**')
    @patch('browserid.views.subscribe')
    @patch.object(settings, 'BASKET_NEWSLETTER', 'test-newsletter')
    def test_email_subscription(self, subscribe):
        """Subscribe user to mailing list if requested."""
        result = self._register({'email_subscribe': True},
                                email='*****@*****.**')
        eq_(result['response'].status_code, 302)
        subscribe.assert_called_once_with('*****@*****.**',
                                          'test-newsletter',
                                          lang='en-us',
                                          source_url=ANY)
コード例 #2
0
ファイル: test_backends.py プロジェクト: rik/affiliates
class BrowserIDSessionBackendTests(TestCase):
    fixtures = ['registered_users']

    def setUp(self):
        self.backend = BrowserIDSessionBackend()
        self.factory = SessionRequestFactory()

    def _auth(self, email, assertion='asdf'):
        request = self.factory.get(reverse('home'))
        with mock_browserid(email):
            return self.backend.authenticate(request, assertion)

    def test_invalid_assertion(self):
        """Return None if the assertion is invalid."""
        result = self._auth(None)
        eq_(result, None)

    def test_invalid_user(self):
        """Return None if the user does not exist."""
        result = self._auth('*****@*****.**')
        eq_(result, None)

    def test_valid_user(self):
        """Return the User object if a user exists."""
        result = self._auth('*****@*****.**')
        ok_(isinstance(result, User))
        eq_(result.email, '*****@*****.**')
コード例 #3
0
class VerifyTests(TestCase):
    def setUp(self):
        self.factory = SessionRequestFactory()

    def _verify(self, assertion=None, email=None):
        with self.activate('en-US'):
            request = self.factory.get(reverse('home'))
        if email:
            request.session[SESSION_VERIFY] = self._verification(email)

        return verify(request, assertion)

    def _verification(self, email):
        return {'status': 'okay', 'email': email}

    @mock_browserid(None)
    def test_invalid_assertion(self):
        """Return None if the assertion is invalid."""
        result = self._verify('asdf')
        eq_(result, None)

    @mock_browserid('*****@*****.**')
    def test_valid_assertion(self):
        """Return verification if the assertion is valid."""
        result = self._verify('asdf')
        eq_(result['status'], 'okay')
        eq_(result['email'], '*****@*****.**')

    @mock_browserid('*****@*****.**')
    @patch('browserid.utils.browserid_verify')
    def test_cached_verification(self, browserid_verify):
        """
        Return verification without connecting to BrowserID if verification
        is cached in session.
        """
        result = self._verify(email='*****@*****.**')
        eq_(result['status'], 'okay')
        eq_(result['email'], '*****@*****.**')
        ok_(not browserid_verify.called, 'browserid_verify was called.')

        # Test that a non-cached call works, in case we mocked incorrectly
        result = self._verify('asdf')
        ok_(browserid_verify.called, 'browserid_verify was not called.')
コード例 #4
0
ファイル: test__utils.py プロジェクト: rik/affiliates
class VerifyTests(TestCase):
    def setUp(self):
        self.factory = SessionRequestFactory()

    def _verify(self, assertion=None, email=None):
        with self.activate("en-US"):
            request = self.factory.get(reverse("home"))
        if email:
            request.session[SESSION_VERIFY] = self._verification(email)

        return verify(request, assertion)

    def _verification(self, email):
        return {"status": "okay", "email": email}

    @mock_browserid(None)
    def test_invalid_assertion(self):
        """Return None if the assertion is invalid."""
        result = self._verify("asdf")
        eq_(result, None)

    @mock_browserid("*****@*****.**")
    def test_valid_assertion(self):
        """Return verification if the assertion is valid."""
        result = self._verify("asdf")
        eq_(result, self._verification("*****@*****.**"))

    @mock_browserid("*****@*****.**")
    @patch("browserid.utils.browserid_verify")
    def test_cached_verification(self, browserid_verify):
        """
        Return verification without connecting to BrowserID if verification
        is cached in session.
        """
        result = self._verify(email="*****@*****.**")
        eq_(result, self._verification("*****@*****.**"))
        ok_(not browserid_verify.called, "browserid_verify was called.")

        # Test that a non-cached call works, in case we mocked incorrectly
        result = self._verify("asdf")
        ok_(browserid_verify.called, "browserid_verify was not called.")
コード例 #5
0
 def setUp(self):
     self.factory = SessionRequestFactory()
コード例 #6
0
class RegisterTests(TestCase):
    fixtures = ['registered_users']

    def setUp(self):
        self.factory = SessionRequestFactory()

    def _register(self, data={}, email=None):
        default_data = {
            'display_name': 'TestUser',
            'agreement': True
        }
        default_data.update(data)

        with self.activate('en-US'):
            request = self.factory.post(reverse('home'), data=default_data)

            # Inject fake verification into session
            if email:
                request.session['browserid_verification'] = {'email': email}

        response = register(request, RegisterForm(default_data))

        return {'request': request, 'response': response}

    def test_invalid_form(self):
        """Return None if the form isn't valid."""
        result = self._register({'agreement': False}, email='*****@*****.**')
        eq_(result['response'], None)

    def test_no_verification(self):
        """Return None if the session verification doesn't exist."""
        result = self._register()
        eq_(result['response'], None)

    @mock_browserid('*****@*****.**')
    def test_registered(self):
        """
        Return redirect and do not register if the user is already registered.
        """
        result = self._register(email='*****@*****.**')
        eq_(result['response'].status_code, 302)
        eq_(User.objects.filter(email='*****@*****.**').count(), 1)

    @mock_browserid('*****@*****.**')
    def test_new_user(self):
        """Return redirect and register if user is new."""
        result = self._register(email='*****@*****.**')
        eq_(result['response'].status_code, 302)
        ok_('_auth_user_id' in result['request'].session,
            'New user was not logged in.')

    @mock_browserid('*****@*****.**')
    @patch('browserid.views.subscribe')
    @patch.object(settings, 'BASKET_NEWSLETTER', 'test-newsletter')
    def test_email_subscription(self, subscribe):
        """Subscribe user to mailing list if requested."""
        result = self._register({'email_subscribe': True},
                                email='*****@*****.**')
        eq_(result['response'].status_code, 302)
        subscribe.assert_called_once_with('*****@*****.**', 'test-newsletter',
                                          lang='en-us', source_url=ANY)
コード例 #7
0
 def setUp(self):
     self.factory = SessionRequestFactory()
コード例 #8
0
ファイル: test_backends.py プロジェクト: rik/affiliates
 def setUp(self):
     self.backend = BrowserIDSessionBackend()
     self.factory = SessionRequestFactory()