Skip to content

Commit

Permalink
Added the support to check the informed configurations for dut config
Browse files Browse the repository at this point in the history
  • Loading branch information
rquidute committed Jan 11, 2024
1 parent 7682985 commit 0023bd3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
30 changes: 29 additions & 1 deletion app/api/api_v1/endpoints/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from http import HTTPStatus
from typing import List, Sequence

from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
from fastapi import APIRouter, Depends, File, HTTPException, Request, UploadFile
from sqlalchemy.orm import Session
from sqlalchemy.orm.attributes import flag_modified

Expand Down Expand Up @@ -61,6 +61,7 @@ def create_project(
*,
db: Session = Depends(get_db),
project_in: schemas.ProjectCreate,
request: Request,
) -> models.Project:
"""Create new project
Expand All @@ -70,6 +71,9 @@ def create_project(
Returns:
Project: newly created project record
"""
# Validade dut config informed arguments
__validate_dut_config(request=request)

return crud.project.create(db=db, obj_in=project_in)


Expand All @@ -83,12 +87,34 @@ def default_config() -> schemas.TestEnvironmentConfig:
return default_environment_config


def __validate_dut_config(request: Request) -> None:
if "config" in request._json and "dut_config" in request._json["config"]:
dut_config = request._json["config"]["dut_config"]

valid_fields = [
"discriminator",
"setup_code",
"pairing_mode",
"chip_timeout",
"chip_use_paa_certs",
]

for field, _ in dut_config.items():
if field not in valid_fields:
raise HTTPException(
status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
detail="Dut config has invalid configuration informed."
f" The valid configuration are: {valid_fields}",
)


@router.put("/{id}", response_model=schemas.Project)
def update_project(
*,
db: Session = Depends(get_db),
id: int,
project_in: schemas.ProjectUpdate,
request: Request,
) -> models.Project:
"""Update an existing project
Expand All @@ -102,6 +128,8 @@ def update_project(
Returns:
Project: updated project record
"""
# Validade dut config informed arguments
__validate_dut_config(request=request)

return crud.project.update(db=db, db_obj=__project(db=db, id=id), obj_in=project_in)

Expand Down
65 changes: 65 additions & 0 deletions app/tests/api/api_v1/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# flake8: noqa
# Ignore flake8 check for this file
from http import HTTPStatus
from pathlib import Path
from typing import Any
Expand All @@ -33,6 +35,36 @@
from app.tests.utils.test_pics_data import create_random_project_with_pics
from app.tests.utils.validate_json_response import validate_json_response

invalid_dut_config = {
"name": "foo",
"config": {
"network": {
"fabric_id": "0",
"thread": {
"dataset": {
"channel": "15",
"panid": "0x1234",
"extpanid": "1111111122222222",
"networkkey": "00112233445566778899aabbccddeeff",
"networkname": "DEMO",
},
"rcp_serial_path": "/dev/ttyACM0",
"rcp_baudrate": 115200,
"on_mesh_prefix": "fd11:22::/64",
"network_interface": "eth0",
},
"wifi": {"ssid": "testharness", "password": "wifi-password"},
},
"dut_config": {
"pairing_mode": "onnetwork",
"setup_code": "20202021",
"discriminator": "3840",
"chip_use_paa_certs": "false",
"invalid_arg": "any value",
},
},
}


def test_create_project_default_config(client: TestClient) -> None:
data: dict[str, Any] = {"name": "Foo"}
Expand Down Expand Up @@ -69,6 +101,22 @@ def test_create_project_custom_config(client: TestClient) -> None:
)


def test_create_project_invalid_dut_config(client: TestClient) -> None:
response = client.post(
f"{settings.API_V1_STR}/projects/",
json=invalid_dut_config,
)

validate_json_response(
response=response,
expected_status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
expected_content={
"detail": "Dut config has invalid configuration informed. The valid configuration are: ['discriminator', 'setup_code', 'pairing_mode', 'chip_timeout', 'chip_use_paa_certs']"
},
expected_keys=["detail"],
)


def test_default_project_config(client: TestClient) -> None:
response = client.get(
f"{settings.API_V1_STR}/projects/default_config",
Expand Down Expand Up @@ -151,6 +199,23 @@ def test_update_project(client: TestClient, db: Session) -> None:
)


def test_update_project_invalid_dut_config(client: TestClient, db: Session) -> None:
project = create_random_project(db)
response = client.put(
f"{settings.API_V1_STR}/projects/{project.id}",
json=invalid_dut_config,
)

validate_json_response(
response=response,
expected_status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
expected_content={
"detail": "Dut config has invalid configuration informed. The valid configuration are: ['discriminator', 'setup_code', 'pairing_mode', 'chip_timeout', 'chip_use_paa_certs']"
},
expected_keys=["detail"],
)


def test_delete_project(client: TestClient, db: Session) -> None:
project = create_random_project(db)
response = client.delete(f"{settings.API_V1_STR}/projects/{project.id}")
Expand Down

0 comments on commit 0023bd3

Please sign in to comment.