Esempio n. 1
0
def post_economy_accounts():
    sample = {
        "number": {
            "required": True,
            "allowed_types": [int]
        },
        "name": {
            "required": True,
            "allowed_types": [str]
        },
        "desc": {
            "required": True,
            "allowed_types": [str]
        }
    }
    sample_args = {"username": {"required": True, "allowed_types": [str]}}
    args = request.args.to_dict(flat=True)
    query = request.get_json()
    succ1, errors1 = check(sample, args)
    succ2, errors2 = check(sample, query)
    if not succ1 or not succ2:
        return make_response(jsonify(errors1 + errors2), 400)
    query["user"] = username
    test = coll_accounts.find_one({"number": query["number"]}, {"_id": 0})
    if test:
        return make_response(
            jsonify(["ERROR: Account number already exists."]), 400)
    else:
        insert = coll_accounts.insert_one(query, {"_id": 0})
        del query["_id"]
        return make_response(jsonify(query), 200)
Esempio n. 2
0
def post_economy_transactions():
    sample = {
        "amount": {
            "required": True,
            "allowed_types": [float, int]
        },
        "date_trans": {
            "required": False,
            "allowed_types": [int]
        },
        "desc": {
            "required": True,
            "allowed_types": [str]
        },
        "from_account": {
            "required": True,
            "allowed_types": [int]
        },
        "to_account": {
            "required": True,
            "allowed_types": [int]
        }
    }
    sample_args = {"username": {"required": True, "allowed_types": [str]}}
    query = request.get_json()
    args = request.args.to_dict(flat=True)
    succ1, errors1 = check(sample, query)
    succ2, errors2 = check(sample_args, args)
    if not succ1 or not succ2:
        return make_response(jsonify(erorrs1 + errors2), 400)
    username = args["username"]
    # Check that both accounts exist.
    accs = [query["from_account"], query["to_account"]]
    errors = []
    for acc in accs:
        test = coll_accounts.find_one({"user": username, "number": acc})
        if not test:
            errors.append(f"ERROR: Account with number {acc} does not exist.")
    if len(errors) != 0:
        return make_response(jsonify(errors), 400)
    # Add date_reg and user
    query["date_reg"] = round(datetime.datetime.today().timestamp())
    query["user"] = username
    if "date_trans" not in query:
        query["date_trans"] = query["date_reg"]
    # return new transaction
    coll_trans.insert_one(query)
    query["_id"] = str(query["_id"])
    return make_response(jsonify(query), 200)
Esempio n. 3
0
def put_economy_accounts():
    sample_json = {
        "number": {
            "required": True,
            "allowed_types": [int]
        },
        "name": {
            "required": True,
            "allowed_types": [str]
        },
        "desc": {
            "required": True,
            "allowed_types": [str]
        }
    }
    sample_args = {
        "number": {
            "required": True,
            "allowed_types": [str]
        },
        "username": {
            "required": True,
            "allowed_types": [str]
        }
    }
    args = request.args.to_dict(flat=True)
    query = request.get_json()
    # Check args
    succ1, errors1 = check(sample_args, args)
    # Check query
    succ2, errors2 = check(sample_json, query)
    if not succ1 or not succ2:
        return make_response(jsonify(errors1 + errors2), 400)
    num = 0
    username = query["username"]
    try:
        num = int(args["number"])
    except:
        return make_response(
            jsonify(["ERROR: Required parameter 'number' was not an integer."
                     ]), 400)
    # check if account exists.
    test = coll_accounts.find_one({"user": username, "number": num})
    if not test:
        return make_response(jsonify("ERROR: No such account exists."), 404)
    query["user"] = username
    coll_accounts.replace_one({"user": username, "number": num}, query)
    return make_response(jsonify(query), 200)
Esempio n. 4
0
def test_20_tuple_with_dict():
    sample = {
        "tuple": {
            "required": True,
            "allowed_types": [tuple],
            "tuple_order": [int, float, dict],
            "embedded_dict": {
                "name": {
                    "required": True,
                    "allowed_types": [str]
                },
                "age": {
                    "required": False,
                    "allowed_types": [int]
                }
            }
        }
    }
    obj = {
        "tuple": (10, 2.2, {
            "name": "Daniel",
            "age": 21
        }),
    }

    succ, errors = check(sample, obj)
    assert len(errors) == 0 and succ, "Basic tuple with dict failed."
Esempio n. 5
0
def get_economy_accounts():
    sample = {
        "number": {
            "required": False,
            "allowed_types": [list],
            "list_element": {
                "allowed_types": [str]
            }
        },
        "username": {
            "required": True,
            "allowed_types": [list],
            "list_element": {
                "allowed_types": [str]
            }
        }
    }
    query = request.args.to_dict(flat=False)
    succ, errors = check(sample, query)
    username = query["username"][0]
    if not succ:
        return make_response(jsonify(errors), 400)
    if "number" in query:
        accs = coll_accounts.find(
            {
                "user": username,
                "number": {
                    "$in": [int(i) for i in query["number"]]
                }
            }, {"_id": 0})
        return make_response(jsonify(list(accs)), 200)
    else:
        accs = coll_accounts.find({"user": username}, {"_id": 0})
        return make_response(jsonify(list(accs)), 200)
Esempio n. 6
0
def test_11_basic_list_of_strings_two_nums():
    sample = {
        "strings": {
            "required": True,
            "allowed_types": [list],
            "list_element": {
                "allowed_types": [str]
            }
        }
    }
    obj = {
        "strings": [
            "Hejsan",
            "där",
            "du",
            5,
            "din",
            "lilla",
            8,
            "råtta"
        ]
    }

    succ, errors = check(sample, obj)
    assert (not succ) and len(errors) == 2, "List of strings failed, with two nums."
Esempio n. 7
0
def v1_minecraft_whitelist():
    sample = {
        "add": {
            "required": False,
            "allowed_types": [str]
        },
        "remove": {
            "required": False,
            "allowed_types": [str]
        }
    }
    query = request.args.to_dict(flat=True)
    succ, errors = check(sample, query)
    if not succ:
        return make_response(jsonify(errors), 400)

    host = config.get_setting("minecraft-server-host", "127.0.0.1")
    port = config.get_setting("minecraft-server-rcon-port", 25575)
    password = config.get_setting("minecraft-server-password", "password")
    responses = list()
    with MCRcon(host, password, port=port) as mcr:
        if "add" in query:
            responses.append(mcr.command(f"whitelist add {query['add']}"))
        if "remove" in query:
            responses.append(
                mcr.command(f"whitelist remove {query['remove']}"))
    return make_response(jsonify(responses), 200)
Esempio n. 8
0
def delete_economy_accounts():
    sample = {
        "number": {
            "required": True,
            "allowed_types": [list],
            "list_element": {
                "allowed_types": [str]
            }
        },
        "username": {
            "required": True,
            "allowed_types": [list],
            "list_element": {
                "allowed_types": [str]
            }
        }
    }
    args = request.args.to_dict(flat=False)
    succ, errors = check(sample, args)
    if not succ:
        return make_response(jsonify(errors), 400)
    username = query["username"][0]
    x = coll_accounts.delete_many({
        "number": {
            "$in": [int(i) for i in args["number"]]
        },
        "user": username
    })
    return make_response(jsonify(x.deleted_count), 200)
Esempio n. 9
0
def put_economy_transactions():
    sample_json = {
        "amount": {
            "required": False,
            "allowed_types": [float, int]
        },
        "date_trans": {
            "required": False,
            "allowed_types": [int]
        },
        "desc": {
            "required": False,
            "allowed_types": [str]
        },
        "from_account": {
            "required": False,
            "allowed_types": [int]
        },
        "to_account": {
            "required": False,
            "allowed_types": [int]
        }
    }
    sample_args = {
        "id": {
            "required": True,
            "allowed_types": [str]
        },
        "username": {
            "required": True,
            "allowed_types": [str]
        }
    }
    query = request.get_json()
    args = request.args.to_dict(flat=True)
    succ1, errors1 = check(sample_args, args)
    succ2, errors2 = check(sample_json, query)
    if not succ1 or not succ2:
        return make_response(jsonify(errors1 + errors2), 400)
    username = args["username"]
    filter_query = {"user": username, "_id": ObjectId(args["id"])}
    update_query = {"$set": dict()}
    for key in query:
        update_query["$set"][key] = query[key]
    transaction = coll_trans.update_one(filter_query, update_query)
    return make_response(jsonify(transaction.modified_count), 200)
Esempio n. 10
0
def test_0_5_base():
    sample = {
        "yes": {
            "required": True,
            "allowed_types": [int]
        }
    }
    obj = None
    succ, errors = check(sample, obj)
    assert (not succ), "Undefined supplied obj passed."
Esempio n. 11
0
def test_13_basic_list_of_objects():
    sample = {
        "people": {
            "required": True,
            "allowed_types": [list],
            "list_element": {
                "allowed_types": [dict],
                "embedded_dict": {
                    "name": {
                        "required": True,
                        "allowed_types": [str]
                    },
                    "age": {
                        "required": True,
                        "allowed_types": [int]
                    },
                    "interests": {
                        "required": True,
                        "allowed_types": [list],
                        "list_element": {
                            "allowed_types": [str]
                        }
                    }
                }
            }
        }
    }
    obj = {
        "people": [
            {
                "name": "Daniel Cronqvist",
                "age": 21,
                "interests": [
                    "programming",
                    "games",
                    5
                ]
            },
            {
                "name": "Saga Kortesaari",
                "age": "21",
                "interests": [
                    "programming",
                    "games",
                    "painting"
                ],
                "extra_key_should_fail": 25
            }
        ]
    }

    succ, errors = check(sample, obj)
    assert (not succ) and len(errors) == 3, "List of people failed."
Esempio n. 12
0
def test_23_tuple_with_list_and_dict():
    sample = {
        "tuple": {
            "required": True,
            "allowed_types": [tuple],
            "tuple_order": [int, float, list, str, dict],
            "list_element": {
                "allowed_types": [dict],
                "embedded_dict": {
                    "name": {
                        "required": True,
                        "allowed_types": [str]
                    },
                    "age": {
                        "required": False,
                        "allowed_types": [int]
                    }
                }
            },
            "embedded_dict": {
                "num": {
                    "required": True,
                    "allowed_types": [int, float]
                },
                "string": {
                    "required": True,
                    "allowed_types": [str]
                }
            }
        }
    }
    obj = {
        "tuple": (1, 2.2, [
            {
                "name": "Daniel",
                "age": 21
            },
            {
                "name": "Saga"
            },
            {
                "name": "Carl",
                "age": 22
            }
        ], "Hello World!", {
            "num": 5,
            "string": "I am a bit of a string."
        })
    }

    succ, errors = check(sample, obj)
    assert len(errors) == 0 and succ, "Basic tuple with list and dict failed."
Esempio n. 13
0
def v1_minecraft_command():
    sample = {"cmd": {"required": True, "allowed_types": [str]}}
    query = request.args.to_dict(flat=True)
    succ, errors = check(sample, query)
    if not succ:
        return make_response(jsonify(errors), 400)
    host = config.get_setting("minecraft-server-host", "127.0.0.1")
    port = config.get_setting("minecraft-server-rcon-port", 25575)
    password = config.get_setting("minecraft-server-password", "password")
    with MCRcon(host, password, port=port) as mcr:
        command = query["cmd"]
        resp = mcr.command(command)
        return make_response(jsonify(resp), 200)
Esempio n. 14
0
def test_4_string_at_number_key():
    sample = {
        "string_key": {
            "required": True,
            "allowed_types": [int, float]
        }
    }
    obj = {
        "string_key": "hello there"
    }

    succ, errors = check(sample, obj)
    assert (not succ), "String at number key failed."
Esempio n. 15
0
def test_1_int_at_string_key():
    sample = {
        "string_key": {
            "required": True,
            "allowed_types": [str]
        }
    }
    obj = {
        "string_key": 5
    }

    succ, errors = check(sample, obj)
    assert (not succ), "Int at string key did not fail."
Esempio n. 16
0
def test_3_float_at_int_key():
    sample = {
        "string_key": {
            "required": True,
            "allowed_types": [int]
        }
    }
    obj = {
        "string_key": 5.1
    }

    succ, errors = check(sample, obj)
    assert (not succ), "Float at int key failed."
Esempio n. 17
0
def test_15_basic_tuple_key():
    sample = {
        "tuple": {
            "required": True,
            "allowed_types": [tuple],
            "tuple_order": [str, int, int, float]
        }
    }
    obj = {
        "tuple": ("Daniel", 5, "10")
    }

    succ, errors = check(sample, obj)
    assert (not succ) and len(errors) == 1, "Basic tuple with different lengths failed."
Esempio n. 18
0
def test_16_basic_tuple_key():
    sample = {
        "tuple": {
            "required": True,
            "allowed_types": [tuple],
            "tuple_order": [str, int, int, float]
        }
    }
    obj = {
        "tuple": ("Daniel", 5, 10, 121.67)
    }

    succ, errors = check(sample, obj)
    assert (succ) and len(errors) == 0, "Basic tuple with correct order and same length failed."
Esempio n. 19
0
def test_6_missing_required_key():
    sample = {
        "string_key": {
            "required": True,
            "allowed_types": [str]
        },
        "number_key": {
            "required": True,
            "allowed_types": [int, float]
        }
    }
    obj = {
        "string_key": "hello there"
    }

    succ, errors = check(sample, obj)
    assert (not succ) and len(errors) == 1, "Missing key didn't fail."
Esempio n. 20
0
def test_5_optional_key():
    sample = {
        "string_key": {
            "required": True,
            "allowed_types": [str]
        },
        "number_key": {
            "required": False,
            "allowed_types": [int, float]
        }
    }
    obj = {
        "string_key": "hello there"
    }

    succ, errors = check(sample, obj)
    assert (succ), "Optional key supplied failed."
Esempio n. 21
0
def test_22_tuple_with_list_wrong():
    sample = {
        "tuple": {
            "required": True,
            "allowed_types": [tuple],
            "tuple_order": [int, float, list, str],
            "list_element": {
                "allowed_types": [str]
            }
        }
    }
    obj = {
        "tuple": (10, 2.2, ["Blue", "Green", "Red", 2], "Hello World!"),
    }

    succ, errors = check(sample, obj)
    assert len(errors) == 1 and not succ, "Basic tuple with list wrong failed."
Esempio n. 22
0
def test_9_basic_list_of_nums():
    sample = {
        "nums": {
            "required": True,
            "allowed_types": [list],
            "list_element": {
                "allowed_types": [int, float]
            }
        }
    }
    obj = {
        "nums": [
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0
        ]
    }

    succ, errors = check(sample, obj)
    assert (succ), "List of nums failed"
Esempio n. 23
0
def test_7_basic_embedded_objects_two_levels():
    sample = {
        "person": {
            "required": True,
            "allowed_types": [dict],
            "embedded_dict": {
                "name": {
                    "required": True,
                    "allowed_types": [str]
                },
                "age": {
                    "required": True,
                    "allowed_types": [int]
                },
                "company": {
                    "required": True,
                    "allowed_types": [dict],
                    "embedded_dict": {
                        "comp_name": {
                            "required": True,
                            "allowed_types": [str]
                        },
                        "employed_date": {
                            "required": False,
                            "allowed_types": [str]
                        }
                    }
                }
            }
        }
    }
    obj = {
        "person": {
            "name": "Daniel Cronqvist",
            "age": 21,
            "company": {
                "comp_name": "Hello World!",
                "employed_date": "2020-02-01"
            }
        }
    }

    succ, errors = check(sample, obj)
    assert (succ), "Basic person data object failed."
Esempio n. 24
0
def test_19_checking_high_depth_overflow():
    sample = {
        "person": {
            "required": True,
            "allowed_types": [dict],
            "embedded_dict": {
                "name": {
                    "required": True,
                    "allowed_types": [str]
                },
                "address": {
                    "required": True,
                    "allowed_types": [dict],
                    "embedded_dict": {
                        "city": {
                            "required": True,
                            "allowed_types": [str]
                        },
                        "street": {
                            "required": True,
                            "allowed_types": [str]
                        }
                    }
                }
            }
        }
    }
    obj = {
        "person": {
            "name": "Daniel Cronqvist",
            "age": 21,
            "address": {
                "city": "Göteborg",
                "street": "Pilegårdsgatan 20B",
                "postal_code": "41877"
            }
        }
    }

    overflow = get_overflow(sample, obj, all_sub=True)
    succ, errors = check(sample, obj, allow_overflow=False)
    assert overflow == ["age", "postal_code"] and not succ, "Basic overflow test with multiple depth failed."
Esempio n. 25
0
def delete_economy_transactions():
    sample_args = {
        "id": {
            "required": True,
            "allowed_types": [str]
        },
        "username": {
            "required": True,
            "allowed_types": [str]
        }
    }
    args = request.args.to_dict(flat=True)
    succ, errors = check(sample_args, args)
    if not succ:
        return make_response(jsonify(errors), 400)
    username = args["username"]
    trans = coll_trans.delete_one({
        "user": username,
        "_id": ObjectId(args["id"])
    })
    return make_response(jsonify(trans.deleted_count), 200)
Esempio n. 26
0
def test_10_basic_list_of_strings():
    sample = {
        "strings": {
            "required": True,
            "allowed_types": [list],
            "list_element": {
                "allowed_types": [str]
            }
        }
    }
    obj = {
        "strings": [
            "Hejsan",
            "där",
            "du",
            "din",
            "lilla",
            "råtta"
        ]
    }

    succ, errors = check(sample, obj)
    assert (succ), "List of strings failed"
Esempio n. 27
0
def get_economy_transactions():
    sample_args_1 = {
        "startDate": {
            "required": True,
            "allowed_types": [str]
        },
        "endDate": {
            "required": True,
            "allowed_types": [str]
        },
        "toAccount": {
            "required": False,
            "allowed_types": [str]
        },
        "fromAccount": {
            "required": False,
            "allowed_types": [str]
        },
        "username": {
            "required": True,
            "allowed_types": [str]
        }
    }
    sample_args_2 = {
        "id": {
            "required": True,
            "allowed_types": [list],
            "list_element": {
                "allowed_types": [str]
            }
        },
        "username": {
            "required": True,
            "allowed_types": [list],
            "list_element": {
                "allowed_types": [str]
            }
        }
    }
    args1 = request.args.to_dict(flat=True)
    succ1, errors1 = check(sample_args_1, args1)
    args2 = request.args.to_dict(flat=False)
    succ2, errors2 = check(sample_args_2, args2)
    if not succ1 and not succ2:
        return make_response(jsonify(errors1 + errors2), 400)
    elif succ2 and not succ1:
        username = args2["username"][0]
        l = [x for x in args2["id"] if len(x) == 24]
        transactions = coll_trans.find({
            "user": username,
            "_id": {
                "$in": [ObjectId(i) for i in l]
            }
        })
        transactions = stringify_ids(list(transactions))
        return make_response(jsonify(transactions), 200)
    elif succ1 and not succ2:
        username = args1["username"]
        find_query = {
            "user": username,
            "date_trans": {
                "$gte": int(args1["startDate"]),
                "$lte": int(args1["endDate"])
            }
        }

        if "toAccount" in args1:
            find_query["to_account"] = int(args1["toAccount"])
        if "fromAccount" in args1:
            find_query["from_account"] = int(args1["fromAccount"])

        transactions = coll_trans.find(find_query)
        transactions = stringify_ids(list(transactions))
        return make_response(jsonify(transactions), 200)
    elif succ1 and succ2:
        return make_response(
            jsonify([
                "ERROR: You may not specify a transaction id if retrieving using dates and accounts."
            ]), 400)
    else:
        return make_response(jsonify(errors1 + errors2), 400)
Esempio n. 28
0
def test_0_base():
    sample = None
    obj = {}
    succ, errors = check(sample, obj)
    assert (not succ), "Undefined sample obj passed."