1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/sync/tests/unit/test_service_attributes.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,119 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +Cu.import("resource://services-sync/constants.js"); 1.8 +Cu.import("resource://services-sync/service.js"); 1.9 +Cu.import("resource://services-sync/util.js"); 1.10 +Cu.import("resource://testing-common/services/sync/fakeservices.js"); 1.11 +Cu.import("resource://testing-common/services/sync/utils.js"); 1.12 + 1.13 +function test_urls() { 1.14 + _("URL related Service properties correspond to preference settings."); 1.15 + try { 1.16 + ensureLegacyIdentityManager(); 1.17 + do_check_true(!!Service.serverURL); // actual value may change 1.18 + do_check_eq(Service.clusterURL, ""); 1.19 + do_check_eq(Service.userBaseURL, undefined); 1.20 + do_check_eq(Service.infoURL, undefined); 1.21 + do_check_eq(Service.storageURL, undefined); 1.22 + do_check_eq(Service.metaURL, undefined); 1.23 + 1.24 + _("The 'clusterURL' attribute updates preferences and cached URLs."); 1.25 + Service.identity.username = "johndoe"; 1.26 + 1.27 + // Since we don't have a cluster URL yet, these will still not be defined. 1.28 + do_check_eq(Service.infoURL, undefined); 1.29 + do_check_eq(Service.userBaseURL, undefined); 1.30 + do_check_eq(Service.storageURL, undefined); 1.31 + do_check_eq(Service.metaURL, undefined); 1.32 + 1.33 + Service.serverURL = "http://weave.server/"; 1.34 + Service.clusterURL = "http://weave.cluster/"; 1.35 + do_check_eq(Svc.Prefs.get("clusterURL"), "http://weave.cluster/"); 1.36 + 1.37 + do_check_eq(Service.userBaseURL, "http://weave.cluster/1.1/johndoe/"); 1.38 + do_check_eq(Service.infoURL, 1.39 + "http://weave.cluster/1.1/johndoe/info/collections"); 1.40 + do_check_eq(Service.storageURL, 1.41 + "http://weave.cluster/1.1/johndoe/storage/"); 1.42 + do_check_eq(Service.metaURL, 1.43 + "http://weave.cluster/1.1/johndoe/storage/meta/global"); 1.44 + 1.45 + _("The 'miscURL' and 'userURL' attributes can be relative to 'serverURL' or absolute."); 1.46 + Svc.Prefs.set("miscURL", "relative/misc/"); 1.47 + Svc.Prefs.set("userURL", "relative/user/"); 1.48 + do_check_eq(Service.miscAPI, 1.49 + "http://weave.server/relative/misc/1.0/"); 1.50 + do_check_eq(Service.userAPIURI, 1.51 + "http://weave.server/relative/user/1.0/"); 1.52 + 1.53 + Svc.Prefs.set("miscURL", "http://weave.misc.services/"); 1.54 + Svc.Prefs.set("userURL", "http://weave.user.services/"); 1.55 + do_check_eq(Service.miscAPI, "http://weave.misc.services/1.0/"); 1.56 + do_check_eq(Service.userAPIURI, "http://weave.user.services/1.0/"); 1.57 + 1.58 + do_check_eq(Service.pwResetURL, 1.59 + "http://weave.server/weave-password-reset"); 1.60 + 1.61 + _("Empty/false value for 'username' resets preference."); 1.62 + Service.identity.username = ""; 1.63 + do_check_eq(Svc.Prefs.get("username"), undefined); 1.64 + do_check_eq(Service.identity.username, null); 1.65 + 1.66 + _("The 'serverURL' attributes updates/resets preferences."); 1.67 + // Identical value doesn't do anything 1.68 + Service.serverURL = Service.serverURL; 1.69 + do_check_eq(Svc.Prefs.get("clusterURL"), "http://weave.cluster/"); 1.70 + 1.71 + Service.serverURL = "http://different.auth.node/"; 1.72 + do_check_eq(Svc.Prefs.get("serverURL"), "http://different.auth.node/"); 1.73 + do_check_eq(Svc.Prefs.get("clusterURL"), undefined); 1.74 + 1.75 + } finally { 1.76 + Svc.Prefs.resetBranch(""); 1.77 + } 1.78 +} 1.79 + 1.80 + 1.81 +function test_syncID() { 1.82 + _("Service.syncID is auto-generated, corresponds to preference."); 1.83 + new FakeGUIDService(); 1.84 + 1.85 + try { 1.86 + // Ensure pristine environment 1.87 + do_check_eq(Svc.Prefs.get("client.syncID"), undefined); 1.88 + 1.89 + // Performing the first get on the attribute will generate a new GUID. 1.90 + do_check_eq(Service.syncID, "fake-guid-0"); 1.91 + do_check_eq(Svc.Prefs.get("client.syncID"), "fake-guid-0"); 1.92 + 1.93 + Svc.Prefs.set("client.syncID", Utils.makeGUID()); 1.94 + do_check_eq(Svc.Prefs.get("client.syncID"), "fake-guid-1"); 1.95 + do_check_eq(Service.syncID, "fake-guid-1"); 1.96 + } finally { 1.97 + Svc.Prefs.resetBranch(""); 1.98 + new FakeGUIDService(); 1.99 + } 1.100 +} 1.101 + 1.102 +function test_locked() { 1.103 + _("The 'locked' attribute can be toggled with lock() and unlock()"); 1.104 + 1.105 + // Defaults to false 1.106 + do_check_eq(Service.locked, false); 1.107 + 1.108 + do_check_eq(Service.lock(), true); 1.109 + do_check_eq(Service.locked, true); 1.110 + 1.111 + // Locking again will return false 1.112 + do_check_eq(Service.lock(), false); 1.113 + 1.114 + Service.unlock(); 1.115 + do_check_eq(Service.locked, false); 1.116 +} 1.117 + 1.118 +function run_test() { 1.119 + test_urls(); 1.120 + test_syncID(); 1.121 + test_locked(); 1.122 +}