2011-04-05 16:08:24 +02:00
|
|
|
import httplib
|
2011-04-06 13:01:21 +02:00
|
|
|
from unittest import TestCase
|
2011-04-05 16:08:24 +02:00
|
|
|
|
|
|
|
from ckan.config.middleware import make_app
|
|
|
|
from paste.deploy import appconfig
|
|
|
|
import paste.fixture
|
|
|
|
from ckan.tests import conf_dir, url_for, CreateTestData
|
|
|
|
|
|
|
|
from mockgoogleanalytics import runmockserver
|
|
|
|
from ckanext.googleanalytics.commands import LoadAnalytics
|
2011-04-07 11:24:22 +02:00
|
|
|
from ckanext.googleanalytics.commands import InitDB
|
2011-04-05 16:08:24 +02:00
|
|
|
from ckanext.googleanalytics import dbutil
|
2012-07-23 13:11:09 +02:00
|
|
|
import ckanext.googleanalytics.gasnippet as gasnippet
|
2011-04-05 16:08:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MockClient(httplib.HTTPConnection):
|
|
|
|
def request(self, http_request):
|
|
|
|
filters = http_request.uri.query.get('filters')
|
|
|
|
path = http_request.uri.path
|
|
|
|
if filters:
|
2011-11-29 18:39:49 +01:00
|
|
|
if "dataset" in filters:
|
|
|
|
path += "/dataset"
|
2011-04-05 16:08:24 +02:00
|
|
|
else:
|
|
|
|
path += "/download"
|
2011-11-29 18:39:49 +01:00
|
|
|
httplib.HTTPConnection.request(self, http_request.method, path)
|
2011-04-05 16:08:24 +02:00
|
|
|
resp = self.getresponse()
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
2011-04-06 13:01:21 +02:00
|
|
|
class TestConfig(TestCase):
|
2011-04-05 16:08:24 +02:00
|
|
|
def test_config(self):
|
|
|
|
config = appconfig('config:test.ini', relative_to=conf_dir)
|
|
|
|
config.local_conf['ckan.plugins'] = 'googleanalytics'
|
2011-04-06 13:01:21 +02:00
|
|
|
config.local_conf['googleanalytics.id'] = ''
|
2011-04-05 16:08:24 +02:00
|
|
|
command = LoadAnalytics("loadanalytics")
|
|
|
|
command.CONFIG = config.local_conf
|
2011-04-06 13:01:21 +02:00
|
|
|
self.assertRaises(Exception, command.run, [])
|
2011-04-05 16:08:24 +02:00
|
|
|
|
|
|
|
|
2011-04-08 12:00:28 +02:00
|
|
|
class TestLoadCommand(TestCase):
|
2011-04-05 16:08:24 +02:00
|
|
|
@classmethod
|
|
|
|
def setup_class(cls):
|
2012-07-23 13:11:09 +02:00
|
|
|
InitDB("initdb").run([]) # set up database tables
|
2011-11-29 18:39:49 +01:00
|
|
|
|
2011-04-05 16:08:24 +02:00
|
|
|
config = appconfig('config:test.ini', relative_to=conf_dir)
|
|
|
|
config.local_conf['ckan.plugins'] = 'googleanalytics'
|
2011-11-29 14:32:03 +01:00
|
|
|
config.local_conf['googleanalytics.username'] = 'borf'
|
|
|
|
config.local_conf['googleanalytics.password'] = 'borf'
|
|
|
|
config.local_conf['googleanalytics.id'] = 'UA-borf-1'
|
|
|
|
config.local_conf['googleanalytics.show_downloads'] = 'true'
|
2011-04-05 16:08:24 +02:00
|
|
|
cls.config = config.local_conf
|
|
|
|
wsgiapp = make_app(config.global_conf, **config.local_conf)
|
|
|
|
env = {'HTTP_ACCEPT': ('text/html;q=0.9,text/plain;'
|
|
|
|
'q=0.8,image/png,*/*;q=0.5')}
|
|
|
|
cls.app = paste.fixture.TestApp(wsgiapp, extra_environ=env)
|
|
|
|
CreateTestData.create()
|
|
|
|
runmockserver()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def teardown_class(cls):
|
|
|
|
CreateTestData.delete()
|
|
|
|
conn = httplib.HTTPConnection("localhost:%d" % 6969)
|
|
|
|
conn.request("QUIT", "/")
|
|
|
|
conn.getresponse()
|
|
|
|
|
2011-04-06 13:01:21 +02:00
|
|
|
def test_analytics_snippet(self):
|
2011-11-29 14:32:03 +01:00
|
|
|
response = self.app.get(url_for(controller='tag', action='index'))
|
2012-07-23 13:11:09 +02:00
|
|
|
code = gasnippet.header_code % (self.config['googleanalytics.id'],
|
|
|
|
'auto')
|
2011-04-06 13:01:21 +02:00
|
|
|
assert code in response.body
|
|
|
|
|
2011-04-05 16:08:24 +02:00
|
|
|
def test_top_packages(self):
|
|
|
|
command = LoadAnalytics("loadanalytics")
|
|
|
|
command.TEST_HOST = MockClient('localhost', 6969)
|
|
|
|
command.CONFIG = self.config
|
|
|
|
command.run([])
|
|
|
|
packages = dbutil.get_top_packages()
|
|
|
|
resources = dbutil.get_top_resources()
|
2011-04-06 13:01:21 +02:00
|
|
|
self.assertEquals(packages[0][1], 2)
|
|
|
|
self.assertEquals(resources[0][1], 4)
|
2011-04-05 16:08:24 +02:00
|
|
|
|
|
|
|
def test_download_count_inserted(self):
|
|
|
|
command = LoadAnalytics("loadanalytics")
|
|
|
|
command.TEST_HOST = MockClient('localhost', 6969)
|
|
|
|
command.CONFIG = self.config
|
|
|
|
command.run([])
|
2011-12-20 11:56:05 +01:00
|
|
|
response = self.app.get(url_for(
|
|
|
|
controller='package', action='read', id='annakarenina'
|
|
|
|
))
|
|
|
|
assert "[downloaded 4 times]" in response.body
|
|
|
|
|
|
|
|
def test_js_inserted_resource_view(self):
|
|
|
|
from nose import SkipTest
|
|
|
|
raise SkipTest("Test won't work until CKAN 1.5.2")
|
|
|
|
|
|
|
|
from ckan.logic.action import get
|
|
|
|
from ckan import model
|
|
|
|
context = {'model': model, 'ignore_auth': True}
|
|
|
|
data = {'id': 'annakarenina'}
|
|
|
|
pkg = get.package_show(context, data)
|
|
|
|
resource_id = pkg['resources'][0]['id']
|
|
|
|
|
|
|
|
command = LoadAnalytics("loadanalytics")
|
|
|
|
command.TEST_HOST = MockClient('localhost', 6969)
|
|
|
|
command.CONFIG = self.config
|
|
|
|
command.run([])
|
|
|
|
response = self.app.get(url_for(
|
|
|
|
controller='package', action='resource_read', id='annakarenina',
|
|
|
|
resource_id=resource_id
|
|
|
|
))
|
|
|
|
assert 'onclick="javascript: _gaq.push(' in response.body
|