Пример #1
0
def usergroup_create():
    """Add a usergroup in the database"""
    # Only POST data are handled
    if request.method != "POST":
        return utils.response("ERROR: POST method is required ", 405)

    # Simplification for the reading
    name = request.form["name"]
    comment = request.form["comment"]

    # Check for required fields
    if not name:
        return utils.response("ERROR: The name is required ", 417)

    # Check unicity for name
    query = db.session.query(usergroup.Usergroup.name)\
        .filter_by(name=name).first()

    if query is not None:
        return utils.response('ERROR: The name "' + name + \
                              '" is already used by another usergroup ', 417)

    g = usergroup.Usergroup(name=name, comment=comment)
    db.session.add(g)

    # Try to add the usergroup on the database
    try:
        db.session.commit()
    except exc.SQLAlchemyError as e:
        return utils.response('ERROR: "' + name + '" -> ' + \
                              e.message, 409)

    return utils.response('OK: "' + name + '" -> created', 200)
Пример #2
0
    def test_remove_usergroup(self):
        """Target removing a usergroup succeeds"""
        name = "clever_server"
        hostname = "127.0.0.1"
        port = 54
        sshoptions = "--zap"
        comment = "Magnificent target"

        usergroupname = "VVVVVV"
        comment_usergroup = "Captain Viridian to the rescue"

        t = target.Target(name=name,
                          hostname=hostname,
                          port=port,
                          sshoptions=sshoptions,
                          comment=comment)

        ug = usergroup.Usergroup(name=usergroupname, comment=comment_usergroup)

        db.session.add(t)
        db.session.commit()

        t.addusergroup(ug)
        db.session.commit()

        t.rmusergroup(ug)
        db.session.commit()

        t_db = db.session.query(target.Target).filter_by(name=name).first()

        assert_equal(t_db.gmembers, [])
Пример #3
0
    def test_add_usergroup_already_in_target(self):
        """Target adding a usergroup already in target does nothing
        (but doesn't raise error)
        """
        name = "clever_server"
        hostname = "127.0.0.1"
        port = 54
        sshoptions = "--zap"
        comment = "Magnificent target"

        usergroupname = "VVVVVV"
        comment_usergroup = "Captain Viridian to the rescue"

        t = target.Target(name=name,
                          hostname=hostname,
                          port=port,
                          sshoptions=sshoptions,
                          comment=comment)

        ug = usergroup.Usergroup(name=usergroupname, comment=comment_usergroup)

        db.session.add(t)
        db.session.commit()

        t.addusergroup(ug)
        db.session.commit()

        t.addusergroup(ug)
        db.session.commit()

        t_db = db.session.query(target.Target).filter_by(name=name).first()

        assert_equal(t_db.gmembers, [ug])
Пример #4
0
    def test_create_existing_name(self):
        """Target creation in database with an already used
        name fails
        """
        name = "clever_server"
        hostname = "127.0.0.1"
        port = 54
        sshoptions = "--zap"
        comment = "Magnificent target"

        name = "*****@*****.**"
        sshkey = "railway"
        comment_user = "******"
        user_list = []

        usergroupname = "Chevaliers_du_zodiaque"
        comment_usergroup = "Energie_du_cosmos"
        usergroup_list = []

        u = user.User(name=name, sshkey=sshkey, comment=comment_user)
        user_list.append(u)

        ug = usergroup.Usergroup(name=usergroupname, comment=comment_usergroup)
        usergroup_list.append(ug)

        t = target.Target(name=name,
                          hostname=hostname,
                          port=port,
                          sshoptions=sshoptions,
                          comment=comment,
                          members=user_list,
                          gmembers=usergroup_list)
        db.session.add(t)
        db.session.commit()

        t = target.Target(name=name,
                          hostname="a great host",
                          port=port,
                          sshoptions=sshoptions,
                          comment=comment,
                          members=user_list,
                          gmembers=usergroup_list)
        db.session.add(t)
        db.session.commit()
Пример #5
0
    def test_create(self):
        """Target creation in database succeeds"""
        targetname = "clever_server"
        hostname = "127.0.0.1"
        port = 54
        sshoptions = "--zap"
        comment = "Magnificent target"
        output = """Name: clever_server\nHostname: 127.0.0.1\nPort: 54\nSSH options: --zap\nComment: Magnificent target\nAttached users: [email protected]\nUsergroup list: Chevaliers_du_zodiaque\nUsers who can access this target: [email protected]\nAll usergroups: Chevaliers_du_zodiaque\nMember of the following targetgroups: """

        username = "******"
        sshkey = "railway"
        comment_user = "******"
        user_list = []

        usergroupname = "Chevaliers_du_zodiaque"
        comment_usergroup = "Energie_du_cosmos"
        usergroup_list = []

        u = user.User(name=username, sshkey=sshkey, comment=comment_user)
        user_list.append(u)

        ug = usergroup.Usergroup(name=usergroupname, comment=comment_usergroup)
        usergroup_list.append(ug)

        t = target.Target(name=targetname,
                          hostname=hostname,
                          port=port,
                          sshoptions=sshoptions,
                          comment=comment,
                          members=user_list,
                          gmembers=usergroup_list)
        db.session.add(t)
        db.session.commit()

        t_db = db.session.query(
            target.Target).filter_by(name="clever_server").first()

        assert_equal(t_db.name, targetname)
        assert_equal(t_db.hostname, hostname)
        assert_equal(t_db.port, port)
        assert_equal(t_db.sshoptions, sshoptions)
        assert_equal(t_db.comment, comment)
        assert_equal(repr(t_db), output)
Пример #6
0
    def test_edit(self):
        """Target edition in database succeeds"""
        targetname = "clever_server"
        hostname = "127.0.0.1"
        port = 54
        sshoptions = "--zap"
        comment = "Magnificent target"
        new_targetname = "cleverer_super_server"
        new_hostname = "192.135.23.54"
        new_port = 80
        new_sshoptions = "--plop"
        new_comment = "Marvellous target"

        username = "******"
        sshkey = "railway"
        comment_user = "******"
        username2 = "*****@*****.**"
        sshkey2 = "rough tell"
        comment_user2 = "slow"
        user_list = []

        usergroupname = "Chevaliers_du_zodiaque"
        comment_usergroup = "Energie_du_cosmos"
        usergroupname2 = "Chevaliers d'or"
        comment_usergroup2 = "COSMOOOOS"
        usergroup_list = []

        u = user.User(name=username, sshkey=sshkey, comment=comment_user)
        user_list.append(u)

        ug = usergroup.Usergroup(name=usergroupname, comment=comment_usergroup)
        usergroup_list.append(ug)

        t = target.Target(name=targetname,
                          hostname=hostname,
                          port=port,
                          sshoptions=sshoptions,
                          comment=comment,
                          members=user_list,
                          gmembers=usergroup_list)
        db.session.add(t)
        db.session.commit()

        u = user.User(name=username2, sshkey=sshkey2, comment=comment_user2)
        user_list.append(u)

        ug = usergroup.Usergroup(name=usergroupname2,
                                 comment=comment_usergroup2)
        usergroup_list.append(ug)

        target_to_edit = db.session.query(
            target.Target).filter_by(name="clever_server")
        updated_rows = target_to_edit.update({
            "name": new_targetname,
            "hostname": new_hostname,
            "port": new_port,
            "sshoptions": new_sshoptions,
            "comment": new_comment
        })
        db.session.commit()

        t_edit = db.session.query(
            target.Target).filter_by(name=new_targetname).first()

        assert_equal(updated_rows, 1)
        assert_equal(t.id, t_edit.id)
        assert_equal(t_edit.name, new_targetname)
        assert_equal(t_edit.hostname, new_hostname)
        assert_equal(t_edit.port, new_port)
        assert_equal(t_edit.sshoptions, new_sshoptions)
        assert_equal(t_edit.comment, new_comment)