test_service_cluster.js (2112B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const { Service } = ChromeUtils.importESModule( 5 "resource://services-sync/service.sys.mjs" 6 ); 7 8 add_task(async function test_findCluster() { 9 syncTestLogging(); 10 _("Test Service._findCluster()"); 11 try { 12 let whenReadyToAuthenticate = Promise.withResolvers(); 13 Service.identity.whenReadyToAuthenticate = whenReadyToAuthenticate; 14 whenReadyToAuthenticate.resolve(true); 15 16 Service.identity._ensureValidToken = () => 17 Promise.reject(new Error("Connection refused")); 18 19 _("_findCluster() throws on network errors (e.g. connection refused)."); 20 await Assert.rejects(Service.identity._findCluster(), /Connection refused/); 21 22 Service.identity._ensureValidToken = () => 23 Promise.resolve({ endpoint: "http://weave.user.node" }); 24 25 _("_findCluster() returns the user's cluster node"); 26 let cluster = await Service.identity._findCluster(); 27 Assert.equal(cluster, "http://weave.user.node/"); 28 } finally { 29 for (const pref of Svc.PrefBranch.getChildList("")) { 30 Svc.PrefBranch.clearUserPref(pref); 31 } 32 } 33 }); 34 35 add_task(async function test_setCluster() { 36 syncTestLogging(); 37 _("Test Service._setCluster()"); 38 try { 39 _("Check initial state."); 40 Assert.equal(Service.clusterURL, ""); 41 42 Service.identity._findCluster = () => "http://weave.user.node/"; 43 44 _("Set the cluster URL."); 45 Assert.ok(await Service.identity.setCluster()); 46 Assert.equal(Service.clusterURL, "http://weave.user.node/"); 47 48 _("Setting it again won't make a difference if it's the same one."); 49 Assert.ok(!(await Service.identity.setCluster())); 50 Assert.equal(Service.clusterURL, "http://weave.user.node/"); 51 52 _("A 'null' response won't make a difference either."); 53 Service.identity._findCluster = () => null; 54 Assert.ok(!(await Service.identity.setCluster())); 55 Assert.equal(Service.clusterURL, "http://weave.user.node/"); 56 } finally { 57 for (const pref of Svc.PrefBranch.getChildList("")) { 58 Svc.PrefBranch.clearUserPref(pref); 59 } 60 } 61 });