2015-09-07 01:22:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
from wakatime.main import execute
|
|
|
|
from wakatime.offlinequeue import Queue
|
|
|
|
from wakatime.packages import requests
|
|
|
|
|
2016-07-06 21:07:55 +00:00
|
|
|
import logging
|
2015-09-07 01:22:05 +00:00
|
|
|
import os
|
2015-09-27 10:17:54 +00:00
|
|
|
import sqlite3
|
|
|
|
import sys
|
2015-09-07 01:47:17 +00:00
|
|
|
import tempfile
|
2015-09-07 01:22:05 +00:00
|
|
|
import time
|
2016-07-06 21:07:55 +00:00
|
|
|
from testfixtures import log_capture
|
2015-09-07 01:22:05 +00:00
|
|
|
from wakatime.compat import u
|
|
|
|
from wakatime.packages.requests.models import Response
|
|
|
|
from . import utils
|
2016-07-06 21:07:55 +00:00
|
|
|
try:
|
|
|
|
from mock import call
|
|
|
|
except ImportError:
|
|
|
|
from unittest.mock import call
|
2015-09-07 01:22:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OfflineQueueTestCase(utils.TestCase):
|
|
|
|
patch_these = [
|
|
|
|
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
|
|
|
'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-09-07 01:22:05 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def test_heartbeat_saved_from_error_response(self):
|
2015-09-27 10:17:54 +00:00
|
|
|
with tempfile.NamedTemporaryFile() as fh:
|
|
|
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
|
|
|
mock_db_file.return_value = fh.name
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
queue = Queue()
|
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|
2015-09-07 01:22:05 +00:00
|
|
|
|
|
|
|
def test_heartbeat_discarded_from_400_response(self):
|
2015-09-27 10:17:54 +00:00
|
|
|
with tempfile.NamedTemporaryFile() as fh:
|
|
|
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 400
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
queue = Queue()
|
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(None, saved_heartbeat)
|
2015-09-07 01:22:05 +00:00
|
|
|
|
|
|
|
def test_offline_heartbeat_sent_after_success_response(self):
|
2015-09-27 10:17:54 +00:00
|
|
|
with tempfile.NamedTemporaryFile() as fh:
|
|
|
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
response.status_code = 201
|
|
|
|
execute(args)
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
queue = Queue()
|
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(None, saved_heartbeat)
|
2015-09-07 01:47:17 +00:00
|
|
|
|
|
|
|
def test_empty_project_can_be_saved(self):
|
2015-09-27 10:17:54 +00:00
|
|
|
with tempfile.NamedTemporaryFile() as fh:
|
|
|
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/emptyfile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-27 10:17:54 +00:00
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
queue = Queue()
|
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|
|
|
|
|
2015-09-27 10:32:28 +00:00
|
|
|
def test_get_handles_connection_exception(self):
|
2015-09-27 10:17:54 +00:00
|
|
|
with tempfile.NamedTemporaryFile() as fh:
|
|
|
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-27 10:17:54 +00:00
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
2015-09-07 01:47:17 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
with utils.mock.patch('wakatime.offlinequeue.Queue.connect') as mock_connect:
|
|
|
|
mock_connect.side_effect = sqlite3.Error('')
|
2015-09-07 01:47:17 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
response.status_code = 201
|
|
|
|
execute(args)
|
2015-09-07 01:47:17 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
queue = Queue()
|
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(None, saved_heartbeat)
|
2015-09-07 01:47:17 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
queue = Queue()
|
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|
2015-09-27 10:32:28 +00:00
|
|
|
|
|
|
|
def test_push_handles_connection_exception(self):
|
|
|
|
with tempfile.NamedTemporaryFile() as fh:
|
|
|
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-27 10:32:28 +00:00
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.offlinequeue.Queue.connect') as mock_connect:
|
|
|
|
mock_connect.side_effect = sqlite3.Error('')
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
response.status_code = 201
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
queue = Queue()
|
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(None, saved_heartbeat)
|
|
|
|
|
|
|
|
def test_get_db_file(self):
|
|
|
|
queue = Queue()
|
|
|
|
db_file = queue.get_db_file()
|
|
|
|
expected = os.path.join(os.path.expanduser('~'), '.wakatime.db')
|
|
|
|
self.assertEquals(db_file, expected)
|
2016-07-06 21:07:55 +00:00
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_heartbeat_saved_when_requests_raises_exception(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
with tempfile.NamedTemporaryFile() as fh:
|
|
|
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].side_effect = AttributeError("'Retry' object has no attribute 'history'")
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
queue = Queue()
|
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|
|
|
|
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
|
|
|
output = [u(' ').join(x) for x in logs.actual()]
|
|
|
|
expected = u("AttributeError: \\'Retry\\' object has no attribute \\'history\\'")
|
|
|
|
self.assertIn(expected, output[0])
|
|
|
|
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_has_calls([call(), call()])
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|