Skip to content

Commit

Permalink
Added import project config
Browse files Browse the repository at this point in the history
  • Loading branch information
rquidute committed Nov 28, 2024
1 parent 30d7087 commit d7c2a5a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
33 changes: 33 additions & 0 deletions app/api/api_v1/endpoints/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import json
from http import HTTPStatus
from typing import List, Sequence, Union

from fastapi import APIRouter, Depends, File, HTTPException, Request, UploadFile
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from pydantic import ValidationError, parse_obj_as
from sqlalchemy.orm import Session
from sqlalchemy.orm.attributes import flag_modified

Expand Down Expand Up @@ -364,3 +366,34 @@ def export_project_config(
jsonable_encoder(project),
**options,
)


@router.post("/import", response_model=schemas.Project)
def importproject_config(
*,
db: Session = Depends(get_db),
import_file: UploadFile = File(...),
) -> models.Project:
"""
Imports a test run execution to the the given project_id.
"""

file_content = import_file.file.read().decode("utf-8")
file_dict = json.loads(file_content)

try:
exported_project: schemas.ProjectCreate = parse_obj_as(
schemas.ProjectCreate, file_dict
)
except ValidationError as error:
raise HTTPException(
status_code=HTTPStatus.UNPROCESSABLE_ENTITY, detail=str(error)
)

try:
return crud.project.create(db=db, obj_in=exported_project)
except TestEnvironmentConfigError as e:
raise HTTPException(
status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
detail=str(e),
)
68 changes: 67 additions & 1 deletion app/tests/api/api_v1/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#
# flake8: noqa
# Ignore flake8 check for this file
import json
from http import HTTPStatus
from io import BytesIO
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -64,6 +66,49 @@
},
}

project_json_data = {
"name": "New Project IMPORTED",
"config": {
"test_parameters": None,
"network": {
"wifi": {"ssid": "testharness", "password": "wifi-password"},
"thread": {
"rcp_serial_path": "/dev/ttyACM0",
"rcp_baudrate": 115200,
"on_mesh_prefix": "fd11:22::/64",
"network_interface": "eth0",
"dataset": {
"channel": "15",
"panid": "0x1234",
"extpanid": "1111111122222222",
"networkkey": "00112233445566778899aabbccddeeff",
"networkname": "DEMO",
},
"otbr_docker_image": None,
},
},
"dut_config": {
"discriminator": "3840",
"setup_code": "20202021",
"pairing_mode": "onnetwork",
"chip_timeout": None,
"chip_use_paa_certs": False,
"trace_log": True,
},
},
"pics": {
"clusters": {
"Access Control cluster": {
"name": "Test PICS",
"items": {
"ACL.S": {"number": "PICS.S", "enabled": False},
"ACL.C": {"number": "PICS.C", "enabled": True},
},
}
}
},
}


def test_create_project_default_config(client: TestClient) -> None:
data: dict[str, Any] = {"name": "Foo"}
Expand Down Expand Up @@ -372,7 +417,7 @@ def test_applicable_test_cases_empty_pics(client: TestClient, db: Session) -> No
assert len(content["test_cases"]) == 0


def test_project_config(client: TestClient, db: Session) -> None:
def test_export_project(client: TestClient, db: Session) -> None:
project = create_random_project_with_pics(db=db, config={})
project_create_schema = schemas.ProjectCreate(**project.__dict__)
# retrieve the project config
Expand All @@ -385,3 +430,24 @@ def test_project_config(client: TestClient, db: Session) -> None:
expected_status_code=HTTPStatus.OK,
expected_content=jsonable_encoder(project_create_schema),
)


def test_import_project(client: TestClient, db: Session) -> None:
imported_file_content = json.dumps(project_json_data).encode("utf-8")
data = BytesIO(imported_file_content)

files = {
"import_file": (
"project.json",
data,
"multipart/form-data",
)
}

response = client.post(f"{settings.API_V1_STR}/projects/import", files=files)

validate_json_response(
response=response,
expected_status_code=HTTPStatus.OK,
expected_content=project_json_data,
)

0 comments on commit d7c2a5a

Please sign in to comment.