<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.

import pytest
from unittest import mock, TestCase
from gosa.backend.plugins.rpc.methods import GOsaException
import gosa
from gosa.common.gjson import loads
from tests.GosaTestCase import slow


@slow
class RpcMethodsTestCase(TestCase):

    def setUp(self):
        super(RpcMethodsTestCase, self).setUp()
        self.rpc = gosa.common.components.PluginRegistry.getInstance("RPCMethods")

    def tearDown(self):
        del self.rpc
        super(RpcMethodsTestCase, self).tearDown()

    def test_getAvailableObjectNames(self):
        res = self.rpc.getAvailableObjectNames()
        assert len(res) > 0
        assert 'Domain' in res
        assert 'SambaUser' in res

    def test_getGuiTemplates(self):

        with pytest.raises(GOsaException):
            self.rpc.getGuiTemplates('UnknownType')

        res = self.rpc.getGuiTemplates('User')
        assert len(res) == 2

    def test_getGuiDialogs(self):

        with pytest.raises(GOsaException):
            self.rpc.getGuiDialogs('UnknownType')

        res = self.rpc.getGuiDialogs('SambaUser')
        assert len(res) == 3


    def test_getTemplateI18N(self):

        res = self.rpc.getTemplateI18N('de')
        assert res['User'] == 'Benutzer'
        assert res['Title'] == 'Titel'

    def test_getUserDetails(self):

        with pytest.raises(GOsaException):
            self.rpc.getUserDetails('unknown')

        res = self.rpc.getUserDetails('admin')
        assert res['sn'] == 'Administrator'
        assert res['givenName'] == 'System'
        assert res['dn'] == 'cn=System Administrator,ou=people,dc=example,dc=net'
        assert res['uuid'] == '7ffcb0f2-b305-1031-916c-47d262a62cc5'

    def test_extensionExists(self):

        with pytest.raises(GOsaException):
            self.rpc.extensionExists('cn=<NAME>,ou=people,dc=example,dc=de', 'PosixUser')

        assert self.rpc.extensionExists('cn=<NAME>,ou=people,dc=example,dc=net', 'PosixUser') is True

    def test_saveUserPreferences(self):
        with pytest.raises(GOsaException):
            self.rpc.saveUserPreferences('unkown', 'description', 'test')

        with mock.patch('gosa.backend.plugins.rpc.methods.ObjectProxy') as m:
            m.return_value.guiPreferences = "{}"
            self.rpc.saveUserPreferences('admin', 'description', 'test')
            assert m.return_value.commit.called

        with mock.patch('gosa.backend.plugins.rpc.methods.ObjectProxy') as m:
            m.return_value.guiPreferences = None
            self.rpc.saveUserPreferences('admin', 'description', 'test')
            assert m.return_value.commit.called

    def test_loadUserPreferences(self):
        with pytest.raises(GOsaException):
            self.rpc.loadUserPreferences('unkown', 'description')

        with mock.patch('gosa.backend.plugins.rpc.methods.ObjectProxy') as m:
            m.return_value.guiPreferences = '{"description": "test"}'
            assert self.rpc.loadUserPreferences('admin', 'description') == "test"
            m.return_value.guiPreferences = ''
            assert self.rpc.loadUserPreferences('admin', 'description') is None<commit_msg>fix tests<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.

import pytest
from unittest import mock, TestCase
from gosa.backend.plugins.rpc.methods import GOsaException
import gosa
from gosa.common.gjson import loads
from tests.GosaTestCase import slow


@slow
class RpcMethodsTestCase(TestCase):

    def setUp(self):
        super(RpcMethodsTestCase, self).setUp()
        self.rpc = gosa.common.components.PluginRegistry.getInstance("RPCMethods")

    def tearDown(self):
        del self.rpc
        super(RpcMethodsTestCase, self).tearDown()

    def test_getAvailableObjectNames(self):
        res = self.rpc.getAvailableObjectNames()
        assert len(res) > 0
        assert 'Domain' in res
        assert 'SambaUser' in res

    def test_getGuiTemplates(self):

        with pytest.raises(GOsaException):
            self.rpc.getGuiTemplates('UnknownType')

        res = self.rpc.getGuiTemplates('User')
        assert len(res) == 2

    def test_getGuiDialogs(self):

        with pytest.raises(GOsaException):
            self.rpc.getGuiDialogs('UnknownType')

        res = self.rpc.getGuiDialogs('SambaUser')
        assert len(res) == 3


    def test_getTemplateI18N(self):

        res = self.rpc.getTemplateI18N('de')
        assert res['User'] == 'Benutzer'
        assert res['Title'] == 'Titel'

    def test_getUserDetails(self):

        with pytest.raises(GOsaException):
            self.rpc.getUserDetails('unknown')

        res = self.rpc.getUserDetails('admin')
        assert res['sn'] == 'Administrator'
        assert res['givenName'] == 'System'
        assert res['dn'] == 'cn=System Administrator,ou=people,dc=example,dc=net'
        assert res['uuid'] == '7ffcb0f2-b305-1031-916c-47d262a62cc5'

    def test_extensionExists(self):

        with pytest.raises(GOsaException):
            self.rpc.extensionExists('cn=<NAME>,ou=people,dc=example,dc=de', 'PosixUser')

        assert self.rpc.extensionExists('cn=<NAME>,ou=people,dc=example,dc=net', 'PosixUser') is True

    def test_saveUserPreferences(self):
        with pytest.raises(GOsaException):
            self.rpc.saveUserPreferences('unkown', 'description', 'test')

        with mock.patch('gosa.backend.plugins.rpc.methods.ObjectProxy') as m:
            m.return_value.guiPreferences = {}
            self.rpc.saveUserPreferences('admin', 'description', 'test')
            assert m.return_value.commit.called

        with mock.patch('gosa.backend.plugins.rpc.methods.ObjectProxy') as m:
            m.return_value.guiPreferences = None
            self.rpc.saveUserPreferences('admin', 'description', 'test')
            assert m.return_value.commit.called

    def test_loadUserPreferences(self):
        with pytest.raises(GOsaException):
            self.rpc.loadUserPreferences('unkown', 'description')

        with mock.patch('gosa.backend.plugins.rpc.methods.ObjectProxy') as m:
            m.return_value.guiPreferences = {"description": "test"}
            assert self.rpc.loadUserPreferences('admin', 'description') == "test"
            m.return_value.guiPreferences = ''
            assert self.rpc.loadUserPreferences('admin', 'description') is None
{'fraction_non_alphanumeric': 0.12264995523724262, 'fraction_numerical': 0.009549388242315727, 'mean_word_length': 3.0871951219512197, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 9, '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': '4550536', 'n_tokens_mistral': 2068, 'n_tokens_neox': 1982, 'n_words': 382}