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

import influxframework
from influxframework.tests.utils import InfluxFrameworkTestCase


class TestSeen(InfluxFrameworkTestCase):
	def tearDown(self):
		influxframework.set_user("Administrator")

	def test_if_user_is_added(self):
		ev = influxframework.get_doc(
			{
				"doctype": "Event",
				"subject": "test event for seen",
				"starts_on": "2016-01-01 10:10:00",
				"event_type": "Public",
			}
		).insert()

		influxframework.set_user("test@example.com")

		from influxframework.desk.form.load import getdoc

		# load the form
		getdoc("Event", ev.name)

		# reload the event
		ev = influxframework.get_doc("Event", ev.name)

		self.assertTrue("test@example.com" in json.loads(ev._seen))

		# test another user
		influxframework.set_user("test1@example.com")

		# load the form
		getdoc("Event", ev.name)

		# reload the event
		ev = influxframework.get_doc("Event", ev.name)

		self.assertTrue("test@example.com" in json.loads(ev._seen))
		self.assertTrue("test1@example.com" in json.loads(ev._seen))

		ev.save()
		ev = influxframework.get_doc("Event", ev.name)

		self.assertFalse("test@example.com" in json.loads(ev._seen))
		self.assertTrue("test1@example.com" in json.loads(ev._seen))
