diff --git a/services/sync/tests/unit/test_service_sync_checkServerError.js b/services/sync/tests/unit/test_service_sync_checkServerError.js index b9fce559dd1a..ddba9abd3029 100644 --- a/services/sync/tests/unit/test_service_sync_checkServerError.js +++ b/services/sync/tests/unit/test_service_sync_checkServerError.js @@ -21,19 +21,28 @@ CatapultEngine.prototype = { }; function sync_httpd_setup() { - let handlers = {}; - handlers["/1.0/johndoe/info/collections"] - = (new ServerWBO("collections", {})).handler(), - handlers["/1.0/johndoe/storage/clients"] - = (new ServerCollection()).handler(); - handlers["/1.0/johndoe/storage/crypto"] - = (new ServerCollection()).handler(); - handlers["/1.0/johndoe/storage/crypto/keys"] - = (new ServerWBO("keys", {})).handler(); - handlers["/1.0/johndoe/storage/crypto/clients"] - = (new ServerWBO("clients", {})).handler(); - handlers["/1.0/johndoe/storage/meta/global"] - = (new ServerWBO("global", {})).handler(); + let collectionsHelper = track_collections_helper(); + let upd = collectionsHelper.with_updated_collection; + let collections = collectionsHelper.collections; + + let catapultEngine = Engines.get("catapult"); + let engines = {catapult: {version: catapultEngine.version, + syncID: catapultEngine.syncID}} + + // Track these using the collections helper, which keeps modified times + // up-to-date. + let clientsColl = new ServerCollection({}, true); + let keysWBO = new ServerWBO("keys"); + let globalWBO = new ServerWBO("global", {storageVersion: STORAGE_VERSION, + syncID: Utils.makeGUID(), + engines: engines}); + + let handlers = { + "/1.0/johndoe/info/collections": collectionsHelper.handler, + "/1.0/johndoe/storage/meta/global": upd("meta", globalWBO.handler()), + "/1.0/johndoe/storage/clients": upd("clients", clientsColl.handler()), + "/1.0/johndoe/storage/crypto/keys": upd("crypto", keysWBO.handler()) + } return httpd_setup(handlers); } @@ -45,28 +54,32 @@ function setUp() { new FakeCryptoService(); } +function generateAndUploadKeys() { + CollectionKeys.generateNewKeys(); + let serverKeys = CollectionKeys.asWBO("crypto", "keys"); + serverKeys.encrypt(Weave.Service.syncKeyBundle); + return serverKeys.upload("http://localhost:8080/1.0/johndoe/storage/crypto/keys").success; +} + function test_backoff500(next) { _("Test: HTTP 500 sets backoff status."); let server = sync_httpd_setup(); setUp(); - Engines.register(CatapultEngine); let engine = Engines.get("catapult"); engine.enabled = true; engine.exception = {status: 500}; try { do_check_false(Status.enforceBackoff); - + // Forcibly create and upload keys here -- otherwise we don't get to the 500! - CollectionKeys.generateNewKeys(); - do_check_true(CollectionKeys.asWBO().upload("http://localhost:8080/1.0/johndoe/storage/crypto/keys").success); - + do_check_true(generateAndUploadKeys()); + Service.login(); Service.sync(); do_check_true(Status.enforceBackoff); } finally { - Engines.unregister("catapult"); Status.resetBackoff(); Service.startOver(); } @@ -79,7 +92,6 @@ function test_backoff503(next) { setUp(); const BACKOFF = 42; - Engines.register(CatapultEngine); let engine = Engines.get("catapult"); engine.enabled = true; engine.exception = {status: 503, @@ -93,13 +105,14 @@ function test_backoff503(next) { try { do_check_false(Status.enforceBackoff); + do_check_true(generateAndUploadKeys()); + Service.login(); Service.sync(); do_check_true(Status.enforceBackoff); do_check_eq(backoffInterval, BACKOFF); } finally { - Engines.unregister("catapult"); Status.resetBackoff(); Service.startOver(); } @@ -111,7 +124,6 @@ function test_overQuota(next) { let server = sync_httpd_setup(); setUp(); - Engines.register(CatapultEngine); let engine = Engines.get("catapult"); engine.enabled = true; engine.exception = {status: 400, @@ -120,12 +132,13 @@ function test_overQuota(next) { try { do_check_eq(Status.sync, SYNC_SUCCEEDED); + do_check_true(generateAndUploadKeys()); + Service.login(); Service.sync(); do_check_eq(Status.sync, OVER_QUOTA); } finally { - Engines.unregister("catapult"); Status.resetSync(); Service.startOver(); } @@ -182,9 +195,15 @@ function test_service_reset_ignorableErrorCount(next) { setUp(); Service._ignorableErrorCount = 10; + // Disable the engine so that sync completes. + let engine = Engines.get("catapult"); + engine.enabled = false; + try { do_check_eq(Status.sync, SYNC_SUCCEEDED); + do_check_true(generateAndUploadKeys()); + Service.login(); Service.sync(); @@ -203,7 +222,6 @@ function test_engine_networkError(next) { setUp(); Service._ignorableErrorCount = 0; - Engines.register(CatapultEngine); let engine = Engines.get("catapult"); engine.enabled = true; engine.exception = Components.Exception("NS_ERROR_UNKNOWN_HOST", @@ -212,13 +230,14 @@ function test_engine_networkError(next) { try { do_check_eq(Status.sync, SYNC_SUCCEEDED); + do_check_true(generateAndUploadKeys()); + Service.login(); Service.sync(); do_check_eq(Status.sync, LOGIN_FAILED_NETWORK_ERROR); do_check_eq(Service._ignorableErrorCount, 1); } finally { - Engines.unregister("catapult"); Status.resetSync(); Service.startOver(); } @@ -231,9 +250,9 @@ function test_engine_applyFailed(next) { let server = sync_httpd_setup(); setUp(); - Engines.register(CatapultEngine); let engine = Engines.get("catapult"); engine.enabled = true; + delete engine.exception; engine.sync = function sync() { Svc.Obs.notify("weave:engine:sync:apply-failed", {}, "steam"); }; @@ -241,12 +260,13 @@ function test_engine_applyFailed(next) { try { do_check_eq(Status.engines["steam"], undefined); + do_check_true(generateAndUploadKeys()); + Service.login(); Service.sync(); do_check_eq(Status.engines["steam"], ENGINE_APPLY_FAIL); } finally { - Engines.unregister("catapult"); Status.resetSync(); Service.startOver(); } @@ -258,6 +278,9 @@ function run_test() { return; do_test_pending(); + + // Register engine once. + Engines.register(CatapultEngine); asyncChainTests(test_backoff500, test_backoff503, test_overQuota,