mirror of
https://github.com/jazzband/django-analytical.git
synced 2026-03-16 22:20:25 +00:00
Add tests for internal IP addresses
This commit is contained in:
parent
f41b0619d8
commit
c76e47714e
14 changed files with 177 additions and 30 deletions
|
|
@ -19,24 +19,24 @@ from analytical.utils import is_internal_ip, disable_html, validate_setting, \
|
|||
USER_ID_RE = re.compile(r'^\d{5}$')
|
||||
INIT_CODE = """<script type="text/javascript">var _sf_startpt=(new Date()).getTime()</script>"""
|
||||
SETUP_CODE = """
|
||||
<script type="text/javascript">
|
||||
var _sf_async_config=%(config)s;
|
||||
(function(){
|
||||
function loadChartbeat() {
|
||||
window._sf_endpt=(new Date()).getTime();
|
||||
var e = document.createElement('script');
|
||||
e.setAttribute('language', 'javascript');
|
||||
e.setAttribute('type', 'text/javascript');
|
||||
e.setAttribute('src',
|
||||
(("https:" == document.location.protocol) ? "https://a248.e.akamai.net/chartbeat.download.akamai.com/102508/" : "http://static.chartbeat.com/") +
|
||||
"js/chartbeat.js");
|
||||
document.body.appendChild(e);
|
||||
}
|
||||
var oldonload = window.onload;
|
||||
window.onload = (typeof window.onload != 'function') ?
|
||||
loadChartbeat : function() { oldonload(); loadChartbeat(); };
|
||||
})();
|
||||
</script>
|
||||
<script type="text/javascript">
|
||||
var _sf_async_config=%(config)s;
|
||||
(function(){
|
||||
function loadChartbeat() {
|
||||
window._sf_endpt=(new Date()).getTime();
|
||||
var e = document.createElement('script');
|
||||
e.setAttribute('language', 'javascript');
|
||||
e.setAttribute('type', 'text/javascript');
|
||||
e.setAttribute('src',
|
||||
(("https:" == document.location.protocol) ? "https://a248.e.akamai.net/chartbeat.download.akamai.com/102508/" : "http://static.chartbeat.com/") +
|
||||
"js/chartbeat.js");
|
||||
document.body.appendChild(e);
|
||||
}
|
||||
var oldonload = window.onload;
|
||||
window.onload = (typeof window.onload != 'function') ?
|
||||
loadChartbeat : function() { oldonload(); loadChartbeat(); };
|
||||
})();
|
||||
</script>
|
||||
"""
|
||||
DOMAIN_CONTEXT_KEY = 'chartbeat_domain'
|
||||
|
||||
|
|
|
|||
|
|
@ -16,10 +16,10 @@ PORTAL_ID_RE = re.compile(r'^\d+$')
|
|||
DOMAIN_RE = re.compile(r'^[\w.-]+$')
|
||||
TRACKING_CODE = """
|
||||
<script type="text/javascript" language="javascript">
|
||||
var hs_portalid = %(portal_id)s;
|
||||
var hs_salog_version = "2.00";
|
||||
var hs_ppa = "%(domain)s";
|
||||
document.write(unescape("%%3Cscript src='" + document.location.protocol + "//" + hs_ppa + "/salog.js.aspx' type='text/javascript'%%3E%%3C/script%%3E"));
|
||||
var hs_portalid = %(portal_id)s;
|
||||
var hs_salog_version = "2.00";
|
||||
var hs_ppa = "%(domain)s";
|
||||
document.write(unescape("%%3Cscript src='" + document.location.protocol + "//" + hs_ppa + "/salog.js.aspx' type='text/javascript'%%3E%%3C/script%%3E"));
|
||||
</script>
|
||||
"""
|
||||
|
||||
|
|
@ -52,7 +52,7 @@ class HubSpotNode(Node):
|
|||
html = TRACKING_CODE % {'portal_id': self.portal_id,
|
||||
'domain': self.domain}
|
||||
if is_internal_ip(context, 'HUBSPOT'):
|
||||
html = disable_html(html, self.name)
|
||||
html = disable_html(html, 'HubSpot')
|
||||
return html
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ class OptimizelyNode(Node):
|
|||
def render(self, context):
|
||||
html = SETUP_CODE % {'account_number': self.account_number}
|
||||
if is_internal_ip(context, 'OPTIMIZELY'):
|
||||
html = disable_html(html, self.name)
|
||||
html = disable_html(html, 'Optimizely')
|
||||
return html
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import re
|
|||
|
||||
from django.conf import settings
|
||||
from django.contrib.sites.models import Site
|
||||
from django.http import HttpRequest
|
||||
from django.template import Context
|
||||
|
||||
from analytical.templatetags.chartbeat import ChartbeatTopNode, \
|
||||
|
|
@ -77,3 +78,13 @@ class ChartbeatTagTestCase(TagTestCase):
|
|||
'var _sf_async_config={.*"uid": "12345".*};', r), r)
|
||||
self.assertTrue(re.search(
|
||||
'var _sf_async_config={.*"domain": "test.com".*};', r), r)
|
||||
|
||||
def test_render_internal_ip(self):
|
||||
self.settings_manager.set(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
||||
req = HttpRequest()
|
||||
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
||||
context = Context({'request': req})
|
||||
r = ChartbeatBottomNode().render(context)
|
||||
self.assertTrue(r.startswith(
|
||||
'<!-- Chartbeat disabled on internal IP address'), r)
|
||||
self.assertTrue(r.endswith('-->'), r)
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ Tests for the Clicky template tags and filters.
|
|||
import re
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.http import HttpRequest
|
||||
from django.template import Context
|
||||
|
||||
from analytical.templatetags.clicky import ClickyNode
|
||||
|
|
@ -55,3 +56,13 @@ class ClickyTagTestCase(TagTestCase):
|
|||
'clicky_var2': 'val2'}))
|
||||
self.assertTrue(re.search('var clicky_custom = {.*'
|
||||
'"var1": "val1", "var2": "val2".*};', r), r)
|
||||
|
||||
def test_render_internal_ip(self):
|
||||
self.settings_manager.set(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
||||
req = HttpRequest()
|
||||
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
||||
context = Context({'request': req})
|
||||
r = ClickyNode().render(context)
|
||||
self.assertTrue(r.startswith(
|
||||
'<!-- Clicky disabled on internal IP address'), r)
|
||||
self.assertTrue(r.endswith('-->'), r)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
Tests for the Crazy Egg template tags and filters.
|
||||
"""
|
||||
|
||||
from django.http import HttpRequest
|
||||
from django.template import Context
|
||||
|
||||
from analytical.templatetags.crazy_egg import CrazyEggNode
|
||||
|
|
@ -41,3 +42,13 @@ class CrazyEggTagTestCase(TagTestCase):
|
|||
r = CrazyEggNode().render(context)
|
||||
self.assertTrue("CE2.set(1, 'foo');" in r, r)
|
||||
self.assertTrue("CE2.set(2, 'bar');" in r, r)
|
||||
|
||||
def test_render_internal_ip(self):
|
||||
self.settings_manager.set(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
||||
req = HttpRequest()
|
||||
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
||||
context = Context({'request': req})
|
||||
r = CrazyEggNode().render(context)
|
||||
self.assertTrue(r.startswith(
|
||||
'<!-- Crazy Egg disabled on internal IP address'), r)
|
||||
self.assertTrue(r.endswith('-->'), r)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
Tests for the Google Analytics template tags and filters.
|
||||
"""
|
||||
|
||||
from django.http import HttpRequest
|
||||
from django.template import Context
|
||||
|
||||
from analytical.templatetags.google_analytics import GoogleAnalyticsNode
|
||||
|
|
@ -44,3 +45,13 @@ class GoogleAnalyticsTagTestCase(TagTestCase):
|
|||
in r, r)
|
||||
self.assertTrue("_gaq.push(['_setCustomVar', 5, 'test2', 'bar', 1]);"
|
||||
in r, r)
|
||||
|
||||
def test_render_internal_ip(self):
|
||||
self.settings_manager.set(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
||||
req = HttpRequest()
|
||||
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
||||
context = Context({'request': req})
|
||||
r = GoogleAnalyticsNode().render(context)
|
||||
self.assertTrue(r.startswith(
|
||||
'<!-- Google Analytics disabled on internal IP address'), r)
|
||||
self.assertTrue(r.endswith('-->'), r)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
Tests for the HubSpot template tags and filters.
|
||||
"""
|
||||
|
||||
from django.http import HttpRequest
|
||||
from django.template import Context
|
||||
|
||||
from analytical.templatetags.hubspot import HubSpotNode
|
||||
|
|
@ -44,3 +45,13 @@ class HubSpotTagTestCase(TagTestCase):
|
|||
def test_wrong_domain(self):
|
||||
self.settings_manager.set(HUBSPOT_DOMAIN='wrong domain')
|
||||
self.assertRaises(AnalyticalException, HubSpotNode)
|
||||
|
||||
def test_render_internal_ip(self):
|
||||
self.settings_manager.set(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
||||
req = HttpRequest()
|
||||
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
||||
context = Context({'request': req})
|
||||
r = HubSpotNode().render(context)
|
||||
self.assertTrue(r.startswith(
|
||||
'<!-- HubSpot disabled on internal IP address'), r)
|
||||
self.assertTrue(r.endswith('-->'), r)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ Tests for the KISSmetrics tags and filters.
|
|||
"""
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.http import HttpRequest
|
||||
from django.template import Context
|
||||
|
||||
from analytical.templatetags.kiss_metrics import KissMetricsNode
|
||||
|
|
@ -52,3 +53,13 @@ class KissMetricsTagTestCase(TagTestCase):
|
|||
('test_event', {'prop1': 'val1', 'prop2': 'val2'})}))
|
||||
self.assertTrue("_kmq.push(['record', 'test_event', "
|
||||
'{"prop1": "val1", "prop2": "val2"}]);' in r, r)
|
||||
|
||||
def test_render_internal_ip(self):
|
||||
self.settings_manager.set(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
||||
req = HttpRequest()
|
||||
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
||||
context = Context({'request': req})
|
||||
r = KissMetricsNode().render(context)
|
||||
self.assertTrue(r.startswith(
|
||||
'<!-- KISSmetrics disabled on internal IP address'), r)
|
||||
self.assertTrue(r.endswith('-->'), r)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ Tests for the Mixpanel tags and filters.
|
|||
"""
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.http import HttpRequest
|
||||
from django.template import Context
|
||||
|
||||
from analytical.templatetags.mixpanel import MixpanelNode
|
||||
|
|
@ -55,3 +56,13 @@ class MixpanelTagTestCase(TagTestCase):
|
|||
('test_event', {'prop1': 'val1', 'prop2': 'val2'})}))
|
||||
self.assertTrue("mpq.push(['track', 'test_event', "
|
||||
'{"prop1": "val1", "prop2": "val2"}]);' in r, r)
|
||||
|
||||
def test_render_internal_ip(self):
|
||||
self.settings_manager.set(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
||||
req = HttpRequest()
|
||||
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
||||
context = Context({'request': req})
|
||||
r = MixpanelNode().render(context)
|
||||
self.assertTrue(r.startswith(
|
||||
'<!-- Mixpanel disabled on internal IP address'), r)
|
||||
self.assertTrue(r.endswith('-->'), r)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
Tests for the Optimizely template tags and filters.
|
||||
"""
|
||||
|
||||
from django.http import HttpRequest
|
||||
from django.template import Context
|
||||
|
||||
from analytical.templatetags.optimizely import OptimizelyNode
|
||||
|
|
@ -37,3 +38,13 @@ class OptimizelyTagTestCase(TagTestCase):
|
|||
self.assertRaises(AnalyticalException, OptimizelyNode)
|
||||
self.settings_manager.set(OPTIMIZELY_ACCOUNT_NUMBER='12345678')
|
||||
self.assertRaises(AnalyticalException, OptimizelyNode)
|
||||
|
||||
def test_render_internal_ip(self):
|
||||
self.settings_manager.set(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
||||
req = HttpRequest()
|
||||
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
||||
context = Context({'request': req})
|
||||
r = OptimizelyNode().render(context)
|
||||
self.assertTrue(r.startswith(
|
||||
'<!-- Optimizely disabled on internal IP address'), r)
|
||||
self.assertTrue(r.endswith('-->'), r)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,51 @@
|
|||
"""
|
||||
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
|
||||
|
||||
|
||||
class InternalIpTestCase(TestCase):
|
||||
def test_render_no_internal_ip(self):
|
||||
self.settings_manager.set(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
||||
context = Context()
|
||||
self.assertFalse(is_internal_ip(context))
|
||||
|
||||
def test_render_internal_ip(self):
|
||||
self.settings_manager.set(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
||||
req = HttpRequest()
|
||||
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
||||
context = Context({'request': req})
|
||||
self.assertTrue(is_internal_ip(context))
|
||||
|
||||
def test_render_prefix_internal_ip(self):
|
||||
self.settings_manager.set(TEST_INTERNAL_IPS=['1.1.1.1'])
|
||||
req = HttpRequest()
|
||||
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
||||
context = Context({'request': req})
|
||||
self.assertTrue(is_internal_ip(context, 'TEST'))
|
||||
|
||||
def test_render_internal_ip_fallback(self):
|
||||
self.settings_manager.set(INTERNAL_IPS=['1.1.1.1'])
|
||||
req = HttpRequest()
|
||||
req.META['REMOTE_ADDR'] = '1.1.1.1'
|
||||
context = Context({'request': req})
|
||||
self.assertTrue(is_internal_ip(context))
|
||||
|
||||
def test_render_internal_ip_forwarded_for(self):
|
||||
self.settings_manager.set(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
||||
req = HttpRequest()
|
||||
req.META['HTTP_X_FORWARDED_FOR'] = '1.1.1.1'
|
||||
context = Context({'request': req})
|
||||
self.assertTrue(is_internal_ip(context))
|
||||
|
||||
def test_render_different_internal_ip(self):
|
||||
self.settings_manager.set(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
|
||||
req = HttpRequest()
|
||||
req.META['REMOTE_ADDR'] = '2.2.2.2'
|
||||
context = Context({'request': req})
|
||||
self.assertFalse(is_internal_ip(context))
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ from django.core.management import call_command
|
|||
from django.db.models import loading
|
||||
from django.template import Template, Context, RequestContext
|
||||
from django.test.simple import run_tests as django_run_tests
|
||||
from django.test.testcases import TestCase
|
||||
from django.test.testcases import TestCase as DjangoTestCase
|
||||
|
||||
|
||||
def run_tests(labels=()):
|
||||
|
|
@ -17,9 +17,9 @@ def run_tests(labels=()):
|
|||
django_run_tests(labels, verbosity=1, interactive=True)
|
||||
|
||||
|
||||
class TagTestCase(TestCase):
|
||||
class TestCase(DjangoTestCase):
|
||||
"""
|
||||
Tests for a template tag.
|
||||
Base test case for the django-analytical tests.
|
||||
|
||||
Includes the settings manager.
|
||||
"""
|
||||
|
|
@ -30,6 +30,14 @@ class TagTestCase(TestCase):
|
|||
def tearDown(self):
|
||||
self.settings_manager.revert()
|
||||
|
||||
|
||||
class TagTestCase(TestCase):
|
||||
"""
|
||||
Tests for a template tag.
|
||||
|
||||
Includes the settings manager.
|
||||
"""
|
||||
|
||||
def render_tag(self, library, tag, vars=None, request=None):
|
||||
if vars is None:
|
||||
vars = {}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ from django.conf import settings
|
|||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
|
||||
HTML_COMMENT = "<!-- %(service)s disabled on internal IP address\n%(html)\n-->"
|
||||
HTML_COMMENT = "<!-- %(service)s disabled on internal IP " \
|
||||
"address\n%(html)s\n-->"
|
||||
|
||||
|
||||
def validate_setting(setting, value_re, invalid_msg):
|
||||
|
|
@ -57,7 +58,9 @@ def is_internal_ip(context, prefix=None):
|
|||
request = context['request']
|
||||
remote_ip = request.META.get('HTTP_X_FORWARDED_FOR', '')
|
||||
if not remote_ip:
|
||||
remote_ip = request.META.get('HTTP_X_FORWARDED_FOR', '')
|
||||
remote_ip = request.META.get('REMOTE_ADDR', '')
|
||||
if not remote_ip:
|
||||
return False
|
||||
|
||||
internal_ips = ''
|
||||
if prefix is not None:
|
||||
|
|
@ -72,7 +75,7 @@ def is_internal_ip(context, prefix=None):
|
|||
return False
|
||||
|
||||
|
||||
def disable_html(self, html, service):
|
||||
def disable_html(html, service):
|
||||
return HTML_COMMENT % locals()
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue