Exemplo n.º 1
0
    def test_handle_add_user(self, stdout):
        """Test handle_add_user function"""
        username = '******'
        user_id = 1001
        principal = 'krb-pricipal'

        # Case 1. no argument error
        expected = self.format_error_message(
            "You must specify the username of the user to add")
        self.assert_system_exit(handle_add_user,
                                self.options,
                                self.session, [],
                                stdout='',
                                stderr=expected,
                                exit_code=2,
                                activate_session=None)
        self.activate_session_mock.assert_not_called()
        self.activate_session_mock.reset_mock()

        # Case 2. Too many argument error
        expected = self.format_error_message(
            "This command only accepts one argument (username)")
        self.assert_system_exit(handle_add_user,
                                self.options,
                                self.session, ['user-1', 'user-2', 'user-3'],
                                stdout='',
                                stderr=expected,
                                exit_code=2,
                                activate_session=None)
        self.activate_session_mock.assert_not_called()
        self.activate_session_mock.reset_mock()

        # Case 3. Add user test
        expected = "Added user %s (%i)" % (username, user_id) + "\n"
        arguments = [username, '--principal', principal]
        self.session.createUser.return_value = user_id
        handle_add_user(self.options, self.session, arguments)
        self.session.createUser.assert_called_with(username,
                                                   status=0,
                                                   krb_principal=principal)
        self.assert_console_message(stdout, expected)
        self.activate_session_mock.assert_called_with(self.session,
                                                      self.options)
        self.activate_session_mock.reset_mock()

        # Case 3. Add blocked user
        arguments = [username, '--principal', principal, '--disable']
        handle_add_user(self.options, self.session, arguments)
        self.session.createUser.assert_called_with(
            username,
            status=1,  # 0: normal, 1: disabled
            krb_principal=principal)
        self.assert_console_message(stdout, expected)
        self.activate_session_mock.assert_called_with(self.session,
                                                      self.options)
Exemplo n.º 2
0
    def test_handle_add_user(
            self,
            activate_session_mock,
            stdout):
        """Test handle_add_user function"""
        session = mock.MagicMock()
        options = mock.MagicMock()
        username = '******'
        user_id = 1001
        principal = 'krb-pricipal'

        # Case 1. no argument error
        expected = self.format_error_message(
            "You must specify the username of the user to add")
        self.assert_system_exit(
            handle_add_user,
            options,
            session,
            [],
            stderr=expected,
            activate_session=None)

        # Case 2. Too many argument error
        expected = self.format_error_message(
            "This command only accepts one argument (username)")
        self.assert_system_exit(
            handle_add_user,
            options,
            session,
            ['user-1', 'user-2', 'user-3'],
            stderr=expected,
            activate_session=None)

        # Case 3. Add user test
        expected = "Added user %s (%i)" % (username, user_id) + "\n"
        arguments = [username, '--principal', principal]
        session.createUser.return_value = user_id
        handle_add_user(options, session, arguments)
        session.createUser.assert_called_with(
            username,
            status=0,
            krb_principal=principal)
        self.assert_console_message(stdout, expected)
        activate_session_mock.assert_called_with(session, options)

        # Case 3. Add blocked user
        arguments = [username, '--principal', principal, '--disable']
        handle_add_user(options, session, arguments)
        session.createUser.assert_called_with(
            username,
            status=1,  # 0: normal, 1: disabled
            krb_principal=principal)
        self.assert_console_message(stdout, expected)
        activate_session_mock.assert_called_with(session, options)