Esempio n. 1
0
def test_device_post(client, sample_room1, device_to_add,
                     sample_member: Adherent):
    """ Can create a valid device """
    r = client.patch(
        f"{host_url}/room/{sample_room1.id}/member/add/",
        data=json.dumps({"id": SAMPLE_CLIENT_ID}),
        content_type='application/json',
        headers=TEST_HEADERS,
    )
    assert r.status_code == 204
    r = client.post(f'{base_url}',
                    data=json.dumps(device_to_add),
                    content_type='application/json',
                    headers=TEST_HEADERS)
    assert r.status_code == 201
    assert_modification_was_created(db.session())

    s = db.session()
    q = s.query(Device)
    q = q.filter(Device.type == (
        DeviceType.wireless.value if device_to_add['connectionType'] ==
        'wireless' else DeviceType.wired.value))
    q = q.filter(Device.mac == device_to_add['mac'])
    assert q.one_or_none() is not None
    r = client.get(
        f'{base_url}{int(r.text)}',
        headers=TEST_HEADERS,
        content_type='application/json',
    )
    res = r.json
    assert res[
        "ipv4Address"] == f'{".".join(str(sample_member.subnet).split(".")[:3])}.{int(str(sample_member.subnet).split(".")[3].split("/")[0])+2}' if device_to_add[
            'connectionType'] == 'wireless' else res[
                "ipv4Address"] == "192.168.42.2"
    assert res["ipv6Address"] == "fe80:42::2"
Esempio n. 2
0
def test_room_add_member_when_no_room(client, sample_room1, sample_room2,
                                      sample_member, sample_vlan69):
    r = client.patch(
        f"{base_url}{sample_room1.id}/member/del/",
        data=json.dumps({"id": sample_member.id}),
        content_type='application/json',
        headers=TEST_HEADERS,
    )
    assert db.session().execute(
        select(Adherent.subnet).where(
            Adherent.id == sample_member.id)).scalar() is None
    assert db.session().execute(
        select(Device.ip).where((Device.adherent_id == sample_member.id) & (
            Device.type == DeviceType.wired.value))).scalar() == 'En attente'
    r = client.patch(
        f"{base_url}{sample_room2.id}/member/add/",
        data=json.dumps({"id": sample_member.id}),
        content_type='application/json',
        headers=TEST_HEADERS,
    )
    assert r.status_code == 204
    assert IPv4Address(db.session().execute(
        select(Device.ip).where((Device.adherent_id == sample_member.id) & (
            Device.type == DeviceType.wired.value))).scalar()) in IPv4Network(
                sample_vlan69.adresses)
Esempio n. 3
0
def test_member_delete_existant(client, sample_member):
    r = client.delete(f'{base_url}{sample_member.id}', headers=TEST_HEADERS)
    assert r.status_code == 204
    assert_modification_was_created(db.session())

    s = db.session()
    q = s.query(Adherent)
    q = q.filter(Adherent.login == "dubois_j")
    assert not s.query(q.exists()).scalar()
Esempio n. 4
0
def test_device_delete(client, device: Device, status_code: int):
    r = client.delete(
        f'{base_url}{device.id}',
        headers=TEST_HEADERS,
    )
    assert r.status_code == status_code
    if status_code == 204:
        assert_modification_was_created(db.session())

        s = db.session()
        q = s.query(Device)
        q = q.filter(Device.type == "wired")
        q = q.filter(Device.mac == device.mac)
        assert not s.query(q.exists()).scalar(), "Object not actually deleted"
Esempio n. 5
0
def assert_switch_in_db(body):
    s = db.session()
    q = s.query(Switch)
    q = q.filter(Switch.ip == body["ip"])
    sw = q.one()
    assert sw.ip == body["ip"]
    assert sw.communaute == body["community"]
    assert sw.description == body["description"]
Esempio n. 6
0
def test_switch_delete_existant_switch(client, sample_switch1: Switch):
    r = client.delete(f"{base_url}{sample_switch1.id}", headers=TEST_HEADERS)
    assert r.status_code == 204
    s = db.session()
    q = s.query(Switch)
    q = q.filter(Switch.id == sample_switch1.id)

    assert not s.query(q.exists()).scalar()
Esempio n. 7
0
def assert_port_in_db(body):
    s = db.session()
    q = s.query(Port)
    q = q.filter(Port.numero == body["portNumber"])
    p = q.one()
    assert body["portNumber"] == p.numero
    assert body["room"] == p.chambre_id
    assert body["switchObj"] == p.switch_id
Esempio n. 8
0
def assert_transaction_in_db(body):
    s = db.session()
    q = s.query(Transaction)
    q = q.filter(Transaction.name == body["name"])
    sw = q.one()
    assert sw.name == body["name"]
    assert sw.src == body["src"]
    assert sw.dst == body["dst"]
    assert sw.value == body["value"]
Esempio n. 9
0
def test_room_delete_existant_room(client, sample_room1):
    r = client.delete(
        f"{base_url}{sample_room1.id}",
        headers=TEST_HEADERS,
    )
    assert r.status_code == 204

    s = db.session()
    q = s.query(Chambre)
    q = q.filter(Chambre.id == sample_room1.id)
    assert q.count() == 0
Esempio n. 10
0
def test_transaction_validate_pending(client, sample_transaction_pending):
    r = client.get(
        f'{base_url}{sample_transaction_pending.id}/validate',
        headers=TEST_HEADERS,
    )
    assert r.status_code == 204
    s = db.session()
    q = s.query(Transaction)
    q = q.filter(Transaction.name == sample_transaction_pending.name)
    sw = q.one()
    assert sw.pending_validation == False, "Transaction was not actually validated"
Esempio n. 11
0
def assert_member_in_db(body):
    # Actually check that the object was inserted
    s = db.session()
    q = s.query(Adherent)
    q = q.filter(Adherent.login == body["username"])
    r = q.one()
    assert r.nom == body["lastName"]
    assert r.prenom == body["firstName"]
    assert r.mail == body["mail"]
    #    assert r.commentaires == body["comment"]
    assert r.login == body["username"]
Esempio n. 12
0
def test_port_delete_port(client, port_id: int, status_code: int):
    r = client.delete(
        f"{base_url}{port_id}",
        headers=TEST_HEADERS,
    )
    assert r.status_code == status_code

    if status_code == 204:
        s = db.session()
        q = s.query(Port)
        q = q.filter(Port.id == port_id)
        assert not s.query(q.exists()).scalar()
Esempio n. 13
0
def test_transaction_delete_pending(client, sample_transaction_pending):
    r = client.delete(
        f'{base_url}{sample_transaction_pending.id}',
        headers=TEST_HEADERS,
    )
    assert r.status_code == 204

    s = db.session()
    q = s.query(Transaction)
    q = q.filter(Transaction.pending_validation == True)
    q = q.filter(Transaction.name == "description 2")
    assert not s.query(q.exists()).scalar(), "Object not actually deleted"
Esempio n. 14
0
def test_member_patch(client, sample_member: Adherent, key: str, value: str):
    body = {
        key: value,
    }
    res = client.patch(f'{base_url}{sample_member.id}',
                       data=json.dumps(body),
                       content_type='application/json',
                       headers=TEST_HEADERS)
    assert res.status_code == 204
    assert_modification_was_created(db.session())
    member_to_check = {
        "firstName": sample_member.prenom,
        "lastName": sample_member.nom,
        # "comment": sample_member.commentaires,
        "mail": sample_member.mail,
        "username": sample_member.login
    }
    member_to_check[key] = value
    assert_member_in_db(member_to_check)
Esempio n. 15
0
def prep_db(*args):
    from adh6.storage.sql.models import db as _db
    _db.create_all()
    session = _db.session()
    session.add(sample_member_admin())
    session.add_all([
        oidc_admin_prod_role(),
        oidc_admin_read_role(),
        oidc_admin_write_role(),
        oidc_network_read_role(),
        oidc_network_write_role(),
        oidc_treasurer_read_role(),
        oidc_treasurer_write_role()
    ])
    session.add_all(args)
    session.add_all([
        api_key_admin(),
    ] + [api_key_admin_read_roles(),
         api_key_admin_write_roles()])
    session.commit()
Esempio n. 16
0
def test_member_post_member_create(client):
    body = {
        "firstName": "John",
        "lastName": "Doe",
        "mail": "*****@*****.**",
        "username": "******"
    }
    res = client.post(f'{base_url}',
                      data=json.dumps(body),
                      content_type='application/json',
                      headers=TEST_HEADERS)
    assert 201 == res.status_code
    assert_modification_was_created(db.session())
    assert_member_in_db(body)

    r = client.get(
        f"{host_url}/mailinglist/member/{int(res.text)}",
        headers=TEST_HEADERS,
    )
    assert r.status_code == 200
    response = json.loads(r.data.decode('utf-8'))
    assert response == 249
Esempio n. 17
0
def test_device_filter_hit_limit(client, sample_member: Adherent):
    s = db.session()
    LIMIT = 10

    # Create a lot of devices
    for _ in range(LIMIT * 2):
        suffix = "{0:04X}"
        dev = Device(
            adherent_id=sample_member.id,
            mac='00-00-00-00-' + suffix[:2] + "-" + suffix[2:],
            type=DeviceType.wired.value,
            ip="127.0.0.1",
            ipv6="::1",
        )
        s.add(dev)
    s.commit()

    r = client.get(
        f'{base_url}?limit={LIMIT}',
        headers=TEST_HEADERS,
    )
    assert r.status_code == 200
    response = json.loads(r.data.decode('utf-8'))
    assert len(response) == LIMIT
Esempio n. 18
0
def assert_room_in_db(body):
    s = db.session()
    q = s.query(Chambre)
    q = q.filter(body["roomNumber"] == Chambre.numero)
    c = q.one()
    assert body["description"] == c.description