def browserid_login(self, email):
     """Logs the test client in using BrowserID."""
     factory = SessionRequestFactory()
     with self.activate('en-US'):
         request = factory.get('/')
     with mock_browserid(email):
         self.client.login(request=request, assertion='asdf')
    def build_user(self, email=None, salt=None, login=False,
                   profile=True, **kwargs):
        """Retrieve a test user account for this class, creating one if it
        does not exist.

        kwargs:
           email: User's email
           salt: Used to generate username. For creating multiple users.
           login: If True, log the user in with the test client.
           profile: If True, create a UserProfile as well.
        """
        name = '%s_%s_%s' % (self.__class__.__name__, email, salt)
        username = base64.urlsafe_b64encode(
            hashlib.sha1(name).digest()).rstrip('=')
        email = email or '*****@*****.**' % username

        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            user = User.objects.create_user(username, email)

        if login:
            with mock_browserid(email):
                self.client.login()

        if profile:
            profile = UserProfile.objects.create(user=user)
            user.userprofile = profile
            user.save()

        if kwargs:
            User.objects.filter(pk=user.pk).update(**kwargs)

        return user
Beispiel #3
0
 def browserid_login(self, email):
     """Logs the test client in using BrowserID."""
     factory = SessionRequestFactory()
     with self.activate('en-US'):
         request = factory.get(reverse('home'))
     with mock_browserid(email):
         self.client.login(request=request, assertion='asdf')
    def test_overriding_valid_email(self):
        class PickyBackend(BrowserIDBackend):
            def is_valid_email(self, email):
                return email != '*****@*****.**'

        new_user('*****@*****.**', 'test1')
        new_user('*****@*****.**', 'test2')

        with mock_browserid('*****@*****.**'):
            backend = PickyBackend()
            result = backend.authenticate(assertion='asdf', audience='asdf')
            self.assertTrue(not result)

        with mock_browserid('*****@*****.**'):
            backend = PickyBackend()
            result = backend.authenticate(assertion='asdf', audience='asdf')
            self.assertTrue(result)
    def test_overriding_valid_email(self):
        class PickyBackend(BrowserIDBackend):
            def is_valid_email(self, email):
                return email != '*****@*****.**'

        new_user('*****@*****.**', 'test1')
        new_user('*****@*****.**', 'test2')

        with mock_browserid('*****@*****.**'):
            backend = PickyBackend()
            result = backend.authenticate(assertion='asdf', audience='asdf')
            self.assertTrue(not result)

        with mock_browserid('*****@*****.**'):
            backend = PickyBackend()
            result = backend.authenticate(assertion='asdf', audience='asdf')
            self.assertTrue(result)
Beispiel #6
0
    def auth(self, verified_email=None, **kwargs):
        """
        Attempt to authenticate a user with BrowserIDBackend.

        If verified_email is None, verification will fail, otherwise it will
        pass and return the specified email.
        """
        with mock_browserid(verified_email):
            backend = BrowserIDBackend()
            return backend.authenticate(assertion='asdf', audience='asdf', **kwargs)
Beispiel #7
0
    def auth(self, verified_email=None):
        """
        Attempt to authenticate a user with BrowserIDBackend.

        If verified_email is None, verification will fail, otherwise it will
        pass and return the specified email.
        """
        with mock_browserid(verified_email):
            backend = BrowserIDBackend()
            return backend.authenticate(assertion='asdf', audience='asdf')
Beispiel #8
0
    def client_login(self, user=None):
        """
        Authenticate the test client as the given user. If no user is
        given, a test user is created and returned.
        """
        if user is None:
            user = UserFactory.create()

        with mock_browserid(user.email):
            self.client.login(assertion='asdf', audience='asdf')

        return user
Beispiel #9
0
    def client_login(self, user=None):
        """
        Authenticate the test client as the given user. If no user is
        given, a test user is created and returned.
        """
        if user is None:
            user = UserFactory.create()

        with mock_browserid(user.email):
            self.client.login(assertion='asdf', audience='asdf')

        return user
Beispiel #10
0
    def test_custom_values(self):
        request = self.factory.get('/')

        User.objects.create_user('asdf', '*****@*****.**')
        with mock_browserid('*****@*****.**'):
            user = authenticate(assertion='asdf', audience='1234')
            request.user = user

        info = browserid_info(request)
        d = pq(info)

        info_div = d('#browserid-info')
        self.assertEqual(info_div.attr('data-user-email'), '*****@*****.**')
        self.assertEqual(info_div.attr('data-request-args'),
                         '{"siteName": "asdf"}')

        form = d('#browserid-form')
        self.assertEqual(form.attr('action'), '/browserid/login/')
Beispiel #11
0
    def test_non_browserid_user(self):
        """
        If the current user was not authenticated via django-browserid,
        data-user-email should be empty.
        """
        request = self.factory.get('/')

        User.objects.create_user('asdf', '*****@*****.**', '1234')
        with mock_browserid(None):
            user = authenticate(username='******', password='******')
            self.assertTrue(user.is_authenticated())
            request.user = user

        info = browserid_info(request)
        d = pq(info)

        info_div = d('#browserid-info')
        self.assertEqual(info_div.attr('data-user-email'), '')
Beispiel #12
0
    def test_custom_values(self):
        request = self.factory.get("/")

        User.objects.create_user("asdf", "*****@*****.**")
        with mock_browserid("*****@*****.**"):
            user = authenticate(assertion="asdf", audience="1234")

        request.user = user
        request.session = SessionStore()

        login(request, user)

        info = browserid_info(request)
        d = pq(info)

        info_div = d("#browserid-info")
        self.assertEqual(info_div.attr("data-user-email"), "*****@*****.**")
        self.assertEqual(info_div.attr("data-request-args"), '{"siteName": "asdf"}')

        form = d("#browserid-form")
        self.assertEqual(form.attr("action"), "/browserid/login/")
Beispiel #13
0
    def test_non_browserid_user(self):
        """
        If the current user was not authenticated via django-browserid,
        data-user-email should be empty.
        """
        request = self.factory.get("/")

        User.objects.create_user("asdf", "*****@*****.**", "1234")
        with mock_browserid(None):
            user = authenticate(username="******", password="******")

        self.assertTrue(user.is_authenticated())
        request.user = user
        request.session = SessionStore()

        login(request, user)

        info = browserid_info(request)
        d = pq(info)

        info_div = d("#browserid-info")
        self.assertEqual(info_div.attr("data-user-email"), "")
Beispiel #14
0
    def build_user(self,
                   email=None,
                   salt=None,
                   login=False,
                   profile=True,
                   **kwargs):
        """Retrieve a test user account for this class, creating one if it
        does not exist.

        kwargs:
           email: User's email
           salt: Used to generate username. For creating multiple users.
           login: If True, log the user in with the test client.
           profile: If True, create a UserProfile as well.
        """
        name = '%s_%s_%s' % (self.__class__.__name__, email, salt)
        username = base64.urlsafe_b64encode(
            hashlib.sha1(name).digest()).rstrip('=')
        email = email or '*****@*****.**' % username

        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            user = User.objects.create_user(username, email)

        if login:
            with mock_browserid(email):
                self.client.login()

        if profile:
            profile = UserProfile.objects.create(user=user)
            user.userprofile = profile
            user.save()

        if kwargs:
            User.objects.filter(pk=user.pk).update(**kwargs)

        return user
Beispiel #15
0
 def _auth(self, email, assertion='asdf'):
     request = self.factory.get(reverse('home'))
     with mock_browserid(email):
         return self.backend.authenticate(request, assertion)
Beispiel #16
0
 def fake_login(username, password=None):
     with mock_browserid(email=username):
         return real_login(username=username, assertion='test',
                           audience='test')
Beispiel #17
0
 def client_login_user(self, user):
     with mock_browserid(user.email):
         self.client.login()
Beispiel #18
0
 def fake_login(username, password):
     with mock_browserid(email=username):
         return real_login(username=username, password=password)
Beispiel #19
0
 def client_login_user(self, user):
     with mock_browserid(user.email):
         ret = self.client.login(audience='faux', assertion='faux')
         assert ret, 'Login failed.'
Beispiel #20
0
 def fake_login(username, password=None):
     with mock_browserid(email=username):
         return real_login(username=username,
                           assertion='test',
                           audience='test')
Beispiel #21
0
 def client_login_user(self, user):
     with mock_browserid(user.email):
         ret = self.client.login(audience='faux', assertion='faux')
         assert ret, 'Login failed.'
Beispiel #22
0
 def client_login_user(self, user):
     with mock_browserid(user.email):
         ret = self.client.login(audience="faux", assertion="faux")
         assert ret, "Login failed."
Beispiel #23
0
        def _auth(self, backend=None, verified_email=None):
            if backend is None:
                backend = BrowserIDBackend()

            with mock_browserid(verified_email):
                return backend.authenticate(assertion='asdf', audience='asdf')
Beispiel #24
0
 def login(self, email, password):
     with mock_browserid(email=email):
         return super(OAuthClient, self).login(email=email,
                                               password=password)
Beispiel #25
0
 def login(self, username, password):
     with mock_browserid(email=username):
         return super(OAuthClient, self).login(username=username,
                                               password=password)
Beispiel #26
0
 def login(self, email, password):
     with mock_browserid(email=email):
         return super(OAuthClient, self).login(email=email,
                                               password=password)
Beispiel #27
0
 def fake_login(email, password=None):
     with mock_browserid(email=email):
         return real_login(email=email, assertion='test',
                           audience='test')
Beispiel #28
0
 def fake_login(email, password=None):
     with mock_browserid(email=email):
         return real_login(email=email, assertion='test',
                           audience='test')
Beispiel #29
0
        def _auth(self, backend=None, verified_email=None):
            if backend is None:
                backend = BrowserIDBackend()

            with mock_browserid(verified_email):
                return backend.authenticate(assertion='asdf', audience='asdf')
Beispiel #30
0
 def fake_login(username, password):
     with mock_browserid(email=username):
         return real_login(username=username, password=password)