From 666f29147ef8719533c0eddfcbcdda68ea1ed7db Mon Sep 17 00:00:00 2001
From: Alan Hamlett
Date: Thu, 1 Sep 2016 00:08:21 +0200
Subject: [PATCH] test 500 error when sending offline heartbeats
---
tests/samples/codefiles/html-django.html | 3 ++
tests/test_offlinequeue.py | 60 ++++++++++++++++++++++++
2 files changed, 63 insertions(+)
diff --git a/tests/samples/codefiles/html-django.html b/tests/samples/codefiles/html-django.html
index f9f3c59..5298839 100644
--- a/tests/samples/codefiles/html-django.html
+++ b/tests/samples/codefiles/html-django.html
@@ -1,3 +1,4 @@
+
{% extends "common/base.html" %}
{% block subtitle %}Login{% endblock %}
@@ -34,4 +35,6 @@
{{form.errors.email[0]}}
{% endif %}
{% endblock %}
diff --git a/tests/test_offlinequeue.py b/tests/test_offlinequeue.py
index 8c1813e..6b9e157 100644
--- a/tests/test_offlinequeue.py
+++ b/tests/test_offlinequeue.py
@@ -13,6 +13,10 @@ import tempfile
import time
from testfixtures import log_capture
from wakatime.compat import u
+from wakatime.constants import (
+ AUTH_ERROR,
+ SUCCESS,
+)
from wakatime.packages.requests.models import Response
from . import utils
try:
@@ -212,8 +216,64 @@ class OfflineQueueTestCase(utils.TestCase):
response = CustomResponse()
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
+ retval = execute(args)
+ self.assertEquals(retval, AUTH_ERROR)
+
+ # offline queue should be empty
+ queue = Queue()
+ saved_heartbeat = queue.pop()
+ self.assertEquals(sys.stdout.getvalue(), '')
+ self.assertEquals(sys.stderr.getvalue(), '')
+ self.assertEquals(os.path.realpath(entity2), saved_heartbeat['entity'])
+
+ def test_500_error_when_sending_offline_heartbeats(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
+
+ config = 'tests/samples/configs/good_config.cfg'
+
+ now1 = u(int(time.time()))
+ entity1 = 'tests/samples/codefiles/emptyfile.txt'
+ project1 = 'proj1'
+
+ args = ['--file', entity1, '--config', config, '--time', now1, '--project', project1]
execute(args)
+ now2 = u(int(time.time()))
+ entity2 = 'tests/samples/codefiles/twolinefile.txt'
+ project2 = 'proj2'
+
+ args = ['--file', entity2, '--config', config, '--time', now2, '--project', project2]
+ execute(args)
+
+ # send heartbeats from offline queue after 201 response
+ now3 = u(int(time.time()))
+ entity3 = 'tests/samples/codefiles/python.py'
+ project3 = 'proj3'
+ args = ['--file', entity3, '--config', config, '--time', now3, '--project', project3]
+
+ class CustomResponse(Response):
+ count = 0
+ @property
+ def status_code(self):
+ if self.count > 2:
+ return 500
+ self.count += 1
+ return 201
+ @status_code.setter
+ def status_code(self, value):
+ pass
+ response = CustomResponse()
+ self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
+
+ retval = execute(args)
+ self.assertEquals(retval, SUCCESS)
+
# offline queue should be empty
queue = Queue()
saved_heartbeat = queue.pop()