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

import influxframework
from influxframework.auth import CookieManager, LoginManager
from influxframework.tests.utils import InfluxFrameworkTestCase


class TestActivityLog(InfluxFrameworkTestCase):
	def test_activity_log(self):

		# test user login log
		influxframework.local.form_dict = influxframework._dict(
			{
				"cmd": "login",
				"sid": "Guest",
				"pwd": influxframework.conf.admin_password or "admin",
				"usr": "Administrator",
			}
		)

		influxframework.local.cookie_manager = CookieManager()
		influxframework.local.login_manager = LoginManager()

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

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

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

		influxframework.local.form_dict = influxframework._dict()

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

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

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

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

		influxframework.local.cookie_manager = CookieManager()
		influxframework.local.login_manager = LoginManager()

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

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

		# test invalid login
		influxframework.form_dict.update({"pwd": "password"})
		self.assertRaises(influxframework.AuthenticationError, LoginManager)
		self.assertRaises(influxframework.AuthenticationError, LoginManager)
		self.assertRaises(influxframework.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(influxframework.AuthenticationError, LoginManager)
		self.assertRaises(influxframework.SecurityException, LoginManager)
		time.sleep(5)
		self.assertRaises(influxframework.AuthenticationError, LoginManager)

		influxframework.local.form_dict = influxframework._dict()


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