summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_service_cluster.js
blob: d7d63e018b91fc5d5f189c484d2d235746da7810 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

const { PromiseUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/PromiseUtils.sys.mjs"
);
const { Service } = ChromeUtils.importESModule(
  "resource://services-sync/service.sys.mjs"
);

add_task(async function test_findCluster() {
  syncTestLogging();
  _("Test Service._findCluster()");
  try {
    let whenReadyToAuthenticate = PromiseUtils.defer();
    Service.identity.whenReadyToAuthenticate = whenReadyToAuthenticate;
    whenReadyToAuthenticate.resolve(true);

    Service.identity._ensureValidToken = () =>
      Promise.reject(new Error("Connection refused"));

    _("_findCluster() throws on network errors (e.g. connection refused).");
    await Assert.rejects(Service.identity._findCluster(), /Connection refused/);

    Service.identity._ensureValidToken = () =>
      Promise.resolve({ endpoint: "http://weave.user.node" });

    _("_findCluster() returns the user's cluster node");
    let cluster = await Service.identity._findCluster();
    Assert.equal(cluster, "http://weave.user.node/");
  } finally {
    Svc.Prefs.resetBranch("");
  }
});

add_task(async function test_setCluster() {
  syncTestLogging();
  _("Test Service._setCluster()");
  try {
    _("Check initial state.");
    Assert.equal(Service.clusterURL, "");

    Service.identity._findCluster = () => "http://weave.user.node/";

    _("Set the cluster URL.");
    Assert.ok(await Service.identity.setCluster());
    Assert.equal(Service.clusterURL, "http://weave.user.node/");

    _("Setting it again won't make a difference if it's the same one.");
    Assert.ok(!(await Service.identity.setCluster()));
    Assert.equal(Service.clusterURL, "http://weave.user.node/");

    _("A 'null' response won't make a difference either.");
    Service.identity._findCluster = () => null;
    Assert.ok(!(await Service.identity.setCluster()));
    Assert.equal(Service.clusterURL, "http://weave.user.node/");
  } finally {
    Svc.Prefs.resetBranch("");
  }
});