File: test_tag_gosquared.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 (75 lines) | stat: -rw-r--r-- 2,505 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
"""
Tests for the GoSquared template 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.gosquared import GoSquaredNode
from analytical.utils import AnalyticalException


@override_settings(GOSQUARED_SITE_TOKEN='ABC-123456-D')
class GoSquaredTagTestCase(TagTestCase):
    """
    Tests for the ``gosquared`` template tag.
    """

    def test_tag(self):
        r = self.render_tag('gosquared', 'gosquared')
        assert 'GoSquared.acct = "ABC-123456-D";' in r

    def test_node(self):
        r = GoSquaredNode().render(Context({}))
        assert 'GoSquared.acct = "ABC-123456-D";' in r

    @override_settings(GOSQUARED_SITE_TOKEN=None)
    def test_no_token(self):
        with pytest.raises(AnalyticalException):
            GoSquaredNode()

    @override_settings(GOSQUARED_SITE_TOKEN='this is not a token')
    def test_wrong_token(self):
        with pytest.raises(AnalyticalException):
            GoSquaredNode()

    @override_settings(ANALYTICAL_AUTO_IDENTIFY=True)
    def test_auto_identify(self):
        r = GoSquaredNode().render(
            Context(
                {
                    'user': User(username='test', first_name='Test', last_name='User'),
                }
            )
        )
        assert 'GoSquared.UserName = "Test User";' in r

    @override_settings(ANALYTICAL_AUTO_IDENTIFY=True)
    def test_manual_identify(self):
        r = GoSquaredNode().render(
            Context(
                {
                    'user': User(username='test', first_name='Test', last_name='User'),
                    'gosquared_identity': 'test_identity',
                }
            )
        )
        assert 'GoSquared.UserName = "test_identity";' in r

    @override_settings(ANALYTICAL_AUTO_IDENTIFY=True)
    def test_identify_anonymous_user(self):
        r = GoSquaredNode().render(Context({'user': AnonymousUser()}))
        assert 'GoSquared.UserName = ' not 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 = GoSquaredNode().render(context)
        assert r.startswith('<!-- GoSquared disabled on internal IP address')
        assert r.endswith('-->')