File: test_tenant_views.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 (124 lines) | stat: -rw-r--r-- 3,263 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
import pytest
from django.apps import apps
from django.test import Client

from django_pgschemas.routing.info import DomainInfo
from django_pgschemas.schema import Schema


@pytest.fixture
def UserModel():
    return apps.get_model("shared_common.User")


@pytest.fixture(autouse=True)
def _setup(UserModel, db):
    with Schema.create("www"):
        UserModel.objects.create(email="user_www@localhost", display_name="Admin")

    with Schema.create("blog"):
        UserModel.objects.create(email="user_blog@localhost", display_name="Admin")


@pytest.fixture
def _setup_dynamic(tenant1, UserModel, DomainModel):
    if DomainModel is None:
        pytest.skip("Domain model is not in use")

    DomainModel.objects.create(tenant=tenant1, domain="tenant1.localhost", is_primary=True)
    DomainModel.objects.create(tenant=tenant1, domain="everyone.localhost", folder="tenant1")

    with tenant1:
        yield UserModel.objects.create(email="user1@localhost", display_name="Admin")


@pytest.mark.parametrize(
    "url, expected_status",
    [
        ("/", 200),
        ("/register/", 200),
        ("/admin/", 302),
        ("/non-existing/", 404),
    ],
)
def test_views_www(url, expected_status):
    client = Client(headers={"host": "localhost"})

    response = client.get(url)

    assert response.status_code == expected_status


@pytest.mark.parametrize(
    "url, expected_status",
    [
        ("/", 200),
        ("/entries/", 200),
        ("/admin/", 302),
        ("/non-existing/", 404),
    ],
)
def test_views_blog(url, expected_status):
    client = Client(headers={"host": "blog.localhost"})

    response = client.get(url)

    assert response.status_code == expected_status


@pytest.mark.parametrize(
    "url, expected_status",
    [
        ("/", 200),
        ("/profile/", 200),
        ("/profile/advanced/", 302),
        ("/login/", 200),
        ("/admin/", 302),
        ("/non-existing/", 404),
    ],
)
def test_tenants_domain(url, expected_status, _setup_dynamic):
    user = _setup_dynamic
    client = Client(headers={"host": "tenant1.localhost"})

    response = client.get(url)

    assert response.status_code == expected_status

    if expected_status == "200":
        assert response.context == {
            "path": url,
            "user": user,
            "schema": "tenant1",
            "routing": DomainInfo(domain="tenant1.localhost", folder=""),
            "admin_url": "/admin/",
        }


@pytest.mark.parametrize(
    "url, expected_status",
    [
        ("/tenant1/", 200),
        ("/tenant1/profile/", 200),
        ("/tenant1/profile/advanced/", 302),
        ("/tenant1/login/", 200),
        ("/tenant1/admin/", 302),
        ("/tenant1/non-existing/", 404),
    ],
)
def test_tenants_folder(url, expected_status, _setup_dynamic):
    user = _setup_dynamic
    client = Client(headers={"host": "everyone.localhost"})

    response = client.get(url)

    assert response.status_code == expected_status

    if expected_status == "200":
        assert response.context == {
            "path": url,
            "user": user,
            "schema": "tenant1",
            "routing": DomainInfo(domain="everyone.localhost", folder="tenant1"),
            "admin_url": "/tenant1/admin/",
        }