|
| 1 | +from typing import TYPE_CHECKING |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +import pytest |
| 5 | +from django.core.exceptions import ValidationError |
| 6 | + |
| 7 | +from hope_live.utils.flags import debug, hostname, superuser, validate_bool |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from django.http import HttpRequest |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize("value", ["true", "1", "yes", "t", "y", "false", "0", "no", "f", "n"]) |
| 14 | +def test_validate_bool(value): |
| 15 | + validate_bool(value) # No exception should be raised |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize("value", ["a", "9"]) |
| 19 | +def test_validate_bool_fail(value): |
| 20 | + with pytest.raises(ValidationError): |
| 21 | + assert validate_bool(value) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.parametrize("value", ["t", "tRue", "yes", "1"]) |
| 25 | +def test_superuser(rf, value, admin_user): |
| 26 | + request = rf.get("/") |
| 27 | + request.user = admin_user |
| 28 | + assert superuser(value, request) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("value", ["t", "tRue", "yes", "1"]) |
| 32 | +def test_debug(settings, value): |
| 33 | + settings.DEBUG = True |
| 34 | + assert debug(value) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize("value", ["myserver.com", "myserver.com:888", "myserver.com:443"]) |
| 38 | +def test_hostname(value, rf): |
| 39 | + request: HttpRequest = rf.get("/") |
| 40 | + with mock.patch.object(request, "get_host", return_value=value): |
| 41 | + assert hostname("myserver.com", request) |
| 42 | + |
| 43 | + |
| 44 | +def test_hostname_mismatch(rf): |
| 45 | + request: HttpRequest = rf.get("/") |
| 46 | + with mock.patch.object(request, "get_host", return_value="production.com"): |
| 47 | + assert not hostname("localhost", request) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("path", ["/api/data/", "/dal/autocomplete/", "/healthcheck/", "/autocomplete/user/"]) |
| 51 | +def test_show_ddt_excluded_paths(rf, settings, path): |
| 52 | + from hope_live.config.fragments.debug_toolbar import show_ddt |
| 53 | + |
| 54 | + settings.DEBUG = True |
| 55 | + request = rf.get(path) |
| 56 | + with mock.patch.object(request, "get_host", return_value="localhost:8000"): |
| 57 | + assert show_ddt(request) is False |
| 58 | + |
| 59 | + |
| 60 | +def test_show_ddt_allowed_path(rf, settings): |
| 61 | + from flags.models import FlagState |
| 62 | + |
| 63 | + from hope_live.config.fragments.debug_toolbar import show_ddt |
| 64 | + |
| 65 | + settings.DEBUG = True |
| 66 | + FlagState.objects.create( |
| 67 | + name="DEVELOP_DEBUG_TOOLBAR", |
| 68 | + condition="hostname", |
| 69 | + value="localhost,127.0.0.1", |
| 70 | + ) |
| 71 | + |
| 72 | + request = rf.get("/dashboard/") |
| 73 | + with mock.patch.object(request, "get_host", return_value="localhost:8000"): |
| 74 | + assert request.path == "/dashboard/" |
| 75 | + assert show_ddt(request) is True |
| 76 | + |
| 77 | + |
| 78 | +def test_show_ddt_production_hostname(rf, settings): |
| 79 | + from flags.models import FlagState |
| 80 | + |
| 81 | + from hope_live.config.fragments.debug_toolbar import show_ddt |
| 82 | + |
| 83 | + settings.DEBUG = True |
| 84 | + FlagState.objects.create( |
| 85 | + name="DEVELOP_DEBUG_TOOLBAR", |
| 86 | + condition="hostname", |
| 87 | + value="localhost,127.0.0.1", |
| 88 | + ) |
| 89 | + |
| 90 | + request = rf.get("/dashboard/") |
| 91 | + with mock.patch.object(request, "get_host", return_value="dashboard-hope-dev.unitst.org:443"): |
| 92 | + assert show_ddt(request) is False |
0 commit comments