2017-02-15 23:02:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
from wakatime.main import execute
|
|
|
|
from wakatime.packages import requests
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
from testfixtures import log_capture
|
|
|
|
from wakatime.compat import u
|
2017-05-25 05:54:58 +00:00
|
|
|
from wakatime.constants import API_ERROR, SUCCESS
|
2017-05-25 06:07:12 +00:00
|
|
|
from wakatime.packages.requests.exceptions import RequestException
|
2017-11-23 02:34:15 +00:00
|
|
|
from wakatime.packages.requests.models import Response
|
2017-02-15 23:02:05 +00:00
|
|
|
from . import utils
|
2017-11-23 02:34:15 +00:00
|
|
|
from .utils import CustomResponse
|
2017-02-15 23:02:05 +00:00
|
|
|
|
|
|
|
try:
|
2017-05-25 05:54:58 +00:00
|
|
|
from mock import ANY, call
|
2017-02-15 23:02:05 +00:00
|
|
|
except ImportError:
|
2017-05-25 05:54:58 +00:00
|
|
|
from unittest.mock import ANY, call
|
2017-02-15 23:02:05 +00:00
|
|
|
|
|
|
|
|
2017-05-06 00:23:26 +00:00
|
|
|
class ProxyTestCase(utils.TestCase):
|
2017-02-15 23:02:05 +00:00
|
|
|
patch_these = [
|
|
|
|
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
|
|
|
'wakatime.offlinequeue.Queue.push',
|
|
|
|
['wakatime.offlinequeue.Queue.pop', None],
|
|
|
|
['wakatime.offlinequeue.Queue.connect', None],
|
|
|
|
'wakatime.session_cache.SessionCache.save',
|
|
|
|
'wakatime.session_cache.SessionCache.delete',
|
|
|
|
['wakatime.session_cache.SessionCache.get', requests.session],
|
|
|
|
['wakatime.session_cache.SessionCache.connect', None],
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_proxy_without_protocol(self):
|
2017-11-23 02:34:15 +00:00
|
|
|
response = CustomResponse()
|
2017-02-15 23:02:05 +00:00
|
|
|
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'))
|
|
|
|
proxy = 'user:pass@localhost:12345'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--file', entity, '--config', config, '--proxy', proxy]
|
|
|
|
|
|
|
|
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_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()
|
|
|
|
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_called_once_with(ANY, cert=None, proxies={'https': proxy}, stream=False, timeout=60, verify=True)
|
|
|
|
|
|
|
|
def test_https_proxy(self):
|
2017-11-23 02:34:15 +00:00
|
|
|
response = CustomResponse()
|
2017-02-15 23:02:05 +00:00
|
|
|
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'))
|
|
|
|
proxy = 'https://user:pass@localhost:12345'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--file', entity, '--config', config, '--proxy', proxy]
|
|
|
|
|
|
|
|
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_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()
|
|
|
|
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_called_once_with(ANY, cert=None, proxies={'https': proxy}, stream=False, timeout=60, verify=True)
|
|
|
|
|
|
|
|
def test_socks_proxy(self):
|
2017-11-23 02:34:15 +00:00
|
|
|
response = CustomResponse()
|
2017-02-15 23:02:05 +00:00
|
|
|
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'))
|
|
|
|
proxy = 'socks5://user:pass@localhost:12345'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--file', entity, '--config', config, '--proxy', proxy]
|
|
|
|
|
|
|
|
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_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()
|
|
|
|
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_called_once_with(ANY, cert=None, proxies={'https': proxy}, stream=False, timeout=60, verify=True)
|
|
|
|
|
2017-05-25 05:54:58 +00:00
|
|
|
def test_ntlm_proxy_used_after_trying_normal_proxy(self):
|
2017-02-15 23:02:05 +00:00
|
|
|
response = Response()
|
2017-05-25 05:54:58 +00:00
|
|
|
response.status_code = 400
|
2017-02-15 23:02:05 +00:00
|
|
|
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'))
|
|
|
|
proxy = 'domain\\user:pass'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--file', entity, '--config', config, '--proxy', proxy]
|
|
|
|
|
|
|
|
retval = execute(args)
|
2017-05-25 05:54:58 +00:00
|
|
|
self.assertEquals(retval, API_ERROR)
|
2017-02-15 23:02:05 +00:00
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
2017-05-25 05:54:58 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_has_calls([call(), call()])
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
2017-02-15 23:02:05 +00:00
|
|
|
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
2017-05-25 05:54:58 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
|
|
|
|
|
|
|
expected_calls = [
|
|
|
|
call(ANY, cert=None, proxies={'https': proxy}, stream=False, timeout=60, verify=True),
|
|
|
|
call(ANY, cert=None, proxies={}, stream=False, timeout=60, verify=True),
|
|
|
|
]
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_has_calls(expected_calls)
|
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_ntlm_proxy_used_after_normal_proxy_raises_exception(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
ex_msg = 'after exception, should still try ntlm proxy'
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].side_effect = RuntimeError(ex_msg)
|
|
|
|
|
|
|
|
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'))
|
|
|
|
proxy = 'domain\\user:pass'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--file', entity, '--config', config, '--proxy', proxy]
|
2017-02-15 23:02:05 +00:00
|
|
|
|
2017-05-25 05:54:58 +00:00
|
|
|
retval = execute(args)
|
|
|
|
|
|
|
|
self.assertEquals(retval, API_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.assertIn(ex_msg, log_output)
|
|
|
|
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_has_calls([call(), call()])
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY)
|
2017-05-25 05:54:58 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
|
|
|
|
|
|
|
expected_calls = [
|
|
|
|
call(ANY, cert=None, proxies={'https': proxy}, stream=False, timeout=60, verify=True),
|
2017-05-25 06:07:12 +00:00
|
|
|
call(ANY, cert=None, proxies={}, stream=False, timeout=60, verify=True),
|
|
|
|
]
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_has_calls(expected_calls)
|
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_ntlm_proxy_used_after_normal_proxy_raises_requests_exception(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
ex_msg = 'after exception, should still try ntlm proxy'
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].side_effect = RequestException(ex_msg)
|
|
|
|
|
|
|
|
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'))
|
|
|
|
proxy = 'domain\\user:pass'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--file', entity, '--config', config, '--proxy', proxy]
|
|
|
|
|
|
|
|
retval = execute(args)
|
|
|
|
|
|
|
|
self.assertEquals(retval, API_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_has_calls([call(), call()])
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY)
|
2017-05-25 06:07:12 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
|
|
|
|
|
|
|
expected_calls = [
|
|
|
|
call(ANY, cert=None, proxies={'https': proxy}, stream=False, timeout=60, verify=True),
|
2017-05-25 05:54:58 +00:00
|
|
|
call(ANY, cert=None, proxies={}, stream=False, timeout=60, verify=True),
|
|
|
|
]
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_has_calls(expected_calls)
|
2017-02-15 23:02:05 +00:00
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_invalid_proxy(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
2017-11-23 02:34:15 +00:00
|
|
|
response = CustomResponse()
|
2017-02-15 23:02:05 +00:00
|
|
|
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'))
|
|
|
|
proxy = 'invaliddd:proxyarg'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--file', entity, '--config', config, '--proxy', proxy]
|
|
|
|
|
|
|
|
with self.assertRaises(SystemExit) as e:
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
self.assertEquals(int(str(e.exception)), 2)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
2017-02-15 23:06:53 +00:00
|
|
|
expected = 'error: Invalid proxy. Must be in format https://user:pass@host:port or socks5://user:pass@host:port or domain\\user:pass.'
|
2017-02-15 23:02:05 +00:00
|
|
|
self.assertIn(expected, sys.stderr.getvalue())
|
|
|
|
|
|
|
|
log_output = u("\n").join([u(' ').join(x) for x in logs.actual()])
|
|
|
|
expected = ''
|
|
|
|
self.assertEquals(log_output, expected)
|
|
|
|
|
|
|
|
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()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
2017-05-25 05:54:58 +00:00
|
|
|
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_not_called()
|