<commit_before># -*- coding: utf-8 -*-
# pylint: disable=missing-docstring
import logging
from unittest import TestCase

import ldap3

from ldap_sync.action import AddAction, IdleAction, DeleteAction, ModifyAction
from ldap_sync.exporter import LdapExporter, fetch_users_to_sync, \
    fetch_groups_to_sync, \
    fetch_properties_to_sync, get_config, establish_and_return_ldap_connection, \
    fetch_current_ldap_users, fetch_current_ldap_groups, \
    fetch_current_ldap_properties, sync_all
from ldap_sync.record import UserRecord, GroupRecord, RecordState, \
    dn_from_username
from pycroft.model.session import session
from tests import FactoryDataTestBase, UserFactory, MembershipFactory
from tests.factories import PropertyGroupFactory
from tests.factories.user import UserWithMembershipFactory


class ExporterInitializationTestCase(TestCase):
    def setUp(self):
        self.desired_user = UserRecord(dn='user', attrs={})
        self.exporter = LdapExporter(desired=[self.desired_user], current=[])

    def test_one_record_state(self):
        self.assertEqual(len(self.exporter.states_dict), 1)
        state = self.exporter.states_dict[self.desired_user.dn]
        self.assertEqual(state, RecordState(current=None, desired=self.desired_user))


class EmptyLdapTestCase(TestCase):
    def setUp(self):
        self.desired_user = UserRecord(dn='user', attrs={})
        self.exporter = LdapExporter(desired=[self.desired_user], current=[])
        self.exporter.compile_actions()

    def test_one_action_is_add(self):
        self.assertEqual(len(self.exporter.actions), 1)
        action = self.exporter.actions[0]
        self.assertIsInstance(action, AddAction)


class LdapSyncLoggerMutedMixin(object):
    def setUp(self):
        super(LdapSyncLoggerMutedMixin, self).setUp()
        logging.getLogger('ldap_sync').addHandler(logging.NullHandler())


class EmptyDatabaseTestCase(LdapSyncLoggerMutedMixin, FactoryDataTestBase):
    def create_factories(self):
        super().create_factories()
        UserFactory.create_batch(5)

    def setUp(self):
        super().setUp()
        self.session = session

    def test_no_users_fetched(self):
        self.assertEqual(fetch_users_to_sync(self.session), [])


class OneUserFetchTestCase(LdapSyncLoggerMutedMixin, FactoryDataTestBase):
    PROPNAME = 'mail'

    def create_factories(self):
        super().create_factories()
        self.propgroup = PropertyGroupFactory.create(
            name="my propgroup",
            granted={self.PROPNAME, 'ldap_login_enabled'}
        )<commit_msg>migrate `test_ldap_sync` to pytest<commit_after># -*- coding: utf-8 -*-
# pylint: disable=missing-docstring
import logging
from unittest import TestCase

import ldap3
import pytest

from ldap_sync.action import AddAction, IdleAction, DeleteAction, ModifyAction
from ldap_sync.exporter import LdapExporter, fetch_users_to_sync, \
    fetch_groups_to_sync, \
    fetch_properties_to_sync, get_config, establish_and_return_ldap_connection, \
    fetch_current_ldap_users, fetch_current_ldap_groups, \
    fetch_current_ldap_properties, sync_all
from ldap_sync.record import UserRecord, GroupRecord, RecordState, \
    dn_from_username
from pycroft.model.session import session
from tests import FactoryDataTestBase, UserFactory, MembershipFactory
from tests.factories import PropertyGroupFactory
from tests.factories.user import UserWithMembershipFactory


class TestEmptyLdap:
    @pytest.fixture(scope='class')
    def desired_user(self):
        return UserRecord(dn='user', attrs={})

    @pytest.fixture(scope='class')
    def exporter(self, desired_user):
        return LdapExporter(desired=[desired_user], current=[])

    def test_one_record_state(self, exporter, desired_user):
        assert len(exporter.states_dict) == 1
        state = exporter.states_dict[desired_user.dn]
        assert state == RecordState(current=None, desired=desired_user)

    def test_one_action_is_add(self, exporter):
        exporter.compile_actions()
        assert len(exporter.actions) == 1
        assert isinstance(exporter.actions[0], AddAction)


class LdapSyncLoggerMutedMixin(object):
    def setUp(self):
        super(LdapSyncLoggerMutedMixin, self).setUp()
        logging.getLogger('ldap_sync').addHandler(logging.NullHandler())


class EmptyDatabaseTestCase(LdapSyncLoggerMutedMixin, FactoryDataTestBase):
    def create_factories(self):
        super().create_factories()
        UserFactory.create_batch(5)

    def setUp(self):
        super().setUp()
        self.session = session

    def test_no_users_fetched(self):
        self.assertEqual(fetch_users_to_sync(self.session), [])


class OneUserFetchTestCase(LdapSyncLoggerMutedMixin, FactoryDataTestBase):
    PROPNAME = 'mail'

    def create_factories(self):
        super().create_factories()
        self.propgroup = PropertyGroupFactory.create(
            name="my propgroup",
            granted={self.PROPNAME, 'ldap_login_enabled'}
        )
{'fraction_non_alphanumeric': 0.09359309098212493, 'fraction_numerical': 0.002410122514561157, 'mean_word_length': 4.401301518438178, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3932250', 'n_tokens_mistral': 1526, 'n_tokens_neox': 1512, 'n_words': 269}