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

import jingrow
from jingrow.templates.includes.comments.comments import add_comment
from jingrow.tests.test_model_utils import set_user
from jingrow.tests.utils import JingrowTestCase, change_settings
from jingrow.website.pagetype.blog_post.test_blog_post import make_test_blog


class TestComment(JingrowTestCase):
	def test_comment_creation(self):
		test_pg = jingrow.get_pg(dict(pagetype="ToDo", description="test"))
		test_pg.insert()
		comment = test_pg.add_comment("Comment", "test comment")

		test_pg.reload()

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

		# check document creation
		comment_1 = jingrow.get_all(
			"Comment",
			fields=["*"],
			filters=dict(reference_pagetype=test_pg.pagetype, reference_name=test_pg.name),
		)[0]

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

	# test via blog
	def test_public_comment(self):
		test_blog = make_test_blog()

		jingrow.db.delete("Comment", {"reference_pagetype": "Blog Post"})
		add_comment_args = {
			"comment": "Good comment with 10 chars",
			"comment_email": "test@test.com",
			"comment_by": "Good Tester",
			"reference_pagetype": test_blog.pagetype,
			"reference_name": test_blog.name,
			"route": test_blog.route,
		}
		add_comment(**add_comment_args)

		self.assertEqual(
			jingrow.get_all(
				"Comment",
				fields=["*"],
				filters=dict(reference_pagetype=test_blog.pagetype, reference_name=test_blog.name),
			)[0].published,
			1,
		)

		jingrow.db.delete("Comment", {"reference_pagetype": "Blog Post"})

		add_comment_args.update(comment="pleez vizits my site http://mysite.com", comment_by="bad commentor")
		add_comment(**add_comment_args)

		self.assertEqual(
			len(
				jingrow.get_all(
					"Comment",
					fields=["*"],
					filters=dict(reference_pagetype=test_blog.pagetype, reference_name=test_blog.name),
				)
			),
			0,
		)

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

		add_comment_args.update(comment="<script>alert(1)</script>Comment", comment_by="hacker")
		add_comment(**add_comment_args)
		self.assertEqual(
			jingrow.get_all(
				"Comment",
				fields=["content"],
				filters=dict(reference_pagetype=test_blog.pagetype, reference_name=test_blog.name),
			)[0]["content"],
			"Comment",
		)

		test_blog.delete()

	@change_settings("Blog Settings", {"allow_guest_to_comment": 0})
	def test_guest_cannot_comment(self):
		test_blog = make_test_blog()
		with set_user("Guest"):
			self.assertEqual(
				add_comment(
					comment="Good comment with 10 chars",
					comment_email="mail@example.org",
					comment_by="Good Tester",
					reference_pagetype="Blog Post",
					reference_name=test_blog.name,
					route=test_blog.route,
				),
				None,
			)

	def test_user_not_logged_in(self):
		some_system_user = jingrow.db.get_value("User", {"name": ("not in", jingrow.STANDARD_USERS)})

		test_blog = make_test_blog()
		with set_user("Guest"):
			self.assertRaises(
				jingrow.ValidationError,
				add_comment,
				comment="Good comment with 10 chars",
				comment_email=some_system_user,
				comment_by="Good Tester",
				reference_pagetype="Blog Post",
				reference_name=test_blog.name,
				route=test_blog.route,
			)
