Browse Source

Added a bunch of test cases

master
Johann Schmitz 5 years ago
parent
commit
55f639bace
Signed by: ercpe <johann@j-schmitz.net> GPG Key ID: A084064277C501ED
1 changed files with 108 additions and 2 deletions
  1. +108
    -2
      tests/tracker.py

+ 108
- 2
tests/tracker.py View File

@ -1,7 +1,113 @@
# -*- coding: utf-8 -*-
import json
import unittest
from pypiwik.tracker import PiwikTracker
import datetime
import pytz
from pypiwik.tracker import PiwikTracker, PARAMETERS
class TrackerTest(unittest.TestCase):
pass
def test_init(self):
tracker = PiwikTracker('http://localhost', 1)
for property_name in PARAMETERS.keys():
assert hasattr(tracker, property_name), "Missing property on %s: %s" % (tracker, property_name)
def test_from_request(self):
class FakeRequest(object):
META = {
'HTTP_USER_AGENT': 'ua',
'HTTP_REFERER': 'ref',
'HTTP_ACCEPT_LANGUAGE': 'lang'
}
tracker = PiwikTracker('http://localhost', 1, FakeRequest())
assert tracker.user_agent == 'ua'
assert tracker.referer == 'ref'
assert tracker.lang == 'lang'
def test_parameters(self):
site_id = 123
tracker = PiwikTracker('http://localhost', site_id)
for property_name in PARAMETERS.keys():
if property_name in ('page_custom_vars', 'visit_custom_vars'):
continue
setattr(tracker, property_name, 'test')
params = tracker._build_parameters()
assert 'apiv' in params and params['apiv'] == PiwikTracker.API_VERSION, "Wrong or missing API version"
assert 'idsite' in params and params['idsite'] == site_id, 'Wrong or missing site id'
assert 'rec' in params and params['rec'] == '1', 'Wrong or missing "rec" parameter'
for property_name, parameter_name in PARAMETERS.items():
if property_name in ('page_custom_vars', 'visit_custom_vars'):
continue
assert parameter_name in params, "Parameter %s not found in dict" % parameter_name
assert params[parameter_name] == getattr(tracker, property_name)
def test_custom_vars_conversion(self):
tracker = PiwikTracker('http://localhost', 1)
tracker.page_custom_vars['foo'] = 'bar'
params = tracker._build_parameters()
cvars = json.loads(params['cvar'])
self.assertDictEqual(cvars, {
'1': ['foo', 'bar']
})
def test_boolean_parameter_conversion(self):
tracker = PiwikTracker('http://localhost', 1)
tracker.track_bots = True
params = tracker._build_parameters()
assert params['bots'] == 1, "Boolean conversion from True to 1 failed"
def test_datatime_parameter_conversion(self):
tracker = PiwikTracker('http://localhost', 1)
tracker.client_dt = datetime.datetime(2015, 7, 12, 11, 22, 33, tzinfo=pytz.UTC)
params = tracker._build_parameters()
assert params['cdt'] == '2015-07-12 11:22:33', "DateTime conversion to string failed"
def test_request_headers_with_spoof(self):
tracker = PiwikTracker('http://localhost', 1)
headers = tracker.build_request_headers({})
self.assertDictEqual(headers, {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
})
def test_request_headers_with_spoof_params(self):
tracker = PiwikTracker('http://localhost', 1)
headers = tracker.build_request_headers({'user_agent': 'fake user agent'})
self.assertDictEqual(headers, {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'User-Agent': 'fake user agent'
})
def test_request_headers_with_spoof_headers(self):
tracker = PiwikTracker('http://localhost', 1)
tracker.request_headers['foo'] = 'bar'
headers = tracker.build_request_headers({})
self.assertDictEqual(headers, {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'foo': 'bar'
})
def test_request_headers_without_spoof(self):
tracker = PiwikTracker('http://localhost', 1)
tracker.spoof_request = False
headers = tracker.build_request_headers({'user_agent': 'fake user agent'})
self.assertDictEqual(headers, {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
})