Ejemplo n.º 1
0
async def test_too_many_rooms_created(app: Starlette) -> None:
    for i in range(MAX_ROOMS_PER_TEN_MINUTES):
        async with emulated_client.connect(app, f'/{uuid4()}') as client:
            await client.receive_json()

    with pytest.raises(WebsocketClosed) as e:
        async with emulated_client.connect(app, f'/{uuid4()}') as client:
            await client.receive_json()

    assert e.value.code == ERR_TOO_MANY_ROOMS_CREATED
Ejemplo n.º 2
0
async def test_bypass_room_create_rate_limit(app: Starlette) -> None:
    headers = {BYPASS_RATE_LIMIT_HEADER: TEST_BYPASS_RATE_LIMIT_KEY}
    for i in range(MAX_ROOMS_PER_TEN_MINUTES):
        async with emulated_client.connect(
            app, f'/{uuid4()}', headers=headers
        ) as client:
            await client.receive_json()

    async with emulated_client.connect(app, f'/{uuid4()}', headers=headers) as client:
        assert await client.receive_json() == {'type': 'connected', 'data': []}
Ejemplo n.º 3
0
async def test_bypass_room_create_rate_limit_invalid_key(app: Starlette) -> None:
    headers = {BYPASS_RATE_LIMIT_HEADER: "invalid-key"}
    for i in range(MAX_ROOMS_PER_TEN_MINUTES):
        async with emulated_client.connect(
            app, f'/{uuid4()}', headers=headers
        ) as client:
            await client.receive_json()

    with pytest.raises(WebsocketClosed) as e:
        async with emulated_client.connect(
            app, f'/{uuid4()}', headers=headers
        ) as client:
            await client.receive_json()

    assert e.value.code == ERR_TOO_MANY_ROOMS_CREATED
Ejemplo n.º 4
0
 async def connect(i: int) -> EmulatedClient:
     ip = f'127.0.0.{i}' if unique_ips else '127.0.0.1'
     room_id = uuid4() if unique_rooms else ROOM_ID
     return await stack.enter_async_context(
         emulated_client.connect(
             app, f'/{room_id}', client_ip=ip, headers=headers
         )
     )
Ejemplo n.º 5
0
async def test_bypass_max_connections_rate_limit(app: Starlette) -> None:
    headers = {BYPASS_RATE_LIMIT_HEADER: TEST_BYPASS_RATE_LIMIT_KEY}
    async with num_active_connections(
        app, MAX_CONNECTIONS_PER_USER, unique_ips=False, headers=headers
    ):
        async with emulated_client.connect(
            app, f'/{uuid4()}', headers=headers
        ) as client:
            assert await client.receive_json() == {'type': 'connected', 'data': []}
Ejemplo n.º 6
0
async def test_too_many_user_connections(app: Starlette) -> None:
    async with num_active_connections(app, MAX_CONNECTIONS_PER_USER, unique_ips=False):
        with pytest.raises(WebsocketClosed) as e:
            async with emulated_client.connect(
                app, f'/{uuid4()}', client_ip='127.0.0.1'
            ) as websocket:
                await websocket.receive_json()

    assert e.value.code == ERR_TOO_MANY_CONNECTIONS
Ejemplo n.º 7
0
async def test_invalid_request(app: Starlette) -> None:
    with pytest.raises(WebsocketClosed) as e:
        async with emulated_client.connect(app, f'/{ROOM_ID}') as client:
            # Wait for the initial connection message
            await client.receive_json()
            await client.send('{invalid_json')
            await client.receive_json()

    assert e.value.code == ERR_INVALID_REQUEST
Ejemplo n.º 8
0
async def test_too_many_room_connections(app: Starlette) -> None:
    async with num_active_connections(
        app, MAX_CONNECTIONS_PER_ROOM, unique_rooms=False
    ):
        with pytest.raises(WebsocketClosed) as e:
            async with emulated_client.connect(
                app, f'/{ROOM_ID}', client_ip='127.0.0.255'
            ) as client:
                await client.receive_json()

        assert e.value.code == ERR_ROOM_FULL
Ejemplo n.º 9
0
async def test_bypass_max_connections_rate_limit_invalid_key(app: Starlette) -> None:
    headers = {BYPASS_RATE_LIMIT_HEADER: "invalid-key"}
    async with num_active_connections(
        app, MAX_CONNECTIONS_PER_USER, unique_ips=False, headers=headers
    ):
        with pytest.raises(WebsocketClosed) as e:
            async with emulated_client.connect(
                app, f'/{uuid4()}', headers=headers
            ) as websocket:
                await websocket.receive_json()

        assert e.value.code == ERR_TOO_MANY_CONNECTIONS
Ejemplo n.º 10
0
async def test_add_token(app: Starlette) -> None:
    async with emulated_client.connect(app, f'/{ROOM_ID}') as client:
        # Grab the connection message
        await client.receive_json()
        await client.send_json(
            {
                'request_id': TEST_REQUEST_ID,
                'actions': [TEST_UPSERT_TOKEN],
            }
        )

        assert_matches(
            await client.receive_json(),
            {
                'type': 'update',
                'request_id': TEST_REQUEST_ID,
                'actions': [TEST_UPSERT_TOKEN],
            },
        )
Ejemplo n.º 11
0
async def test_invalid_uuid(app: Starlette) -> None:
    with pytest.raises(WebsocketClosed) as e:
        async with emulated_client.connect(app, '/invalid-uuid'):
            pass

    assert e.value.code == ERR_INVALID_UUID
Ejemplo n.º 12
0
async def test_connect(app: Starlette) -> None:
    async with emulated_client.connect(app, f'/{ROOM_ID}') as client:
        assert await client.receive_json() == {'type': 'connected', 'data': []}