services/sync/tests/unit/test_score_triggers.js

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 Cu.import("resource://services-sync/engines.js");
michael@0 5 Cu.import("resource://services-sync/engines/clients.js");
michael@0 6 Cu.import("resource://services-sync/constants.js");
michael@0 7 Cu.import("resource://services-sync/service.js");
michael@0 8 Cu.import("resource://services-sync/status.js");
michael@0 9 Cu.import("resource://services-sync/util.js");
michael@0 10 Cu.import("resource://testing-common/services/sync/rotaryengine.js");
michael@0 11 Cu.import("resource://testing-common/services/sync/utils.js");
michael@0 12
michael@0 13 Service.engineManager.clear();
michael@0 14 Service.engineManager.register(RotaryEngine);
michael@0 15 let engine = Service.engineManager.get("rotary");
michael@0 16 let tracker = engine._tracker;
michael@0 17 engine.enabled = true;
michael@0 18
michael@0 19 // Tracking info/collections.
michael@0 20 let collectionsHelper = track_collections_helper();
michael@0 21 let upd = collectionsHelper.with_updated_collection;
michael@0 22
michael@0 23 function sync_httpd_setup() {
michael@0 24 let handlers = {};
michael@0 25
michael@0 26 handlers["/1.1/johndoe/storage/meta/global"] =
michael@0 27 new ServerWBO("global", {}).handler();
michael@0 28 handlers["/1.1/johndoe/storage/steam"] =
michael@0 29 new ServerWBO("steam", {}).handler();
michael@0 30
michael@0 31 handlers["/1.1/johndoe/info/collections"] = collectionsHelper.handler;
michael@0 32 delete collectionsHelper.collections.crypto;
michael@0 33 delete collectionsHelper.collections.meta;
michael@0 34
michael@0 35 let cr = new ServerWBO("keys");
michael@0 36 handlers["/1.1/johndoe/storage/crypto/keys"] =
michael@0 37 upd("crypto", cr.handler());
michael@0 38
michael@0 39 let cl = new ServerCollection();
michael@0 40 handlers["/1.1/johndoe/storage/clients"] =
michael@0 41 upd("clients", cl.handler());
michael@0 42
michael@0 43 return httpd_setup(handlers);
michael@0 44 }
michael@0 45
michael@0 46 function setUp(server) {
michael@0 47 new SyncTestingInfrastructure(server, "johndoe", "ilovejane", "sekrit");
michael@0 48 }
michael@0 49
michael@0 50 function run_test() {
michael@0 51 initTestLogging("Trace");
michael@0 52
michael@0 53 Log.repository.getLogger("Sync.Service").level = Log.Level.Trace;
michael@0 54
michael@0 55 run_next_test();
michael@0 56 }
michael@0 57
michael@0 58 add_test(function test_tracker_score_updated() {
michael@0 59 let scoreUpdated = 0;
michael@0 60
michael@0 61 function onScoreUpdated() {
michael@0 62 scoreUpdated++;
michael@0 63 }
michael@0 64
michael@0 65 Svc.Obs.add("weave:engine:score:updated", onScoreUpdated());
michael@0 66
michael@0 67 try {
michael@0 68 do_check_eq(engine.score, 0);
michael@0 69
michael@0 70 tracker.score += SCORE_INCREMENT_SMALL;
michael@0 71 do_check_eq(engine.score, SCORE_INCREMENT_SMALL);
michael@0 72
michael@0 73 do_check_eq(scoreUpdated, 1);
michael@0 74 } finally {
michael@0 75 Svc.Obs.remove("weave:engine:score:updated", onScoreUpdated);
michael@0 76 tracker.resetScore();
michael@0 77 run_next_test();
michael@0 78 }
michael@0 79 });
michael@0 80
michael@0 81 add_test(function test_sync_triggered() {
michael@0 82 let server = sync_httpd_setup();
michael@0 83 setUp(server);
michael@0 84
michael@0 85 Service.login();
michael@0 86
michael@0 87 Service.scheduler.syncThreshold = MULTI_DEVICE_THRESHOLD;
michael@0 88 Svc.Obs.add("weave:service:sync:finish", function onSyncFinish() {
michael@0 89 Svc.Obs.remove("weave:service:sync:finish", onSyncFinish);
michael@0 90 _("Sync completed!");
michael@0 91 server.stop(run_next_test);
michael@0 92 });
michael@0 93
michael@0 94 do_check_eq(Status.login, LOGIN_SUCCEEDED);
michael@0 95 tracker.score += SCORE_INCREMENT_XLARGE;
michael@0 96 });
michael@0 97
michael@0 98 add_test(function test_clients_engine_sync_triggered() {
michael@0 99 _("Ensure that client engine score changes trigger a sync.");
michael@0 100
michael@0 101 // The clients engine is not registered like other engines. Therefore,
michael@0 102 // it needs special treatment throughout the code. Here, we verify the
michael@0 103 // global score tracker gives it that treatment. See bug 676042 for more.
michael@0 104
michael@0 105 let server = sync_httpd_setup();
michael@0 106 setUp(server);
michael@0 107 Service.login();
michael@0 108
michael@0 109 const TOPIC = "weave:service:sync:finish";
michael@0 110 Svc.Obs.add(TOPIC, function onSyncFinish() {
michael@0 111 Svc.Obs.remove(TOPIC, onSyncFinish);
michael@0 112 _("Sync due to clients engine change completed.");
michael@0 113 server.stop(run_next_test);
michael@0 114 });
michael@0 115
michael@0 116 Service.scheduler.syncThreshold = MULTI_DEVICE_THRESHOLD;
michael@0 117 do_check_eq(Status.login, LOGIN_SUCCEEDED);
michael@0 118 Service.clientsEngine._tracker.score += SCORE_INCREMENT_XLARGE;
michael@0 119 });
michael@0 120
michael@0 121 add_test(function test_incorrect_credentials_sync_not_triggered() {
michael@0 122 _("Ensure that score changes don't trigger a sync if Status.login != LOGIN_SUCCEEDED.");
michael@0 123 let server = sync_httpd_setup();
michael@0 124 setUp(server);
michael@0 125
michael@0 126 // Ensure we don't actually try to sync.
michael@0 127 function onSyncStart() {
michael@0 128 do_throw("Should not get here!");
michael@0 129 }
michael@0 130 Svc.Obs.add("weave:service:sync:start", onSyncStart);
michael@0 131
michael@0 132 // First wait >100ms (nsITimers can take up to that much time to fire, so
michael@0 133 // we can account for the timer in delayedAutoconnect) and then one event
michael@0 134 // loop tick (to account for a possible call to weave:service:sync:start).
michael@0 135 Utils.namedTimer(function() {
michael@0 136 Utils.nextTick(function() {
michael@0 137 Svc.Obs.remove("weave:service:sync:start", onSyncStart);
michael@0 138
michael@0 139 do_check_eq(Status.login, LOGIN_FAILED_LOGIN_REJECTED);
michael@0 140
michael@0 141 Service.startOver();
michael@0 142 server.stop(run_next_test);
michael@0 143 });
michael@0 144 }, 150, {}, "timer");
michael@0 145
michael@0 146 // Faking incorrect credentials to prevent score update.
michael@0 147 Status.login = LOGIN_FAILED_LOGIN_REJECTED;
michael@0 148 tracker.score += SCORE_INCREMENT_XLARGE;
michael@0 149 });

mercurial