services/sync/tests/unit/test_service_cluster.js

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 Cu.import("resource://services-sync/service.js");
michael@0 5 Cu.import("resource://services-sync/util.js");
michael@0 6 Cu.import("resource://testing-common/services/sync/utils.js");
michael@0 7
michael@0 8 function do_check_throws(func) {
michael@0 9 var raised = false;
michael@0 10 try {
michael@0 11 func();
michael@0 12 } catch (ex) {
michael@0 13 raised = true;
michael@0 14 }
michael@0 15 do_check_true(raised);
michael@0 16 }
michael@0 17
michael@0 18 add_test(function test_findCluster() {
michael@0 19 _("Test Service._findCluster()");
michael@0 20 let server;
michael@0 21 ensureLegacyIdentityManager();
michael@0 22 try {
michael@0 23 _("_findCluster() throws on network errors (e.g. connection refused).");
michael@0 24 do_check_throws(function() {
michael@0 25 Service.serverURL = "http://dummy:9000/";
michael@0 26 Service.identity.account = "johndoe";
michael@0 27 Service._clusterManager._findCluster();
michael@0 28 });
michael@0 29
michael@0 30 server = httpd_setup({
michael@0 31 "/user/1.0/johndoe/node/weave": httpd_handler(200, "OK", "http://weave.user.node/"),
michael@0 32 "/user/1.0/jimdoe/node/weave": httpd_handler(200, "OK", "null"),
michael@0 33 "/user/1.0/janedoe/node/weave": httpd_handler(404, "Not Found", "Not Found"),
michael@0 34 "/user/1.0/juliadoe/node/weave": httpd_handler(400, "Bad Request", "Bad Request"),
michael@0 35 "/user/1.0/joedoe/node/weave": httpd_handler(500, "Server Error", "Server Error")
michael@0 36 });
michael@0 37
michael@0 38 Service.serverURL = server.baseURI;
michael@0 39 Service.identity.account = "johndoe";
michael@0 40
michael@0 41 _("_findCluster() returns the user's cluster node");
michael@0 42 let cluster = Service._clusterManager._findCluster();
michael@0 43 do_check_eq(cluster, "http://weave.user.node/");
michael@0 44
michael@0 45 _("A 'null' response is converted to null.");
michael@0 46 Service.identity.account = "jimdoe";
michael@0 47 cluster = Service._clusterManager._findCluster();
michael@0 48 do_check_eq(cluster, null);
michael@0 49
michael@0 50 _("If a 404 is encountered, the server URL is taken as the cluster URL");
michael@0 51 Service.identity.account = "janedoe";
michael@0 52 cluster = Service._clusterManager._findCluster();
michael@0 53 do_check_eq(cluster, Service.serverURL);
michael@0 54
michael@0 55 _("A 400 response will throw an error.");
michael@0 56 Service.identity.account = "juliadoe";
michael@0 57 do_check_throws(function() {
michael@0 58 Service._clusterManager._findCluster();
michael@0 59 });
michael@0 60
michael@0 61 _("Any other server response (e.g. 500) will throw an error.");
michael@0 62 Service.identity.account = "joedoe";
michael@0 63 do_check_throws(function() {
michael@0 64 Service._clusterManager._findCluster();
michael@0 65 });
michael@0 66
michael@0 67 } finally {
michael@0 68 Svc.Prefs.resetBranch("");
michael@0 69 if (server) {
michael@0 70 server.stop(run_next_test);
michael@0 71 }
michael@0 72 }
michael@0 73 });
michael@0 74
michael@0 75 add_test(function test_setCluster() {
michael@0 76 _("Test Service._setCluster()");
michael@0 77 let server = httpd_setup({
michael@0 78 "/user/1.0/johndoe/node/weave": httpd_handler(200, "OK", "http://weave.user.node/"),
michael@0 79 "/user/1.0/jimdoe/node/weave": httpd_handler(200, "OK", "null")
michael@0 80 });
michael@0 81 try {
michael@0 82 Service.serverURL = server.baseURI;
michael@0 83 Service.identity.account = "johndoe";
michael@0 84
michael@0 85 _("Check initial state.");
michael@0 86 do_check_eq(Service.clusterURL, "");
michael@0 87
michael@0 88 _("Set the cluster URL.");
michael@0 89 do_check_true(Service._clusterManager.setCluster());
michael@0 90 do_check_eq(Service.clusterURL, "http://weave.user.node/");
michael@0 91
michael@0 92 _("Setting it again won't make a difference if it's the same one.");
michael@0 93 do_check_false(Service._clusterManager.setCluster());
michael@0 94 do_check_eq(Service.clusterURL, "http://weave.user.node/");
michael@0 95
michael@0 96 _("A 'null' response won't make a difference either.");
michael@0 97 Service.identity.account = "jimdoe";
michael@0 98 do_check_false(Service._clusterManager.setCluster());
michael@0 99 do_check_eq(Service.clusterURL, "http://weave.user.node/");
michael@0 100
michael@0 101 } finally {
michael@0 102 Svc.Prefs.resetBranch("");
michael@0 103 server.stop(run_next_test);
michael@0 104 }
michael@0 105 });
michael@0 106
michael@0 107 function run_test() {
michael@0 108 initTestLogging();
michael@0 109 run_next_test();
michael@0 110 }

mercurial