|
5 | 5 | import pytest_asyncio
|
6 | 6 | from channels.db import database_sync_to_async
|
7 | 7 | from channels.testing import WebsocketCommunicator
|
| 8 | +from django.conf import settings |
8 | 9 | from django.utils import timezone
|
9 | 10 | from pydantic.error_wrappers import ValidationError
|
10 | 11 |
|
| 12 | +from aap_eda.asgi import application |
11 | 13 | from aap_eda.core import models
|
12 | 14 | from aap_eda.wsapi.consumers import AnsibleRulebookConsumer
|
13 | 15 |
|
|
45 | 47 | DUMMY_UUID2 = "8472ff2c-6045-4418-8d4e-46f6cfffffff"
|
46 | 48 |
|
47 | 49 |
|
| 50 | +@pytest.mark.parametrize("path", ["ws/unexpected", "unexpected"]) |
| 51 | +async def test_invalid_websocket_route(path: str): |
| 52 | + """Test that the websocket consumer rejects unsupported routes.""" |
| 53 | + communicator = WebsocketCommunicator(application, path) |
| 54 | + |
| 55 | + connected, _ = await communicator.connect() |
| 56 | + assert connected, "Connection failed" |
| 57 | + |
| 58 | + response = await communicator.receive_from() |
| 59 | + assert response == '{"error": "invalid path"}', "Invalid error message" |
| 60 | + close_message = await communicator.receive_output() |
| 61 | + assert ( |
| 62 | + close_message["type"] == "websocket.close" |
| 63 | + ), "Did not receive close message" |
| 64 | + |
| 65 | + await communicator.disconnect() |
| 66 | + |
| 67 | + |
| 68 | +async def test_valid_websocket_route_wrong_type(): |
| 69 | + """Test that the websocket consumer rejects unsupported types.""" |
| 70 | + communicator = WebsocketCommunicator( |
| 71 | + application, |
| 72 | + f"{settings.API_PREFIX}/ws/ansible-rulebook", |
| 73 | + ) |
| 74 | + connected, _ = await communicator.connect() |
| 75 | + assert connected, "Connection failed" |
| 76 | + nothing = await communicator.receive_nothing() |
| 77 | + assert nothing, "Received unexpected message" |
| 78 | + await communicator.send_to(text_data='{"type": "unsuported_type"}') |
| 79 | + response = await communicator.receive_from() |
| 80 | + assert ( |
| 81 | + response == '{"error": "unsupported message type"}' |
| 82 | + ), "Unexpected error message" |
| 83 | + await communicator.disconnect() |
| 84 | + |
| 85 | + |
48 | 86 | @pytest.mark.django_db(transaction=True)
|
49 | 87 | async def test_handle_workers(ws_communicator: WebsocketCommunicator):
|
50 | 88 | activation_instance_with_extra_var = await _prepare_db_data()
|
|
0 commit comments