|
12 | 12 | ) |
13 | 13 | from tests.test_modules.utils import ( |
14 | 14 | ENCODED_AUTH_TOKEN, |
| 15 | + ENCODED_AUTH_TOKEN_V_2_3_0, |
15 | 16 | ClientAuthenticator, |
16 | 17 | ) |
17 | 18 |
|
@@ -488,3 +489,87 @@ def test_push_token_module_uses_emsp_role(): |
488 | 489 | crud.get.assert_awaited_once() |
489 | 490 | call_kwargs = crud.get.call_args |
490 | 491 | assert call_kwargs[0][1] == enums.RoleEnum.emsp |
| 492 | + |
| 493 | + |
| 494 | +def test_push_v_2_3_0_uses_receiver_role_and_base64_token(): |
| 495 | + """Push for v2.3.0 matches endpoints by RECEIVER role and base64-encodes the token.""" |
| 496 | + crud = AsyncMock() |
| 497 | + adapter = MagicMock() |
| 498 | + crud.get.return_value = LOCATIONS[0] |
| 499 | + adapter.location_adapter.return_value.model_dump.return_value = LOCATIONS[0] |
| 500 | + |
| 501 | + app = get_application( |
| 502 | + version_numbers=[VersionNumber.v_2_3_0], |
| 503 | + roles=[enums.RoleEnum.cpo], |
| 504 | + crud=crud, |
| 505 | + adapter=adapter, |
| 506 | + authenticator=ClientAuthenticator, |
| 507 | + modules=[], |
| 508 | + http_push=True, |
| 509 | + ) |
| 510 | + |
| 511 | + client = TestClient(app) |
| 512 | + push_data = schemas.Push( |
| 513 | + module_id=enums.ModuleID.locations, |
| 514 | + object_id="loc-1", |
| 515 | + receivers=[ |
| 516 | + schemas.Receiver( |
| 517 | + endpoints_url="http://example.com/versions", auth_token="token" |
| 518 | + ), |
| 519 | + ], |
| 520 | + ).model_dump() |
| 521 | + |
| 522 | + with patch("ocpi.core.push.httpx.AsyncClient") as mock_client: |
| 523 | + mock_endpoints_response = MagicMock() |
| 524 | + mock_endpoints_response.status_code = 200 |
| 525 | + mock_endpoints_response.json.return_value = { |
| 526 | + "data": { |
| 527 | + "endpoints": [ |
| 528 | + # SENDER endpoint — should be ignored |
| 529 | + { |
| 530 | + "identifier": enums.ModuleID.locations, |
| 531 | + "role": "SENDER", |
| 532 | + "url": "http://example.com/sender/locations/", |
| 533 | + }, |
| 534 | + # RECEIVER endpoint — should be picked up |
| 535 | + { |
| 536 | + "identifier": enums.ModuleID.locations, |
| 537 | + "role": "RECEIVER", |
| 538 | + "url": "http://example.com/locations/", |
| 539 | + }, |
| 540 | + ] |
| 541 | + } |
| 542 | + } |
| 543 | + |
| 544 | + mock_push_response = MagicMock() |
| 545 | + mock_push_response.status_code = 200 |
| 546 | + mock_push_response.json.return_value = {"status_code": 1000} |
| 547 | + |
| 548 | + mock_client.return_value.__aenter__.return_value.get = AsyncMock( |
| 549 | + return_value=mock_endpoints_response |
| 550 | + ) |
| 551 | + mock_client.return_value.__aenter__.return_value.send = AsyncMock( |
| 552 | + return_value=mock_push_response |
| 553 | + ) |
| 554 | + build_request = MagicMock() |
| 555 | + mock_client.return_value.__aenter__.return_value.build_request = build_request |
| 556 | + |
| 557 | + response = client.post( |
| 558 | + "/push/2.3.0", |
| 559 | + json=push_data, |
| 560 | + headers={"Authorization": f"Token {ENCODED_AUTH_TOKEN_V_2_3_0}"}, |
| 561 | + ) |
| 562 | + |
| 563 | + assert response.status_code == 200 |
| 564 | + |
| 565 | + # Token must be base64-encoded for 2.3.0 |
| 566 | + call_args = build_request.call_args |
| 567 | + auth_header = call_args[1]["headers"]["Authorization"] |
| 568 | + assert auth_header.startswith("Token ") |
| 569 | + # The raw "token" string encoded in base64 is "dG9rZW4=" |
| 570 | + assert auth_header == "Token dG9rZW4=" |
| 571 | + |
| 572 | + # Must use the RECEIVER url, not the SENDER url |
| 573 | + url_arg = call_args[0][1] |
| 574 | + assert "sender" not in url_arg |
| 575 | + assert "locations" in url_arg |
0 commit comments