2012-04-11 21:43:35 +04:00
|
|
|
# -*- coding: utf8 -*-
|
|
|
|
import json
|
|
|
|
import urlparse
|
|
|
|
|
2012-04-12 02:21:09 +04:00
|
|
|
from django.conf import settings
|
|
|
|
|
2012-04-11 21:43:35 +04:00
|
|
|
import mock
|
|
|
|
from nose.tools import eq_
|
|
|
|
|
2012-04-12 02:21:09 +04:00
|
|
|
import amo.tests
|
2012-04-11 21:43:35 +04:00
|
|
|
from lib.metrics import metrics, send, send_request
|
|
|
|
|
|
|
|
|
|
|
|
@mock.patch('lib.metrics.urllib2.urlopen')
|
|
|
|
@mock.patch.object(settings, 'METRICS_SERVER', 'http://localhost')
|
2012-04-12 02:21:09 +04:00
|
|
|
class TestMetrics(amo.tests.TestCase):
|
2012-04-11 21:43:35 +04:00
|
|
|
|
|
|
|
def test_called(self, urlopen):
|
|
|
|
send('install', {})
|
|
|
|
eq_(urlopen.call_args[0][0].data, '{}')
|
|
|
|
|
|
|
|
def test_called_data(self, urlopen):
|
|
|
|
data = {'foo': 'bar'}
|
|
|
|
send('install', data)
|
|
|
|
eq_(urlopen.call_args[0][0].data, json.dumps(data))
|
|
|
|
|
|
|
|
def test_called_url(self, urlopen):
|
|
|
|
send('install', {})
|
|
|
|
url = urlopen.call_args[0][0].get_full_url()
|
|
|
|
eq_(urlparse.urlparse(url)[:2], ('http', 'localhost'))
|
|
|
|
|
|
|
|
def test_some_unicode(self, urlopen):
|
|
|
|
send('install', {'name': u'Вагиф Сәмәдоғлу'})
|
|
|
|
|
2013-03-01 22:48:57 +04:00
|
|
|
@mock.patch('lib.metrics.record_stat')
|
|
|
|
def test_send_request(self, record_stat, urlopen):
|
2012-04-11 21:43:35 +04:00
|
|
|
request = mock.Mock()
|
2012-04-13 20:34:21 +04:00
|
|
|
request.GET = {'src': 'foo'}
|
2012-04-12 03:53:14 +04:00
|
|
|
request.LANG = 'en'
|
2012-04-11 21:43:35 +04:00
|
|
|
request.META = {'HTTP_USER_AGENT': 'py'}
|
|
|
|
send_request('install', request, {})
|
2013-03-01 22:48:57 +04:00
|
|
|
assert record_stat.called
|
2012-04-12 03:53:14 +04:00
|
|
|
data = json.loads(urlopen.call_args[0][0].data)
|
|
|
|
eq_(data['user-agent'], 'py')
|
|
|
|
eq_(data['locale'], 'en')
|
2012-04-13 20:34:21 +04:00
|
|
|
eq_(data['src'], 'foo')
|
2012-04-11 21:43:35 +04:00
|
|
|
|
|
|
|
def get_response(self, code):
|
|
|
|
response = mock.Mock()
|
2012-04-26 01:58:43 +04:00
|
|
|
response.getcode.return_value = code
|
2012-04-11 21:43:35 +04:00
|
|
|
return response
|
|
|
|
|
|
|
|
def test_error(self, urlopen):
|
|
|
|
urlopen.return_value = self.get_response(403)
|
|
|
|
eq_(metrics('x', 'install', {}), 403)
|
|
|
|
|
|
|
|
def test_good(self, urlopen):
|
2012-04-26 01:58:43 +04:00
|
|
|
urlopen.return_value = self.get_response(201)
|
|
|
|
eq_(metrics('x', 'install', {}), 201)
|
2012-04-11 21:43:35 +04:00
|
|
|
|
|
|
|
def test_other(self, urlopen):
|
2012-04-26 01:58:43 +04:00
|
|
|
urlopen.return_value = self.get_response(200)
|
|
|
|
eq_(metrics('x', 'install', {}), 200)
|
2012-04-11 21:43:35 +04:00
|
|
|
|
|
|
|
def test_uid(self, urlopen):
|
|
|
|
metrics('x', 'install', {})
|
|
|
|
assert urlopen.call_args[0][0].get_full_url().endswith('x')
|