1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/sync/tests/unit/test_fxa_service_cluster.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,68 @@ 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/service.js"); 1.8 +Cu.import("resource://services-sync/util.js"); 1.9 +Cu.import("resource://testing-common/services/sync/fxa_utils.js"); 1.10 +Cu.import("resource://testing-common/services/sync/utils.js"); 1.11 + 1.12 +add_task(function test_findCluster() { 1.13 + _("Test FxA _findCluster()"); 1.14 + 1.15 + _("_findCluster() throws on 500 errors."); 1.16 + initializeIdentityWithTokenServerResponse({ 1.17 + status: 500, 1.18 + headers: [], 1.19 + body: "", 1.20 + }); 1.21 + 1.22 + yield Service.identity.initializeWithCurrentIdentity(); 1.23 + yield Assert_rejects(Service.identity.whenReadyToAuthenticate.promise, 1.24 + "should reject due to 500"); 1.25 + 1.26 + Assert.throws(function() { 1.27 + Service._clusterManager._findCluster(); 1.28 + }); 1.29 + 1.30 + _("_findCluster() returns null on authentication errors."); 1.31 + initializeIdentityWithTokenServerResponse({ 1.32 + status: 401, 1.33 + headers: {"content-type": "application/json"}, 1.34 + body: "{}", 1.35 + }); 1.36 + 1.37 + yield Service.identity.initializeWithCurrentIdentity(); 1.38 + yield Assert_rejects(Service.identity.whenReadyToAuthenticate.promise, 1.39 + "should reject due to 401"); 1.40 + 1.41 + cluster = Service._clusterManager._findCluster(); 1.42 + Assert.strictEqual(cluster, null); 1.43 + 1.44 + _("_findCluster() works with correct tokenserver response."); 1.45 + let endpoint = "http://example.com/something"; 1.46 + initializeIdentityWithTokenServerResponse({ 1.47 + status: 200, 1.48 + headers: {"content-type": "application/json"}, 1.49 + body: 1.50 + JSON.stringify({ 1.51 + api_endpoint: endpoint, 1.52 + duration: 300, 1.53 + id: "id", 1.54 + key: "key", 1.55 + uid: "uid", 1.56 + }) 1.57 + }); 1.58 + 1.59 + yield Service.identity.initializeWithCurrentIdentity(); 1.60 + yield Service.identity.whenReadyToAuthenticate.promise; 1.61 + cluster = Service._clusterManager._findCluster(); 1.62 + // The cluster manager ensures a trailing "/" 1.63 + Assert.strictEqual(cluster, endpoint + "/"); 1.64 + 1.65 + Svc.Prefs.resetBranch(""); 1.66 +}); 1.67 + 1.68 +function run_test() { 1.69 + initTestLogging(); 1.70 + run_next_test(); 1.71 +}