# Copyright (c) 2015, Xhive Global Ltd. and Contributors
# License: MIT. See LICENSE

import xhiveframework
import xhiveframework.share
from xhiveframework.automation.doctype.auto_repeat.test_auto_repeat import create_submittable_doctype
from xhiveframework.tests.utils import XhiveFrameworkTestCase

test_dependencies = ["User"]


class TestDocShare(XhiveFrameworkTestCase):
	def setUp(self):
		self.user = "test@example.com"
		self.event = xhiveframework.get_doc(
			{
				"doctype": "Event",
				"subject": "test share event",
				"starts_on": "2015-01-01 10:00:00",
				"event_type": "Private",
			}
		).insert()

	def tearDown(self):
		xhiveframework.set_user("Administrator")
		self.event.delete()

	def test_add(self):
		# user not shared
		self.assertTrue(self.event.name not in xhiveframework.share.get_shared("Event", self.user))
		xhiveframework.share.add("Event", self.event.name, self.user)
		self.assertTrue(self.event.name in xhiveframework.share.get_shared("Event", self.user))

	def test_doc_permission(self):
		xhiveframework.set_user(self.user)
		self.assertFalse(self.event.has_permission())

		xhiveframework.set_user("Administrator")
		xhiveframework.share.add("Event", self.event.name, self.user)

		xhiveframework.set_user(self.user)
		self.assertTrue(self.event.has_permission())

	def test_share_permission(self):
		xhiveframework.share.add("Event", self.event.name, self.user, write=1, share=1)

		xhiveframework.set_user(self.user)
		self.assertTrue(self.event.has_permission("share"))

		# test cascade
		self.assertTrue(self.event.has_permission("read"))
		self.assertTrue(self.event.has_permission("write"))

	def test_set_permission(self):
		xhiveframework.share.add("Event", self.event.name, self.user)

		xhiveframework.set_user(self.user)
		self.assertFalse(self.event.has_permission("share"))

		xhiveframework.set_user("Administrator")
		xhiveframework.share.set_permission("Event", self.event.name, self.user, "share")

		xhiveframework.set_user(self.user)
		self.assertTrue(self.event.has_permission("share"))

	def test_permission_to_share(self):
		xhiveframework.set_user(self.user)
		self.assertRaises(xhiveframework.PermissionError, xhiveframework.share.add, "Event", self.event.name, self.user)

		xhiveframework.set_user("Administrator")
		xhiveframework.share.add("Event", self.event.name, self.user, write=1, share=1)

		# test not raises
		xhiveframework.set_user(self.user)
		xhiveframework.share.add("Event", self.event.name, "test1@example.com", write=1, share=1)

	def test_remove_share(self):
		xhiveframework.share.add("Event", self.event.name, self.user, write=1, share=1)

		xhiveframework.set_user(self.user)
		self.assertTrue(self.event.has_permission("share"))

		xhiveframework.set_user("Administrator")
		xhiveframework.share.remove("Event", self.event.name, self.user)

		xhiveframework.set_user(self.user)
		self.assertFalse(self.event.has_permission("share"))

	def test_share_with_everyone(self):
		self.assertTrue(self.event.name not in xhiveframework.share.get_shared("Event", self.user))

		xhiveframework.share.set_permission("Event", self.event.name, None, "read", everyone=1)
		self.assertTrue(self.event.name in xhiveframework.share.get_shared("Event", self.user))
		self.assertTrue(self.event.name in xhiveframework.share.get_shared("Event", "test1@example.com"))
		self.assertTrue(self.event.name not in xhiveframework.share.get_shared("Event", "Guest"))

		xhiveframework.share.set_permission("Event", self.event.name, None, "read", value=0, everyone=1)
		self.assertTrue(self.event.name not in xhiveframework.share.get_shared("Event", self.user))
		self.assertTrue(self.event.name not in xhiveframework.share.get_shared("Event", "test1@example.com"))
		self.assertTrue(self.event.name not in xhiveframework.share.get_shared("Event", "Guest"))

	def test_share_with_submit_perm(self):
		doctype = "Test DocShare with Submit"
		create_submittable_doctype(doctype, submit_perms=0)

		submittable_doc = xhiveframework.get_doc(
			dict(doctype=doctype, test="test docshare with submit")
		).insert()

		xhiveframework.set_user(self.user)
		self.assertFalse(xhiveframework.has_permission(doctype, "submit", user=self.user))

		xhiveframework.set_user("Administrator")
		xhiveframework.share.add(doctype, submittable_doc.name, self.user, submit=1)

		xhiveframework.set_user(self.user)
		self.assertTrue(
			xhiveframework.has_permission(doctype, "submit", doc=submittable_doc.name, user=self.user)
		)

		# test cascade
		self.assertTrue(xhiveframework.has_permission(doctype, "read", doc=submittable_doc.name, user=self.user))
		self.assertTrue(
			xhiveframework.has_permission(doctype, "write", doc=submittable_doc.name, user=self.user)
		)

		xhiveframework.share.remove(doctype, submittable_doc.name, self.user)

	def test_share_int_pk(self):
		test_doc = xhiveframework.new_doc("Console Log")

		test_doc.insert()
		xhiveframework.share.add("Console Log", test_doc.name, self.user)

		xhiveframework.set_user(self.user)
		self.assertIn(
			str(test_doc.name), [str(name) for name in xhiveframework.get_list("Console Log", pluck="name")]
		)

		test_doc.reload()
		self.assertTrue(test_doc.has_permission("read"))
