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 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.º 4
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.º 5
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.º 6
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)