Exemplo n.º 1
0
 def assert_raises(username, password, message):
     with self.assertRaises(AuthenticationError):
         try:
             User.authenticate(username, password)
         except AuthenticationError as e:
             self.assertEqual(str(e), message)
             raise
Exemplo n.º 2
0
 def assert_raises(username, password, message):
     with self.assertRaises(AuthenticationError):
         try:
             User.authenticate(username, password)
         except AuthenticationError as e:
             self.assertEqual(str(e), message)
             raise
Exemplo n.º 3
0
 def post(self, request, *args, **kwargs):
     '''Reset a user's password and email it to them.'''
     try:
         User.reset_password(request.DATA.get('username'))
         return Response(status=status.HTTP_201_CREATED)
     except User.DoesNotExist:
         raise Http404
Exemplo n.º 4
0
 def post(self, request, *args, **kwargs):
     '''Reset a user's password and email it to them.'''
     try:
         User.reset_password(request.DATA.get('username'))
         return Response(status=status.HTTP_201_CREATED)
     except User.DoesNotExist:
         raise Http404
Exemplo n.º 5
0
Arquivo: user.py Projeto: bmun/huxley
 def post(self, request, *args, **kwargs):
     '''Reset a user's password and email it to them.'''
     try:
         username = request.data.get('username', '')
         User.reset_password(username=username)
         return Response({}, status=status.HTTP_201_CREATED)
     except User.DoesNotExist:
         raise Http404
Exemplo n.º 6
0
 def post(self, request, *args, **kwargs):
     '''Reset a user's password and email it to them.'''
     try:
         username = request.data.get('username', '')
         User.reset_password(username=username)
         return Response({}, status=status.HTTP_201_CREATED)
     except User.DoesNotExist:
         raise Http404
Exemplo n.º 7
0
 def post(self, request, *args, **kwargs):
     '''Reset a delegate's password and email it to them.'''
     try:
         delegate_id = request.data.get('delegate_id', -1)
         user = User.objects.get(delegate__id=delegate_id)
         User.reset_password(user=user)
         return Response({}, status=status.HTTP_201_CREATED)
     except User.DoesNotExist:
         raise Http404
Exemplo n.º 8
0
Arquivo: user.py Projeto: bmun/huxley
 def post(self, request, *args, **kwargs):
     '''Reset a delegate's password and email it to them.'''
     try:
         delegate_id = request.data.get('delegate_id', -1)
         user = User.objects.get(delegate__id=delegate_id)
         User.reset_password(user=user)
         return Response({}, status=status.HTTP_201_CREATED)
     except User.DoesNotExist:
         raise Http404
Exemplo n.º 9
0
    def test_reset_delegate_password(self):
        '''It should correctly reset a delegate's password or raise an error.'''
        password = '******'
        delegate = Delegate.objects.create()
        delegate_user = User.objects.create(
            username='******', delegate=delegate)
        delegate_user.set_password(password)
        delegate_user.save()

        User.reset_password(user=delegate_user)
        self.assertFalse(delegate_user.check_password(password))
Exemplo n.º 10
0
    def test_reset_delegate_password(self):
        '''It should correctly reset a delegate's password or raise an error.'''
        password = '******'
        delegate = Delegate.objects.create()
        delegate_user = User.objects.create(username='******',
                                            delegate=delegate)
        delegate_user.set_password(password)
        delegate_user.save()

        User.reset_password(user=delegate_user)
        self.assertFalse(delegate_user.check_password(password))
Exemplo n.º 11
0
    def test_reset_password(self):
        '''It should correctly reset a user's password or raise an error.'''
        password = '******'
        user = models.new_user(username='******', password=password)
        self.assertTrue(user.check_password(password))

        User.reset_password('lololol')
        user = User.objects.get(id=user.id)
        self.assertFalse(user.check_password(password))

        with self.assertRaises(User.DoesNotExist):
            models.new_user(username='', email='')
            User.reset_password('')
Exemplo n.º 12
0
    def test_reset_password(self):
        '''It should correctly reset a user's password or raise an error.'''
        password = '******'
        user = TestUsers.new_user(username='******', password=password)
        self.assertTrue(user.check_password(password))

        User.reset_password('lololol')
        user = User.objects.get(id=user.id)
        self.assertFalse(user.check_password(password))

        with self.assertRaises(User.DoesNotExist):
            TestUsers.new_user(username='', email='')
            User.reset_password('')
Exemplo n.º 13
0
    def test_authenticate(self):
        '''It should correctly authenticate and return a user, or return an
        error message.'''
        kunal = User.objects.create(username='******', email='*****@*****.**')
        kunal.set_password('mehta')
        kunal.is_active = False
        kunal.save()

        def assert_raises(username, password, message):
            with self.assertRaises(AuthenticationError):
                try:
                    User.authenticate(username, password)
                except AuthenticationError as e:
                    self.assertEqual(str(e), message)
                    raise

        assert_raises('kunal', '', AuthenticationError.MISSING_FIELDS)
        assert_raises('', 'mehta', AuthenticationError.MISSING_FIELDS)
        assert_raises('kunal', 'm', AuthenticationError.INVALID_CREDENTIALS)
        assert_raises('k', 'mehta', AuthenticationError.INVALID_CREDENTIALS)
        assert_raises('kunal', 'mehta', AuthenticationError.INACTIVE_ACCOUNT)

        kunal.is_active = True
        kunal.save();

        user = User.authenticate('kunal', 'mehta')
        self.assertEqual(user, kunal)
Exemplo n.º 14
0
    def test_authenticate(self):
        '''It should correctly authenticate and return a user, or return an
        error message.'''
        kunal = User.objects.create(username='******', email='*****@*****.**')
        kunal.set_password('mehta')
        kunal.is_active = False
        kunal.save()

        def assert_raises(username, password, message):
            with self.assertRaises(AuthenticationError):
                try:
                    User.authenticate(username, password)
                except AuthenticationError as e:
                    self.assertEqual(str(e), message)
                    raise

        assert_raises('kunal', '', AuthenticationError.MISSING_FIELDS)
        assert_raises('', 'mehta', AuthenticationError.MISSING_FIELDS)
        assert_raises('kunal', 'm', AuthenticationError.INVALID_CREDENTIALS)
        assert_raises('k', 'mehta', AuthenticationError.INVALID_CREDENTIALS)
        assert_raises('kunal', 'mehta',
                      AuthenticationError.INVALID_CREDENTIALS)

        kunal.is_active = True
        kunal.save()

        user = User.authenticate('kunal', 'mehta')
        self.assertEqual(user, kunal)
Exemplo n.º 15
0
def new_user(**kwargs):
    username = kwargs.pop('username', None) or str(uuid.uuid4())
    u = User(username=username, email=kwargs.pop('email', '*****@*****.**'))

    password = kwargs.pop('password', 'test')
    u.set_password(password)
    u.PASSWORD_FOR_TESTS_ONLY = password

    u.first_name = kwargs.pop('first_name', 'Test')
    u.last_name = kwargs.pop('last_name', 'User')

    for attr, value in kwargs.items():
        setattr(u, attr, value)

    u.save()
    return u
Exemplo n.º 16
0
    def new_user(**kwargs):
        u = User(username=kwargs.pop('username', 'testuser'),
                 email=kwargs.pop('email', '*****@*****.**'))
        u.set_password(kwargs.pop('password', 'test'))

        u.first_name = kwargs.pop('first_name', 'Test')
        u.last_name = kwargs.pop('last_name', 'User')

        for attr, value in kwargs.items():
            setattr(u, attr, value)

        u.save()
        return u
Exemplo n.º 17
0
    def new_user(**kwargs):
        u = User(username=kwargs.pop("username", "testuser"), email=kwargs.pop("email", "*****@*****.**"))
        u.set_password(kwargs.pop("password", "test"))

        u.first_name = kwargs.pop("first_name", "Test")
        u.last_name = kwargs.pop("last_name", "User")

        for attr, value in kwargs.items():
            setattr(u, attr, value)

        u.save()
        return u
Exemplo n.º 18
0
    def post(self, request, *args, **kwargs):
        '''Log in a new user.'''
        if request.user.is_authenticated():
            raise PermissionDenied('Another user is currently logged in.')

        try:
            data = request.DATA
            user = User.authenticate(data['username'], data['password'])
        except AuthenticationError as e:
            raise AuthenticationFailed(str(e))

        login(request, user)
        return Response(UserSerializer(user).data,
                        status=status.HTTP_201_CREATED)
Exemplo n.º 19
0
    def post(self, request, *args, **kwargs):
        '''Log in a new user.'''
        if request.user.is_authenticated():
            raise PermissionDenied('Another user is currently logged in.')

        try:
            data = request.data
            user = User.authenticate(data['username'], data['password'])
        except AuthenticationError as e:
            raise AuthenticationFailed(str(e))

        login(request, user)
        return Response(UserSerializer(user).data,
                        status=status.HTTP_201_CREATED)
Exemplo n.º 20
0
    def new_user(**kwargs):
        u = User(username=kwargs.pop('username', 'testuser'),
                 email=kwargs.pop('email', '*****@*****.**'))
        u.set_password(kwargs.pop('password', 'test'))

        u.first_name = kwargs.pop('first_name', 'Test')
        u.last_name = kwargs.pop('last_name', 'User')

        for attr, value in kwargs.items():
            setattr(u, attr, value)

        u.save()
        return u