Esempio n. 1
0
 def test_duplicate_user(self, mock_f, mock_model):
     """
     Test handling of Mongoengine's NotUniqueError.
     """
     mock_user = mock_model.return_value
     mock_user.save.side_effect = NotUniqueError()
     try:
         user_controller.create_user("new_user")
     except pulp_exceptions.DuplicateResource, e:
         pass
Esempio n. 2
0
 def test_duplicate_user(self, mock_f, mock_model):
     """
     Test handling of Mongoengine's NotUniqueError.
     """
     mock_user = mock_model.return_value
     mock_user.save.side_effect = NotUniqueError()
     try:
         user_controller.create_user('new_user')
     except pulp_exceptions.DuplicateResource, e:
         pass
Esempio n. 3
0
 def _create_user(self):
     username = ''.join(random.sample(self.alpha_num, random.randint(6,
                                                                     10)))
     password = ''.join(random.sample(self.alpha_num, random.randint(6,
                                                                     10)))
     return user_controller.create_user(login=username,
                                        password=password,
                                        name=username)
Esempio n. 4
0
 def test_as_expected(self, mock_f, mock_model):
     """
     Test the creatation of a new users that works as expected.
     """
     mock_perm_manager = mock_f.permission_manager()
     user = user_controller.create_user("curiosity", password="******")
     mock_perm_manager.grant_automatic_permissions_for_user.assert_called_once_with(user.login)
     user.set_password.assert_called_once_with("pahump_hills")
     self.assertTrue(user is mock_model.return_value)
Esempio n. 5
0
 def test_as_expected(self, mock_f, mock_model):
     """
     Test the creatation of a new users that works as expected.
     """
     mock_perm_manager = mock_f.permission_manager()
     user = user_controller.create_user('curiosity',
                                        password='******')
     mock_perm_manager.grant_automatic_permissions_for_user.assert_called_once_with(
         user.login)
     user.set_password.assert_called_once_with('pahump_hills')
     self.assertTrue(user is mock_model.return_value)
Esempio n. 6
0
    def post(self, request):
        """
        Create a new user.

        :param request: WSGI request object
        :type request: django.core.handlers.wsgi.WSGIRequest

        :return: Response containing the user
        :rtype: django.http.HttpResponse

        :raises: MissingValue if login field is missing
        :raises: InvalidValue if some parameters are invalid
        """
        user_data = request.body_as_json
        login = user_data.pop('login', None)
        if login is None:
            raise pulp_exceptions.MissingValue(['login'])
        password = user_data.pop('password', None)

        # name defaults to login
        name = user_data.pop('name', login)

        # Raise if extra data is passed
        if user_data:
            raise pulp_exceptions.InvalidValue(user_data.keys())

        new_user = user_controller.create_user(login,
                                               password=password,
                                               name=name)
        serialized_user = model.User.SERIALIZER(new_user).data

        # For backwards compatability. See https://pulp.plan.io/issues/1125
        serialized_user['id'] = str(serialized_user['_id'])

        # Grant permissions
        permission_manager = factory.permission_manager()
        permission_manager.grant_automatic_permissions_for_resource(
            serialized_user['_href'])

        response = generate_json_response_with_pulp_encoder(serialized_user)
        return generate_redirect_response(response, serialized_user['_href'])
Esempio n. 7
0
    def _add_from_ldap(self, username, userdata):
        """
        @param username:  Username to be added
        @param userdata: tuple of user data as returned by lookup_user

        Adds a user to the pulp user database with no password and
        returns a pulp.server.db.model.User object
        """
        user = model.User.objects(login=username).first()
        if user is None:
            attrs = userdata[1]
            if 'gecos' in attrs and isinstance(attrs['gecos'], basestring):
                name = attrs['gecos']
            else:
                name = username
            user = user_controller.create_user(login=username, name=name)
            if config.has_option('ldap', 'default_role'):
                role_id = config.get('ldap', 'default_role')
                self.role_manager.add_user_to_role(role_id, username)

        return user
Esempio n. 8
0
    def _add_from_ldap(self, username, userdata):
        """
        @param username:  Username to be added
        @param userdata: tuple of user data as returned by lookup_user

        Adds a user to the pulp user database with no password and
        returns a pulp.server.db.model.User object
        """
        user = model.User.objects(login=username).first()
        if user is None:
            attrs = userdata[1]
            if 'gecos' in attrs and isinstance(attrs['gecos'], basestring):
                name = attrs['gecos']
            else:
                name = username
            user = user_controller.create_user(login=username, name=name)
            if config.has_option('ldap', 'default_role'):
                role_id = config.get('ldap', 'default_role')
                self.role_manager.add_user_to_role(role_id, username)

        return user
Esempio n. 9
0
File: users.py Progetto: alanoe/pulp
    def post(self, request):
        """
        Create a new user.

        :param request: WSGI request object
        :type request: django.core.handlers.wsgi.WSGIRequest

        :return: Response containing the user
        :rtype: django.http.HttpResponse

        :raises: MissingValue if login field is missing
        :raises: InvalidValue if some parameters are invalid
        """
        user_data = request.body_as_json
        login = user_data.pop('login', None)
        if login is None:
            raise pulp_exceptions.MissingValue(['login'])
        password = user_data.pop('password', None)

        # name defaults to login
        name = user_data.pop('name', login)

        # Raise if extra data is passed
        if user_data:
            raise pulp_exceptions.InvalidValue(user_data.keys())

        new_user = user_controller.create_user(login, password=password, name=name)
        serialized_user = model.User.SERIALIZER(new_user).data

        # For backwards compatability. See https://pulp.plan.io/issues/1125
        serialized_user['id'] = str(serialized_user['_id'])

        # Grant permissions
        permission_manager = factory.permission_manager()
        permission_manager.grant_automatic_permissions_for_resource(serialized_user['_href'])

        response = generate_json_response_with_pulp_encoder(serialized_user)
        return generate_redirect_response(response, serialized_user['_href'])
Esempio n. 10
0
 def _create_user(self):
     username = ''.join(random.sample(self.alpha_num, random.randint(6, 10)))
     password = ''.join(random.sample(self.alpha_num, random.randint(6, 10)))
     return user_controller.create_user(login=username, password=password, name=username)