File: test_tag_mixpanel.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 (76 lines) | stat: -rw-r--r-- 2,600 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
"""
Tests for the Mixpanel tags and filters.
"""

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.mixpanel import MixpanelNode
from analytical.utils import AnalyticalException


@override_settings(MIXPANEL_API_TOKEN='0123456789abcdef0123456789abcdef')
class MixpanelTagTestCase(TagTestCase):
    """
    Tests for the ``mixpanel`` template tag.
    """

    def test_tag(self):
        r = self.render_tag('mixpanel', 'mixpanel')
        assert "mixpanel.init('0123456789abcdef0123456789abcdef');" in r

    def test_node(self):
        r = MixpanelNode().render(Context())
        assert "mixpanel.init('0123456789abcdef0123456789abcdef');" in r

    @override_settings(MIXPANEL_API_TOKEN=None)
    def test_no_token(self):
        with pytest.raises(AnalyticalException):
            MixpanelNode()

    @override_settings(MIXPANEL_API_TOKEN='0123456789abcdef0123456789abcdef0')
    def test_token_too_long(self):
        with pytest.raises(AnalyticalException):
            MixpanelNode()

    @override_settings(MIXPANEL_API_TOKEN='0123456789abcdef0123456789abcde')
    def test_token_too_short(self):
        with pytest.raises(AnalyticalException):
            MixpanelNode()

    @override_settings(ANALYTICAL_AUTO_IDENTIFY=True)
    def test_identify(self):
        r = MixpanelNode().render(Context({'user': User(username='test')}))
        assert "mixpanel.identify('test');" in r

    @override_settings(ANALYTICAL_AUTO_IDENTIFY=True)
    def test_identify_anonymous_user(self):
        r = MixpanelNode().render(Context({'user': AnonymousUser()}))
        assert 'mixpanel.register_once({distinct_id:' not in r

    def test_event(self):
        r = MixpanelNode().render(
            Context(
                {
                    'mixpanel_event': (
                        'test_event',
                        {'prop1': 'val1', 'prop2': 'val2'},
                    ),
                }
            )
        )
        assert "mixpanel.track('test_event', "
        '{"prop1": "val1", "prop2": "val2"});' in 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 = MixpanelNode().render(context)
        assert r.startswith('<!-- Mixpanel disabled on internal IP address')
        assert r.endswith('-->')