def test_maj_etab(self, ldap: Ldap, db: Database, config: Config):
        ldap_utils.run_ldif('data/default-structures.ldif', ldap)
        ldap_utils.run_ldif('data/default-personnes-short.ldif', ldap)
        ldap_utils.run_ldif('data/default-groups.ldif', ldap)
        db_utils.run_script('data/default-context.sql', db, connect=False)

        synchronizer = Synchronizer(ldap, db, config)
        synchronizer.initialize()
        structure = ldap.get_structure("0290009C")
        assert structure is not None
        etab_context = synchronizer.handle_etablissement(structure.uai)
        assert etab_context.uai == "0290009C"
        assert etab_context.gere_admin_local is True
        assert etab_context.etablissement_regroupe is False
        assert etab_context.regexp_admin_moodle == "(esco|clg37):admin:Moodle:local:.*_0290009C$"
        assert etab_context.regexp_admin_local == "(esco|clg37):admin:local:.*_0290009C$"
        assert etab_context.etablissement_theme == "0290009c"
        assert etab_context.id_context_categorie is not None
        assert etab_context.id_zone_privee is not None
        assert etab_context.id_context_course_forum is not None

        etablissement_ou = ldap.get_structure("0290009C").nom
        db.mark.execute("SELECT * FROM {entete}course_categories "
                        "WHERE name = %(name)s "
                        "AND theme = %(theme)s".format(entete=db.entete),
                        params={
                            'name': etablissement_ou,
                            'theme': etab_context.uai
                        })
        result = db.mark.fetchone()
        assert result is not None
def test_structures(ldap: Ldap):
    ldap.connect()
    ldap_utils.run_ldif('data/default-structures.ldif', ldap)
    structures = ldap.search_structure()
    assert len(structures) == 2
    for structure in structures:
        assert isinstance(structure, StructureLdap)
        getted_structure = ldap.get_structure(structure.uai)
        assert isinstance(getted_structure, StructureLdap)
    ldap.disconnect()
    def test_eleve_passage_lycee(self, ldap: Ldap, db: Database,
                                 config: Config):
        ldap_utils.run_ldif('data/default-structures.ldif', ldap)
        ldap_utils.run_ldif('data/default-personnes-short.ldif', ldap)
        ldap_utils.run_ldif('data/default-groups.ldif', ldap)
        db_utils.run_script('data/default-context.sql', db, connect=False)

        synchronizer = Synchronizer(ldap, db, config)
        synchronizer.initialize()
        college = ldap.get_structure("0291595B")
        lycee = ldap.get_structure("0290009C")
        eleves = ldap.search_eleve(None, "0291595B")
        eleve = eleves[0]
        college_context = synchronizer.handle_etablissement(college.uai)
        lycee_context = synchronizer.handle_etablissement(lycee.uai)
        synchronizer.handle_eleve(college_context, eleve)

        db.mark.execute(
            "SELECT * FROM {entete}user WHERE username = %(username)s".format(
                entete=db.entete),
            params={'username': str(eleve.uid).lower()})
        result = db.mark.fetchone()
        eleve_id = result[0]
        db.mark.execute(
            "SELECT * FROM {entete}role_assignments WHERE userid = %(userid)s".
            format(entete=db.entete),
            params={'userid': eleve_id})
        roles_results = db.mark.fetchall()
        assert len(roles_results) == 1
        assert roles_results[0][1] == 14

        eleve.uai_courant = "0290009C"
        synchronizer.handle_eleve(lycee_context, eleve)
        db.mark.execute(
            "SELECT * FROM {entete}role_assignments WHERE userid = %(userid)s".
            format(entete=db.entete),
            params={'userid': eleve_id})
        roles_results = db.mark.fetchall()
        assert len(roles_results) == 0
    def test_maj_eleve(self, ldap: Ldap, db: Database, config: Config):
        ldap_utils.run_ldif('data/default-structures.ldif', ldap)
        ldap_utils.run_ldif('data/default-personnes-short.ldif', ldap)
        ldap_utils.run_ldif('data/default-groups.ldif', ldap)
        db_utils.run_script('data/default-context.sql', db, connect=False)

        synchronizer = Synchronizer(ldap, db, config)
        synchronizer.initialize()
        structure = ldap.get_structure("0290009C")
        eleves = ldap.search_eleve(None, "0290009C")
        eleve = eleves[1]
        etab_context = synchronizer.handle_etablissement(structure.uai)
        synchronizer.handle_eleve(etab_context, eleve)

        db.mark.execute(
            "SELECT * FROM {entete}user WHERE username = %(username)s".format(
                entete=db.entete),
            params={'username': str(eleve.uid).lower()})
        result = db.mark.fetchone()
        assert result is not None
        assert result[10] == 'Dorian'
        assert result[12] == '*****@*****.**'
        assert result[27] == '0290009c'
        eleve_id = result[0]
        db.mark.execute(
            "SELECT * FROM {entete}role_assignments WHERE userid = %(userid)s".
            format(entete=db.entete),
            params={'userid': eleve_id})
        roles_results = db.mark.fetchall()
        assert len(roles_results) == 0
        for classe in eleve.classes:
            cohort_name = "Élèves de la Classe %s" % classe
            db.mark.execute(
                "SELECT * FROM {entete}cohort WHERE name = %(name)s".format(
                    entete=db.entete),
                params={'name': cohort_name})
            cohort = db.mark.fetchone()
            cohort_id = cohort[0]
            db.mark.execute(
                "SELECT * FROM {entete}cohort_members WHERE cohortid = %(cohortid)s AND userid = %(userid)s"
                .format(entete=db.entete),
                params={
                    'cohortid': cohort_id,
                    'userid': eleve_id
                })
            result_cohort_enrollment = db.mark.fetchone()
            assert result_cohort_enrollment is not None
            assert result_cohort_enrollment[2] == eleve_id
    def test_maj_enseignant(self, ldap: Ldap, db: Database, config: Config):
        ldap_utils.run_ldif('data/default-structures.ldif', ldap)
        ldap_utils.run_ldif('data/default-personnes-short.ldif', ldap)
        ldap_utils.run_ldif('data/default-groups.ldif', ldap)
        db_utils.run_script('data/default-context.sql', db, connect=False)

        synchronizer = Synchronizer(ldap, db, config)
        synchronizer.initialize()
        structure = ldap.get_structure("0290009C")
        enseignants = ldap.search_enseignant(None, "0290009C")
        enseignant = enseignants[1]
        etab_context = synchronizer.handle_etablissement(structure.uai)
        synchronizer.handle_enseignant(etab_context, enseignant)

        db.mark.execute(
            "SELECT * FROM {entete}user WHERE username = %(username)s".format(
                entete=db.entete),
            params={'username': str(enseignant.uid).lower()})
        result = db.mark.fetchone()
        assert result is not None
        assert result[10] == 'Jules'
        assert result[11] == 'PICARD'
        assert result[12] == '*****@*****.**'
        assert result[27] == '0290009c'
        enseignant_id = result[0]

        db.mark.execute(
            "SELECT * FROM {entete}role_assignments WHERE userid = %(userid)s".
            format(entete=db.entete),
            params={'userid': enseignant_id})
        roles_results = db.mark.fetchall()
        assert len(roles_results) == 3
        assert roles_results[0][1] == 2
        assert roles_results[0][2] == 3
        assert roles_results[1][1] == 2
        assert roles_results[1][2] == 1184277
        assert roles_results[2][1] == 5
        assert roles_results[2][2] == 1184278