예제 #1
0
def add_collection(datasets: list = None) -> uuid.UUID:
    """
    Add a collection that can be used for tests.

    The "edit" user is the editor.

    Args:
      datasets (list): List of dataset uuids to use for the collection.

    Returns:
        uuid.UUID: The _id of the collection.
    """
    mongo_db = db_connection()
    indata = structure.collection()
    edit_user = mongo_db["users"].find_one({"auth_ids": USERS["edit"]})
    indata.update(
        {
            "description": "Added by fixture.",
            "title": "Test title from fixture",
            "tags": ["fromFixture", "testing"],
            "editors": [edit_user["_id"]],
            "datasets": datasets or [],
        }
    )
    indata.update(TEST_LABEL)
    mongo_db["collections"].insert_one(indata)
    return indata["_id"]
예제 #2
0
def get_collection_data_structure():
    """
    Get an empty collection entry.

    Returns:
        flask.Response: JSON structure of a collection.
    """
    empty_collection = structure.collection()
    empty_collection["_id"] = ""
    return utils.response_json({"collection": empty_collection})
예제 #3
0
def add_collection():
    """
    Add a collection.

    Returns:
        flask.Response: Json structure with the ``_id`` of the collection.
    """
    perm_status = utils.req_check_permissions(["DATA_EDIT"])
    if perm_status != 200:
        flask.abort(status=perm_status)

    # create new collection
    collection = structure.collection()

    jsondata = flask.request.json
    if not jsondata or "collection" not in jsondata or not isinstance(
            jsondata["collection"], dict):
        flask.abort(status=400)
    indata = jsondata["collection"]

    # indata validation
    validation = utils.basic_check_indata(indata,
                                          collection,
                                          prohibited=["_id"])
    if not validation.result:
        flask.abort(status=validation.status)

    if not indata.get("editors"):
        indata["editors"] = [flask.g.current_user["_id"]]
    # add current user if missing and only DATA_EDIT
    elif (not utils.req_has_permission("DATA_MANAGEMENT")
          and str(flask.g.current_user["_id"]) not in indata["editors"]):
        indata["editors"].append(flask.g.current_user["_id"])

    indata = utils.prepare_for_db(indata)

    collection.update(indata)

    # add to db
    result = utils.req_commit_to_db("collections", "add", collection)
    if not result.log or not result.data:
        flask.abort(status=500)

    return utils.response_json({"_id": result.ins_id})
예제 #4
0
def add_dataset_full():
    """
    Add an order with a dataset.

    Returns:
        tuple: (order_uuid, dataset_uuid)
    """
    mongo_db = db_connection()
    # prepare
    order_indata = structure.order()
    order_indata.update({"description": "Added by fixture.", "title": "Test title from fixture"})
    order_indata.update(TEST_LABEL)
    edit_user = mongo_db["users"].find_one({"auth_ids": USERS["edit"]})
    order_indata["authors"] = [edit_user["_id"]]
    order_indata["editors"] = [edit_user["_id"]]
    order_indata["generators"] = [edit_user["_id"]]
    order_indata["organisation"] = edit_user["_id"]

    dataset_indata = structure.dataset()
    dataset_indata.update({"description": "Added by fixture.", "title": "Test title from fixture"})
    dataset_indata.update(TEST_LABEL)

    collection_indata = structure.collection()
    collection_indata.update(
        {
            "description": "Added by fixture.",
            "title": "Test title from fixture",
            "editors": [edit_user["_id"]],
        }
    )
    collection_indata.update(TEST_LABEL)

    mongo_db["datasets"].insert_one(dataset_indata)
    order_indata["datasets"].append(dataset_indata["_id"])
    collection_indata["datasets"].append(dataset_indata["_id"])
    mongo_db["orders"].insert_one(order_indata)
    mongo_db["collections"].insert_one(collection_indata)
    return (order_indata["_id"], dataset_indata["_id"], collection_indata["_id"])
def gen_collections(db, nr_collections: int = 300):
    datasets = tuple(db["datasets"].find())
    user_re = re.compile(".*::local")
    facility_re = re.compile("facility[0-9]*::local")
    facilities = tuple(db["users"].find({"auth_ids": facility_re}))
    users = tuple(
        db["users"].find({"$and": [{"auth_ids": user_re}, {"permissions": "DATA_EDIT"}]})
    )
    for i in range(1, nr_collections + 1):
        collection = structure.collection()
        changes = {
            "description": make_description(),
            "datasets": [
                random.choice(datasets)["_id"] for _ in range(random.randint(0, 5))
            ],
            "editors": [
                random.choice(users + facilities)["_id"]
                for _ in range(random.randint(1, 5))
            ],
            "title": f"Collection {i} Title",
        }
        if random.random() > 0.4:
            key = random.choice(EXTRA_KEYS)
            changes["properties"] = {key: random.choice(EXTRA_FIELDS[key])}
            changes["tags"] = list(
                set(random.choice(EXTRA_VALUES) for _ in range(random.randint(0, 6)))
            )
        collection.update(changes)
        db["collections"].insert_one(collection)
        make_log(
            db,
            action="add",
            data=collection,
            data_type="collection",
            comment="Generated",
            user="******",
        )
def gen_frontend_test_entries(db):
    apikey = {"salt": "abc", "key": "frontend"}
    apihash = utils.gen_api_key_hash(apikey["key"], apikey["salt"])
    changes = [
        {
            "_id": "u-4f2418f7-2609-43f1-8c7f-82c08e6daf26",
            "affiliation": "Frontend Test University",
            "api_key": apihash,
            "api_salt": apikey["salt"],
            "auth_ids": [f"author::frontend"],
            "email": f"*****@*****.**",
            "contact": f"*****@*****.**",
            "name": f"Frontend Author",
            "permissions": [],
            "url": "https://www.example.com/frontend_author",
        },
        {
            "_id": "u-d54dc97d-ff9e-4e73-86bb-9c7a029e1b43",
            "affiliation": "Frontend Test University",
            "api_key": apihash,
            "api_salt": apikey["salt"],
            "auth_ids": [f"generator::frontend"],
            "email": f"*****@*****.**",
            "contact": f"*****@*****.**",
            "name": f"Frontend Generator",
            "permissions": ["DATA_EDIT"],
            "url": "https://www.example.com/frontend_generator",
        },
        {
            "_id": "u-a5a7534b-1b49-41a5-b909-738e49cd137d",
            "affiliation": "Frontend Test University",
            "api_key": apihash,
            "api_salt": apikey["salt"],
            "auth_ids": [f"organisation::frontend"],
            "email": f"*****@*****.**",
            "contact": f"*****@*****.**",
            "name": f"Frontend Organisation",
            "permissions": ["DATA_MANAGEMENT", "USER_MANAGEMENT"],
            "url": "https://www.example.com/frontend_organisation",
        },
        {
            "_id": "u-3a9a19a7-cd30-4c7b-b280-e35220e1a611",
            "affiliation": "Frontend Test University",
            "api_key": apihash,
            "api_salt": apikey["salt"],
            "auth_ids": [f"editor::frontend"],
            "email": f"*****@*****.**",
            "contact": f"*****@*****.**",
            "name": f"Frontend Editor",
            "permissions": ["DATA_EDIT"],
            "url": "https://www.example.com/frontend_editor",
        },
    ]
    for entry in changes:
        user = structure.user()
        user.update(entry)
        db["users"].insert_one(user)
        make_log(
            db,
            action="add",
            data=user,
            data_type="user",
            comment="Generated",
            user="******",
        )

    order = structure.order()
    changes = {
        "_id": "o-d4467732-8ddd-43a6-a904-5b7376f60e5c",
        "authors": ["u-4f2418f7-2609-43f1-8c7f-82c08e6daf26"],
        "generators": ["u-d54dc97d-ff9e-4e73-86bb-9c7a029e1b43"],
        "organisation": "u-a5a7534b-1b49-41a5-b909-738e49cd137d",
        "editors": ["u-3a9a19a7-cd30-4c7b-b280-e35220e1a611"],
        "description": "An order added for frontend tests",
        "title": f"Frontend Test Order",
        "properties": {"Type": "Frontend Test Entry"},
        "tags": ["Frontend", "Test"],
        "datasets": ["d-79a755f1-69b0-4734-9977-ac945c4c51c1",
                     "d-27cc1144-67bf-45b2-af21-425f9bfc7333"]
    }
    order.update(changes)
    db["orders"].insert_one(order)
    make_log(
        db,
        action="add",
        data=order,
        data_type="order",
        comment="Generated",
        user="******",
    )

    dataset = structure.dataset()
    changes = {
        "_id": "d-79a755f1-69b0-4734-9977-ac945c4c51c1",
        "description": "A dataset added for frontend tests",
        "title": f"Frontend Test Dataset",
        "properties": {"Type": "Frontend Test Entry"},
        "tags": ["Frontend", "Test"],
    }
    dataset.update(changes)
    db["datasets"].insert_one(dataset)
    make_log(
        db,
        action="add",
        data=dataset,
        data_type="dataset",
        comment="Generated",
        user="******",
    )
    dataset = structure.dataset()
    changes = {
        "_id": "d-27cc1144-67bf-45b2-af21-425f9bfc7333",
        "description": "A dataset added for frontend tests 2",
        "title": f"Frontend Test Dataset 2",
        "properties": {"Type": "Frontend Test Entry"},
        "tags": ["Frontend", "Test"],
    }
    dataset.update(changes)
    db["datasets"].insert_one(dataset)
    make_log(
        db,
        action="add",
        data=dataset,
        data_type="dataset",
        comment="Generated",
        user="******",
    )

    collection = structure.collection()
    changes = {
        "_id": "c-21c8ecd1-9908-462f-ba84-3ca399074b36",
        "editors": ["u-3a9a19a7-cd30-4c7b-b280-e35220e1a611"],
        "description": "A collection added for frontend tests",
        "title": f"Frontend Test Collection",
        "properties": {"Type": "Frontend Test Entry"},
        "tags": ["Frontend", "Test"],
        "datasets": ["d-79a755f1-69b0-4734-9977-ac945c4c51c1",
                     "d-27cc1144-67bf-45b2-af21-425f9bfc7333"]
    }
    collection.update(changes)
    db["collections"].insert_one(collection)
    make_log(
        db,
        action="add",
        data=dataset,
        data_type="collection",
        comment="Generated",
        user="******",
    )