|
23 | 23 |
|
24 | 24 | from iam.contrib.django.dispatcher import DjangoBasicResourceApiDispatcher, InvalidPageException |
25 | 25 | from iam.exceptions import AuthInvalidOperation |
26 | | -from iam.resource.provider import ListResult, ResourceProvider |
| 26 | +from iam.resource.provider import ListResult, SchemaResult, ResourceProvider |
27 | 27 |
|
28 | 28 |
|
29 | 29 | def test_basic_resource_api_dispatcher_register(): |
@@ -53,6 +53,12 @@ def list_instance_by_policy(self, filter, page, **options): |
53 | 53 | def search_instance(self, filter, page, **options): |
54 | 54 | return ListResult(results=[filter, page], count=100) |
55 | 55 |
|
| 56 | + def fetch_instance_list(self, filter, page, **options): |
| 57 | + return ListResult(results=[filter, page], count=100) |
| 58 | + |
| 59 | + def fetch_resource_type_schema(self, **options): |
| 60 | + return SchemaResult(properties={"id": {"type": "string"}}) |
| 61 | + |
56 | 62 | with pytest.raises(AuthInvalidOperation): |
57 | 63 | dispatcher.register("type", "provider") |
58 | 64 |
|
@@ -257,12 +263,16 @@ def __init__(self): |
257 | 263 | self.fetch_instance_info_spy = {} |
258 | 264 | self.list_instance_by_policy_spy = {} |
259 | 265 | self.search_instance_spy = {} |
| 266 | + self.fetch_instance_list_spy = {} |
| 267 | + self.fetch_resource_type_schema_spy = {} |
260 | 268 | self.pre_list_attr = MagicMock() |
261 | 269 | self.pre_list_attr_value = MagicMock() |
262 | 270 | self.pre_list_instance = MagicMock() |
263 | 271 | self.pre_fetch_instance_info = MagicMock() |
264 | 272 | self.pre_list_instance_by_policy = MagicMock() |
265 | 273 | self.pre_search_instance = MagicMock() |
| 274 | + self.pre_fetch_instance_list = MagicMock() |
| 275 | + self.pre_fetch_resource_type_schema = MagicMock() |
266 | 276 |
|
267 | 277 | def list_attr(self, **options): |
268 | 278 | self.list_attr_spy["options"] = options |
@@ -297,6 +307,16 @@ def search_instance(self, filter, page, **options): |
297 | 307 | self.search_instance_spy["options"] = options |
298 | 308 | return ListResult(results=["search_instance_token"], count=100) |
299 | 309 |
|
| 310 | + def fetch_instance_list(self, filter, page, **options): |
| 311 | + self.fetch_instance_list_spy["filter"] = filter |
| 312 | + self.fetch_instance_list_spy["page"] = page |
| 313 | + self.fetch_instance_list_spy["options"] = options |
| 314 | + return ListResult(results=["fetch_instance_list_token"], count=100) |
| 315 | + |
| 316 | + def fetch_resource_type_schema(self, **options): |
| 317 | + self.fetch_resource_type_schema_spy["options"] = options |
| 318 | + return SchemaResult(properties={"fetch_resource_type_schema_token"}) |
| 319 | + |
300 | 320 | provider = SpyResourceProvider() |
301 | 321 | dispatcher.register("spy", provider) |
302 | 322 |
|
@@ -450,3 +470,48 @@ def search_instance(self, filter, page, **options): |
450 | 470 | "filter": {"expression": "expression"}, |
451 | 471 | "page": {"limit": "limit", "offset": "offset"}, |
452 | 472 | } |
| 473 | + |
| 474 | + # test fetch_instance_list |
| 475 | + fetch_instance_list_req = MagicMock() |
| 476 | + fetch_instance_list_req.body = json.dumps( |
| 477 | + { |
| 478 | + "method": "fetch_instance_list", |
| 479 | + "type": "spy", |
| 480 | + "filter": {"start_time": 1654012800, "end_time": 1654099199}, |
| 481 | + "page": {"limit": "limit", "offset": "offset"}, |
| 482 | + } |
| 483 | + ) |
| 484 | + fetch_instance_list_req.META = {"HTTP_X_REQUEST_ID": "rid", "HTTP_BLUEKING_LANGUAGE": "en"} |
| 485 | + |
| 486 | + resp = dispatcher._dispatch(fetch_instance_list_req) |
| 487 | + |
| 488 | + provider.pre_fetch_instance_list.assert_called_once_with( |
| 489 | + {"start_time": 1654012800, "end_time": 1654099199}, |
| 490 | + {"limit": "limit", "offset": "offset"}, |
| 491 | + language="en", |
| 492 | + ) |
| 493 | + assert resp["code"] == 0 |
| 494 | + assert resp["result"] is True |
| 495 | + assert resp["data"] == {"count": 100, "results": ["fetch_instance_list_token"]} |
| 496 | + assert resp["X-Request-Id"] == "rid" |
| 497 | + assert "message" in resp |
| 498 | + assert provider.fetch_instance_list_spy == { |
| 499 | + "options": {"language": "en"}, |
| 500 | + "filter": {"start_time": 1654012800, "end_time": 1654099199}, |
| 501 | + "page": {"limit": "limit", "offset": "offset"}, |
| 502 | + } |
| 503 | + |
| 504 | + # test fetch_resource_type_schema |
| 505 | + fetch_resource_type_schema_req = MagicMock() |
| 506 | + fetch_resource_type_schema_req.body = json.dumps({"method": "fetch_resource_type_schema", "type": "spy"}) |
| 507 | + fetch_resource_type_schema_req.META = {"HTTP_X_REQUEST_ID": "rid", "HTTP_BLUEKING_LANGUAGE": "en"} |
| 508 | + |
| 509 | + resp = dispatcher._dispatch(fetch_resource_type_schema_req) |
| 510 | + |
| 511 | + provider.pre_fetch_resource_type_schema.assert_called_once_with(language="en") |
| 512 | + assert resp["code"] == 0 |
| 513 | + assert resp["result"] is True |
| 514 | + assert resp["data"] == SchemaResult(properties={"fetch_resource_type_schema_token"}).to_dict() |
| 515 | + assert resp["X-Request-Id"] == "rid" |
| 516 | + assert "message" in resp |
| 517 | + assert provider.fetch_resource_type_schema_spy == {"options": {"language": "en"}} |
0 commit comments