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

import time

from werkzeug.wrappers import Response

import influxframework
import influxframework.rate_limiter
from influxframework.rate_limiter import RateLimiter
from influxframework.tests.utils import InfluxFrameworkTestCase
from influxframework.utils import cint


class TestRateLimiter(InfluxFrameworkTestCase):
	def setUp(self):
		pass

	def test_apply_with_limit(self):
		influxframework.conf.rate_limit = {"window": 86400, "limit": 1}
		influxframework.rate_limiter.apply()

		self.assertTrue(hasattr(influxframework.local, "rate_limiter"))
		self.assertIsInstance(influxframework.local.rate_limiter, RateLimiter)

		influxframework.cache().delete(influxframework.local.rate_limiter.key)
		delattr(influxframework.local, "rate_limiter")

	def test_apply_without_limit(self):
		influxframework.conf.rate_limit = None
		influxframework.rate_limiter.apply()

		self.assertFalse(hasattr(influxframework.local, "rate_limiter"))

	def test_respond_over_limit(self):
		limiter = RateLimiter(0.01, 86400)
		time.sleep(0.01)
		limiter.update()

		influxframework.conf.rate_limit = {"window": 86400, "limit": 0.01}
		self.assertRaises(influxframework.TooManyRequestsError, influxframework.rate_limiter.apply)
		influxframework.rate_limiter.update()

		response = influxframework.rate_limiter.respond()

		self.assertIsInstance(response, Response)
		self.assertEqual(response.status_code, 429)

		headers = influxframework.local.rate_limiter.headers()
		self.assertIn("Retry-After", headers)
		self.assertNotIn("X-RateLimit-Used", headers)
		self.assertIn("X-RateLimit-Reset", headers)
		self.assertIn("X-RateLimit-Limit", headers)
		self.assertIn("X-RateLimit-Remaining", headers)
		self.assertTrue(int(headers["X-RateLimit-Reset"]) <= 86400)
		self.assertEqual(int(headers["X-RateLimit-Limit"]), 10000)
		self.assertEqual(int(headers["X-RateLimit-Remaining"]), 0)

		influxframework.cache().delete(limiter.key)
		influxframework.cache().delete(influxframework.local.rate_limiter.key)
		delattr(influxframework.local, "rate_limiter")

	def test_respond_under_limit(self):
		influxframework.conf.rate_limit = {"window": 86400, "limit": 0.01}
		influxframework.rate_limiter.apply()
		influxframework.rate_limiter.update()
		response = influxframework.rate_limiter.respond()
		self.assertEqual(response, None)

		influxframework.cache().delete(influxframework.local.rate_limiter.key)
		delattr(influxframework.local, "rate_limiter")

	def test_headers_under_limit(self):
		influxframework.conf.rate_limit = {"window": 86400, "limit": 0.01}
		influxframework.rate_limiter.apply()
		influxframework.rate_limiter.update()
		headers = influxframework.local.rate_limiter.headers()
		self.assertNotIn("Retry-After", headers)
		self.assertIn("X-RateLimit-Reset", headers)
		self.assertTrue(int(headers["X-RateLimit-Reset"] < 86400))
		self.assertEqual(int(headers["X-RateLimit-Used"]), influxframework.local.rate_limiter.duration)
		self.assertEqual(int(headers["X-RateLimit-Limit"]), 10000)
		self.assertEqual(int(headers["X-RateLimit-Remaining"]), 10000)

		influxframework.cache().delete(influxframework.local.rate_limiter.key)
		delattr(influxframework.local, "rate_limiter")

	def test_reject_over_limit(self):
		limiter = RateLimiter(0.01, 86400)
		time.sleep(0.01)
		limiter.update()

		limiter = RateLimiter(0.01, 86400)
		self.assertRaises(influxframework.TooManyRequestsError, limiter.apply)

		influxframework.cache().delete(limiter.key)

	def test_do_not_reject_under_limit(self):
		limiter = RateLimiter(0.01, 86400)
		time.sleep(0.01)
		limiter.update()

		limiter = RateLimiter(0.02, 86400)
		self.assertEqual(limiter.apply(), None)

		influxframework.cache().delete(limiter.key)

	def test_update_method(self):
		limiter = RateLimiter(0.01, 86400)
		time.sleep(0.01)
		limiter.update()

		self.assertEqual(limiter.duration, cint(influxframework.cache().get(limiter.key)))

		influxframework.cache().delete(limiter.key)
