Example #1
0
    def test_present__creation(self):
        # test=True
        with patch.dict(OPTS, {'test': True}):
            ret = postgres_user.present('foo')
            self.assertEqual(
                ret,
                {'comment': 'User foo is set to be created',
                 'changes': {}, 'name': 'foo', 'result': None}
            )
            self.assertEqual(SALT_STUB['postgres.user_create'].call_count, 0)

        # test=False
        ret = postgres_user.present('foo')
        self.assertEqual(
            ret,
            {'comment': 'The user foo has been created',
             'changes': {'foo': 'Present'}, 'name': 'foo', 'result': True}
        )
        SALT_STUB['postgres.user_create'].assert_called_once_with(username='******',
                                                                  superuser=None,
                                                                  encrypted=True,
                                                                  runas=None,
                                                                  inherit=None,
                                                                  rolepassword=None,
                                                                  port=None,
                                                                  replication=None,
                                                                  host=None,
                                                                  createroles=None,
                                                                  user=None,
                                                                  groups=None,
                                                                  maintenance_db=None,
                                                                  login=None,
                                                                  password=None,
                                                                  createdb=None)
Example #2
0
    def test_present(self):
        """
        Test to ensure that the named user is present
        with the specified privileges.
        """
        name = "frank"

        ret = {"name": name, "changes": {}, "result": False, "comment": ""}

        mock_t = MagicMock(return_value=True)
        mock = MagicMock(return_value=None)
        with patch.dict(
                postgres_user.__salt__,
            {
                "postgres.role_get": mock,
                "postgres.user_create": mock_t
            },
        ):
            with patch.dict(postgres_user.__opts__, {"test": True}):
                comt = "User {0} is set to be created".format(name)
                ret.update({"comment": comt, "result": None})
                self.assertDictEqual(postgres_user.present(name), ret)

            with patch.dict(postgres_user.__opts__, {"test": False}):
                comt = "The user {0} has been created".format(name)
                ret.update({
                    "comment": comt,
                    "result": True,
                    "changes": {
                        name: "Present"
                    }
                })
                self.assertDictEqual(postgres_user.present(name), ret)
Example #3
0
    def test_present__no_update(self):
        # test=True
        with patch.dict(postgres_user.__salt__, {'postgres.role_get': Mock(return_value={
                                                    'can create databases': False,
                                                    'can create roles': False,
                                                    'can login': False,
                                                    'can update system catalogs': False,
                                                    'connections': None,
                                                    'defaults variables': {},
                                                    'expiry time': None,
                                                    'inherits privileges': True,
                                                    'replication': False,
                                                    'superuser': False,
                                                 }),
                                                 'postgres.user_update': MagicMock()}):
            with patch.dict(postgres_user.__opts__, {'test': True}):
                ret = postgres_user.present('foo', login=False, replication=False)
                self.assertEqual(
                    ret,
                    {'comment': 'User foo is already present',
                     'changes': {}, 'name': 'foo', 'result': True}
                )
                self.assertEqual(self.salt_stub['postgres.user_update'].call_count, 0)

            # test=False
            ret = postgres_user.present('foo', login=False, replication=False)
            self.assertEqual(
                ret,
                {'comment': 'User foo is already present',
                 'changes': {}, 'name': 'foo', 'result': True}
            )
            self.assertEqual(self.salt_stub['postgres.user_update'].call_count, 0)
Example #4
0
    def test_present__creation(self):
        # test=True
        with patch.dict(postgres_user.__opts__, {'test': True}):
            ret = postgres_user.present('foo')
            self.assertEqual(
                ret,
                {'comment': 'User foo is set to be created',
                 'changes': {}, 'name': 'foo', 'result': None}
            )
            self.assertEqual(SALT_STUB['postgres.user_create'].call_count, 0)

        # test=False
        ret = postgres_user.present('foo')
        self.assertEqual(
            ret,
            {'comment': 'The user foo has been created',
             'changes': {'foo': 'Present'}, 'name': 'foo', 'result': True}
        )
        SALT_STUB['postgres.user_create'].assert_called_once_with(username='******',
                                                                  superuser=None,
                                                                  encrypted=True,
                                                                  runas=None,
                                                                  inherit=None,
                                                                  rolepassword=None,
                                                                  port=None,
                                                                  replication=None,
                                                                  host=None,
                                                                  createroles=None,
                                                                  user=None,
                                                                  groups=None,
                                                                  maintenance_db=None,
                                                                  login=None,
                                                                  password=None,
                                                                  createdb=None)
Example #5
0
    def test_present(self):
        '''
        Test to ensure that the named user is present
        with the specified privileges.
        '''
        name = 'frank'

        ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}

        mock_t = MagicMock(return_value=True)
        mock = MagicMock(return_value=None)
        with patch.dict(postgres_user.__salt__, {
                'postgres.role_get': mock,
                'postgres.user_create': mock_t
        }):
            with patch.dict(postgres_user.__opts__, {'test': True}):
                comt = ('User {0} is set to be created'.format(name))
                ret.update({'comment': comt, 'result': None})
                self.assertDictEqual(postgres_user.present(name), ret)

            with patch.dict(postgres_user.__opts__, {'test': False}):
                comt = ('The user {0} has been created'.format(name))
                ret.update({
                    'comment': comt,
                    'result': True,
                    'changes': {
                        name: 'Present'
                    }
                })
                self.assertDictEqual(postgres_user.present(name), ret)
Example #6
0
    def test_present__creation(self):
        # test=True
        with patch.dict(
                postgres_user.__salt__,
            {
                "postgres.role_get": Mock(return_value=None),
                "postgres.user_create": MagicMock(),
            },
        ):
            with patch.dict(postgres_user.__opts__, {"test": True}):
                ret = postgres_user.present("foo")
                self.assertEqual(
                    ret,
                    {
                        "comment": "User foo is set to be created",
                        "changes": {},
                        "name": "foo",
                        "result": None,
                    },
                )
                self.assertEqual(
                    postgres_user.__salt__["postgres.user_create"].call_count,
                    0)

            # test=False
            ret = postgres_user.present("foo")
            self.assertEqual(
                ret,
                {
                    "comment": "The user foo has been created",
                    "changes": {
                        "foo": "Present"
                    },
                    "name": "foo",
                    "result": True,
                },
            )
            postgres_user.__salt__[
                "postgres.user_create"].assert_called_once_with(
                    username="******",
                    superuser=None,
                    encrypted=True,
                    runas=None,
                    inherit=None,
                    rolepassword=None,
                    port=None,
                    replication=None,
                    host=None,
                    createroles=None,
                    user=None,
                    groups=None,
                    maintenance_db=None,
                    login=None,
                    password=None,
                    valid_until=None,
                    createdb=None,
                )
Example #7
0
    def test_present__no_update(self):
        # test=True
        with patch.dict(
                postgres_user.__salt__,
            {
                "postgres.role_get":
                Mock(
                    return_value={
                        "can create databases": False,
                        "can create roles": False,
                        "can login": False,
                        "can update system catalogs": False,
                        "connections": None,
                        "defaults variables": {},
                        "expiry time": None,
                        "inherits privileges": True,
                        "replication": False,
                        "superuser": False,
                    }),
                "postgres.user_update":
                MagicMock(),
            },
        ):
            with patch.dict(postgres_user.__opts__, {"test": True}):
                ret = postgres_user.present("foo",
                                            login=False,
                                            replication=False)
                self.assertEqual(
                    ret,
                    {
                        "comment": "User foo is already present",
                        "changes": {},
                        "name": "foo",
                        "result": True,
                    },
                )
                self.assertEqual(
                    postgres_user.__salt__["postgres.user_update"].call_count,
                    0)

            # test=False
            ret = postgres_user.present("foo", login=False, replication=False)
            self.assertEqual(
                ret,
                {
                    "comment": "User foo is already present",
                    "changes": {},
                    "name": "foo",
                    "result": True,
                },
            )
            self.assertEqual(
                postgres_user.__salt__["postgres.user_update"].call_count, 0)
Example #8
0
    def test_present__creation(self):
        # test=True
        with patch.dict(
                postgres_user.__salt__, {
                    'postgres.role_get': Mock(return_value=None),
                    'postgres.user_create': MagicMock()
                }):
            with patch.dict(postgres_user.__opts__, {'test': True}):
                ret = postgres_user.present('foo')
                self.assertEqual(
                    ret, {
                        'comment': 'User foo is set to be created',
                        'changes': {},
                        'name': 'foo',
                        'result': None
                    })
                self.assertEqual(
                    self.salt_stub['postgres.user_create'].call_count, 0)

            # test=False
            ret = postgres_user.present('foo')
            self.assertEqual(
                ret, {
                    'comment': 'The user foo has been created',
                    'changes': {
                        'foo': 'Present'
                    },
                    'name': 'foo',
                    'result': True
                })
            self.salt_stub['postgres.user_create'].assert_called_once_with(
                username='******',
                superuser=None,
                encrypted=True,
                runas=None,
                inherit=None,
                rolepassword=None,
                port=None,
                replication=None,
                host=None,
                createroles=None,
                user=None,
                groups=None,
                maintenance_db=None,
                login=None,
                password=None,
                valid_until=None,
                createdb=None)
Example #9
0
    def test_present__update(self):
        # test=True
        with patch.dict(postgres_user.__salt__, {'postgres.role_get': Mock(return_value={
                                                     'can create databases': False,
                                                     'can create roles': False,
                                                     'can login': False,
                                                     'can update system catalogs': False,
                                                     'connections': None,
                                                     'defaults variables': {},
                                                     'expiry time': None,
                                                     'inherits privileges': True,
                                                     'replication': False,
                                                     'superuser': False,
                                                 }),
                                                 'postgres.user_update': MagicMock()}):
            with patch.dict(postgres_user.__opts__, {'test': True}):
                ret = postgres_user.present('foo', login=True, replication=False)
                self.assertEqual(
                    ret,
                    {'comment': 'User foo is set to be updated',
                     'changes': {'foo': {'login': True}}, 'name': 'foo', 'result': None}
                )
                self.assertEqual(self.salt_stub['postgres.user_update'].call_count, 0)

            # test=False
            ret = postgres_user.present('foo', login=True, replication=False)
            self.assertEqual(
                ret,
                {'comment': 'The user foo has been updated',
                 'changes': {'foo': {'login': True}}, 'name': 'foo', 'result': True}
            )
            self.salt_stub['postgres.user_update'].assert_called_once_with(username='******',
                                                                           superuser=None,
                                                                           encrypted=True,
                                                                           runas=None,
                                                                           inherit=None,
                                                                           rolepassword=None,
                                                                           port=None,
                                                                           replication=False,
                                                                           host=None,
                                                                           createroles=None,
                                                                           user=None,
                                                                           groups=None,
                                                                           maintenance_db=None,
                                                                           login=True,
                                                                           password=None,
                                                                           valid_until=None,
                                                                           createdb=None)
Example #10
0
def test_present_create_md5_password_default_encrypted(mocks, monkeypatch,
                                                       md5_pw, db_args):
    monkeypatch.setattr(postgres, "_DEFAULT_PASSWORDS_ENCRYPTION", True)

    assert postgres_user.present("username", password="******") == {
        "name": "username",
        "result": True,
        "changes": {
            "username": "******"
        },
        "comment": "The user username has been created",
    }
    mocks["postgres.role_get"].assert_called_once()
    mocks["postgres.user_create"].assert_called_once_with(username="******",
                                                          createdb=None,
                                                          createroles=None,
                                                          encrypted=True,
                                                          superuser=None,
                                                          login=None,
                                                          inherit=None,
                                                          replication=None,
                                                          rolepassword=md5_pw,
                                                          valid_until=None,
                                                          groups=None,
                                                          **db_args)
    mocks["postgres.user_update"].assert_not_called()
Example #11
0
def test_present_scram_to_md5(mocks, existing_user, scram_pw, md5_pw, db_args):
    existing_user["password"] = scram_pw
    mocks["postgres.role_get"].return_value = existing_user

    assert postgres_user.present("username",
                                 password="******",
                                 encrypted="md5") == {
                                     "name": "username",
                                     "result": True,
                                     "changes": {
                                         "username": {
                                             "password": True
                                         }
                                     },
                                     "comment":
                                     "The user username has been updated",
                                 }
    mocks["postgres.role_get"].assert_called_once()
    mocks["postgres.user_create"].assert_not_called()
    mocks["postgres.user_update"].assert_called_once_with(username="******",
                                                          createdb=None,
                                                          createroles=None,
                                                          encrypted="md5",
                                                          superuser=None,
                                                          login=None,
                                                          inherit=None,
                                                          replication=None,
                                                          rolepassword=md5_pw,
                                                          valid_until=None,
                                                          groups=None,
                                                          **db_args)
Example #12
0
def test_present_create_unused_default_password(mocks, md5_pw, db_args):
    assert postgres_user.present("username",
                                 password="******",
                                 default_password="******",
                                 encrypted=True) == {
                                     "name": "username",
                                     "result": True,
                                     "changes": {
                                         "username": "******"
                                     },
                                     "comment":
                                     "The user username has been created",
                                 }
    mocks["postgres.role_get"].assert_called_once()
    mocks["postgres.user_create"].assert_called_once_with(username="******",
                                                          createdb=None,
                                                          createroles=None,
                                                          encrypted=True,
                                                          superuser=None,
                                                          login=None,
                                                          inherit=None,
                                                          replication=None,
                                                          rolepassword=md5_pw,
                                                          valid_until=None,
                                                          groups=None,
                                                          **db_args)
    mocks["postgres.user_update"].assert_not_called()
Example #13
0
    def test_present__no_update(self):
        # test=True
        with patch.dict(OPTS, {'test': True}):
            ret = postgres_user.present('foo', login=False, replication=False)
            self.assertEqual(
                ret,
                {'comment': 'User foo is already present',
                 'changes': {}, 'name': 'foo', 'result': True}
            )
            self.assertEqual(SALT_STUB['postgres.user_update'].call_count, 0)

        # test=False
        ret = postgres_user.present('foo', login=False, replication=False)
        self.assertEqual(
            ret,
            {'comment': 'User foo is already present',
             'changes': {}, 'name': 'foo', 'result': True}
        )
        self.assertEqual(SALT_STUB['postgres.user_update'].call_count, 0)
Example #14
0
    def test_present__no_update(self):
        # test=True
        with patch.dict(OPTS, {'test': True}):
            ret = postgres_user.present('foo', login=False, replication=False)
            self.assertEqual(
                ret,
                {'comment': 'User foo is already present',
                 'changes': {}, 'name': 'foo', 'result': True}
            )
            self.assertEqual(SALT_STUB['postgres.user_update'].call_count, 0)

        # test=False
        ret = postgres_user.present('foo', login=False, replication=False)
        self.assertEqual(
            ret,
            {'comment': 'User foo is already present',
             'changes': {}, 'name': 'foo', 'result': True}
        )
        self.assertEqual(SALT_STUB['postgres.user_update'].call_count, 0)
Example #15
0
    def test_present(self):
        """
        Test to ensure that the named user is present
        with the specified privileges.
        """
        name = "frank"

        ret = {"name": name, "changes": {}, "result": False, "comment": ""}

        mock_t = MagicMock(return_value=True)
        mock = MagicMock(return_value=None)
        with patch.dict(postgres_user.__salt__, {"postgres.role_get": mock, "postgres.user_create": mock_t}):
            with patch.dict(postgres_user.__opts__, {"test": True}):
                comt = "User {0} is set to be created".format(name)
                ret.update({"comment": comt, "result": None})
                self.assertDictEqual(postgres_user.present(name), ret)

            with patch.dict(postgres_user.__opts__, {"test": False}):
                comt = "The user {0} has been created".format(name)
                ret.update({"comment": comt, "result": True, "changes": {name: "Present"}})
                self.assertDictEqual(postgres_user.present(name), ret)
Example #16
0
def test_present_create_basic_test(mocks, db_args):
    assert postgres_user.present("username") == {
        "name": "username",
        "result": None,
        "changes": {},
        "comment": "User username is set to be created",
    }
    mocks["postgres.role_get"].assert_called_once_with("username",
                                                       return_password=True,
                                                       **db_args)
    mocks["postgres.user_create"].assert_not_called()
    mocks["postgres.user_update"].assert_not_called()
Example #17
0
def test_present_exists_basic(mocks, existing_user, db_args):
    mocks["postgres.role_get"].return_value = existing_user

    assert postgres_user.present("username") == {
        "name": "username",
        "result": True,
        "changes": {},
        "comment": "User username is already present",
    }
    mocks["postgres.role_get"].assert_called_once_with("username",
                                                       return_password=True,
                                                       **db_args)
    mocks["postgres.user_create"].assert_not_called()
    mocks["postgres.user_update"].assert_not_called()
Example #18
0
def test_present_create_basic_error(mocks, db_args):
    mocks["postgres.user_create"].return_value = False

    assert postgres_user.present("username") == {
        "name": "username",
        "result": False,
        "changes": {},
        "comment": "Failed to create user username",
    }
    mocks["postgres.role_get"].assert_called_once_with("username",
                                                       return_password=True,
                                                       **db_args)
    mocks["postgres.user_create"].assert_called_once()
    mocks["postgres.user_update"].assert_not_called()
Example #19
0
def test_present_md5_matches_prehashed(mocks, existing_user, md5_pw):
    mocks["postgres.role_get"].return_value = existing_user

    assert postgres_user.present("username", password=md5_pw,
                                 encrypted=True) == {
                                     "name": "username",
                                     "result": True,
                                     "changes": {},
                                     "comment":
                                     "User username is already present",
                                 }
    mocks["postgres.role_get"].assert_called_once()
    mocks["postgres.user_create"].assert_not_called()
    mocks["postgres.user_update"].assert_not_called()
Example #20
0
def test_present_scram_matches(mocks, existing_user, scram_pw):
    existing_user["password"] = scram_pw
    mocks["postgres.role_get"].return_value = existing_user

    assert postgres_user.present("username",
                                 password="******",
                                 encrypted="scram-sha-256") == {
                                     "name": "username",
                                     "result": True,
                                     "changes": {},
                                     "comment":
                                     "User username is already present",
                                 }
    mocks["postgres.role_get"].assert_called_once()
    mocks["postgres.user_create"].assert_not_called()
    mocks["postgres.user_update"].assert_not_called()
Example #21
0
def test_present_existing_default_password(mocks, existing_user):
    mocks["postgres.role_get"].return_value = existing_user

    assert postgres_user.present("username",
                                 default_password="******",
                                 encrypted=True,
                                 refresh_password=True) == {
                                     "name": "username",
                                     "result": True,
                                     "changes": {},
                                     "comment":
                                     "User username is already present",
                                 }
    mocks["postgres.role_get"].assert_called_once()
    mocks["postgres.user_create"].assert_not_called()
    mocks["postgres.user_update"].assert_not_called()
Example #22
0
def test_present_update_error(mocks, existing_user):
    existing_user["password"] = "******"
    mocks["postgres.role_get"].return_value = existing_user
    mocks["postgres.user_update"].return_value = False

    assert postgres_user.present("username",
                                 password="******",
                                 encrypted=True) == {
                                     "name": "username",
                                     "result": False,
                                     "changes": {},
                                     "comment":
                                     "Failed to update user username",
                                 }
    mocks["postgres.role_get"].assert_called_once()
    mocks["postgres.user_create"].assert_not_called()
    mocks["postgres.user_update"].assert_called_once()
Example #23
0
    def test_present__update(self):
        # test=True
        with patch.dict(
                postgres_user.__salt__,
            {
                "postgres.role_get":
                Mock(
                    return_value={
                        "can create databases": False,
                        "can create roles": False,
                        "can login": False,
                        "can update system catalogs": False,
                        "connections": None,
                        "defaults variables": {},
                        "expiry time": None,
                        "inherits privileges": True,
                        "replication": False,
                        "superuser": False,
                    }),
                "postgres.user_update":
                MagicMock(),
            },
        ):
            with patch.dict(postgres_user.__opts__, {"test": True}):
                ret = postgres_user.present("foo",
                                            login=True,
                                            replication=False)
                self.assertEqual(
                    ret,
                    {
                        "comment": "User foo is set to be updated",
                        "changes": {
                            "foo": {
                                "login": True
                            }
                        },
                        "name": "foo",
                        "result": None,
                    },
                )
                self.assertEqual(
                    self.salt_stub["postgres.user_update"].call_count, 0)

            # test=False
            ret = postgres_user.present("foo", login=True, replication=False)
            self.assertEqual(
                ret,
                {
                    "comment": "The user foo has been updated",
                    "changes": {
                        "foo": {
                            "login": True
                        }
                    },
                    "name": "foo",
                    "result": True,
                },
            )
            self.salt_stub["postgres.user_update"].assert_called_once_with(
                username="******",
                superuser=None,
                encrypted=True,
                runas=None,
                inherit=None,
                rolepassword=None,
                port=None,
                replication=False,
                host=None,
                createroles=None,
                user=None,
                groups=None,
                maintenance_db=None,
                login=True,
                password=None,
                valid_until=None,
                createdb=None,
            )