Python implementation of the Piwik HTTP API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
6.3KB

  1. # -*- coding: utf-8 -*-
  2. import json
  3. import unittest
  4. import datetime
  5. import pytz
  6. from pypiwik.tracker import PiwikTracker, PARAMETERS, AUTH_RESTRICTED_PARAMS
  7. class TrackerTest(unittest.TestCase):
  8. def test_init(self):
  9. tracker = PiwikTracker('http://localhost', 1)
  10. for property_name in PARAMETERS.keys():
  11. assert hasattr(tracker, property_name), "Missing property on %s: %s" % (tracker, property_name)
  12. def test_from_request(self):
  13. class FakeRequest(object):
  14. META = {
  15. 'HTTP_USER_AGENT': 'ua',
  16. 'HTTP_REFERER': 'ref',
  17. 'HTTP_ACCEPT_LANGUAGE': 'lang'
  18. }
  19. tracker = PiwikTracker('http://localhost', 1, FakeRequest())
  20. assert tracker.user_agent == 'ua'
  21. assert tracker.referer == 'ref'
  22. assert tracker.lang == 'lang'
  23. assert tracker.url is None
  24. assert tracker.client_ip is None
  25. def test_from_request_with_url(self):
  26. class FakeRequest(object):
  27. def build_absolute_uri(self):
  28. return "http://foobar.local/test123"
  29. tracker = PiwikTracker('http://localhost', 1, FakeRequest())
  30. assert tracker.url == "http://foobar.local/test123"
  31. def test_from_request_client_ip(self):
  32. class FakeRequest(object):
  33. META = { 'HTTP_X_FORWARDED_FOR': '127.1.2.3' }
  34. tracker = PiwikTracker('http://localhost', 1, FakeRequest())
  35. assert tracker.client_ip == '127.1.2.3'
  36. def test_from_bad_request(self):
  37. class FakeRequest(object):
  38. pass
  39. tracker = PiwikTracker('http://localhost', 1, FakeRequest())
  40. self.assertDictEqual({'apiv': PiwikTracker.API_VERSION, 'idsite': 1, 'rec': '1'}, tracker._build_parameters())
  41. class FakeRequest2(object):
  42. META = "test"
  43. tracker = PiwikTracker('http://localhost', 1, FakeRequest2())
  44. self.assertDictEqual({'apiv': PiwikTracker.API_VERSION, 'idsite': 1, 'rec': '1'}, tracker._build_parameters())
  45. def test_parameters(self):
  46. site_id = 123
  47. tracker = PiwikTracker('http://localhost', site_id)
  48. for property_name in PARAMETERS.keys():
  49. if property_name in ('page_custom_vars', 'visit_custom_vars'):
  50. continue
  51. setattr(tracker, property_name, 'test')
  52. params = tracker._build_parameters()
  53. assert 'apiv' in params and params['apiv'] == PiwikTracker.API_VERSION, "Wrong or missing API version"
  54. assert 'idsite' in params and params['idsite'] == site_id, 'Wrong or missing site id'
  55. assert 'rec' in params and params['rec'] == '1', 'Wrong or missing "rec" parameter'
  56. for property_name, parameter_name in PARAMETERS.items():
  57. if not parameter_name:
  58. continue
  59. if property_name in ('page_custom_vars', 'visit_custom_vars'):
  60. continue
  61. assert parameter_name in params, "Parameter %s not found in dict" % parameter_name
  62. assert params[parameter_name] == getattr(tracker, property_name)
  63. def test_custom_vars_conversion(self):
  64. tracker = PiwikTracker('http://localhost', 1)
  65. tracker.page_custom_vars['foo'] = 'bar'
  66. params = tracker._build_parameters()
  67. cvars = json.loads(params['cvar'])
  68. self.assertDictEqual(cvars, {
  69. '1': ['foo', 'bar']
  70. })
  71. def test_boolean_parameter_conversion(self):
  72. tracker = PiwikTracker('http://localhost', 1)
  73. tracker.track_bots = True
  74. params = tracker._build_parameters()
  75. assert params['bots'] == 1, "Boolean conversion from True to 1 failed"
  76. def test_datetime_parameter_conversion(self):
  77. tracker = PiwikTracker('http://localhost', 1)
  78. tracker.token_auth = 'foobar'
  79. tracker.client_dt = datetime.datetime(2015, 7, 12, 11, 22, 33, tzinfo=pytz.UTC)
  80. params = tracker._build_parameters()
  81. assert params['cdt'] == '2015-07-12 11:22:33', "DateTime conversion to string failed"
  82. def test_skip_auth_params_on_unauth_request(self):
  83. tracker = PiwikTracker('http://localhost', 1)
  84. for p in AUTH_RESTRICTED_PARAMS:
  85. if p == "token_auth":
  86. continue
  87. setattr(tracker, p, 'test')
  88. params = tracker._build_parameters()
  89. assert not any((k in AUTH_RESTRICTED_PARAMS for k in params.keys())),\
  90. "Tracking variables with authentication required should be filtered out if token_auth is not set"
  91. def test_request_headers_with_spoof(self):
  92. tracker = PiwikTracker('http://localhost', 1)
  93. headers = tracker.build_request_headers({})
  94. self.assertDictEqual(headers, {
  95. 'Accept': '*/*',
  96. 'Accept-Encoding': 'gzip, deflate',
  97. })
  98. def test_request_headers_with_spoof_params(self):
  99. tracker = PiwikTracker('http://localhost', 1)
  100. headers = tracker.build_request_headers({'ua': 'fake user agent'})
  101. self.assertDictEqual(headers, {
  102. 'Accept': '*/*',
  103. 'Accept-Encoding': 'gzip, deflate',
  104. 'User-Agent': 'fake user agent'
  105. })
  106. def test_request_headers_with_spoof_headers(self):
  107. tracker = PiwikTracker('http://localhost', 1)
  108. tracker.request_headers['foo'] = 'bar'
  109. headers = tracker.build_request_headers({})
  110. self.assertDictEqual(headers, {
  111. 'Accept': '*/*',
  112. 'Accept-Encoding': 'gzip, deflate',
  113. 'foo': 'bar'
  114. })
  115. def test_request_headers_without_spoof(self):
  116. tracker = PiwikTracker('http://localhost', 1)
  117. tracker.spoof_request = False
  118. headers = tracker.build_request_headers({'user_agent': 'fake user agent'})
  119. self.assertDictEqual(headers, {
  120. 'Accept': '*/*',
  121. 'Accept-Encoding': 'gzip, deflate',
  122. })
  123. def test_tracker_url_builder(self):
  124. tracker = PiwikTracker('http://localhost', 1)
  125. assert tracker.php_url == "http://localhost/piwik.php"
  126. tracker.piwik_php_file = 'test123.php'
  127. assert tracker.php_url == "http://localhost/test123.php"
  128. def test_js_url_builder(self):
  129. tracker = PiwikTracker('http://localhost', 1)
  130. assert tracker.js_url == "http://localhost/piwik.js"
  131. tracker.piwik_js_file = 'test123.js'
  132. assert tracker.js_url == "http://localhost/test123.js"