# Copyright (c) 2015, XhiveFramework Technologies and Contributors
# License: MIT. See LICENSE
import time

import xhiveframework
from xhiveframework.auth import CookieManager, LoginManager
from xhiveframework.tests.utils import XhiveFrameworkTestCase


class TestActivityLog(XhiveFrameworkTestCase):
	def test_activity_log(self):
		# test user login log
		xhiveframework.local.form_dict = xhiveframework._dict(
			{
				"cmd": "login",
				"sid": "Guest",
				"pwd": xhiveframework.conf.admin_password or "admin",
				"usr": "Administrator",
			}
		)

		xhiveframework.local.request_ip = "127.0.0.1"
		xhiveframework.local.cookie_manager = CookieManager()
		xhiveframework.local.login_manager = LoginManager()

		auth_log = self.get_auth_log()
		self.assertFalse(xhiveframework.form_dict.pwd)
		self.assertEqual(auth_log.status, "Success")

		# test user logout log
		xhiveframework.local.login_manager.logout()
		auth_log = self.get_auth_log(operation="Logout")
		self.assertEqual(auth_log.status, "Success")

		# test invalid login
		xhiveframework.form_dict.update({"pwd": "password"})
		self.assertRaises(xhiveframework.AuthenticationError, LoginManager)
		auth_log = self.get_auth_log()
		self.assertEqual(auth_log.status, "Failed")

		xhiveframework.local.form_dict = xhiveframework._dict()

	def get_auth_log(self, operation="Login"):
		names = xhiveframework.get_all(
			"Activity Log",
			filters={
				"user": "Administrator",
				"operation": operation,
			},
			order_by="`creation` DESC",
		)

		name = names[0]
		return xhiveframework.get_doc("Activity Log", name)

	def test_brute_security(self):
		update_system_settings({"allow_consecutive_login_attempts": 3, "allow_login_after_fail": 5})

		xhiveframework.local.form_dict = xhiveframework._dict(
			{"cmd": "login", "sid": "Guest", "pwd": "admin", "usr": "Administrator"}
		)

		xhiveframework.local.request_ip = "127.0.0.1"
		xhiveframework.local.cookie_manager = CookieManager()
		xhiveframework.local.login_manager = LoginManager()

		auth_log = self.get_auth_log()
		self.assertEqual(auth_log.status, "Success")

		# test user logout log
		xhiveframework.local.login_manager.logout()
		auth_log = self.get_auth_log(operation="Logout")
		self.assertEqual(auth_log.status, "Success")

		# test invalid login
		xhiveframework.form_dict.update({"pwd": "password"})
		self.assertRaises(xhiveframework.AuthenticationError, LoginManager)
		self.assertRaises(xhiveframework.AuthenticationError, LoginManager)
		self.assertRaises(xhiveframework.AuthenticationError, LoginManager)

		# REMOVE ME: current logic allows allow_consecutive_login_attempts+1 attempts
		# before raising security exception, remove below line when that is fixed.
		self.assertRaises(xhiveframework.AuthenticationError, LoginManager)
		self.assertRaises(xhiveframework.SecurityException, LoginManager)
		time.sleep(5)
		self.assertRaises(xhiveframework.AuthenticationError, LoginManager)

		xhiveframework.local.form_dict = xhiveframework._dict()


def update_system_settings(args):
	doc = xhiveframework.get_doc("System Settings")
	doc.update(args)
	doc.flags.ignore_mandatory = 1
	doc.save()
