# Copyright (c) 2015, JINGROW and Contributors
# License: MIT. See LICENSE
import json

import jingrow
from jingrow.tests.utils import JingrowTestCase
from jingrow.utils import set_request
from jingrow.website.pagetype.web_form.web_form import accept
from jingrow.website.serve import get_response_content

test_dependencies = ["Web Form"]


class TestWebForm(JingrowTestCase):
	def setUp(self):
		jingrow.conf.disable_website_cache = True

	def tearDown(self):
		jingrow.conf.disable_website_cache = False

	def test_accept(self):
		jingrow.set_user("Administrator")

		pg = {
			"pagetype": "Event",
			"subject": "_Test Event Web Form",
			"description": "_Test Event Description",
			"starts_on": "2014-09-09",
		}

		accept(web_form="manage-events", data=json.dumps(pg))

		self.event_name = jingrow.db.get_value("Event", {"subject": "_Test Event Web Form"})
		self.assertTrue(self.event_name)

	def test_edit(self):
		self.test_accept()

		pg = {
			"pagetype": "Event",
			"subject": "_Test Event Web Form",
			"description": "_Test Event Description 1",
			"starts_on": "2014-09-09",
			"name": self.event_name,
		}

		self.assertNotEqual(
			jingrow.db.get_value("Event", self.event_name, "description"), pg.get("description")
		)

		accept("manage-events", json.dumps(pg))

		self.assertEqual(jingrow.db.get_value("Event", self.event_name, "description"), pg.get("description"))

	def test_webform_render(self):
		set_request(method="GET", path="manage-events/new")
		content = get_response_content("manage-events/new")
		self.assertIn('<h1 class="ellipsis">New Manage Events</h1>', content)
		self.assertIn('data-pagetype="Web Form"', content)
		self.assertIn('data-path="manage-events/new"', content)
		self.assertIn('source-type="Generator"', content)

	def test_webform_html_meta_is_added(self):
		set_request(method="GET", path="manage-events/new")
		content = self.normalize_html(get_response_content("manage-events/new"))

		self.assertIn(self.normalize_html('<meta name="title" content="Test Meta Form Title">'), content)
		self.assertIn(
			self.normalize_html('<meta property="og:title" content="Test Meta Form Title">'), content
		)
		self.assertIn(
			self.normalize_html('<meta property="og:description" content="Test Meta Form Description">'),
			content,
		)
		self.assertIn(
			self.normalize_html('<meta property="og:image" content="https://framework.jingrow.com/files/jingrow.png">'),
			content,
		)
