File: test_channels.py

package info (click to toggle)
python-django-pgschemas 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 848 kB
  • sloc: python: 3,887; makefile: 33; sh: 10; sql: 2
file content (129 lines) | stat: -rw-r--r-- 3,433 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import pytest
from channels.testing import WebsocketCommunicator

from sandbox.routing import domain_application, headers_application


@pytest.mark.asyncio
async def test_on_main_domain():
    communicator = WebsocketCommunicator(
        domain_application,
        "/ws/main/",
        headers=[(b"host", b"localhost")],
    )
    connected, subprotocol = await communicator.connect()

    assert connected

    # Send a message
    await communicator.send_json_to({"message": "hello"})

    # Receive the message
    response = await communicator.receive_json_from()
    assert response["message"] == "www: hello"

    # Close the connection
    await communicator.disconnect()


@pytest.mark.asyncio
async def test_on_tenant_subdomain(tenant3, DomainModel):
    if DomainModel is None:
        pytest.skip("Domain model is not in use")

    await DomainModel.objects.acreate(tenant=tenant3, domain="ws_tenant3.localhost")

    communicator = WebsocketCommunicator(
        domain_application,
        "/ws/tenant/",
        headers=[(b"host", b"ws_tenant3.localhost")],
    )
    connected, subprotocol = await communicator.connect()

    assert connected

    # Send a message
    await communicator.send_json_to({"message": "hello"})

    # Receive the message
    response = await communicator.receive_json_from()
    assert response["message"] == "tenant3: hello"

    # Close the connection
    await communicator.disconnect()


@pytest.mark.asyncio
async def test_on_tenant_subfolder(tenant3, DomainModel):
    if DomainModel is None:
        pytest.skip("Domain model is not in use")

    await DomainModel.objects.acreate(
        tenant=tenant3, domain="ws_tenants.localhost", folder="tenant3"
    )

    communicator = WebsocketCommunicator(
        domain_application,
        "/tenant3/ws/tenant/",
        headers=[(b"host", b"ws_tenants.localhost")],
    )
    connected, subprotocol = await communicator.connect()

    assert connected

    # Send a message
    await communicator.send_json_to({"message": "hello"})

    # Receive the message
    response = await communicator.receive_json_from()
    assert response["message"] == "tenant3: hello"

    # Close the connection
    await communicator.disconnect()


@pytest.mark.asyncio
async def test_on_main_header():
    communicator = WebsocketCommunicator(
        headers_application,
        "/ws/main/",
        headers=[(b"tenant", b"main")],
    )
    connected, subprotocol = await communicator.connect()

    assert connected

    # Send a message
    await communicator.send_json_to({"message": "hello"})

    # Receive the message
    response = await communicator.receive_json_from()
    assert response["message"] == "www: hello"

    # Close the connection
    await communicator.disconnect()


@pytest.mark.asyncio
async def test_on_tenant_header(TenantModel, db):
    if TenantModel is None:
        pytest.skip("Dynamic tenants are not in use")

    communicator = WebsocketCommunicator(
        headers_application,
        "/ws/tenant/",
        headers=[(b"tenant", b"tenant3")],
    )
    connected, subprotocol = await communicator.connect()

    assert connected

    # Send a message
    await communicator.send_json_to({"message": "hello"})

    # Receive the message
    response = await communicator.receive_json_from()
    assert response["message"] == "tenant3: hello"

    # Close the connection
    await communicator.disconnect()