コード例 #1
0
ファイル: setup.py プロジェクト: ericwburden/hearthlight
def query_permission_setup(
    db: Session,
    *,
    permission_type: PermissionTypeEnum,
    permission_enabled: bool = True
) -> Dict[str, Union[models.QueryInterface, models.Permission,
                     models.UserGroup, models.User]]:
    query = create_random_query_interface(db)
    user = create_random_user(db)
    user_group = create_random_user_group(db)
    permission = crud.query.get_permission(db,
                                           id=query.id,
                                           permission_type=permission_type)
    crud.user_group.add_user(db, user_group=user_group, user_id=user.id)
    crud.permission.grant(db,
                          user_group_id=user_group.id,
                          permission_id=permission.id)
    if not permission_enabled:
        crud.permission.revoke(db,
                               user_group_id=user_group.id,
                               permission_id=permission.id)
    return {
        "query": query,
        "permission": permission,
        "user_group": user_group,
        "user": user,
    }
コード例 #2
0
def test_read_form_input_interface_fail_not_superuser(
        client: TestClient, normal_user_token_headers: dict,
        db: Session) -> None:
    """If the user attempting to access the endpoint is not a superuser"""
    form_input = create_random_query_interface(db)
    response = client.get(
        f"{settings.API_V1_STR}/interfaces/queries/{form_input.id}",
        headers=normal_user_token_headers,
    )
    content = response.json()
    assert response.status_code == 400
    assert content["detail"] == "The user is not a superuser"
コード例 #3
0
def test_query_interface_run_query(client: TestClient,
                                   superuser_token_headers: dict,
                                   db: Session) -> None:
    """Successfully run a query interface query"""
    query = create_random_query_interface(db)
    response = client.get(
        f"{settings.API_V1_STR}/interfaces/queries/{query.id}/run",
        headers=superuser_token_headers,
    )
    content = response.json()
    assert response.status_code == 200
    assert content
コード例 #4
0
def test_delete_query_interface_fail_not_superuser(
        client: TestClient, normal_user_token_headers: dict,
        db: Session) -> None:
    """Fail if the user is not a superuser"""
    query = create_random_query_interface(db)
    response = client.delete(
        f"{settings.API_V1_STR}/interfaces/queries/{query.id}",
        headers=normal_user_token_headers,
    )
    content = response.json()
    assert response.status_code == 400
    assert content["detail"] == "The user is not a superuser"
コード例 #5
0
def test_delete_query_interface(client: TestClient,
                                superuser_token_headers: dict,
                                db: Session) -> None:
    """Successfully delete a queryinterface"""
    query = create_random_query_interface(db)
    response = client.delete(
        f"{settings.API_V1_STR}/interfaces/queries/{query.id}",
        headers=superuser_token_headers,
    )
    stored_query = crud.query.get(db, id=query.id)
    content = response.json()
    assert response.status_code == 200
    assert content["name"] == query.name
    assert stored_query is None
コード例 #6
0
def test_read_form_input_interface(client: TestClient,
                                   superuser_token_headers: dict,
                                   db: Session) -> None:
    """Successfully retrieve a query by ID"""
    query = create_random_query_interface(db)
    response = client.get(
        f"{settings.API_V1_STR}/interfaces/queries/{query.id}",
        headers=superuser_token_headers,
    )
    content = response.json()
    assert response.status_code == 200
    assert content["id"] == query.id
    assert content["name"] == query.name
    assert content["interface_type"] == QUERY_INTERFACE_TYPE
    assert content["template"] == query.template
    assert content["refresh_interval"] == query.refresh_interval
コード例 #7
0
def test_update_query_interface(client: TestClient,
                                superuser_token_headers: dict,
                                db: Session) -> None:
    """Successfully update a query interface"""
    query = create_random_query_interface(db)
    data = {"name": random_lower_string()}
    response = client.put(
        f"{settings.API_V1_STR}/interfaces/queries/{query.id}",
        headers=superuser_token_headers,
        json=data,
    )
    content = response.json()
    assert response.status_code == 200
    assert content["name"] == data["name"]
    assert content["id"] == query.id
    assert content["interface_type"] == QUERY_INTERFACE_TYPE
    assert content["template"] == query.template
    assert content["refresh_interval"] == query.refresh_interval
コード例 #8
0
def test_read_multi_query_interface(client: TestClient,
                                    superuser_token_headers: dict,
                                    db: Session) -> None:
    """Successfully retrieve multiple interfaces"""
    form_inputs = [create_random_query_interface(db) for i in range(10)]
    response = client.get(
        f"{settings.API_V1_STR}/interfaces/queries/",
        headers=superuser_token_headers,
    )
    content = response.json()
    assert response.status_code == 200
    for form_input in form_inputs:
        found_match = False
        for stored_form_input in content["records"]:
            id_match = stored_form_input["id"] == form_input.id
            name_match = stored_form_input["name"] == form_input.name
            type_match = stored_form_input[
                "interface_type"] == QUERY_INTERFACE_TYPE
            template_match = stored_form_input[
                "template"] == form_input.template
            if id_match and name_match and type_match and template_match:
                found_match = True
                break
        assert found_match
コード例 #9
0
def create_random_interface(db):
    die = random.randint(0, 100)
    if die < 50:
        return create_random_form_input_interface(db)
    return create_random_query_interface(db)