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

import jingrow
from jingrow.tests.utils import JingrowTestCase


class TestSeen(JingrowTestCase):
	def tearDown(self):
		jingrow.set_user("Administrator")

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

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

		from jingrow.desk.form.load import getpage

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

		# reload the event
		ev = jingrow.get_pg("Event", ev.name)

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

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

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

		# reload the event
		ev = jingrow.get_pg("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 = jingrow.get_pg("Event", ev.name)

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