Beispiel #1
0
def test_structures(structure_repo, db_session: scoped_session):
    universite = Structure(nom="SU", type_name=UN.name)
    structure_repo.put(universite)

    result = sg_get_structure(universite.id)
    expected = {
        "_depth": -1,
        "active": True,
        "ancestors": [],
        "can_be_deleted": True,
        "children": [],
        "dn": "",
        "email": "",
        "id": universite.id,
        "is_reelle": True,
        "nom": "SU",
        "old_dn": "",
        "old_id": None,
        "parents": [],
        "permettre_reponse_directe": True,
        "permettre_soummission_directe": False,
        "permissions": {},
        "sigle": "",
        "supann_code_entite": None,
        "type_name": "Université",
    }
    assert result == expected
Beispiel #2
0
def test_contacts(structure_repo, profile_repo, db_session: scoped_session):
    universite = Structure(nom="SU", type_name=UN.name)
    structure_repo.put(universite)

    result = get_contacts(universite.id)
    assert len(result) == 8

    user = Profile(uid="toto")
    profile_repo.put(user)

    contacts = {ct.name: "" for ct in ContactType}
    update_contacts(universite.id, contacts)
    result = get_contacts(universite.id)
    assert len(result) == 8
    assert glom(result, ["uid"]) == [None] * 8

    contacts["CONTACT_ENTREPRISES"] = user.uid
    update_contacts(universite.id, contacts)
    result = get_contacts(universite.id)
    assert len(result) == 8

    result1 = glom(result, ["uid"])
    assert result1[0] == "toto"
    assert result1[1:8] == [None] * 7

    contacts = {ct.name: user.uid for ct in ContactType}
    update_contacts(universite.id, contacts)
    result = get_contacts(universite.id)
    assert len(result) == 8
    assert glom(result, ["uid"]) == ["toto"] * 8
Beispiel #3
0
def test_contact_service(db_session: scoped_session):
    # # TODO: remove
    # profile_repo.clear()
    # structure_repo.clear()
    # contact_service.clear()

    assert profile_repo.is_empty()
    assert structure_repo.is_empty()
    assert contact_service.is_empty()

    user = Profile()
    profile_repo.put(user)

    universite = Structure(nom="SU", type_name=UN.name)
    structure_repo.put(universite)

    # Actual test
    contact_service.set_contact(universite, ContactType.CDP, user)
    assert contact_service.get_contact(universite, ContactType.CDP) == user

    map = contact_service.get_mapping()
    map1 = map[universite]
    assert map1[ContactType.CDP] == user

    contact_service.delete_contact(universite, ContactType.CDP)
    assert contact_service.get_contact(universite, ContactType.CDP) == None

    map = contact_service.get_mapping()
    assert universite not in map

    # Cleanup
    profile_repo.clear()
    structure_repo.clear()
    contact_service.clear()
Beispiel #4
0
def test_structures(client: FlaskClient, db_session: scoped_session) -> None:
    login_as_dgrtt(client, db_session)

    universite = Structure(
        nom="Sorbonne Université",
        type_name=UN.name,
        sigle="SU",
        dn="ou=SU,ou=Affectations,dc=chapeau,dc=fr",
    )
    fac_sciences = Structure(nom="Faculté des Sciences", type_name=FA.name)
    structure_repo.put(universite)
    structure_repo.put(fac_sciences)

    r = client.get(url_for("v3.all_structures"))
    assert r.status_code == 200

    structure_repo.clear()
Beispiel #5
0
def sg_create_child_structure(id: str, model: dict[str, str]):
    parent_structure = structure_repo.get_by_id(StructureId(id))
    if not parent_structure:
        raise NotFound()

    check_permission(parent_structure, "P3")

    new_structure = Structure()
    new_structure.nom = model["nom"]
    type_structure = get_type_structure_by_id(model["type_id"])
    new_structure.type_name = type_structure.name

    parent_structure.add_child(new_structure)
    structure_repo.put(new_structure)

    db.session.commit()
    cache.evict("structures")
Beispiel #6
0
def test_repo(injector: Injector, db_session):
    repo = injector.get(StructureRepository)

    universite = Structure(nom="Sorbonne Université",
                           type_name=UN.name,
                           sigle="SU",
                           dn="Top")
    fac_sciences = Structure(nom="Faculté des Sciences", type_name=FA.name)
    repo.put(universite)
    repo.put(fac_sciences)
    assert universite in repo.get_all()
    assert fac_sciences in repo.get_all()
    repo.check_all()

    assert universite == repo.get_by_id(universite.id)
    assert universite == repo.get_by_dn(universite.dn)
    assert universite == repo.get_by_sigle(universite.sigle)

    universite.add_child(fac_sciences)
    assert universite in repo.get_all()
    assert fac_sciences in repo.get_all()
    repo.check_all()
Beispiel #7
0
    class NS:
        # Create structures
        labo = Structure(dn="labo", nom="Labo", type_name=LA.name)
        dri = Structure(dn=DRI_DN, nom="DR&I", type_name=DU.name)

        # Create profiles
        joe_gdl = Profile(uid="joe_gdl", email="1", prenom="Joe", nom="GDL")
        jim_porteur = Profile(uid="jim_porteur",
                              email="2",
                              prenom="Jim",
                              nom="Porteur")
        jake_directeur = Profile(uid="jake_directeur",
                                 email="3",
                                 prenom="Jake",
                                 nom="Directeur")
        jules_dri = Profile(uid="jules_dgrtt",
                            email="4",
                            prenom="Jules",
                            nom="Contact DR&I")
        jules2_dri = Profile(uid="jules2_dgrtt",
                             email="5",
                             prenom="Jules2",
                             nom="Contact DR&I")
Beispiel #8
0
def test_single():
    universite = Structure(nom="Sorbonne Université",
                           type_name=UN.name,
                           sigle="SU")
    assert universite.nom == "Sorbonne Université"
    assert universite.name == "Sorbonne Université"
    assert universite.sigle_ou_nom == "SU"
    assert universite.is_reelle
    assert universite.active
    assert len(universite.ancestors) == 0
    assert len(universite.descendants) == 0
    universite.check()

    universite.delete()
    assert not universite.active
Beispiel #9
0
def test_roles(structure_repo, profile_repo):
    universite = Structure(nom="SU", type_name=UN.name)
    structure_repo.put(universite)

    user = Profile(uid="toto")
    profile_repo.put(user)

    result = get_roles(universite.id)
    assert glom(result, (["users"], [["id"]])) == [[], [], [], [], []]

    add_roles(universite.id, [user.id], Role.PORTEUR.name)
    result = get_roles(universite.id)
    assert glom(result, (["users"], [["id"]])) == [[], [], [], [], [user.id]]

    delete_role(universite.id, user.id, Role.PORTEUR.name)
    result = get_roles(universite.id)
    assert glom(result, (["users"], [["id"]])) == [[], [], [], [], []]

    result = get_role_selectors(universite.id)
    # First select is not multiple
    assert glom(result, ["value"]) == [None, [], [], [], []]
Beispiel #10
0
def test_all_simple_endpoints(role, client, app, db_session):
    # Setup repo with a root
    structure_repo.clear()
    universite = Structure(
        nom="Sorbonne Université",
        type_name=UN.name,
        sigle="SU",
        dn="ou=SU,ou=Affectations,dc=chapeau,dc=fr",
    )
    structure_repo.put(universite)

    login_as_dgrtt(client, db_session)

    Profile.__roles = [role]

    endpoints = get_endpoints(app)
    try:
        for endpoint in endpoints:
            print(f"checking endpoint '{endpoint}' with role '{role}'")
            check_endpoint(endpoint, client)
    finally:
        del Profile.__roles

    structure_repo.clear()
Beispiel #11
0
def test_with_context(db_session: scoped_session):
    user = Profile()
    profile_repo.put(user)

    structure = Structure()
    structure_repo.put(structure)

    role_service.grant_role(user, Role.ADMIN_LOCAL, structure)
    assert role_service.has_role(user, Role.ADMIN_LOCAL, structure)
    assert role_service.get_users_with_role(Role.ADMIN_LOCAL,
                                            structure) == {user}

    role_to_users = role_service.get_users_with_role_on(structure)
    assert len(role_to_users) == 1

    user1 = role_to_users[Role.ADMIN_LOCAL].pop()
    assert user1 == user

    roles_for_user = role_service.get_roles_for_user(user)
    assert len(roles_for_user) == 1
    assert roles_for_user[Role.ADMIN_LOCAL] == {structure}

    role_service.clear()
    profile_repo.clear()
Beispiel #12
0
def possible_children(structure: Structure,
                      repo: StructureRepository) -> Collection[Structure]:
    s1 = repo.get_all()
    s2 = [s for s in s1 if structure.can_have_child(s)]
    s3 = set(s2) - set(structure.children)
    return s3
Beispiel #13
0
def test_constraints_on_parent():
    un = Structure(nom="Sorbonne Université", type_name=UN.name)
    la = Structure(nom="Labo", type_name=LA.name)
    du = Structure(nom="DU", type_name=DU.name)

    assert not un.can_have_parent(un)
    assert not un.can_have_parent(la)
    assert not la.can_have_parent(la)
    assert not la.can_have_parent(un)

    assert not un.can_have_parent(du)
    assert du.can_have_parent(un)

    assert not un.can_have_child(un)
    assert not un.can_have_child(la)
    assert not la.can_have_child(la)
    assert not la.can_have_child(un)

    assert un.can_have_child(du)
    assert not du.can_have_child(un)
Beispiel #14
0
def test_deep_hierarchy():
    universite = Structure(nom="Sorbonne Université", type_name=UN.name)
    fac = Structure(nom="Faculté", type_name=FA.name)
    composante = Structure(nom="Composante", type_name=CO.name)
    labo = Structure(nom="Labo", type_name=LA.name)

    universite.add_child(fac)
    fac.add_child(composante)
    composante.add_child(labo)

    universite.check()
    fac.check()
    composante.check()
    labo.check()

    assert labo.ancestors == [composante, fac, universite]
Beispiel #15
0
def test_hierarchy():
    universite = Structure(nom="Sorbonne Université", type_name=UN.name)
    fac_sciences = Structure(nom="Faculté des Sciences", type_name=FA.name)
    assert universite not in fac_sciences.parents
    assert fac_sciences not in universite.children

    universite.add_child(fac_sciences)
    assert universite in fac_sciences.parents
    assert fac_sciences in universite.children
    assert universite.depth == 0
    assert fac_sciences.depth == 1
    assert fac_sciences.ancestors == [universite]
    universite.check()
    fac_sciences.check()

    universite.remove_child(fac_sciences)
    assert universite not in fac_sciences.parents
    assert fac_sciences not in universite.children
    assert universite.depth == 0
    assert fac_sciences.depth == 0
    universite.check()
    fac_sciences.check()

    fac_sciences.add_parent(universite)
    assert universite in fac_sciences.parents
    assert fac_sciences in universite.children
    assert universite.depth == 0
    assert fac_sciences.depth == 1
    universite.check()
    fac_sciences.check()

    fac_sciences.remove_parent(universite)
    assert universite not in fac_sciences.parents
    assert fac_sciences not in universite.children
    assert universite.depth == 0
    assert fac_sciences.depth == 0
    universite.check()
    fac_sciences.check()