File: test_tag_chartbeat.py

package info (click to toggle)
python-django-analytical 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 720 kB
  • sloc: python: 4,693; makefile: 7
file content (117 lines) | stat: -rw-r--r-- 3,866 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
Tests for the Chartbeat template tags and filters.
"""

import re

import pytest
from django.http import HttpRequest
from django.template import Context
from django.test import TestCase
from django.test.utils import override_settings
from utils import TagTestCase

from analytical.templatetags.chartbeat import ChartbeatBottomNode, ChartbeatTopNode
from analytical.utils import AnalyticalException


@override_settings(CHARTBEAT_USER_ID='12345')
class ChartbeatTagTestCaseNoSites(TestCase):
    def test_rendering_setup_no_site(self):
        r = ChartbeatBottomNode().render(Context())
        self.assertTrue('var _sf_async_config={"uid": "12345"};' in r, r)


@override_settings(
    INSTALLED_APPS=(
        'analytical',
        'django.contrib.sites',
        'django.contrib.auth',
        'django.contrib.contenttypes',
    )
)
@override_settings(CHARTBEAT_USER_ID='12345')
class ChartbeatTagTestCaseWithSites(TestCase):
    def setUp(self):
        from django.core.management import call_command

        call_command('migrate', verbosity=0)

    def test_rendering_setup_site(self):
        from django.contrib.sites.models import Site

        site = Site.objects.create(domain='test.com', name='test')
        with override_settings(SITE_ID=site.id):
            r = ChartbeatBottomNode().render(Context())
            assert re.search('var _sf_async_config={.*"uid": "12345".*};', r)
            assert re.search('var _sf_async_config={.*"domain": "test.com".*};', r)

    @override_settings(CHARTBEAT_AUTO_DOMAIN=False)
    def test_auto_domain_false(self):
        """
        Even if 'django.contrib.sites' is in INSTALLED_APPS, if
        CHARTBEAT_AUTO_DOMAIN is False, ensure there is no 'domain'
        in _sf_async_config.
        """
        r = ChartbeatBottomNode().render(Context())
        assert 'var _sf_async_config={"uid": "12345"};' in r


@override_settings(CHARTBEAT_USER_ID='12345')
class ChartbeatTagTestCase(TagTestCase):
    """
    Tests for the ``chartbeat`` template tag.
    """

    def test_top_tag(self):
        r = self.render_tag(
            'chartbeat', 'chartbeat_top', {'chartbeat_domain': 'test.com'}
        )
        assert 'var _sf_startpt=(new Date()).getTime()' in r

    def test_bottom_tag(self):
        r = self.render_tag(
            'chartbeat', 'chartbeat_bottom', {'chartbeat_domain': 'test.com'}
        )
        assert re.search('var _sf_async_config={.*"uid": "12345".*};', r)
        assert re.search('var _sf_async_config={.*"domain": "test.com".*};', r)

    def test_top_node(self):
        r = ChartbeatTopNode().render(
            Context(
                {
                    'chartbeat_domain': 'test.com',
                }
            )
        )
        assert 'var _sf_startpt=(new Date()).getTime()' in r

    def test_bottom_node(self):
        r = ChartbeatBottomNode().render(
            Context(
                {
                    'chartbeat_domain': 'test.com',
                }
            )
        )
        assert re.search('var _sf_async_config={.*"uid": "12345".*};', r)
        assert re.search('var _sf_async_config={.*"domain": "test.com".*};', r)

    @override_settings(CHARTBEAT_USER_ID=None)
    def test_no_user_id(self):
        with pytest.raises(AnalyticalException):
            ChartbeatBottomNode()

    @override_settings(CHARTBEAT_USER_ID='123abc')
    def test_wrong_user_id(self):
        with pytest.raises(AnalyticalException):
            ChartbeatBottomNode()

    @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})
        r = ChartbeatBottomNode().render(context)
        assert r.startswith('<!-- Chartbeat disabled on internal IP address')
        assert r.endswith('-->')