<commit_before># This file is part of the GOsa framework.
#
#  http://gosa-project.org
#
# Copyright:
#  (C) 2016 GONICUS GmbH, Germany, http://www.gonicus.de
#
# See the LICENSE file in the project's top-level directory for details.

from unittest import mock
from gosa.backend.objects import ObjectFactory
from tests.GosaTestCase import *
from gosa.backend.objects.object import *


@slow
class ObjectTestCase(TestCase):

    def test_listProperties(self):
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        res = obj.listProperties()
        # just test if something is there
        assert 'uid' in res
        assert 'gender' in res

    def test_getProperties(self):
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        res = obj.getProperties()
        # just test if something is there
        assert res['uid']['value'][0] == "freich"

    def test_listMethods(self):
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        res = obj.listMethods()
        # just test if something is there
        assert 'lock' in res
        assert 'unlock' in res

    def test_listMethods2(self):
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        res = obj.listMethods()
        # just test if something is there
        assert obj.hasattr('uid') is True
        assert obj.hasattr('gender') is True

    def test_getTemplate(self):
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        res = obj.getTemplate()
        assert res[0][0:3] == "{\n "

    def test_getNamedTemplate(self):
        assert Object.getNamedTemplate({}, []) == []

        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        res = Object.getNamedTemplate(obj.env, obj._templates)
        assert res[0][0:3] == "{\n "

        with mock.patch('os.path.exists', return_value=False):
            assert Object.getNamedTemplate(obj.env, obj._templates) is None

    def test_getAttrType(self):
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        assert obj.getAttrType('uid') == "String"

        with pytest.raises(AttributeError):
            obj.getAttrType('unknown')

    def test_attributes(self):
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        assert obj.gender == 'M'

        # just test the change
        obj.gender = 'F'
        assert obj.gender == 'F'

        # cannot delete mandatory attribute
        with pytest.raises(AttributeError):
            del obj.uid
        # cannot delete readonly attribute
        with pytest.raises(AttributeError):
            del obj.passwordMethod
        # cannot delete blockedBy attribute
        with pytest.raises(AttributeError):
            del obj.displayName
        # cannot delete unknown attribute
        with pytest.raises(AttributeError):
            del obj.unknown
        # cannot get unknown attribute
        with pytest.raises(AttributeError):
            obj.unknown

        del obj.gender
        assert obj.gender is None

        # try to set some wrong types
        with pytest.raises(TypeError):
            obj.gender = True
        with pytest.raises(TypeError):
            obj.gender = "T"

        obj.gender = 'M'
        assert obj.gender == 'M'

        # delete by setting None
        obj.gender = None
        assert obj.gender is None

        # cannot change readonly attribute
        with pytest.raises(AttributeError):
            obj.passwordMethod = 'MD5'
        # cannot change blockedBy attribute
        with pytest.raises(AttributeError):
            obj.displayName = "Name"
        # cannot change unknown attribute
        with pytest.raises(AttributeError):
            obj.unknown = "Test"
        # multivalue (no list)
        with pytest.raises(TypeError):
            obj.telephoneNumber = "0123456789"
        # validation error
        with pytest.raises(ValueError):
            obj.telephoneNumber = ["wrong"]

    def test_check(self):
        # wrong mode for base object
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a', 'delete')
        with pytest.raises(ObjectException):
            obj.check()

        # remove a non base object
        obj = ObjectFactory.getInstance().getObject('PosixUser', '78475884-c7f2-1035-8262-f535be14d43a', 'remove')
        with pytest.raises(ObjectException):
            obj.check()

        # update base object
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        res = obj.check()
        # just test if something is there
        assert 'uid' in res
        assert 'gender' in res

        # create new object with wrong dn
        obj = ObjectFactory.getInstance().getObject('User', 'cn=Test User,ou=people,dc=example,dc=net', 'create')
        with pytest.raises(ObjectException):<commit_msg>do not test gender as has been removed from User object declaration<commit_after># This file is part of the GOsa framework.
#
#  http://gosa-project.org
#
# Copyright:
#  (C) 2016 GONICUS GmbH, Germany, http://www.gonicus.de
#
# See the LICENSE file in the project's top-level directory for details.

from unittest import mock
from gosa.backend.objects import ObjectFactory
from tests.GosaTestCase import *
from gosa.backend.objects.object import *


@slow
class ObjectTestCase(TestCase):

    def test_listProperties(self):
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        res = obj.listProperties()
        # just test if something is there
        assert 'uid' in res

    def test_getProperties(self):
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        res = obj.getProperties()
        # just test if something is there
        assert res['uid']['value'][0] == "freich"

    def test_listMethods(self):
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        res = obj.listMethods()
        # just test if something is there
        assert 'lock' in res
        assert 'unlock' in res

    def test_listMethods2(self):
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        res = obj.listMethods()
        # just test if something is there
        assert obj.hasattr('uid') is True

    def test_getTemplate(self):
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        res = obj.getTemplate()
        assert res[0][0:3] == "{\n "

    def test_getNamedTemplate(self):
        assert Object.getNamedTemplate({}, []) == []

        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        res = Object.getNamedTemplate(obj.env, obj._templates)
        assert res[0][0:3] == "{\n "

        with mock.patch('os.path.exists', return_value=False):
            assert Object.getNamedTemplate(obj.env, obj._templates) is None

    def test_getAttrType(self):
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        assert obj.getAttrType('uid') == "String"

        with pytest.raises(AttributeError):
            obj.getAttrType('unknown')

    def test_attributes(self):
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        assert obj.uid == 'freich'

        # just test the change
        obj.uid = 'test'
        assert obj.uid == 'test'

        # cannot delete mandatory attribute
        with pytest.raises(AttributeError):
            del obj.uid
        # cannot delete readonly attribute
        with pytest.raises(AttributeError):
            del obj.passwordMethod
        # cannot delete blockedBy attribute
        with pytest.raises(AttributeError):
            del obj.displayName
        # cannot delete unknown attribute
        with pytest.raises(AttributeError):
            del obj.unknown
        # cannot get unknown attribute
        with pytest.raises(AttributeError):
            obj.unknown

        del obj.dateOfBirth
        assert obj.dateOfBirth is None

        # cannot change readonly attribute
        with pytest.raises(AttributeError):
            obj.passwordMethod = '<PASSWORD>'
        # cannot change blockedBy attribute
        with pytest.raises(AttributeError):
            obj.displayName = "Name"
        # cannot change unknown attribute
        with pytest.raises(AttributeError):
            obj.unknown = "Test"
        # multivalue (no list)
        with pytest.raises(TypeError):
            obj.telephoneNumber = "0123456789"
        # validation error
        with pytest.raises(ValueError):
            obj.telephoneNumber = ["wrong"]

    def test_check(self):
        # wrong mode for base object
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a', 'delete')
        with pytest.raises(ObjectException):
            obj.check()

        # remove a non base object
        obj = ObjectFactory.getInstance().getObject('PosixUser', '78475884-c7f2-1035-8262-f535be14d43a', 'remove')
        with pytest.raises(ObjectException):
            obj.check()

        # update base object
        obj = ObjectFactory.getInstance().getObject('User', '78475884-c7f2-1035-8262-f535be14d43a')
        res = obj.check()
        # just test if something is there
        assert 'uid' in res

        # create new object with wrong dn
        obj = ObjectFactory.getInstance().getObject('User', 'cn=Test User,ou=people,dc=example,dc=net', 'create')
        with pytest.raises(ObjectException):
{'fraction_non_alphanumeric': 0.10857142857142857, 'fraction_numerical': 0.05964912280701754, 'mean_word_length': 2.596250901225667, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3010794', 'n_tokens_mistral': 3274, 'n_tokens_neox': 2966, 'n_words': 695}