"""Markdown rendering + sanitization tests."""

from django.test import SimpleTestCase, TestCase

from allianceauth.tests.auth_utils import AuthUtils

from shop.markdown import render_markdown
from shop.models import HullListing, Shop
from shop.tests.test_stock import make_ship_type


class RenderMarkdownTests(SimpleTestCase):
    def test_blank_returns_empty(self):
        self.assertEqual(render_markdown(""), "")
        self.assertEqual(render_markdown("   "), "")

    def test_bold_renders(self):
        self.assertIn("<strong>hi</strong>", render_markdown("**hi**"))

    def test_script_is_stripped(self):
        out = render_markdown("<script>alert(1)</script>")
        self.assertNotIn("<script>", out)

    def test_image_is_stripped(self):
        out = render_markdown("![x](http://e.com/x.png)")
        self.assertNotIn("<img", out)

    def test_link_is_hardened(self):
        out = render_markdown("[x](http://e.com)")
        self.assertIn('href="http://e.com"', out)
        self.assertIn('rel="nofollow noopener"', out)
        self.assertIn('target="_blank"', out)


class ShopDescriptionHtmlTests(TestCase):
    def setUp(self):
        self.user = AuthUtils.create_user("descowner")

    def test_default_blank(self):
        shop = Shop.objects.create(slug="d", name="D", created_by=self.user)
        self.assertEqual(shop.description, "")
        self.assertEqual(shop.description_html, "")

    def test_html_rendered(self):
        shop = Shop.objects.create(
            slug="d2", name="D2", created_by=self.user, description="**hi**"
        )
        self.assertIn("<strong>hi</strong>", shop.description_html)


class HullListingDescriptionTests(TestCase):
    def setUp(self):
        self.user = AuthUtils.create_user("hldesc")
        self.ship = make_ship_type()
        self.shop = Shop.objects.create(slug="hl", name="HL", created_by=self.user)

    def test_default_blank(self):
        listing = HullListing.objects.create(shop=self.shop, eve_type=self.ship)
        self.assertEqual(listing.description, "")
        self.assertEqual(listing.description_html, "")

    def test_html_rendered(self):
        listing = HullListing.objects.create(
            shop=self.shop, eve_type=self.ship, description="**hi**"
        )
        self.assertIn("<strong>hi</strong>", listing.description_html)
