"""
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("