summaryrefslogtreecommitdiffstats
path: root/core/js
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2014-02-04 13:56:41 +0100
committerVincent Petry <pvince81@owncloud.com>2014-02-04 13:56:41 +0100
commite75f7e58e9778bbe5bed6ba387c781c2ece94bbf (patch)
tree7dbcd375885dc290c85d4c2997b9832f70d31c95 /core/js
parent912da8d27756d9f0d55821338f6d8698af2dbd27 (diff)
downloadnextcloud-server-e75f7e58e9778bbe5bed6ba387c781c2ece94bbf.tar.gz
nextcloud-server-e75f7e58e9778bbe5bed6ba387c781c2ece94bbf.zip
Added unit tests for session_keepalive / heartbeat
Diffstat (limited to 'core/js')
-rw-r--r--core/js/tests/specHelper.js18
-rw-r--r--core/js/tests/specs/coreSpec.js74
2 files changed, 91 insertions, 1 deletions
diff --git a/core/js/tests/specHelper.js b/core/js/tests/specHelper.js
index 4a30878df51..1848d08354e 100644
--- a/core/js/tests/specHelper.js
+++ b/core/js/tests/specHelper.js
@@ -19,6 +19,8 @@
*
*/
+/* global OC */
+
/**
* Simulate the variables that are normally set by PHP code
*/
@@ -57,10 +59,15 @@ window.oc_webroot = location.href + '/';
window.oc_appswebroots = {
"files": window.oc_webroot + '/apps/files/'
};
+window.oc_config = {
+ session_lifetime: 600 * 1000,
+ session_keepalive: false
+};
// global setup for all tests
(function setupTests() {
- var fakeServer = null;
+ var fakeServer = null,
+ routesRequestStub;
beforeEach(function() {
// enforce fake XHR, tests should not depend on the server and
@@ -78,9 +85,18 @@ window.oc_appswebroots = {
// make it globally available, so that other tests can define
// custom responses
window.fakeServer = fakeServer;
+
+ OC.Router.routes = [];
+ OC.Router.routes_request = {
+ state: sinon.stub().returns('resolved'),
+ done: sinon.stub()
+ };
});
afterEach(function() {
+ OC.Router.routes_request.state.reset();
+ OC.Router.routes_request.done.reset();
+
// uncomment this to log requests
// console.log(window.fakeServer.requests);
fakeServer.restore();
diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js
index 28c20a0642e..18652d4177f 100644
--- a/core/js/tests/specs/coreSpec.js
+++ b/core/js/tests/specs/coreSpec.js
@@ -104,4 +104,78 @@ describe('Core base tests', function() {
})).toEqual('number=123');
});
});
+ describe('Session heartbeat', function() {
+ var clock,
+ oldConfig,
+ loadedStub,
+ routeStub,
+ counter;
+
+ beforeEach(function() {
+ clock = sinon.useFakeTimers();
+ oldConfig = window.oc_config;
+ loadedStub = sinon.stub(OC.Router, 'registerLoadedCallback');
+ routeStub = sinon.stub(OC.Router, 'generate').returns('/heartbeat');
+ counter = 0;
+
+ fakeServer.autoRespond = true;
+ fakeServer.autoRespondAfter = 0;
+ fakeServer.respondWith(/\/heartbeat/, function(xhr) {
+ counter++;
+ xhr.respond(200, {'Content-Type': 'application/json'}, '{}');
+ });
+ });
+ afterEach(function() {
+ clock.restore();
+ window.oc_config = oldConfig;
+ loadedStub.restore();
+ routeStub.restore();
+ });
+ it('sends heartbeat half the session lifetime when heartbeat enabled', function() {
+ window.oc_config = {
+ session_keepalive: true,
+ session_lifetime: 300
+ };
+ window.initCore();
+ expect(loadedStub.calledOnce).toEqual(true);
+ loadedStub.yield();
+ expect(routeStub.calledWith('heartbeat')).toEqual(true);
+
+ expect(counter).toEqual(0);
+
+ // less than half, still nothing
+ clock.tick(100 * 1000);
+ expect(counter).toEqual(0);
+
+ // reach past half (160), one call
+ clock.tick(55 * 1000);
+ expect(counter).toEqual(1);
+
+ // almost there to the next, still one
+ clock.tick(140 * 1000);
+ expect(counter).toEqual(1);
+
+ // past it, second call
+ clock.tick(20 * 1000);
+ expect(counter).toEqual(2);
+ });
+ it('does no send heartbeat when heartbeat disabled', function() {
+ window.oc_config = {
+ session_keepalive: false,
+ session_lifetime: 300
+ };
+ window.initCore();
+ expect(loadedStub.notCalled).toEqual(true);
+ expect(routeStub.notCalled).toEqual(true);
+
+ expect(counter).toEqual(0);
+
+ clock.tick(1000000);
+
+ // still nothing
+ expect(counter).toEqual(0);
+ });
+
+ });
});
+