2015-08-11 18:19:22 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
2015-08-29 23:16:14 +00:00
|
|
|
from wakatime.main import execute
|
2015-08-12 21:21:32 +00:00
|
|
|
from wakatime.packages import requests
|
|
|
|
|
2016-07-20 18:32:08 +00:00
|
|
|
import logging
|
2015-08-12 21:21:32 +00:00
|
|
|
import os
|
|
|
|
import time
|
2016-01-25 22:49:37 +00:00
|
|
|
import re
|
2016-06-08 18:33:36 +00:00
|
|
|
import shutil
|
2015-08-12 01:59:29 +00:00
|
|
|
import sys
|
2016-07-20 18:32:08 +00:00
|
|
|
from testfixtures import log_capture
|
2016-03-06 22:08:00 +00:00
|
|
|
from wakatime.compat import u, is_py3
|
2016-01-25 22:49:37 +00:00
|
|
|
from wakatime.constants import (
|
|
|
|
API_ERROR,
|
|
|
|
AUTH_ERROR,
|
|
|
|
CONFIG_FILE_PARSE_ERROR,
|
|
|
|
SUCCESS,
|
2016-08-31 12:27:10 +00:00
|
|
|
MALFORMED_HEARTBEAT_ERROR,
|
2016-01-25 22:49:37 +00:00
|
|
|
)
|
2015-08-11 19:15:35 +00:00
|
|
|
from wakatime.packages.requests.models import Response
|
2015-08-12 01:59:29 +00:00
|
|
|
from . import utils
|
2015-08-11 18:19:22 +00:00
|
|
|
|
2015-08-30 00:48:42 +00:00
|
|
|
try:
|
|
|
|
from .packages import simplejson as json
|
|
|
|
except (ImportError, SyntaxError):
|
|
|
|
import json
|
2015-08-11 18:19:22 +00:00
|
|
|
try:
|
2016-04-20 22:30:44 +00:00
|
|
|
from mock import ANY, call
|
2015-08-11 21:53:35 +00:00
|
|
|
except ImportError:
|
2016-04-20 22:30:44 +00:00
|
|
|
from unittest.mock import ANY, call
|
2016-04-18 21:38:38 +00:00
|
|
|
from wakatime.packages import tzlocal
|
2015-08-11 18:19:22 +00:00
|
|
|
|
|
|
|
|
2016-07-20 18:32:08 +00:00
|
|
|
class MainTestCase(utils.TestCase):
|
2015-08-12 21:21:32 +00:00
|
|
|
patch_these = [
|
|
|
|
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
|
|
|
'wakatime.offlinequeue.Queue.push',
|
|
|
|
['wakatime.offlinequeue.Queue.pop', None],
|
2015-09-27 10:17:54 +00:00
|
|
|
['wakatime.offlinequeue.Queue.connect', None],
|
2015-08-12 21:21:32 +00:00
|
|
|
'wakatime.session_cache.SessionCache.save',
|
|
|
|
'wakatime.session_cache.SessionCache.delete',
|
|
|
|
['wakatime.session_cache.SessionCache.get', requests.session],
|
2015-09-27 10:17:54 +00:00
|
|
|
['wakatime.session_cache.SessionCache.connect', None],
|
2015-08-12 21:21:32 +00:00
|
|
|
]
|
2015-08-11 18:19:22 +00:00
|
|
|
|
2015-08-12 21:21:32 +00:00
|
|
|
def test_help_contents(self):
|
2015-08-12 17:28:01 +00:00
|
|
|
args = ['--help']
|
2015-08-11 18:19:22 +00:00
|
|
|
with self.assertRaises(SystemExit):
|
2015-08-29 23:16:14 +00:00
|
|
|
execute(args)
|
2015-08-12 01:59:29 +00:00
|
|
|
expected_stdout = open('tests/samples/output/test_help_contents').read()
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), expected_stdout)
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
2015-08-11 18:19:22 +00:00
|
|
|
|
2015-08-12 21:21:32 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
|
|
|
|
|
|
|
def test_argument_parsing(self):
|
2015-08-11 19:15:35 +00:00
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
2015-08-12 21:21:32 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--file', entity, '--key', '123', '--config', config]
|
2015-08-12 01:59:29 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, SUCCESS)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY)
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2015-09-27 02:10:12 +00:00
|
|
|
def test_config_file_not_passed_in_command_line_args(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/emptyfile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.main.open') as mock_open:
|
|
|
|
mock_open.side_effect = IOError('')
|
|
|
|
|
|
|
|
config = os.path.join(os.path.expanduser('~'), '.wakatime.cfg')
|
|
|
|
args = ['--file', entity]
|
|
|
|
|
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
execute(args)
|
|
|
|
expected_stdout = u("Error: Could not read from config file {0}\n").format(u(config))
|
|
|
|
expected_stderr = open('tests/samples/output/test_missing_config_file').read()
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), expected_stdout)
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), expected_stderr)
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
|
|
|
|
|
|
|
|
def test_missing_config_file(self):
|
|
|
|
config = 'foo'
|
2015-09-27 02:10:12 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
2015-09-27 02:10:12 +00:00
|
|
|
entity = 'tests/samples/codefiles/emptyfile.txt'
|
2016-06-08 18:33:36 +00:00
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt'))
|
2015-09-27 02:10:12 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
args = ['--file', entity, '--config', config]
|
2015-09-27 02:10:12 +00:00
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
execute(args)
|
2016-06-08 18:33:36 +00:00
|
|
|
expected_stdout = u("Error: Could not read from config file foo\n")
|
2015-09-27 02:10:12 +00:00
|
|
|
expected_stderr = open('tests/samples/output/test_missing_config_file').read()
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), expected_stdout)
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), expected_stderr)
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2015-09-30 05:34:14 +00:00
|
|
|
def test_good_config_file(self):
|
2015-08-12 01:59:29 +00:00
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
2015-08-12 21:21:32 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/emptyfile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt'))
|
2015-08-12 01:59:29 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
config = 'tests/samples/configs/has_everything.cfg'
|
|
|
|
args = ['--file', entity, '--config', config]
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, SUCCESS)
|
|
|
|
expected_stdout = open('tests/samples/output/main_test_good_config_file').read()
|
|
|
|
traceback_file = os.path.realpath('wakatime/main.py')
|
|
|
|
lineno = int(re.search(r' line (\d+),', sys.stdout.getvalue()).group(1))
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), expected_stdout.format(file=traceback_file, lineno=lineno))
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
2015-09-27 02:12:54 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY)
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
|
2015-09-27 02:12:54 +00:00
|
|
|
|
|
|
|
def test_api_key_without_underscore_accepted(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/emptyfile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt'))
|
2015-09-27 02:12:54 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
config = 'tests/samples/configs/sample_alternate_apikey.cfg'
|
|
|
|
args = ['--file', entity, '--config', config]
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, SUCCESS)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY)
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
|
2015-08-12 21:21:32 +00:00
|
|
|
|
|
|
|
def test_bad_config_file(self):
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/emptyfile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
|
|
|
|
config = 'tests/samples/configs/bad_config.cfg'
|
|
|
|
args = ['--file', entity, '--config', config]
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, CONFIG_FILE_PARSE_ERROR)
|
|
|
|
self.assertIn('ParsingError', sys.stdout.getvalue())
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
2015-08-12 21:21:32 +00:00
|
|
|
|
|
|
|
def test_non_hidden_filename(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
now = u(int(time.time()))
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
args = ['--file', entity, '--key', '123', '--config', config, '--time', now]
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, API_ERROR)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'language': 'Text only',
|
|
|
|
'lines': 2,
|
|
|
|
'entity': os.path.realpath(entity),
|
|
|
|
'project': os.path.basename(os.path.abspath('.')),
|
|
|
|
'time': float(now),
|
|
|
|
'type': 'file',
|
|
|
|
}
|
|
|
|
stats = {
|
|
|
|
u('cursorpos'): None,
|
|
|
|
u('dependencies'): [],
|
|
|
|
u('language'): u('Text only'),
|
|
|
|
u('lineno'): None,
|
|
|
|
u('lines'): 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None)
|
|
|
|
for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items():
|
|
|
|
self.assertEquals(heartbeat[key], val)
|
|
|
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
2015-08-12 21:21:32 +00:00
|
|
|
|
|
|
|
def test_hidden_filename(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
now = u(int(time.time()))
|
|
|
|
config = 'tests/samples/configs/paranoid.cfg'
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
args = ['--file', entity, '--key', '123', '--config', config, '--time', now]
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, API_ERROR)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'language': 'Text only',
|
|
|
|
'lines': 2,
|
|
|
|
'entity': 'HIDDEN.txt',
|
|
|
|
'project': os.path.basename(os.path.abspath('.')),
|
|
|
|
'time': float(now),
|
|
|
|
'type': 'file',
|
|
|
|
}
|
|
|
|
stats = {
|
|
|
|
u('cursorpos'): None,
|
|
|
|
u('dependencies'): [],
|
|
|
|
u('language'): u('Text only'),
|
|
|
|
u('lineno'): None,
|
|
|
|
u('lines'): 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None)
|
|
|
|
for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items():
|
|
|
|
self.assertEquals(heartbeat[key], val)
|
|
|
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-07-28 07:00:58 +00:00
|
|
|
def test_invalid_timeout_passed_via_command_line(self):
|
2015-09-30 05:43:39 +00:00
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
|
2015-09-30 05:43:39 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--file', entity, '--key', '123', '--config', config, '--timeout', 'abc']
|
2015-09-30 05:43:39 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
execute(args)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
expected_stderr = open('tests/samples/output/main_test_timeout_passed_via_command_line').read()
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), expected_stderr)
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
|
2015-09-30 05:43:39 +00:00
|
|
|
|
2015-08-12 21:21:32 +00:00
|
|
|
def test_500_response(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
now = u(int(time.time()))
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
args = ['--file', entity, '--key', '123',
|
|
|
|
'--config', 'tests/samples/configs/paranoid.cfg', '--time', now]
|
2015-08-12 21:21:32 +00:00
|
|
|
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, API_ERROR)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'language': 'Text only',
|
|
|
|
'lines': 2,
|
|
|
|
'entity': 'HIDDEN.txt',
|
|
|
|
'project': os.path.basename(os.path.abspath('.')),
|
|
|
|
'time': float(now),
|
|
|
|
'type': 'file',
|
|
|
|
}
|
|
|
|
stats = {
|
|
|
|
u('cursorpos'): None,
|
|
|
|
u('dependencies'): [],
|
|
|
|
u('language'): u('Text only'),
|
|
|
|
u('lineno'): None,
|
|
|
|
u('lines'): 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None)
|
|
|
|
for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items():
|
|
|
|
self.assertEquals(heartbeat[key], val)
|
|
|
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
2015-08-12 21:21:32 +00:00
|
|
|
|
|
|
|
def test_400_response(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 400
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
now = u(int(time.time()))
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
args = ['--file', entity, '--key', '123',
|
|
|
|
'--config', 'tests/samples/configs/paranoid.cfg', '--time', now]
|
2015-08-12 21:21:32 +00:00
|
|
|
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, API_ERROR)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
2015-08-12 22:03:39 +00:00
|
|
|
|
2016-01-25 22:49:37 +00:00
|
|
|
def test_401_response(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 401
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
|
2016-01-25 22:49:37 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
now = u(int(time.time()))
|
2016-01-25 22:49:37 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
args = ['--file', entity, '--key', '123',
|
|
|
|
'--config', 'tests/samples/configs/paranoid.cfg', '--time', now]
|
2016-01-25 22:49:37 +00:00
|
|
|
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, AUTH_ERROR)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
2016-01-25 22:49:37 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'language': 'Text only',
|
|
|
|
'lines': 2,
|
|
|
|
'entity': 'HIDDEN.txt',
|
|
|
|
'project': os.path.basename(os.path.abspath('.')),
|
|
|
|
'time': float(now),
|
|
|
|
'type': 'file',
|
|
|
|
}
|
|
|
|
stats = {
|
|
|
|
u('cursorpos'): None,
|
|
|
|
u('dependencies'): [],
|
|
|
|
u('language'): u('Text only'),
|
|
|
|
u('lineno'): None,
|
|
|
|
u('lines'): 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None)
|
|
|
|
for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items():
|
|
|
|
self.assertEquals(heartbeat[key], val)
|
|
|
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
2016-01-25 22:49:37 +00:00
|
|
|
|
2015-09-15 16:07:58 +00:00
|
|
|
def test_missing_entity_file(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
entity = 'tests/samples/codefiles/missingfile.txt'
|
2016-06-08 18:33:36 +00:00
|
|
|
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-15 16:07:58 +00:00
|
|
|
args = ['--file', entity, '--config', config]
|
|
|
|
retval = execute(args)
|
2016-01-25 22:49:37 +00:00
|
|
|
self.assertEquals(retval, SUCCESS)
|
2015-09-15 16:07:58 +00:00
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
2016-04-28 17:25:52 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
|
2015-09-27 02:17:06 +00:00
|
|
|
|
|
|
|
def test_proxy_argument(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/emptyfile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt'))
|
2015-09-27 02:17:06 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--file', entity, '--config', config, '--proxy', 'localhost:1234']
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, SUCCESS)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
2015-09-27 02:17:06 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY)
|
2015-09-27 02:17:06 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
|
|
|
|
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_called_once_with(ANY, cert=None, proxies={'https': 'localhost:1234'}, stream=False, timeout=60, verify=True)
|
2015-09-27 08:04:06 +00:00
|
|
|
|
2016-03-06 22:08:00 +00:00
|
|
|
def test_entity_type_domain(self):
|
2015-09-27 08:04:06 +00:00
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
entity = 'google.com'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-27 08:04:06 +00:00
|
|
|
now = u(int(time.time()))
|
|
|
|
|
2016-04-28 21:57:15 +00:00
|
|
|
args = ['--entity', entity, '--entity-type', 'domain', '--config', config, '--time', now]
|
2015-09-27 08:04:06 +00:00
|
|
|
retval = execute(args)
|
|
|
|
|
2016-01-25 22:49:37 +00:00
|
|
|
self.assertEquals(retval, API_ERROR)
|
2015-09-27 08:04:06 +00:00
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'entity': u(entity),
|
|
|
|
'time': float(now),
|
|
|
|
'type': 'domain',
|
|
|
|
}
|
|
|
|
stats = {
|
|
|
|
u('cursorpos'): None,
|
|
|
|
u('dependencies'): [],
|
|
|
|
u('language'): None,
|
|
|
|
u('lineno'): None,
|
|
|
|
u('lines'): None,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None)
|
|
|
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
|
|
|
|
2016-03-06 22:08:00 +00:00
|
|
|
def test_entity_type_app(self):
|
2015-09-27 08:04:06 +00:00
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
entity = 'Firefox'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-27 08:04:06 +00:00
|
|
|
now = u(int(time.time()))
|
|
|
|
|
2016-04-28 21:57:15 +00:00
|
|
|
args = ['--entity', entity, '--entity-type', 'app', '--config', config, '--time', now]
|
2015-09-27 08:04:06 +00:00
|
|
|
retval = execute(args)
|
|
|
|
|
2016-01-25 22:49:37 +00:00
|
|
|
self.assertEquals(retval, API_ERROR)
|
2015-09-27 08:04:06 +00:00
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'entity': u(entity),
|
|
|
|
'time': float(now),
|
|
|
|
'type': 'app',
|
|
|
|
}
|
|
|
|
stats = {
|
|
|
|
u('cursorpos'): None,
|
|
|
|
u('dependencies'): [],
|
|
|
|
u('language'): None,
|
|
|
|
u('lineno'): None,
|
|
|
|
u('lines'): None,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None)
|
|
|
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
2016-03-06 22:08:00 +00:00
|
|
|
|
|
|
|
def test_nonascii_hostname(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
2016-03-06 22:08:00 +00:00
|
|
|
entity = 'tests/samples/codefiles/emptyfile.txt'
|
2016-06-08 18:33:36 +00:00
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
|
|
|
|
hostname = 'test汉语' if is_py3 else 'test\xe6\xb1\x89\xe8\xaf\xad'
|
|
|
|
with utils.mock.patch('socket.gethostname') as mock_gethostname:
|
|
|
|
mock_gethostname.return_value = hostname
|
|
|
|
self.assertEquals(type(hostname).__name__, 'str')
|
|
|
|
|
2016-07-06 07:33:50 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2016-06-08 18:33:36 +00:00
|
|
|
args = ['--file', entity, '--config', config]
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, SUCCESS)
|
2016-07-06 07:33:50 +00:00
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
2016-06-08 18:33:36 +00:00
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY)
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
|
|
|
|
|
|
|
|
headers = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].headers
|
|
|
|
self.assertEquals(headers.get('X-Machine-Name'), hostname.encode('utf-8') if is_py3 else hostname)
|
2016-03-06 22:08:00 +00:00
|
|
|
|
2016-07-06 07:33:50 +00:00
|
|
|
def test_hostname_set_from_config_file(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/emptyfile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
|
|
|
|
hostname = 'fromcfgfile'
|
|
|
|
config = 'tests/samples/configs/has_everything.cfg'
|
|
|
|
args = ['--file', entity, '--config', config, '--timeout', '15']
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, SUCCESS)
|
|
|
|
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY)
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
|
|
|
|
|
|
|
|
headers = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].headers
|
|
|
|
self.assertEquals(headers.get('X-Machine-Name'), hostname.encode('utf-8') if is_py3 else hostname)
|
|
|
|
|
2016-03-06 22:08:00 +00:00
|
|
|
def test_nonascii_timezone(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
2016-03-06 22:08:00 +00:00
|
|
|
entity = 'tests/samples/codefiles/emptyfile.txt'
|
2016-06-08 18:33:36 +00:00
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt'))
|
2016-03-06 22:08:00 +00:00
|
|
|
|
2016-08-31 10:23:38 +00:00
|
|
|
package_path = 'wakatime.packages.tzlocal.get_localzone'
|
2016-06-08 18:33:36 +00:00
|
|
|
timezone = tzlocal.get_localzone()
|
|
|
|
timezone.zone = 'tz汉语' if is_py3 else 'tz\xe6\xb1\x89\xe8\xaf\xad'
|
|
|
|
with utils.mock.patch(package_path) as mock_getlocalzone:
|
|
|
|
mock_getlocalzone.return_value = timezone
|
2016-03-06 22:08:00 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
config = 'tests/samples/configs/has_everything.cfg'
|
|
|
|
args = ['--file', entity, '--config', config, '--timeout', '15']
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, SUCCESS)
|
2016-03-06 22:08:00 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY)
|
2016-04-18 21:52:15 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
|
2016-04-18 21:52:15 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
headers = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].headers
|
|
|
|
self.assertEquals(headers.get('TimeZone'), u(timezone.zone).encode('utf-8') if is_py3 else timezone.zone)
|
2016-04-18 21:52:15 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
def test_timezone_header(self):
|
2016-04-20 22:30:44 +00:00
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/emptyfile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt'))
|
2016-04-20 22:30:44 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--file', entity, '--config', config]
|
2016-04-20 22:30:44 +00:00
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, SUCCESS)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
2016-04-20 22:30:44 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
|
2016-06-08 18:33:36 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY)
|
2016-04-20 22:30:44 +00:00
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
2016-04-28 17:25:52 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
|
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
timezone = tzlocal.get_localzone()
|
|
|
|
headers = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].headers
|
|
|
|
self.assertEquals(headers.get('TimeZone'), u(timezone.zone).encode('utf-8') if is_py3 else timezone.zone)
|
2016-04-28 17:25:52 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
def test_extra_heartbeats_argument(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
2016-04-28 17:25:52 +00:00
|
|
|
|
2016-06-08 18:33:36 +00:00
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
|
|
|
|
|
|
|
|
project1 = os.path.basename(os.path.abspath('.'))
|
|
|
|
project2 = 'xyz'
|
|
|
|
entity1 = os.path.abspath('tests/samples/codefiles/emptyfile.txt')
|
|
|
|
entity2 = os.path.abspath('tests/samples/codefiles/twolinefile.txt')
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--file', entity1, '--config', config, '--extra-heartbeats']
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.main.sys.stdin') as mock_stdin:
|
|
|
|
now = int(time.time())
|
|
|
|
heartbeats = json.dumps([{
|
|
|
|
'timestamp': now,
|
|
|
|
'entity': entity2,
|
|
|
|
'entity_type': 'file',
|
|
|
|
'project': project2,
|
|
|
|
'is_write': True,
|
|
|
|
}])
|
|
|
|
mock_stdin.readline.return_value = heartbeats
|
|
|
|
|
|
|
|
retval = execute(args)
|
|
|
|
|
|
|
|
self.assertEquals(retval, SUCCESS)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_has_calls([call(), call()])
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_has_calls([call(ANY), call(ANY)])
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
|
|
|
|
|
|
|
|
calls = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args_list
|
|
|
|
|
|
|
|
body = calls[0][0][0].body
|
|
|
|
data = json.loads(body)
|
|
|
|
self.assertEquals(data.get('entity'), entity1)
|
|
|
|
self.assertEquals(data.get('project'), project1)
|
|
|
|
|
|
|
|
body = calls[1][0][0].body
|
|
|
|
data = json.loads(body)
|
|
|
|
self.assertEquals(data.get('entity'), entity2)
|
|
|
|
self.assertEquals(data.get('project'), project2)
|
2016-07-20 18:32:08 +00:00
|
|
|
|
2016-08-31 12:27:10 +00:00
|
|
|
@log_capture()
|
|
|
|
def test_extra_heartbeats_with_malformed_json(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
|
|
|
|
|
|
|
|
entity = os.path.abspath('tests/samples/codefiles/emptyfile.txt')
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--file', entity, '--config', config, '--extra-heartbeats']
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.main.sys.stdin') as mock_stdin:
|
|
|
|
heartbeats = '[{foobar}]'
|
|
|
|
mock_stdin.readline.return_value = heartbeats
|
|
|
|
|
|
|
|
retval = execute(args)
|
|
|
|
|
|
|
|
self.assertEquals(retval, MALFORMED_HEARTBEAT_ERROR)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
|
|
|
log_output = u("\n").join([u(' ').join(x) for x in logs.actual()])
|
|
|
|
self.assertEquals(log_output, '')
|
|
|
|
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY)
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
|
|
|
|
2016-07-20 18:32:08 +00:00
|
|
|
@log_capture()
|
|
|
|
def test_nonascii_filename(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
filename = os.listdir('tests/samples/codefiles/unicode')[0]
|
|
|
|
entity = os.path.join('tests/samples/codefiles/unicode', filename)
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, filename))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, filename))
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--key', '123', '--config', config, '--time', now]
|
|
|
|
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, API_ERROR)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
|
|
|
output = [u(' ').join(x) for x in logs.actual()]
|
|
|
|
self.assertEquals(len(output), 0)
|
|
|
|
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'language': 'Text only',
|
|
|
|
'lines': 0,
|
|
|
|
'entity': os.path.realpath(entity),
|
|
|
|
'project': os.path.basename(os.path.abspath('.')),
|
|
|
|
'time': float(now),
|
|
|
|
'type': 'file',
|
|
|
|
}
|
|
|
|
stats = {
|
|
|
|
u('cursorpos'): None,
|
|
|
|
u('dependencies'): [],
|
|
|
|
u('language'): u('Text only'),
|
|
|
|
u('lineno'): None,
|
|
|
|
u('lines'): 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None)
|
|
|
|
for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items():
|
|
|
|
self.assertEquals(heartbeat[key], val)
|
|
|
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
2016-08-31 12:27:10 +00:00
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_unhandled_exception(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.main.process_heartbeat') as mock_process_heartbeat:
|
|
|
|
ex_msg = 'testing unhandled exception'
|
|
|
|
mock_process_heartbeat.side_effect = RuntimeError(ex_msg)
|
|
|
|
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--entity', entity, '--key', '123', '--config', config]
|
|
|
|
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
self.assertIn(ex_msg, sys.stdout.getvalue())
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
|
|
|
log_output = u("\n").join([u(' ').join(x) for x in logs.actual()])
|
|
|
|
self.assertIn(ex_msg, log_output)
|
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
|