summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_service_cluster.js
blob: 65f0c3a9568a3b1c3e7568e748ddf1406a9e9736 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/util.js");
Cu.import("resource://testing-common/services/sync/utils.js");

function do_check_throws(func) {
  var raised = false;
  try {
    func();
  } catch (ex) {
    raised = true;
  }
  do_check_true(raised);
}

add_test(function test_findCluster() {
  _("Test Service._findCluster()");
  let server;
  ensureLegacyIdentityManager();
  try {
    _("_findCluster() throws on network errors (e.g. connection refused).");
    do_check_throws(function() {
      Service.serverURL = "http://dummy:9000/";
      Service.identity.account = "johndoe";
      Service._clusterManager._findCluster();
    });

    server = httpd_setup({
      "/user/1.0/johndoe/node/weave": httpd_handler(200, "OK", "http://weave.user.node/"),
      "/user/1.0/jimdoe/node/weave": httpd_handler(200, "OK", "null"),
      "/user/1.0/janedoe/node/weave": httpd_handler(404, "Not Found", "Not Found"),
      "/user/1.0/juliadoe/node/weave": httpd_handler(400, "Bad Request", "Bad Request"),
      "/user/1.0/joedoe/node/weave": httpd_handler(500, "Server Error", "Server Error")
    });

    Service.serverURL = server.baseURI;
    Service.identity.account = "johndoe";

    _("_findCluster() returns the user's cluster node");
    let cluster = Service._clusterManager._findCluster();
    do_check_eq(cluster, "http://weave.user.node/");

    _("A 'null' response is converted to null.");
    Service.identity.account = "jimdoe";
    cluster = Service._clusterManager._findCluster();
    do_check_eq(cluster, null);

    _("If a 404 is encountered, the server URL is taken as the cluster URL");
    Service.identity.account = "janedoe";
    cluster = Service._clusterManager._findCluster();
    do_check_eq(cluster, Service.serverURL);

    _("A 400 response will throw an error.");
    Service.identity.account = "juliadoe";
    do_check_throws(function() {
      Service._clusterManager._findCluster();
    });

    _("Any other server response (e.g. 500) will throw an error.");
    Service.identity.account = "joedoe";
    do_check_throws(function() {
      Service._clusterManager._findCluster();
    });

  } finally {
    Svc.Prefs.resetBranch("");
    if (server) {
      server.stop(run_next_test);
    }
  }
});

add_test(function test_setCluster() {
  _("Test Service._setCluster()");
  let server = httpd_setup({
    "/user/1.0/johndoe/node/weave": httpd_handler(200, "OK", "http://weave.user.node/"),
    "/user/1.0/jimdoe/node/weave": httpd_handler(200, "OK", "null")
  });
  try {
    Service.serverURL = server.baseURI;
    Service.identity.account = "johndoe";

    _("Check initial state.");
    do_check_eq(Service.clusterURL, "");

    _("Set the cluster URL.");
    do_check_true(Service._clusterManager.setCluster());
    do_check_eq(Service.clusterURL, "http://weave.user.node/");

    _("Setting it again won't make a difference if it's the same one.");
    do_check_false(Service._clusterManager.setCluster());
    do_check_eq(Service.clusterURL, "http://weave.user.node/");

    _("A 'null' response won't make a difference either.");
    Service.identity.account = "jimdoe";
    do_check_false(Service._clusterManager.setCluster());
    do_check_eq(Service.clusterURL, "http://weave.user.node/");

  } finally {
    Svc.Prefs.resetBranch("");
    server.stop(run_next_test);
  }
});

function run_test() {
  initTestLogging();
  run_next_test();
}