# Copyright (c) 2019, XhiveFramework Technologies and Contributors
# License: MIT. See LICENSE
import json

import xhiveframework
from xhiveframework.tests.utils import XhiveFrameworkTestCase


class TestComment(XhiveFrameworkTestCase):
	def tearDown(self):
		xhiveframework.form_dict.comment = None
		xhiveframework.form_dict.comment_email = None
		xhiveframework.form_dict.comment_by = None
		xhiveframework.form_dict.reference_doctype = None
		xhiveframework.form_dict.reference_name = None
		xhiveframework.form_dict.route = None
		xhiveframework.local.request_ip = None

	def test_comment_creation(self):
		test_doc = xhiveframework.get_doc(dict(doctype="ToDo", description="test"))
		test_doc.insert()
		comment = test_doc.add_comment("Comment", "test comment")

		test_doc.reload()

		# check if updated in _comments cache
		comments = json.loads(test_doc.get("_comments"))
		self.assertEqual(comments[0].get("name"), comment.name)
		self.assertEqual(comments[0].get("comment"), comment.content)

		# check document creation
		comment_1 = xhiveframework.get_all(
			"Comment",
			fields=["*"],
			filters=dict(reference_doctype=test_doc.doctype, reference_name=test_doc.name),
		)[0]

		self.assertEqual(comment_1.content, "test comment")

	# test via blog
	def test_public_comment(self):
		from xhiveframework.website.doctype.blog_post.test_blog_post import make_test_blog

		test_blog = make_test_blog()

		xhiveframework.db.delete("Comment", {"reference_doctype": "Blog Post"})

		from xhiveframework.templates.includes.comments.comments import add_comment

		xhiveframework.form_dict.comment = "Good comment with 10 chars"
		xhiveframework.form_dict.comment_email = "test@test.com"
		xhiveframework.form_dict.comment_by = "Good Tester"
		xhiveframework.form_dict.reference_doctype = "Blog Post"
		xhiveframework.form_dict.reference_name = test_blog.name
		xhiveframework.form_dict.route = test_blog.route
		xhiveframework.local.request_ip = "127.0.0.1"

		add_comment()

		self.assertEqual(
			xhiveframework.get_all(
				"Comment",
				fields=["*"],
				filters=dict(reference_doctype=test_blog.doctype, reference_name=test_blog.name),
			)[0].published,
			1,
		)

		xhiveframework.db.delete("Comment", {"reference_doctype": "Blog Post"})

		xhiveframework.form_dict.comment = "pleez vizits my site http://mysite.com"
		xhiveframework.form_dict.comment_by = "bad commentor"

		add_comment()

		self.assertEqual(
			len(
				xhiveframework.get_all(
					"Comment",
					fields=["*"],
					filters=dict(reference_doctype=test_blog.doctype, reference_name=test_blog.name),
				)
			),
			0,
		)

		# test for filtering html and css injection elements
		xhiveframework.db.delete("Comment", {"reference_doctype": "Blog Post"})

		xhiveframework.form_dict.comment = "<script>alert(1)</script>Comment"
		xhiveframework.form_dict.comment_by = "hacker"

		add_comment()

		self.assertEqual(
			xhiveframework.get_all(
				"Comment",
				fields=["content"],
				filters=dict(reference_doctype=test_blog.doctype, reference_name=test_blog.name),
			)[0]["content"],
			"Comment",
		)

		test_blog.delete()
