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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
import pytest
from django.apps import apps
from django.core.checks import Critical, Error, Warning
from django.core.exceptions import ImproperlyConfigured
from django_pgschemas import checks
@pytest.fixture
def app_config():
return apps.get_app_config("django_pgschemas")
@pytest.fixture
def tenant_manager(TenantModel, db):
if TenantModel is None:
yield None
else:
backup = TenantModel.auto_create_schema
TenantModel.auto_create_schema = False
yield TenantModel._default_manager
TenantModel.auto_create_schema = backup
def test_get_tenant_app(tenants_settings, TenantModel):
if TenantModel:
assert checks.get_tenant_app() == "sandbox.shared_public"
del tenants_settings["default"]
assert checks.get_tenant_app() is None
def test_get_domain_app(tenants_settings, DomainModel):
if DomainModel:
assert checks.get_domain_app() == "sandbox.shared_public"
del tenants_settings["default"]["DOMAIN_MODEL"]
assert checks.get_domain_app() is None
def test_get_user_app(settings):
assert checks.get_user_app() == "sandbox.shared_common"
del settings.AUTH_USER_MODEL
assert checks.get_user_app() is None
def test_get_session_app(settings):
assert checks.get_session_app() == "django.contrib.sessions"
def test_ensure_tenant_dict(settings):
backup = settings.TENANTS
del settings.TENANTS
with pytest.raises(ImproperlyConfigured) as ctx:
checks.ensure_tenant_dict()
assert str(ctx.value) == "TENANTS dict setting not set."
settings.TENANTS = backup
class TestEnsurePublicSchema:
@pytest.mark.parametrize("value", [None, "", 1])
def test_no_dict(self, tenants_settings, value):
tenants_settings["public"] = value
with pytest.raises(ImproperlyConfigured) as ctx:
checks.ensure_public_schema()
assert str(ctx.value) == "TENANTS must contain a 'public' dict."
@pytest.mark.parametrize(
"member",
[
"URLCONF",
"WS_URLCONF",
"DOMAINS",
"FALLBACK_DOMAINS",
"SESSION_KEY",
"HEADER",
],
)
def test_invalid_members(self, tenants_settings, member):
tenants_settings["public"][member] = None
with pytest.raises(ImproperlyConfigured) as ctx:
checks.ensure_public_schema()
assert str(ctx.value) == f"TENANTS['public'] cannot contain a '{member}' key."
class TestEnsureDefaultSchema:
def test_static_only(self, tenants_settings):
if "default" in tenants_settings:
del tenants_settings["default"]
checks.ensure_default_schemas()
@pytest.mark.parametrize("value", [None, "", 1])
def test_no_dict(self, tenants_settings, value):
tenants_settings["default"] = value
with pytest.raises(ImproperlyConfigured) as ctx:
checks.ensure_default_schemas()
assert str(ctx.value) == "TENANTS must contain a 'default' dict."
@pytest.mark.parametrize(
"member",
[
"DOMAINS",
"FALLBACK_DOMAINS",
"SESSION_KEY",
"HEADER",
],
)
def test_invalid_members(self, tenants_settings, member):
if "default" not in tenants_settings:
pytest.skip("default not in tenant settings")
tenants_settings["default"][member] = None
with pytest.raises(ImproperlyConfigured) as ctx:
checks.ensure_default_schemas()
assert str(ctx.value) == f"TENANTS['default'] cannot contain a '{member}' key."
@pytest.mark.parametrize(
"member",
[
"TENANT_MODEL",
"URLCONF",
],
)
def test_required_members(self, tenants_settings, member):
if "default" not in tenants_settings:
pytest.skip("default not in tenant settings")
del tenants_settings["default"][member]
with pytest.raises(ImproperlyConfigured) as ctx:
checks.ensure_default_schemas()
assert str(ctx.value) == f"TENANTS['default'] must contain a '{member}' key."
@pytest.mark.parametrize(
"name",
[
"public",
"default",
"blog",
"www",
],
)
def test_clone_reference_invalid_name(self, tenants_settings, name):
if "default" not in tenants_settings:
pytest.skip("default not in tenant settings")
tenants_settings["default"]["CLONE_REFERENCE"] = name
with pytest.raises(ImproperlyConfigured) as ctx:
checks.ensure_default_schemas()
assert (
str(ctx.value) == "TENANTS['default']['CLONE_REFERENCE'] must be a unique schema name."
)
class TestEnsureOverallSchema:
@pytest.mark.parametrize(
"name",
[
"pg_something",
"1something",
".something",
"-something",
"tomanycharacters0123456789abcdef0123456789abcef0123456789abcdef0",
],
)
def test_invalid_names(self, tenants_settings, name):
tenants_settings[name] = {}
with pytest.raises(ImproperlyConfigured) as ctx:
checks.ensure_overall_schemas()
assert str(ctx.value) == f"'{name}' is not a valid schema name."
@pytest.mark.parametrize(
"extra",
[
"public",
"blog",
"www",
# "default",
],
)
def test_ensure_extra_search_paths(settings, extra, db):
settings.PGSCHEMAS_EXTRA_SEARCH_PATHS = [extra]
with pytest.raises(ImproperlyConfigured) as ctx:
checks.ensure_extra_search_paths()
invalid = ", ".join([extra])
assert str(ctx.value) == f"Do not include '{invalid}' on PGSCHEMAS_EXTRA_SEARCH_PATHS."
class TestCheckPrincipalApps:
BASE_DEFAULT = {"TENANT_MODEL": "shared_public.Tenant", "DOMAIN_MODEL": "shared_public.DOMAIN"}
def test_location_wrong(self, tenants_settings, app_config):
tenants_settings.update(
{
"public": {"APPS": []},
"default": self.BASE_DEFAULT,
}
)
expected_errors = [
Error(
"Your tenant app 'sandbox.shared_public' must be on the 'public' schema.",
id="pgschemas.W001",
),
Error(
"Your domain app 'sandbox.shared_public' must be on the 'public' schema.",
id="pgschemas.W001",
),
]
errors = checks.check_principal_apps(app_config)
assert errors == expected_errors
def test_location_twice(self, tenants_settings, app_config):
tenants_settings.update(
{
"public": {"APPS": ["sandbox.shared_public"]},
"default": {**self.BASE_DEFAULT, "APPS": ["sandbox.shared_public"]},
}
)
expected_errors = [
Error(
"Your tenant app 'sandbox.shared_public' in TENANTS['default']['APPS'] "
"must be on the 'public' schema only.",
id="pgschemas.W001",
),
Error(
"Your domain app 'sandbox.shared_public' in TENANTS['default']['APPS'] "
"must be on the 'public' schema only.",
id="pgschemas.W001",
),
]
errors = checks.check_principal_apps(app_config)
assert errors == expected_errors
class TestCheckOtherApps:
def test_contenttypes_location_wrong(self, tenants_settings, app_config):
tenants_settings.update(
{
"default": {"APPS": ["django.contrib.contenttypes"]},
}
)
expected_errors = [
Warning(
"'django.contrib.contenttypes' in TENANTS['default']['APPS'] "
"must be on 'public' schema only.",
id="pgschemas.W002",
)
]
errors = checks.check_other_apps(app_config)
assert errors == expected_errors
def test_contenttypes_location_twice(self, tenants_settings, app_config):
tenants_settings.update(
{
"default": {},
"www": {"APPS": ["django.contrib.contenttypes"]},
}
)
expected_errors = [
Warning(
"'django.contrib.contenttypes' in TENANTS['www']['APPS'] "
"must be on 'public' schema only.",
id="pgschemas.W002",
)
]
errors = checks.check_other_apps(app_config)
assert errors == expected_errors
def test_user_location_wrong(self, tenants_settings, app_config):
user_app = checks.get_user_app()
tenants_settings.update(
{
"default": {"APPS": ["django.contrib.sessions"]},
}
)
expected_errors = [
Warning(
f"'{user_app}' must be together with 'django.contrib.sessions' "
"in TENANTS['default']['APPS'].",
id="pgschemas.W003",
)
]
errors = checks.check_other_apps(app_config)
assert errors == expected_errors
def test_session_location_wrong(self, tenants_settings, app_config):
user_app = checks.get_user_app()
tenants_settings.update(
{
"www": {"APPS": ["shared_common", user_app]},
"default": {"APPS": ["shared_common"]},
}
)
expected_errors = [
Warning(
f"'django.contrib.sessions' must be together with '{user_app}' "
"in TENANTS['www']['APPS'].",
id="pgschemas.W003",
)
]
errors = checks.check_other_apps(app_config)
assert errors == expected_errors
@pytest.mark.parametrize("schema", ["public", "www", "blog", "sample"])
def test_check_schema_names(schema, app_config, tenant_manager):
if tenant_manager is None:
pytest.skip("Dynamic tenants are not in use")
tenant_manager.create(schema_name=schema)
expected_errors = [
Critical(
f"Name clash found between static and dynamic tenants: {{'{schema}'}}",
id="pgschemas.W004",
),
]
errors = checks.check_schema_names(app_config)
assert errors == expected_errors
|