-
Notifications
You must be signed in to change notification settings - Fork 14.8k
/
Copy pathtest_rpc_api_endpoint.py
161 lines (137 loc) · 6 KB
/
test_rpc_api_endpoint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
import json
from typing import TYPE_CHECKING, Generator
from unittest import mock
import pytest
from airflow.models.baseoperator import BaseOperator
from airflow.models.connection import Connection
from airflow.models.taskinstance import TaskInstance
from airflow.operators.empty import EmptyOperator
from airflow.serialization.pydantic.taskinstance import TaskInstancePydantic
from airflow.serialization.serialized_objects import BaseSerialization
from airflow.settings import _ENABLE_AIP_44
from airflow.utils.state import State
from airflow.www import app
from tests.test_utils.config import conf_vars
from tests.test_utils.decorators import dont_initialize_flask_app_submodules
pytestmark = pytest.mark.db_test
if TYPE_CHECKING:
from flask import Flask
TEST_METHOD_NAME = "test_method"
TEST_METHOD_WITH_LOG_NAME = "test_method_with_log"
mock_test_method = mock.MagicMock()
@pytest.fixture(scope="session")
def minimal_app_for_internal_api() -> Flask:
@dont_initialize_flask_app_submodules(
skip_all_except=[
"init_appbuilder",
"init_api_internal",
]
)
def factory() -> Flask:
with conf_vars({("webserver", "run_internal_api"): "true"}):
return app.create_app(testing=True, config={"WTF_CSRF_ENABLED": False}) # type:ignore
return factory()
def equals(a, b) -> bool:
return a == b
@pytest.mark.skipif(not _ENABLE_AIP_44, reason="AIP-44 is disabled")
class TestRpcApiEndpoint:
@pytest.fixture(autouse=True)
def setup_attrs(self, minimal_app_for_internal_api: Flask) -> Generator:
self.app = minimal_app_for_internal_api
self.client = self.app.test_client() # type:ignore
mock_test_method.reset_mock()
mock_test_method.side_effect = None
with mock.patch(
"airflow.api_internal.endpoints.rpc_api_endpoint._initialize_map"
) as mock_initialize_map:
mock_initialize_map.return_value = {
TEST_METHOD_NAME: mock_test_method,
}
yield mock_initialize_map
@pytest.mark.parametrize(
"input_params, method_result, result_cmp_func, method_params",
[
("", None, lambda got, _: got == b"", {}),
("", "test_me", equals, {}),
(
json.dumps(BaseSerialization.serialize({"dag_id": 15, "task_id": "fake-task"})),
("dag_id_15", "fake-task", 1),
equals,
{"dag_id": 15, "task_id": "fake-task"},
),
(
"",
TaskInstance(task=EmptyOperator(task_id="task"), run_id="run_id", state=State.RUNNING),
lambda a, b: a.model_dump() == TaskInstancePydantic.model_validate(b).model_dump()
and isinstance(a.task, BaseOperator),
{},
),
(
"",
Connection(conn_id="test_conn", conn_type="http", host="", password=""),
lambda a, b: a.get_uri() == b.get_uri() and a.conn_id == b.conn_id,
{},
),
],
)
def test_method(self, input_params, method_result, result_cmp_func, method_params):
mock_test_method.return_value = method_result
input_data = {
"jsonrpc": "2.0",
"method": TEST_METHOD_NAME,
"params": input_params,
}
response = self.client.post(
"/internal_api/v1/rpcapi",
headers={"Content-Type": "application/json"},
data=json.dumps(input_data),
)
assert response.status_code == 200
if method_result:
response_data = BaseSerialization.deserialize(json.loads(response.data), use_pydantic_models=True)
else:
response_data = response.data
assert result_cmp_func(response_data, method_result)
mock_test_method.assert_called_once_with(**method_params, session=mock.ANY)
def test_method_with_exception(self):
mock_test_method.side_effect = ValueError("Error!!!")
data = {"jsonrpc": "2.0", "method": TEST_METHOD_NAME, "params": ""}
response = self.client.post(
"/internal_api/v1/rpcapi", headers={"Content-Type": "application/json"}, data=json.dumps(data)
)
assert response.status_code == 500
assert response.data, b"Error executing method: test_method."
mock_test_method.assert_called_once()
def test_unknown_method(self):
data = {"jsonrpc": "2.0", "method": "i-bet-it-does-not-exist", "params": ""}
response = self.client.post(
"/internal_api/v1/rpcapi", headers={"Content-Type": "application/json"}, data=json.dumps(data)
)
assert response.status_code == 400
assert response.data == b"Unrecognized method: i-bet-it-does-not-exist."
mock_test_method.assert_not_called()
def test_invalid_jsonrpc(self):
data = {"jsonrpc": "1.0", "method": TEST_METHOD_NAME, "params": ""}
response = self.client.post(
"/internal_api/v1/rpcapi", headers={"Content-Type": "application/json"}, data=json.dumps(data)
)
assert response.status_code == 400
assert response.data == b"Expected jsonrpc 2.0 request."
mock_test_method.assert_not_called()