2015-09-27 08:04:06 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
2017-10-29 18:16:23 +00:00
|
|
|
import os
|
2015-09-27 08:04:06 +00:00
|
|
|
from wakatime.session_cache import SessionCache
|
2017-05-21 22:06:39 +00:00
|
|
|
from wakatime.logger import setup_logging
|
2015-09-27 08:04:06 +00:00
|
|
|
from . import utils
|
|
|
|
|
|
|
|
|
|
|
|
class SessionCacheTestCase(utils.TestCase):
|
2015-09-27 10:17:54 +00:00
|
|
|
patch_these = [
|
|
|
|
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
|
|
|
'wakatime.offlinequeue.Queue.push',
|
|
|
|
['wakatime.offlinequeue.Queue.pop', None],
|
|
|
|
['wakatime.offlinequeue.Queue.connect', None],
|
|
|
|
]
|
2015-09-27 08:04:06 +00:00
|
|
|
|
2017-05-21 22:06:39 +00:00
|
|
|
def setUp(self):
|
2017-05-21 22:11:06 +00:00
|
|
|
super(SessionCacheTestCase, self).setUp()
|
2017-10-29 18:16:23 +00:00
|
|
|
|
2017-05-21 22:06:39 +00:00
|
|
|
class MockArgs(object):
|
|
|
|
timestamp = 0
|
|
|
|
is_write = False
|
|
|
|
entity = ''
|
|
|
|
version = ''
|
|
|
|
plugin = ''
|
|
|
|
verbose = False
|
2018-01-05 07:24:19 +00:00
|
|
|
log_file = ''
|
2017-05-21 22:06:39 +00:00
|
|
|
self.args = MockArgs()
|
|
|
|
setup_logging(self.args, self.args.version)
|
|
|
|
|
2015-09-27 08:52:53 +00:00
|
|
|
def test_can_crud_session(self):
|
2017-05-21 22:58:48 +00:00
|
|
|
with utils.NamedTemporaryFile() as fh:
|
2015-09-27 08:59:39 +00:00
|
|
|
cache = SessionCache()
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
with utils.mock.patch('wakatime.session_cache.SessionCache._get_db_file') as mock_dbfile:
|
2017-10-29 18:16:23 +00:00
|
|
|
mock_dbfile.return_value = fh.name
|
2015-09-27 08:59:39 +00:00
|
|
|
|
|
|
|
session = cache.get()
|
|
|
|
session.headers.update({'x-test': 'abc'})
|
|
|
|
cache.save(session)
|
|
|
|
session = cache.get()
|
2017-10-29 18:16:23 +00:00
|
|
|
self.assertEquals(session.headers.get('x-test'), 'abc')
|
2015-09-27 08:59:39 +00:00
|
|
|
cache.delete()
|
|
|
|
session = cache.get()
|
|
|
|
self.assertEquals(session.headers.get('x-test'), None)
|
2017-10-29 18:16:23 +00:00
|
|
|
|
|
|
|
def test_handles_connection_exception(self):
|
|
|
|
with utils.NamedTemporaryFile() as fh:
|
|
|
|
cache = SessionCache()
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
with utils.mock.patch('wakatime.session_cache.SessionCache._get_db_file') as mock_dbfile:
|
2017-10-29 18:16:23 +00:00
|
|
|
mock_dbfile.return_value = fh.name
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.session_cache.SessionCache.connect') as mock_connect:
|
|
|
|
mock_connect.side_effect = OSError('')
|
|
|
|
|
|
|
|
session = cache.get()
|
|
|
|
session.headers.update({'x-test': 'abc'})
|
|
|
|
cache.save(session)
|
|
|
|
session = cache.get()
|
|
|
|
self.assertEquals(session.headers.get('x-test'), None)
|
|
|
|
cache.delete()
|
|
|
|
session = cache.get()
|
|
|
|
self.assertEquals(session.headers.get('x-test'), None)
|
|
|
|
|
|
|
|
def test_uses_wakatime_home_env_variable(self):
|
|
|
|
with utils.TemporaryDirectory() as tempdir:
|
|
|
|
expected = os.path.realpath(os.path.join(os.path.expanduser('~'), '.wakatime.db'))
|
|
|
|
|
|
|
|
cache = SessionCache()
|
2017-11-09 06:54:33 +00:00
|
|
|
actual = cache._get_db_file()
|
2017-10-29 18:16:23 +00:00
|
|
|
self.assertEquals(actual, expected)
|
|
|
|
|
|
|
|
with utils.mock.patch('os.environ.get') as mock_env:
|
|
|
|
mock_env.return_value = os.path.realpath(tempdir)
|
|
|
|
|
|
|
|
expected = os.path.realpath(os.path.join(tempdir, '.wakatime.db'))
|
2017-11-09 06:54:33 +00:00
|
|
|
actual = cache._get_db_file()
|
2017-10-29 18:16:23 +00:00
|
|
|
self.assertEquals(actual, expected)
|