--- a/browser/extensions/formautofill/test/unit/test_sync.js
+++ b/browser/extensions/formautofill/test/unit/test_sync.js
@@ -71,18 +71,19 @@ async function setup() {
// should always start with no profiles.
Assert.equal(profileStorage.addresses.getAll({includeDeleted: true}).length, 0);
Services.prefs.setCharPref("services.sync.log.logger.engine.addresses", "Trace");
let engine = new AddressesEngine(Service);
await engine.initialize();
// Avoid accidental automatic sync due to our own changes
Service.scheduler.syncThreshold = 10000000;
+ let syncID = await engine.resetLocalSyncID();
let server = serverForUsers({"foo": "password"}, {
- meta: {global: {engines: {addresses: {version: engine.version, syncID: engine.syncID}}}},
+ meta: {global: {engines: {addresses: {version: engine.version, syncID}}}},
addresses: {},
});
Service.engineManager._engines.addresses = engine;
engine.enabled = true;
engine._store._storage = profileStorage.addresses;
generateNewKeys(Service.collectionKeys);
@@ -141,17 +142,17 @@ add_task(async function test_outgoing()
deleted: true,
},
]);
await engine._tracker.asyncObserver.promiseObserversComplete();
// The tracker should have a score recorded for the 2 additions we had.
equal(engine._tracker.score, SCORE_INCREMENT_XLARGE * 2);
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
Assert.equal(collection.count(), 2);
Assert.ok(collection.wbo(existingGUID));
Assert.ok(collection.wbo(deletedGUID));
expectLocalProfiles(profileStorage, [
{
@@ -185,17 +186,17 @@ add_task(async function test_incoming_ne
server.insertWBO("foo", "addresses", new ServerWBO(deletedID, encryptPayload({
id: deletedID,
deleted: true,
}), Date.now() / 1000));
// The tracker should start with no score.
equal(engine._tracker.score, 0);
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
expectLocalProfiles(profileStorage, [
{
guid: profileID,
}, {
guid: deletedID,
deleted: true,
@@ -215,33 +216,34 @@ add_task(async function test_incoming_ne
add_task(async function test_incoming_existing() {
let {profileStorage, server, engine} = await setup();
try {
let guid1 = profileStorage.addresses.add(TEST_PROFILE_1);
let guid2 = profileStorage.addresses.add(TEST_PROFILE_2);
// an initial sync so we don't think they are locally modified.
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
// now server records that modify the existing items.
let modifiedEntry1 = Object.assign({}, TEST_PROFILE_1, {
"version": 1,
"given-name": "NewName",
});
+ let lastSync = await engine.getLastSync();
server.insertWBO("foo", "addresses", new ServerWBO(guid1, encryptPayload({
id: guid1,
entry: modifiedEntry1,
- }), engine.lastSync + 10));
+ }), lastSync + 10));
server.insertWBO("foo", "addresses", new ServerWBO(guid2, encryptPayload({
id: guid2,
deleted: true,
- }), engine.lastSync + 10));
+ }), lastSync + 10));
await engine.sync();
expectLocalProfiles(profileStorage, [
Object.assign({}, modifiedEntry1, {guid: guid1}),
{guid: guid2, deleted: true},
]);
} finally {
@@ -249,17 +251,17 @@ add_task(async function test_incoming_ex
}
});
add_task(async function test_tombstones() {
let {profileStorage, server, collection, engine} = await setup();
try {
let existingGUID = profileStorage.addresses.add(TEST_PROFILE_1);
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
Assert.equal(collection.count(), 1);
let payload = collection.payloads()[0];
equal(payload.id, existingGUID);
equal(payload.deleted, undefined);
profileStorage.addresses.remove(existingGUID);
@@ -275,28 +277,29 @@ add_task(async function test_tombstones(
}
});
add_task(async function test_applyIncoming_both_deleted() {
let {profileStorage, server, engine} = await setup();
try {
let guid = profileStorage.addresses.add(TEST_PROFILE_1);
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
// Delete synced record locally.
profileStorage.addresses.remove(guid);
// Delete same record remotely.
+ let lastSync = await engine.getLastSync();
let collection = server.user("foo").collection("addresses");
collection.insert(guid, encryptPayload({
id: guid,
deleted: true,
- }), engine.lastSync + 10);
+ }), lastSync + 10);
await engine.sync();
ok(!profileStorage.addresses.get(guid),
"Should not return record for locally deleted item");
let localRecords = profileStorage.addresses.getAll({
includeDeleted: true,
@@ -315,17 +318,17 @@ add_task(async function test_applyIncomi
try {
let guid = profileStorage.addresses._generateGUID();
let collection = server.user("foo").collection("addresses");
collection.insert(guid, encryptPayload({
id: guid,
deleted: true,
}), Date.now() / 1000);
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
ok(!profileStorage.addresses.get(guid),
"Should not return record for uknown deleted item");
let localTombstone = profileStorage.addresses.getAll({
includeDeleted: true,
}).find(record => record.guid == guid);
ok(localTombstone, "Should store tombstone for unknown item");
@@ -334,25 +337,26 @@ add_task(async function test_applyIncomi
}
});
add_task(async function test_applyIncoming_incoming_deleted() {
let {profileStorage, server, engine} = await setup();
try {
let guid = profileStorage.addresses.add(TEST_PROFILE_1);
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
// Delete the record remotely.
+ let lastSync = await engine.getLastSync();
let collection = server.user("foo").collection("addresses");
collection.insert(guid, encryptPayload({
id: guid,
deleted: true,
- }), engine.lastSync + 10);
+ }), lastSync + 10);
await engine.sync();
ok(!profileStorage.addresses.get(guid), "Should delete unmodified item locally");
let localTombstone = profileStorage.addresses.getAll({
includeDeleted: true,
}).find(record => record.guid == guid);
@@ -365,27 +369,28 @@ add_task(async function test_applyIncomi
});
add_task(async function test_applyIncoming_incoming_restored() {
let {profileStorage, server, engine} = await setup();
try {
let guid = profileStorage.addresses.add(TEST_PROFILE_1);
// Upload the record to the server.
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
// Removing a synced record should write a tombstone.
profileStorage.addresses.remove(guid);
// Modify the deleted record remotely.
let collection = server.user("foo").collection("addresses");
let serverPayload = JSON.parse(JSON.parse(collection.payload(guid)).ciphertext);
serverPayload.entry["street-address"] = "I moved!";
- collection.insert(guid, encryptPayload(serverPayload), engine.lastSync + 10);
+ let lastSync = await engine.getLastSync();
+ collection.insert(guid, encryptPayload(serverPayload), lastSync + 10);
// Sync again.
await engine.sync();
// We should replace our tombstone with the server's version.
let localRecord = profileStorage.addresses.get(guid);
ok(objectMatches(localRecord, {
"given-name": "Timothy",
@@ -401,30 +406,31 @@ add_task(async function test_applyIncomi
});
add_task(async function test_applyIncoming_outgoing_restored() {
let {profileStorage, server, engine} = await setup();
try {
let guid = profileStorage.addresses.add(TEST_PROFILE_1);
// Upload the record to the server.
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
// Modify the local record.
let localCopy = Object.assign({}, TEST_PROFILE_1);
localCopy["street-address"] = "I moved!";
profileStorage.addresses.update(guid, localCopy);
// Replace the record with a tombstone on the server.
+ let lastSync = await engine.getLastSync();
let collection = server.user("foo").collection("addresses");
collection.insert(guid, encryptPayload({
id: guid,
deleted: true,
- }), engine.lastSync + 10);
+ }), lastSync + 10);
// Sync again.
await engine.sync();
// We should resurrect the record on the server.
let serverPayload = JSON.parse(JSON.parse(collection.payload(guid)).ciphertext);
ok(!serverPayload.deleted, "Should resurrect record on server");
ok(objectMatches(serverPayload.entry, {
@@ -449,54 +455,55 @@ add_task(async function test_reconcile_b
let guid = profileStorage.addresses.add(TEST_PROFILE_1);
// and an identical record on the server.
server.insertWBO("foo", "addresses", new ServerWBO(guid, encryptPayload({
id: guid,
entry: TEST_PROFILE_1,
}), Date.now() / 1000));
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
expectLocalProfiles(profileStorage, [{guid}]);
} finally {
await cleanup(server);
}
});
add_task(async function test_incoming_dupes() {
let {profileStorage, server, engine} = await setup();
try {
// Create a profile locally, then sync to upload the new profile to the
// server.
let guid1 = profileStorage.addresses.add(TEST_PROFILE_1);
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
// Create another profile locally, but don't sync it yet.
profileStorage.addresses.add(TEST_PROFILE_2);
// Now create two records on the server with the same contents as our local
// profiles, but different GUIDs.
+ let lastSync = await engine.getLastSync();
let guid1_dupe = Utils.makeGUID();
server.insertWBO("foo", "addresses", new ServerWBO(guid1_dupe, encryptPayload({
id: guid1_dupe,
entry: Object.assign({
version: 1,
}, TEST_PROFILE_1),
- }), engine.lastSync + 10));
+ }), lastSync + 10));
let guid2_dupe = Utils.makeGUID();
server.insertWBO("foo", "addresses", new ServerWBO(guid2_dupe, encryptPayload({
id: guid2_dupe,
entry: Object.assign({
version: 1,
}, TEST_PROFILE_2),
- }), engine.lastSync + 10));
+ }), lastSync + 10));
// Sync again. We should download `guid1_dupe` and `guid2_dupe`, then
// reconcile changes.
await engine.sync();
expectLocalProfiles(profileStorage, [
// We uploaded `guid1` during the first sync. Even though its contents
// are the same as `guid1_dupe`, we keep both.
@@ -522,17 +529,17 @@ add_task(async function test_dedupe_iden
notEqual(localGuid, remoteGuid);
server.insertWBO("foo", "addresses", new ServerWBO(remoteGuid, encryptPayload({
id: remoteGuid,
entry: Object.assign({
version: 1,
}, TEST_PROFILE_1),
}), Date.now() / 1000));
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
// Should have 1 item locally with GUID changed to the remote one.
// There's no tombstone as the original was unsynced.
expectLocalProfiles(profileStorage, [
{
guid: remoteGuid,
},
@@ -544,27 +551,28 @@ add_task(async function test_dedupe_iden
add_task(async function test_dedupe_identical_synced() {
let {profileStorage, server, engine} = await setup();
try {
// create a record locally.
let localGuid = profileStorage.addresses.add(TEST_PROFILE_1);
// sync it - it will no longer be a candidate for de-duping.
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
// and an identical record on the server but different GUID.
+ let lastSync = await engine.getLastSync();
let remoteGuid = Utils.makeGUID();
server.insertWBO("foo", "addresses", new ServerWBO(remoteGuid, encryptPayload({
id: remoteGuid,
entry: Object.assign({
version: 1,
}, TEST_PROFILE_1),
- }), engine.lastSync + 10));
+ }), lastSync + 10));
await engine.sync();
// Should have 2 items locally, since the first was synced.
expectLocalProfiles(profileStorage, [
{guid: localGuid},
{guid: remoteGuid},
]);
@@ -603,17 +611,17 @@ add_task(async function test_dedupe_mult
id: bGuid,
entry: serverRecord,
}), Date.now() / 1000));
server.insertWBO("foo", "addresses", new ServerWBO(aGuid, encryptPayload({
id: aGuid,
entry: serverRecord,
}), Date.now() / 1000));
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
expectLocalProfiles(profileStorage, [
{
"guid": aGuid,
"given-name": "Mark",
"family-name": "Hammond",
"organization": "Mozilla",
@@ -643,31 +651,32 @@ add_task(async function test_dedupe_mult
// and if materially different, create a duplicate.
add_task(async function test_reconcile_both_modified_conflict() {
let {profileStorage, server, engine} = await setup();
try {
// create a record locally.
let guid = profileStorage.addresses.add(TEST_PROFILE_1);
// Upload the record to the server.
- engine.lastSync = 0;
+ await engine.setLastSync(0);
await engine.sync();
strictEqual(getSyncChangeCounter(profileStorage.addresses, guid), 0,
"Original record should be marked as syncing");
// Change the same field locally and on the server.
let localCopy = Object.assign({}, TEST_PROFILE_1);
localCopy["street-address"] = "I moved!";
profileStorage.addresses.update(guid, localCopy);
+ let lastSync = await engine.getLastSync();
let collection = server.user("foo").collection("addresses");
let serverPayload = JSON.parse(JSON.parse(collection.payload(guid)).ciphertext);
serverPayload.entry["street-address"] = "I moved, too!";
- collection.insert(guid, encryptPayload(serverPayload), engine.lastSync + 10);
+ collection.insert(guid, encryptPayload(serverPayload), lastSync + 10);
// Sync again.
await engine.sync();
// Since we wait to pull changes until we're ready to upload, both records
// should now exist on the server; we don't need a follow-up sync.
let serverPayloads = collection.payloads();
equal(serverPayloads.length, 2, "Both records should exist on server");