# Copyright (c) 2015, InfluxFramework LLC
# License: MIT. See LICENSE

import influxframework
import influxframework.share
from influxframework.automation.doctype.auto_repeat.test_auto_repeat import create_submittable_doctype
from influxframework.tests.utils import InfluxFrameworkTestCase

test_dependencies = ["User"]


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

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

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

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

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

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

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

		influxframework.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):
		influxframework.share.add("Event", self.event.name, self.user)

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

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

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

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

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

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

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

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

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

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

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

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

		influxframework.share.set_permission("Event", self.event.name, None, "read", value=0, everyone=1)
		self.assertTrue(self.event.name not in influxframework.share.get_shared("Event", self.user))
		self.assertTrue(self.event.name not in influxframework.share.get_shared("Event", "test1@example.com"))
		self.assertTrue(self.event.name not in influxframework.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 = influxframework.get_doc(
			dict(doctype=doctype, test="test docshare with submit")
		).insert()

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

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

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

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

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