""" Tests for django-nepkit views (views.py). Covers district_list_view, municipality_list_view, and private helpers. """ import pytest from django.test import RequestFactory @pytest.fixture def rf(): return RequestFactory() class TestRenderOptions: """Tests for _render_options internal helper.""" def test_renders_placeholder_option(self): from django_nepkit.views import _render_options response = _render_options([], "Select District") html = response.content.decode() assert 'value=""' in html assert "Select District" in html def test_renders_data_options(self): from django_nepkit.views import _render_options data = [{"id": "Kathmandu", "text": "Kathmandu"}] response = _render_options(data, "Select") html = response.content.decode() assert 'value="Kathmandu"' in html assert ">Kathmandu<" in html def test_renders_multiple_options(self): from django_nepkit.views import _render_options data = [ {"id": "Kathmandu", "text": "Kathmandu"}, {"id": "Lalitpur", "text": "Lalitpur"}, ] response = _render_options(data, "Select") html = response.content.decode() assert "Kathmandu" in html assert "Lalitpur" in html def test_escapes_xss_in_id(self): """District names with HTML should be escaped, not injected verbatim.""" from django_nepkit.views import _render_options data = [ { "id": '">', "text": "Bad", } ] response = _render_options(data, "Select") html = response.content.decode() assert "" not in html assert "<script>" in html def test_escapes_xss_in_text(self): from django_nepkit.views import _render_options data = [{"id": "X", "text": ""}] response = _render_options(data, "Select") html = response.content.decode() assert "') html = response.content.decode() assert "" not in html assert "<script>" in html def test_missing_keys_use_empty_string(self): """Items missing ``id`` / ``text`` render an empty option, not a crash.""" from django_nepkit.views import _render_options data = [{"id": "KTM"}, {"text": "Only Text"}, {}] response = _render_options(data, "Select") html = response.content.decode() assert 'value="KTM"' in html assert ">Only Text<" in html # The empty item should still produce a (value-less) option. assert html.count("= 3 def test_non_dict_item_does_not_crash(self): """Malformed rows (e.g. a stray string) are silently skipped.""" from django_nepkit.views import _render_options data = ["not-a-dict", {"id": "KTM", "text": "KTM"}, None, 42] response = _render_options(data, "Select") html = response.content.decode() assert 'value="KTM"' in html def test_escapes_quote_breakout_in_id_attribute(self): """A ``">', "text": "x"}] response = _render_options(data, "Select") html = response.content.decode() assert 'value=""' not in html or """ in html assert " 0 # Each item has id and text assert "id" in data[0] assert "text" in data[0] def test_returns_empty_json_when_no_province(self, rf): from django_nepkit.views import district_list_view request = rf.get("/") response = district_list_view(request) assert response.status_code == 200 import json data = json.loads(response.content) assert data == [] def test_returns_html_when_html_true(self, rf): from django_nepkit.views import district_list_view request = rf.get("/", {"province": "Bagmati Province", "html": "true"}) response = district_list_view(request) assert response.status_code == 200 assert "text/html" in response["Content-Type"] assert b" 0 # Nepali names should contain Devanagari characters import re has_devanagari = any( re.search(r"[\u0900-\u097F]", item["text"]) for item in data ) assert has_devanagari def test_returns_html_with_nepali_placeholder(self, rf): from django_nepkit.views import district_list_view request = rf.get( "/", {"province": "Bagmati Province", "html": "true", "ne": "true"} ) response = district_list_view(request) # Placeholder should contain Devanagari text (जिल्ला छान्नुहोस्) decoded = response.content.decode("utf-8") assert "जिल्ला" in decoded class TestMunicipalityListView: """Tests for municipality_list_view.""" def test_returns_json_for_valid_district(self, rf): from django_nepkit.views import municipality_list_view import json request = rf.get("/", {"district": "Kathmandu"}) response = municipality_list_view(request) assert response.status_code == 200 data = json.loads(response.content) assert isinstance(data, list) assert len(data) > 0 def test_returns_empty_json_when_no_district(self, rf): from django_nepkit.views import municipality_list_view import json request = rf.get("/") response = municipality_list_view(request) assert response.status_code == 200 data = json.loads(response.content) assert data == [] def test_returns_html_when_html_true(self, rf): from django_nepkit.views import municipality_list_view request = rf.get("/", {"district": "Kathmandu", "html": "true"}) response = municipality_list_view(request) assert response.status_code == 200 assert "text/html" in response["Content-Type"] assert b" 0 has_devanagari = any( re.search(r"[\u0900-\u097F]", item["text"]) for item in data ) assert has_devanagari def test_htmx_request_returns_html(self, rf): from django_nepkit.views import municipality_list_view request = rf.get("/", {"district": "Kathmandu"}, HTTP_HX_REQUEST="true") response = municipality_list_view(request) assert "text/html" in response["Content-Type"] assert b"