"""
Tests for the analytical.utils module.
"""

from django.http import HttpRequest
from django.template import Context

from analytical.utils import is_internal_ip
from analytical.tests.utils import TestCase, override_settings


class InternalIpTestCase(TestCase):

    @override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
    def test_render_no_internal_ip(self):
        context = Context()
        self.assertFalse(is_internal_ip(context))

    @override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
    def test_render_internal_ip(self):
        req = HttpRequest()
        req.META['REMOTE_ADDR'] = '1.1.1.1'
        context = Context({'request': req})
        self.assertTrue(is_internal_ip(context))

    @override_settings(TEST_INTERNAL_IPS=['1.1.1.1'])
    def test_render_prefix_internal_ip(self):
        req = HttpRequest()
        req.META['REMOTE_ADDR'] = '1.1.1.1'
        context = Context({'request': req})
        self.assertTrue(is_internal_ip(context, 'TEST'))

    @override_settings(INTERNAL_IPS=['1.1.1.1'])
    def test_render_internal_ip_fallback(self):
        req = HttpRequest()
        req.META['REMOTE_ADDR'] = '1.1.1.1'
        context = Context({'request': req})
        self.assertTrue(is_internal_ip(context))

    @override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
    def test_render_internal_ip_forwarded_for(self):
        req = HttpRequest()
        req.META['HTTP_X_FORWARDED_FOR'] = '1.1.1.1'
        context = Context({'request': req})
        self.assertTrue(is_internal_ip(context))

    @override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
    def test_render_different_internal_ip(self):
        req = HttpRequest()
        req.META['REMOTE_ADDR'] = '2.2.2.2'
        context = Context({'request': req})
        self.assertFalse(is_internal_ip(context))

@@ -112,16 +112,49 @@
  Context
+%0Afrom django.conf import settings
 %0A%0Afrom a
@@ -176,16 +176,22 @@
  import 
+(%0A    
 is_inter
@@ -196,16 +196,60 @@
 ernal_ip
+, get_required_setting, AnalyticalException)
 %0Afrom an
@@ -276,16 +276,22 @@
  import 
+(%0A    
 TestCase
@@ -309,16 +309,1093 @@
 settings
+, SETTING_DELETED)%0A%0A%0Aclass SettingDeletedTestCase(TestCase):%0A    @override_settings(USER_ID=SETTING_DELETED)%0A    def test_deleted_setting_raises_exception(self):%0A        with self.assertRaises(AttributeError):%0A            getattr(settings, %22USER_ID%22)%0A%0A    @override_settings(USER_ID=1)%0A    def test_only_disable_within_context_manager(self):%0A        %22%22%22%0A        Make sure deleted settings returns once the block exits.%0A        %22%22%22%0A        self.assertEqual(settings.USER_ID, 1)%0A%0A        with override_settings(USER_ID=SETTING_DELETED):%0A            with self.assertRaises(AttributeError):%0A                getattr(settings, %22USER_ID%22)%0A%0A        self.assertEqual(settings.USER_ID, 1)%0A%0A    @override_settings(USER_ID=SETTING_DELETED)%0A    def test_get_required_setting(self):%0A        %22%22%22%0A        Make sure using get_required_setting fails in the right place.%0A        %22%22%22%0A        with self.assertRaises(AnalyticalException) as e:%0A            user_id = get_required_setting(%22USER_ID%22, %22%5Cd+%22, %22invalid USER_ID%22)%0A        self.assertEqual(e.exception.message, %22USER_ID setting: not found%22)
 %0A%0A%0Aclass

Add some tests for SETTING_DELETED