Esempio n. 1
0
def delete_wireless_device(admin, macAddress, s):
    """ Delete a wireless device from the database """
    q = s.query(Portable).filter(Portable.mac == macAddress)
    dev = q.one()

    dev.start_modif_tracking()
    s.delete(dev)
    s.flush()

    Modification.add_and_commit(s, dev.adherent, dev.get_ruby_modif(), admin)
Esempio n. 2
0
def update_wireless_device(admin, macAddress, body, s):
    """ Update a wireless device in the database """
    q = s.query(Portable).filter(Portable.mac == macAddress)
    dev = q.one()

    dev.start_modif_tracking()
    dev.mac = body['mac']
    dev.adherent = Adherent.find(s, body['username'])
    s.flush()

    Modification.add_and_commit(s, dev.adherent, dev.get_ruby_modif(), admin)
Esempio n. 3
0
def create_wireless_device(admin, body, s):
    """ Create a wireless device in the database """
    dev = Portable(
        mac=body['mac'],
        adherent=Adherent.find(s, body['username']),
    )

    s.add(dev)
    s.flush()

    Modification.add_and_commit(s, dev.adherent, dev.get_ruby_modif(), admin)
Esempio n. 4
0
def create_wired_device(admin, body, s):
    """ Create a wired device in the database """
    dev = Ordinateur(
        mac=body['mac'],
        ip=body['ipAddress'],
        ipv6=body['ipv6Address'],
        adherent=Adherent.find(s, body['username']),
    )

    s.add(dev)
    s.flush()

    Modification.add_and_commit(s, dev.adherent, dev.get_ruby_modif(), admin)
Esempio n. 5
0
def test_modification_add_new_user(api_client, sample_member2):
    s = db.get_db().get_session()

    a = sample_member2
    s.add(a)
    s.flush()

    # Build the corresponding modification
    Modification.add_and_commit(s, a, a.get_ruby_modif(),
                                Utilisateur.find_or_create(s, "test"))
    q = s.query(Modification)
    m = q.first()
    assert m.action == (
        '--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\n'
        'chambre_id:\n'
        '- \n'
        '- 1\n'
        'commentaires:\n'
        '- \n'
        '- Desauthent pour routeur\n'
        'id:\n'
        '- \n'
        '- 2\n'
        'login:\n'
        '- \n'
        '- reignier\n'
        'mail:\n'
        '- \n'
        '- [email protected]\n'
        'mode_association:\n'
        '- \n'
        '- 2011-04-30 17:50:17\n'
        'nom:\n'
        '- \n'
        '- Reignier\n'
        'password:\n'
        '- \n'
        '- a\n'
        'prenom:\n'
        '- \n'
        '- Edouard\n')
    assert m.adherent_id == a.id
    now = datetime.datetime.now()
    one_sec = datetime.timedelta(seconds=1)
    assert now - m.created_at < one_sec
    assert now - m.updated_at < one_sec
    assert m.utilisateur_id == 1
Esempio n. 6
0
def test_modification_delete_member(api_client, sample_member):
    s = db.get_db().get_session()
    a = Adherent.find(s, sample_member.login)

    a.start_modif_tracking()
    s.delete(a)
    s.flush()

    # Build the corresponding modification
    Modification.add_and_commit(s, a, a.get_ruby_modif(),
                                Utilisateur.find_or_create(s, "test"))
    q = s.query(Modification)
    m = q.first()
    assert m.action == (
        '--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\n'
        'chambre_id:\n'
        '- 1\n'
        '- \n'
        'id:\n'
        '- 1\n'
        '- \n'
        'login:\n'
        '- dubois_j\n'
        '- \n'
        'mail:\n'
        '- [email protected]\n'
        '- \n'
        'mode_association:\n'
        '- 2011-04-30 17:50:17\n'
        '- \n'
        'nom:\n'
        '- Dubois\n'
        '- \n'
        'password:\n'
        '- a\n'
        '- \n'
        'prenom:\n'
        '- Jean-Louis\n'
        '- \n')
    assert m.adherent_id == sample_member.id
    now = datetime.datetime.now()
    one_sec = datetime.timedelta(seconds=1)
    assert now - m.created_at < one_sec
    assert now - m.updated_at < one_sec
    assert m.utilisateur_id == 1
Esempio n. 7
0
def updatePassword(admin, username, body):
    password = body["password"]
    s = db.get_db().get_session()

    try:
        a = Adherent.find(s, username)
    except UserNotFound:
        return NoContent, 404

    try:
        a.start_modif_tracking()
        a.password = ntlm_hash(password)
        s.flush()

        # Build the corresponding modification
        Modification.add_and_commit(s, a, a.get_ruby_modif(), admin)

    except Exception:
        s.rollback()
        raise

    return NoContent, 204
Esempio n. 8
0
def putUser(admin, username, body):
    """ [API] Create/Update user from the database """
    s = db.get_db().get_session()

    # Create a valid object
    try:
        new_user = Adherent.from_dict(s, body)
    except InvalidEmail:
        return "Invalid email", 400
    except RoomNotFound:
        return "No room found", 400
    except ValueError:
        return "String must not be empty", 400

    try:
        # Check if it already exists
        update = adherentExists(s, username)

        if update:
            current_adh = Adherent.find(s, username)
            new_user.id = current_adh.id
            current_adh.start_modif_tracking()

        # Merge the object (will create a new if it doesn't exist)
        new_user = s.merge(new_user)
        s.flush()

        # Create the corresponding modification
        Modification.add_and_commit(s, new_user, new_user.get_ruby_modif(),
                                    admin)
    except Exception:
        s.rollback()
        raise

    if update:
        return NoContent, 204
    else:
        return NoContent, 201
Esempio n. 9
0
def test_modification_multiple_changes_updated(api_client, sample_member):
    s = db.get_db().get_session()
    a = Adherent.find(s, sample_member.login)

    a.start_modif_tracking()
    a.commentaires = "Hey I am a comment"
    a.nom = "Test"
    a.prenom = "Test"
    a.mail = "*****@*****.**"
    s.flush()

    # Build the corresponding modification
    Modification.add_and_commit(s, a, a.get_ruby_modif(),
                                Utilisateur.find_or_create(s, "test"))
    q = s.query(Modification)
    m = q.first()
    assert m.action == (
        '--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\n'
        'commentaires:\n'
        '- \n'
        '- Hey I am a comment\n'
        'mail:\n'
        '- [email protected]\n'
        '- [email protected]\n'
        'nom:\n'
        '- Dubois\n'
        '- Test\n'
        'prenom:\n'
        '- Jean-Louis\n'
        '- Test\n')
    assert m.adherent_id == sample_member.id
    now = datetime.datetime.now()
    one_sec = datetime.timedelta(seconds=1)
    assert now - m.created_at < one_sec
    assert now - m.updated_at < one_sec
    assert m.utilisateur_id == 1
Esempio n. 10
0
def test_modification_pass_updated(api_client, sample_member):
    s = db.get_db().get_session()
    a = Adherent.find(s, sample_member.login)

    a.start_modif_tracking()
    a.password = "******"
    s.flush()

    # Build the corresponding modification
    Modification.add_and_commit(s, a, a.get_ruby_modif(),
                                Utilisateur.find_or_create(s, "test"))
    q = s.query(Modification)
    m = q.first()
    assert m.action == (
        '--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\n'
        'password:\n'
        '- a\n'
        '- TESTESTEST\n')
    assert m.adherent_id == sample_member.id
    now = datetime.datetime.now()
    one_sec = datetime.timedelta(seconds=1)
    assert now - m.created_at < one_sec
    assert now - m.updated_at < one_sec
    assert m.utilisateur_id == 1
Esempio n. 11
0
def deleteUser(admin, username):
    """ [API] Delete the specified User from the database """
    s = db.get_db().get_session()

    # Find the soon-to-be deleted user
    try:
        a = Adherent.find(s, username)
    except UserNotFound:
        return NoContent, 404

    try:
        # if so, start tracking for modifications
        a.start_modif_tracking()

        # Actually delete it
        s.delete(a)
        s.flush()

        # Write it in the modification table
        Modification.add_and_commit(s, a, a.get_ruby_modif(), admin)
    except Exception:
        s.rollback()
        raise
    return NoContent, 204