Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
jx2lee committed Dec 19, 2024
1 parent 823a6f0 commit a0ae953
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
83 changes: 83 additions & 0 deletions tests/api_fastapi/execution_api/routes/test_task_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,39 @@ def test_ti_run_state_conflict_if_not_queued(

assert session.scalar(select(TaskInstance.state).where(TaskInstance.id == ti.id)) == initial_ti_state

def test_ti_run_failed_with_extra(self, client, session, create_task_instance, time_machine):
"""
Test that a 422 error is returned when extra fields are included in the payload.
"""
instant_str = "2024-12-19T00:00:00Z"
instant = timezone.parse(instant_str)
time_machine.move_to(instant, tick=False)

ti = create_task_instance(
task_id="test_ti_run_failed_with_extra",
state=State.QUEUED,
session=session,
start_date=instant,
)

session.commit()

response = client.patch(
f"/execution/task-instances/{ti.id}/run",
json={
"state": "running",
"hostname": "random-hostname",
"unixname": "random-unixname",
"pid": 100,
"start_date": instant_str,
"foo": "bar",
},
)

assert response.status_code == 422
assert response.json()["detail"][0]["type"] == "extra_forbidden"
assert response.json()["detail"][0]["msg"] == "Extra inputs are not permitted"


class TestTIUpdateState:
def setup_method(self):
Expand Down Expand Up @@ -340,6 +373,27 @@ def test_ti_update_state_to_reschedule(self, client, session, create_task_instan
assert trs[0].map_index == -1
assert trs[0].duration == 129600

def test_ti_update_state_failed_with_extra(self, client, session, create_task_instance, time_machine):
"""
Test that a 422 error is returned when extra fields are included in the payload.
"""
ti = create_task_instance(
task_id="test_ti_update_state_failed_with_extra",
state=State.RUNNING,
session=session,
start_date=DEFAULT_START_DATE,
)

session.commit()

response = client.patch(
f"/execution/task-instances/{ti.id}/state", json={"state": "scheduled", "foo": "bar"}
)

assert response.status_code == 422
assert response.json()["detail"][0]["type"] == "extra_forbidden"
assert response.json()["detail"][0]["msg"] == "Extra inputs are not permitted"


class TestTIHealthEndpoint:
def setup_method(self):
Expand Down Expand Up @@ -536,6 +590,35 @@ def test_ti_update_state_to_failed_table_check(self, client, session, create_tas
assert ti.next_kwargs is None
assert ti.duration == 3600.00

def test_ti_heartbeat_with_extra(
self,
client,
session,
create_task_instance,
time_machine,
):
"""
Test that a 422 error is returned when extra fields are included in the payload.
"""
ti = create_task_instance(
task_id="test_ti_heartbeat_when_task_not_running",
state=State.RUNNING,
hostname="random-hostname",
pid=1547,
session=session,
)
session.commit()
task_instance_id = ti.id

response = client.put(
f"/execution/task-instances/{task_instance_id}/heartbeat",
json={"hostname": "random-hostname", "pid": 1547, "foo": "bar"},
)

assert response.status_code == 422
assert response.json()["detail"][0]["type"] == "extra_forbidden"
assert response.json()["detail"][0]["msg"] == "Extra inputs are not permitted"


class TestTIPutRTIF:
def setup_method(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/api_fastapi/execution_api/routes/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_variable_get_from_db(self, client, session):
{"AIRFLOW_VAR_KEY1": "VALUE"},
)
def test_variable_get_from_env_var(self, client, session):
response = client.get("/execution/variables/key1", params={"foo": "bar"})
response = client.get("/execution/variables/key1")

assert response.status_code == 200
assert response.json() == {"key": "key1", "value": "VALUE"}
Expand Down Expand Up @@ -136,7 +136,7 @@ def test_variable_missing_mandatory_fields(self, client, key, status_code, paylo
),
pytest.param(
"key",
{"value": "{}", "description": "description", "lorem": "ipsum", "foo": "bar"},
{"value": "{}", "description": "description", "lorem": "ipsum"},
id="adding-extra-fields",
),
],
Expand Down

0 comments on commit a0ae953

Please sign in to comment.