Skip to content

Commit 4b845df

Browse files
Refactors socket connection setup in project state tests
Replaces direct websocket connection calls with a unified helper for setting up socket connections, improving test readability and maintainability. Ensures connections are only created for authorized or non-anonymous users, reducing redundant code.
1 parent e9b432a commit 4b845df

File tree

1 file changed

+76
-77
lines changed

1 file changed

+76
-77
lines changed

services/web/server/tests/unit/with_dbs/02/test_projects_states_handlers.py

Lines changed: 76 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,9 @@ async def test_open_project(
479479
logged_user: UserInfoDict,
480480
user_project: ProjectDict,
481481
client_session_id_factory: Callable[[], str],
482-
socketio_client_factory: Callable,
482+
create_socketio_connection_with_handlers: Callable[
483+
[TestClient, str], Awaitable[tuple[socketio.AsyncClient, _SocketHandlers]]
484+
],
483485
expected: HTTPStatus,
484486
save_state: bool,
485487
mocked_dynamic_services_interface: dict[str, mock.Mock],
@@ -494,12 +496,11 @@ async def test_open_project(
494496
# open project
495497
assert client.app
496498
client_id = client_session_id_factory()
497-
await _connect_websocket(
498-
socketio_client_factory,
499-
expected != status.HTTP_401_UNAUTHORIZED,
500-
client,
501-
client_id,
502-
)
499+
500+
# Only create socketio connection for non-anonymous users
501+
if expected != status.HTTP_401_UNAUTHORIZED:
502+
await create_socketio_connection_with_handlers(client, client_id)
503+
503504
url = client.app.router["open_project"].url_for(project_id=user_project["uuid"])
504505
resp = await client.post(f"{url}", json=client_id)
505506

@@ -571,7 +572,10 @@ async def test_open_project__in_debt(
571572
logged_user: UserInfoDict,
572573
user_project: ProjectDict,
573574
client_session_id_factory: Callable[[], str],
574-
socketio_client_factory: Callable,
575+
# socketio_client_factory: Callable,
576+
create_socketio_connection_with_handlers: Callable[
577+
[TestClient, str], Awaitable[tuple[socketio.AsyncClient, _SocketHandlers]]
578+
],
575579
expected: HTTPStatus,
576580
mocked_dynamic_services_interface: dict[str, mock.Mock],
577581
mock_service_resources: ServiceResourcesDict,
@@ -610,12 +614,9 @@ async def test_open_project__in_debt(
610614
# POST /v0/projects/{project_id}:open
611615
assert client.app
612616
client_id = client_session_id_factory()
613-
await _connect_websocket(
614-
socketio_client_factory,
615-
check_connection=True, # USER role always connects
616-
client=client,
617-
client_id=client_id,
618-
)
617+
618+
await create_socketio_connection_with_handlers(client, client_id)
619+
619620
url = client.app.router["open_project"].url_for(project_id=user_project["uuid"])
620621
resp = await client.post(f"{url}", json=client_id)
621622
await assert_status(resp, expected)
@@ -637,7 +638,9 @@ async def test_open_template_project_for_edition(
637638
logged_user: UserInfoDict,
638639
create_template_project: Callable[..., Awaitable[ProjectDict]],
639640
client_session_id_factory: Callable[[], str],
640-
socketio_client_factory: Callable,
641+
create_socketio_connection_with_handlers: Callable[
642+
[TestClient, str], Awaitable[tuple[socketio.AsyncClient, _SocketHandlers]]
643+
],
641644
expected: HTTPStatus,
642645
save_state: bool,
643646
mocked_dynamic_services_interface: dict[str, mock.Mock],
@@ -658,12 +661,10 @@ async def test_open_template_project_for_edition(
658661
}
659662
)
660663
client_id = client_session_id_factory()
661-
await _connect_websocket(
662-
socketio_client_factory,
663-
check_connection=expected != status.HTTP_401_UNAUTHORIZED,
664-
client=client,
665-
client_id=client_id,
666-
)
664+
665+
# Only create socketio connection for non-anonymous users
666+
if expected != status.HTTP_401_UNAUTHORIZED:
667+
await create_socketio_connection_with_handlers(client, client_id)
667668
url = client.app.router["open_project"].url_for(project_id=template_project["uuid"])
668669
resp = await client.post(f"{url}", json=client_id)
669670
await assert_status(resp, expected)
@@ -724,7 +725,9 @@ async def test_open_template_project_for_edition_with_missing_write_rights(
724725
logged_user: UserInfoDict,
725726
create_template_project: Callable[..., Awaitable[ProjectDict]],
726727
client_session_id_factory: Callable[[], str],
727-
socketio_client_factory: Callable,
728+
create_socketio_connection_with_handlers: Callable[
729+
[TestClient, str], Awaitable[tuple[socketio.AsyncClient, _SocketHandlers]]
730+
],
728731
expected: HTTPStatus,
729732
mocked_dynamic_services_interface: dict[str, mock.Mock],
730733
mock_service_resources: ServiceResourcesDict,
@@ -741,12 +744,10 @@ async def test_open_template_project_for_edition_with_missing_write_rights(
741744
}
742745
)
743746
client_id = client_session_id_factory()
744-
await _connect_websocket(
745-
socketio_client_factory,
746-
check_connection=expected != status.HTTP_401_UNAUTHORIZED,
747-
client=client,
748-
client_id=client_id,
749-
)
747+
748+
# Only create socketio connection for non-anonymous users
749+
if expected != status.HTTP_401_UNAUTHORIZED:
750+
await create_socketio_connection_with_handlers(client, client_id)
750751
url = client.app.router["open_project"].url_for(project_id=template_project["uuid"])
751752
resp = await client.post(f"{url}", json=client_id)
752753
await assert_status(resp, expected)
@@ -758,7 +759,9 @@ async def test_open_project_with_small_amount_of_dynamic_services_starts_them_au
758759
logged_user: UserInfoDict,
759760
user_project_with_num_dynamic_services: Callable[[int], Awaitable[ProjectDict]],
760761
client_session_id_factory: Callable[[], str],
761-
socketio_client_factory: Callable,
762+
create_socketio_connection_with_handlers: Callable[
763+
[TestClient, str], Awaitable[tuple[socketio.AsyncClient, _SocketHandlers]]
764+
],
762765
expected: ExpectedResponse,
763766
mocked_dynamic_services_interface: dict[str, mock.Mock],
764767
mock_catalog_api: dict[str, mock.Mock],
@@ -785,12 +788,9 @@ async def test_open_project_with_small_amount_of_dynamic_services_starts_them_au
785788
]
786789

787790
client_id = client_session_id_factory()
788-
await _connect_websocket(
789-
socketio_client_factory,
790-
check_connection=True, # standard_user_role is always USER or TESTER
791-
client=client,
792-
client_id=client_id,
793-
)
791+
# Only create socketio connection for non-anonymous users
792+
if expected.ok:
793+
await create_socketio_connection_with_handlers(client, client_id)
794794
url = client.app.router["open_project"].url_for(project_id=project["uuid"])
795795
resp = await client.post(f"{url}", json=client_id)
796796
await assert_status(resp, expected.ok)
@@ -812,7 +812,9 @@ async def test_open_project_with_disable_service_auto_start_set_overrides_behavi
812812
logged_user: UserInfoDict,
813813
user_project_with_num_dynamic_services: Callable[[int], Awaitable[ProjectDict]],
814814
client_session_id_factory: Callable[[], str],
815-
socketio_client_factory: Callable,
815+
create_socketio_connection_with_handlers: Callable[
816+
[TestClient, str], Awaitable[tuple[socketio.AsyncClient, _SocketHandlers]]
817+
],
816818
expected: ExpectedResponse,
817819
mocked_dynamic_services_interface: dict[str, mock.Mock],
818820
mock_catalog_api: dict[str, mock.Mock],
@@ -833,12 +835,9 @@ async def test_open_project_with_disable_service_auto_start_set_overrides_behavi
833835
]
834836

835837
client_id = client_session_id_factory()
836-
sio = await _connect_websocket(
837-
socketio_client_factory,
838-
check_connection=True, # standard_user_role is always USER or TESTER
839-
client=client,
840-
client_id=client_id,
841-
)
838+
# Only create socketio connection for non-anonymous users
839+
if expected.ok:
840+
sio = await create_socketio_connection_with_handlers(client, client_id)
842841
url = (
843842
client.app.router["open_project"]
844843
.url_for(project_id=project["uuid"])
@@ -847,7 +846,8 @@ async def test_open_project_with_disable_service_auto_start_set_overrides_behavi
847846

848847
resp = await client.post(f"{url}", json=client_id)
849848
await assert_status(resp, expected.ok)
850-
await sio.disconnect()
849+
if expected.ok:
850+
await sio[0].disconnect()
851851
mocked_notifications_plugin["subscribe"].assert_called_once_with(
852852
client.app, ProjectID(project["uuid"])
853853
)
@@ -863,7 +863,9 @@ async def test_open_project_with_large_amount_of_dynamic_services_does_not_start
863863
logged_user: UserInfoDict,
864864
user_project_with_num_dynamic_services: Callable[[int], Awaitable[ProjectDict]],
865865
client_session_id_factory: Callable[[], str],
866-
socketio_client_factory: Callable,
866+
create_socketio_connection_with_handlers: Callable[
867+
[TestClient, str], Awaitable[tuple[socketio.AsyncClient, _SocketHandlers]]
868+
],
867869
expected: ExpectedResponse,
868870
mocked_dynamic_services_interface: dict[str, mock.Mock],
869871
mock_catalog_api: dict[str, mock.Mock],
@@ -892,12 +894,9 @@ async def test_open_project_with_large_amount_of_dynamic_services_does_not_start
892894
]
893895

894896
client_id = client_session_id_factory()
895-
await _connect_websocket(
896-
socketio_client_factory,
897-
check_connection=True, # standard_user_role is always USER or TESTER
898-
client=client,
899-
client_id=client_id,
900-
)
897+
# Only create socketio connection for non-anonymous users
898+
if expected.ok:
899+
await create_socketio_connection_with_handlers(client, client_id)
901900
url = client.app.router["open_project"].url_for(project_id=project["uuid"])
902901
resp = await client.post(f"{url}", json=client_id)
903902
await assert_status(resp, expected.ok)
@@ -918,7 +917,9 @@ async def test_open_project_with_large_amount_of_dynamic_services_starts_them_if
918917
logged_user: UserInfoDict,
919918
user_project_with_num_dynamic_services: Callable[[int], Awaitable[ProjectDict]],
920919
client_session_id_factory: Callable[[], str],
921-
socketio_client_factory: Callable,
920+
create_socketio_connection_with_handlers: Callable[
921+
[TestClient, str], Awaitable[tuple[socketio.AsyncClient, _SocketHandlers]]
922+
],
922923
expected: ExpectedResponse,
923924
mocked_dynamic_services_interface: dict[str, mock.Mock],
924925
mock_catalog_api: dict[str, mock.Mock],
@@ -950,12 +951,9 @@ async def test_open_project_with_large_amount_of_dynamic_services_starts_them_if
950951
]
951952

952953
client_id = client_session_id_factory()
953-
await _connect_websocket(
954-
socketio_client_factory,
955-
check_connection=True, # standard_user_role is always USER or TESTER
956-
client=client,
957-
client_id=client_id,
958-
)
954+
# Only create socketio connection for non-anonymous users
955+
if expected.ok:
956+
await create_socketio_connection_with_handlers(client, client_id)
959957
url = client.app.router["open_project"].url_for(project_id=project["uuid"])
960958
resp = await client.post(f"{url}", json=client_id)
961959
await assert_status(resp, expected.ok)
@@ -974,7 +972,9 @@ async def test_open_project_with_deprecated_services_ok_but_does_not_start_dynam
974972
logged_user,
975973
user_project,
976974
client_session_id_factory: Callable[[], str],
977-
socketio_client_factory: Callable,
975+
create_socketio_connection_with_handlers: Callable[
976+
[TestClient, str], Awaitable[tuple[socketio.AsyncClient, _SocketHandlers]]
977+
],
978978
expected: ExpectedResponse,
979979
mocked_dynamic_services_interface: dict[str, mock.Mock],
980980
mock_service_resources: ServiceResourcesDict,
@@ -986,12 +986,9 @@ async def test_open_project_with_deprecated_services_ok_but_does_not_start_dynam
986986
datetime.now(UTC) - timedelta(days=1)
987987
).isoformat()
988988
client_id = client_session_id_factory()
989-
await _connect_websocket(
990-
socketio_client_factory,
991-
check_connection=True, # standard_user_role is always USER or TESTER
992-
client=client,
993-
client_id=client_id,
994-
)
989+
# Only create socketio connection for non-anonymous users
990+
if expected.ok:
991+
await create_socketio_connection_with_handlers(client, client_id)
995992
url = client.app.router["open_project"].url_for(project_id=user_project["uuid"])
996993
resp = await client.post(url, json=client_id)
997994
await assert_status(resp, expected.ok)
@@ -1034,7 +1031,9 @@ async def test_open_project_more_than_limitation_of_max_studies_open_per_user(
10341031
client: TestClient,
10351032
logged_user,
10361033
client_session_id_factory: Callable[[], str],
1037-
socketio_client_factory: Callable,
1034+
create_socketio_connection_with_handlers: Callable[
1035+
[TestClient, str], Awaitable[tuple[socketio.AsyncClient, _SocketHandlers]]
1036+
],
10381037
user_project: ProjectDict,
10391038
shared_project: ProjectDict,
10401039
expected: ExpectedResponse,
@@ -1044,12 +1043,12 @@ async def test_open_project_more_than_limitation_of_max_studies_open_per_user(
10441043
mocked_notifications_plugin: dict[str, mock.Mock],
10451044
):
10461045
client_id_1 = client_session_id_factory()
1047-
await _connect_websocket(
1048-
socketio_client_factory,
1049-
user_role != UserRole.ANONYMOUS,
1050-
client,
1051-
client_id_1,
1052-
)
1046+
# Only create socketio connection for non-anonymous users
1047+
if user_role != UserRole.ANONYMOUS:
1048+
await create_socketio_connection_with_handlers(
1049+
client,
1050+
client_id_1,
1051+
)
10531052
await _open_project(
10541053
client,
10551054
client_id_1,
@@ -1058,12 +1057,12 @@ async def test_open_project_more_than_limitation_of_max_studies_open_per_user(
10581057
)
10591058

10601059
client_id_2 = client_session_id_factory()
1061-
await _connect_websocket(
1062-
socketio_client_factory,
1063-
user_role != UserRole.ANONYMOUS,
1064-
client,
1065-
client_id_2,
1066-
)
1060+
# Only create socketio connection for non-anonymous users
1061+
if user_role != UserRole.ANONYMOUS:
1062+
await create_socketio_connection_with_handlers(
1063+
client,
1064+
client_id_2,
1065+
)
10671066
await _open_project(
10681067
client,
10691068
client_id_2,

0 commit comments

Comments
 (0)