# Copyright (c) 2019, JINGROW and Contributors
# License: MIT. See LICENSE
import jingrow
from jingrow.tests.utils import JingrowTestCase
from jingrow.utils import set_request
from jingrow.website.serve import get_response

test_dependencies = ["Blog Post"]


class TestWebsiteRouteMeta(JingrowTestCase):
	def test_meta_tag_generation(self):
		blogs = jingrow.get_all(
			"Blog Post", fields=["name", "route"], filters={"published": 1, "route": ("!=", "")}, limit=1
		)

		blog = blogs[0]

		# create meta tags for this route
		pg = jingrow.new_pg("Website Route Meta")
		pg.append("meta_tags", {"key": "type", "value": "blog_post"})
		pg.append("meta_tags", {"key": "og:title", "value": "My Blog"})
		pg.name = blog.route
		pg.insert()

		# set request on this route
		set_request(path=blog.route)
		response = get_response()

		self.assertTrue(response.status_code, 200)

		html = self.normalize_html(response.get_data().decode())

		self.assertIn(self.normalize_html("""<meta name="type" content="blog_post">"""), html)
		self.assertIn(self.normalize_html("""<meta property="og:title" content="My Blog">"""), html)

	def tearDown(self):
		jingrow.db.rollback()
