# Copyright (c) 2019, InfluxFramework LLC
# License: MIT. See LICENSE
import json

import influxframework
from influxframework.tests.utils import InfluxFrameworkTestCase


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

	def test_comment_creation(self):
		test_doc = influxframework.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 = influxframework.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 influxframework.website.doctype.blog_post.test_blog_post import make_test_blog

		test_blog = make_test_blog()

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

		from influxframework.templates.includes.comments.comments import add_comment

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

		add_comment()

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

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

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

		add_comment()

		self.assertEqual(
			len(
				influxframework.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
		influxframework.db.delete("Comment", {"reference_doctype": "Blog Post"})

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

		add_comment()

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

		test_blog.delete()
