File: test_tag_clicky.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 (74 lines) | stat: -rw-r--r-- 2,401 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
"""
Tests for the Clicky template tags and filters.
"""

import re

import pytest
from django.contrib.auth.models import AnonymousUser, User
from django.http import HttpRequest
from django.template import Context
from django.test.utils import override_settings
from utils import TagTestCase

from analytical.templatetags.clicky import ClickyNode
from analytical.utils import AnalyticalException


@override_settings(CLICKY_SITE_ID='12345678')
class ClickyTagTestCase(TagTestCase):
    """
    Tests for the ``clicky`` template tag.
    """

    def test_tag(self):
        r = self.render_tag('clicky', 'clicky')
        assert 'clicky_site_ids.push(12345678);' in r
        assert 'src="//in.getclicky.com/12345678ns.gif"' in r

    def test_node(self):
        r = ClickyNode().render(Context({}))
        assert 'clicky_site_ids.push(12345678);' in r
        assert 'src="//in.getclicky.com/12345678ns.gif"' in r

    @override_settings(CLICKY_SITE_ID=None)
    def test_no_site_id(self):
        with pytest.raises(AnalyticalException):
            ClickyNode()

    @override_settings(CLICKY_SITE_ID='123abc')
    def test_wrong_site_id(self):
        with pytest.raises(AnalyticalException):
            ClickyNode()

    @override_settings(ANALYTICAL_AUTO_IDENTIFY=True)
    def test_identify(self):
        r = ClickyNode().render(Context({'user': User(username='test')}))
        assert 'var clicky_custom = {"session": {"username": "test"}};' in r

    @override_settings(ANALYTICAL_AUTO_IDENTIFY=True)
    def test_identify_anonymous_user(self):
        r = ClickyNode().render(Context({'user': AnonymousUser()}))
        assert 'var clicky_custom = {"session": {"username":' not in r

    def test_custom(self):
        r = ClickyNode().render(
            Context(
                {
                    'clicky_var1': 'val1',
                    'clicky_var2': 'val2',
                }
            )
        )
        assert re.search(
            r'var clicky_custom = {.*"var1": "val1", "var2": "val2".*};', r
        )

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