Esempio n. 1
0
def post(request):
    """
    Create new test plan.
    """
    try:
        new = json.loads(request.body)
        assert "name" in new
    except ValueError:
        return HttpResponseBadRequest("invalid JSON")
    except AssertionError:
        return HttpResponseBadRequest("argument mismatch")

    if 'rules' in new:
        new['rules'] = [rule_model.validate(rule) for rule in new['rules']]
        if None in new['rules']:  # Invalid rules are re-assigned to None
            return HttpResponse("invalid rule(s) provided")

    dbc = db_model.connect()
    testplan = dbc.testplan.find_one({"name": new['name']})
    if testplan is not None:
        return HttpResponseBadRequest("testplan named '%s' already exists" % new['name'])

    new['createdAt'] = datetime.isoformat(datetime.now())
    new['updatedAt'] = datetime.isoformat(datetime.now())
    testplan_id = str(dbc.testplan.save(new))
    r = JsonResponse({"id": testplan_id}, status=200)
    r['location'] = "/api/testplan/%s" % testplan_id
    logger.info("test plan '%s' created by '%s'" % (testplan_id, request.user['username']))
    return r
Esempio n. 2
0
def put(request, testplan_id):
    """
    Update existing test plan based on testplan_id.
    """
    try:
        in_json = json.loads(request.body)
    except ValueError:
        return HttpResponseBadRequest("invalid JSON")

    dbc = db_model.connect()
    try:
        testplan = dbc.testplan.find_one({"_id": ObjectId(testplan_id)})
    except InvalidId:
        return HttpResponseNotFound()
    if testplan is None:
        return HttpResponseNotFound()
    else:
        if "name" in in_json:
            testplan['name'] = in_json['name']
        if "description" in in_json:
            testplan['description'] = in_json['description']
        if "rules" in in_json:
            testplan['rules'] = [rule_model.validate(rule) for rule in in_json['rules']]
            if None in in_json['rules']:
                return HttpResponse("invalid rule(s) provided")
        try:
            testplan['updatedAt'] = datetime.isoformat(datetime.now())
            dbc.testplan.save(testplan)
        except DuplicateKeyError:
            return HttpResponseBadRequest("testplan named '%s' already exists" % in_json['name'])
        logger.info("test plan '%s' updated by '%s'" % (testplan_id, request.user['username']))
        return HttpResponse(status=200)
Esempio n. 3
0
def put(request, rule_id):
    """
    Update existing rule based on rule_id.
    """
    try:
        in_json = json.loads(request.body)
    except ValueError:
        return HttpResponseBadRequest("invalid JSON")

    dbc = db_model.connect()
    try:
        rule = dbc.rule.find_one({"_id": ObjectId(rule_id)})
    except InvalidId:
        return HttpResponseNotFound()
    if rule is None:
        return HttpResponseNotFound()
    else:
        in_json['createdAt'] = rule['createdAt']
        rule = rule_model.validate(in_json)
        if rule is None:
            return HttpResponseBadRequest("invalid rule")
        else:

            rule['_id'] = ObjectId(rule_id)
            rule['updatedAt'] = datetime.isoformat(datetime.now())
            dbc.rule.save(rule)
            r = JsonResponse({"id": rule_id})
            r['location'] = "/api/rule/%s" % rule_id
            logger.info("rule '%s' updated by '%s'" % (rule_id, request.user['username']))
            return r
Esempio n. 4
0
def post(request):
    """
    Create new test plan.
    """
    try:
        new = json.loads(request.body)
        assert "name" in new
    except ValueError:
        return HttpResponseBadRequest("invalid JSON")
    except AssertionError:
        return HttpResponseBadRequest("argument mismatch")

    if 'rules' in new:
        new['rules'] = [rule_model.validate(rule) for rule in new['rules']]
        if None in new['rules']:  # Invalid rules are re-assigned to None
            return HttpResponse("invalid rule(s) provided")

    dbc = db_model.connect()
    testplan = dbc.testplan.find_one({"name": new['name']})
    if testplan is not None:
        return HttpResponseBadRequest("testplan named '%s' already exists" %
                                      new['name'])

    new['createdAt'] = datetime.isoformat(datetime.now())
    new['updatedAt'] = datetime.isoformat(datetime.now())
    testplan_id = str(dbc.testplan.save(new))
    r = JsonResponse({"id": testplan_id}, status=200)
    r['location'] = "/api/testplan/%s" % testplan_id
    logger.info("test plan '%s' created by '%s'" %
                (testplan_id, request.user['username']))
    return r
Esempio n. 5
0
def post(request, testplan_id):
    """
    Create a new rule within a testplan based on testplan_id.
    """
    if request.method != 'POST':
        return HttpResponseBadRequest("only POST supported")

    try:
        new = json.loads(request.body)
    except ValueError:
        return HttpResponseBadRequest("invalid JSON")
    except AssertionError:
        return HttpResponseBadRequest("argument mismatch")

    if 'id' in new:  # Don't allow the user to shoot themselves in the foot providing dubious id
        del new['id']

    rule = rule_model.validate(new)
    if rule is None:
        return HttpResponseBadRequest("invalid rule")

    dbc = db_model.connect()
    try:
        testplan = dbc.testplan.find_one({"_id": ObjectId(testplan_id)})
    except InvalidId:
        return HttpResponseNotFound("testplan '%s' not found" % testplan_id)
    if testplan is None:
        return HttpResponseNotFound("testplan '%s' not found" % testplan_id)

    if 'rules' in testplan:
        testplan['rules'].append(rule)
    else:
        testplan['rules'] = [rule]

    dbc.testplan.save(testplan)
    r = JsonResponse({"id": rule['id']}, status=200)
    r['location'] = "/api/testplan/%s/rule/%s" % (testplan_id, rule['id'])
    logger.info("rule '%s' within testplan '%s' created by '%s'" % (rule['id'], testplan_id, request.user['username']))
    return r
Esempio n. 6
0
def post(request):
    """
    Create new rule.
    """

    try:
        new = json.loads(request.body)
    except ValueError:
        return HttpResponseBadRequest("invalid JSON")

    rule = rule_model.validate(new)
    if rule is None:
        return HttpResponseBadRequest("invalid rule")
    else:
        dbc = db_model.connect()
        rule['createdAt'] = datetime.isoformat(datetime.now())
        rule['updatedAt'] = datetime.isoformat(datetime.now())
        rule_id = str(dbc.rule.save(rule))
        r = JsonResponse({"id": rule_id})
        r['location'] = "/api/rule/%s" % rule_id
        logger.info("rule '%s' created by '%s'" % (rule_id, request.user['username']))
        return r
Esempio n. 7
0
def put(request, testplan_id, rule_id):
    """
    Update existing test plan based on testplan_id.
    """

    dbc = db_model.connect()
    try:
        testplan = dbc.testplan.find_one({"_id": ObjectId(testplan_id)})
    except InvalidId:
        return HttpResponseNotFound("testplan '%s' not found" % testplan_id)
    if testplan is None:
        return HttpResponseNotFound("testplan '%s' not found" % testplan_id)

    for ri in range(0, len(testplan['rules'])):
        if testplan['rules'][ri]['id'] == rule_id:
            rule = testplan['rules'][ri]
            break

    try:
        new = json.loads(request.body)
    except ValueError:
        return HttpResponseBadRequest("invalid JSON")
    except AssertionError:
        return HttpResponseBadRequest("argument mismatch")

    rule.update(new)
    rule = rule_model.validate(rule)
    if rule is None:
        return HttpResponseBadRequest("invalid rule")

    testplan['rules'][ri] = rule

    dbc.testplan.save(testplan)
    r = HttpResponse(status=200)
    r['location'] = "/api/testplan/%s/rule/%s" % (testplan_id, rule['id'])
    logger.info("rule '%s' within testplan '%s' updated by '%s'" % (rule['id'], testplan_id, request.user['username']))
    return r
Esempio n. 8
0
def put(request, testplan_id):
    """
    Update existing test plan based on testplan_id.
    """
    try:
        in_json = json.loads(request.body)
    except ValueError:
        return HttpResponseBadRequest("invalid JSON")

    dbc = db_model.connect()
    try:
        testplan = dbc.testplan.find_one({"_id": ObjectId(testplan_id)})
    except InvalidId:
        return HttpResponseNotFound()
    if testplan is None:
        return HttpResponseNotFound()
    else:
        if "name" in in_json:
            testplan['name'] = in_json['name']
        if "description" in in_json:
            testplan['description'] = in_json['description']
        if "rules" in in_json:
            testplan['rules'] = [
                rule_model.validate(rule) for rule in in_json['rules']
            ]
            if None in in_json['rules']:
                return HttpResponse("invalid rule(s) provided")
        try:
            testplan['updatedAt'] = datetime.isoformat(datetime.now())
            dbc.testplan.save(testplan)
        except DuplicateKeyError:
            return HttpResponseBadRequest(
                "testplan named '%s' already exists" % in_json['name'])
        logger.info("test plan '%s' updated by '%s'" %
                    (testplan_id, request.user['username']))
        return HttpResponse(status=200)