summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/unit/test_sync_deprecate_credit_card_v4.js
blob: 2362796b0ca7355d15dc05526f5e3d49fa930b15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
 * Tests sync functionality.
 */

/* import-globals-from ../../../../../services/sync/tests/unit/head_appinfo.js */
/* import-globals-from ../../../../../services/common/tests/unit/head_helpers.js */
/* import-globals-from ../../../../../services/sync/tests/unit/head_helpers.js */
/* import-globals-from ../../../../../services/sync/tests/unit/head_http_server.js */

"use strict";

const { Service } = ChromeUtils.importESModule(
  "resource://services-sync/service.sys.mjs"
);

const { CreditCardsEngine } = ChromeUtils.importESModule(
  "resource://autofill/FormAutofillSync.sys.mjs"
);

Services.prefs.setCharPref("extensions.formautofill.loglevel", "Trace");
initTestLogging("Trace");

const TEST_STORE_FILE_NAME = "test-profile.json";

const TEST_CREDIT_CARD_1 = {
  guid: "86d961c7717a",
  "cc-name": "John Doe",
  "cc-number": "4111111111111111",
  "cc-exp-month": 4,
  "cc-exp-year": new Date().getFullYear(),
};

const TEST_CREDIT_CARD_2 = {
  guid: "cf57a7ac3539",
  "cc-name": "Timothy Berners-Lee",
  "cc-number": "4929001587121045",
  "cc-exp-month": 12,
  "cc-exp-year": new Date().getFullYear() + 10,
};

function expectProfiles(profiles, expected) {
  expected.sort((a, b) => a.guid.localeCompare(b.guid));
  profiles.sort((a, b) => a.guid.localeCompare(b.guid));
  try {
    deepEqual(
      profiles.map(p => p.guid),
      expected.map(p => p.guid)
    );
    for (let i = 0; i < expected.length; i++) {
      let thisExpected = expected[i];
      let thisGot = profiles[i];
      // always check "deleted".
      equal(thisExpected.deleted, thisGot.deleted);
      ok(objectMatches(thisGot, thisExpected));
    }
  } catch (ex) {
    info("Comparing expected profiles:");
    info(JSON.stringify(expected, undefined, 2));
    info("against actual profiles:");
    info(JSON.stringify(profiles, undefined, 2));
    throw ex;
  }
}

async function expectServerProfiles(collection, expected) {
  const profiles = collection
    .payloads()
    .map(payload => Object.assign({ guid: payload.id }, payload.entry));
  expectProfiles(profiles, expected);
}

async function expectLocalProfiles(profileStorage, expected) {
  const profiles = await profileStorage.creditCards.getAll({
    rawData: true,
    includeDeleted: true,
  });
  expectProfiles(profiles, expected);
}

async function setup() {
  let profileStorage = await initProfileStorage(TEST_STORE_FILE_NAME);
  // should always start with no profiles.
  Assert.equal(
    (await profileStorage.creditCards.getAll({ includeDeleted: true })).length,
    0
  );

  Services.prefs.setCharPref(
    "services.sync.log.logger.engine.CreditCards",
    "Trace"
  );
  let engine = new CreditCardsEngine(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: { creditcards: { version: engine.version, syncID } },
        },
      },
      creditcards: {},
    }
  );

  Service.engineManager._engines.creditcards = engine;
  engine.enabled = true;
  engine._store._storage = profileStorage.creditCards;

  generateNewKeys(Service.collectionKeys);

  await SyncTestingInfrastructure(server);

  let collection = server.user("foo").collection("creditcards");

  return { profileStorage, server, collection, engine };
}

async function cleanup(server) {
  let promiseStartOver = promiseOneObserver("weave:service:start-over:finish");
  await Service.startOver();
  await promiseStartOver;
  await promiseStopServer(server);
}

function getTestRecords(profileStorage, version) {
  return [
    Object.assign({ version }, TEST_CREDIT_CARD_1),
    Object.assign({ version }, TEST_CREDIT_CARD_2),
  ];
}

function setupServerRecords(server, records) {
  for (const record of records) {
    server.insertWBO(
      "foo",
      "creditcards",
      new ServerWBO(
        record.guid,
        encryptPayload({
          id: record.guid,
          entry: Object.assign({}, record),
        }),
        getDateForSync()
      )
    );
  }
}

/**
 * We want to setup old records and run init() to migrate records.
 * However, We don't have an easy way to setup an older version record with
 * init() function now.
 * So as a workaround, we simulate the behavior by directly setting data and then
 * run migration.
 */
async function setupLocalProfilesAndRunMigration(profileStorage, records) {
  for (const record of records) {
    profileStorage._store.data.creditCards.push(Object.assign({}, record));
  }
  await Promise.all(
    profileStorage.creditCards._data.map(async (record, index) =>
      profileStorage.creditCards._migrateRecord(record, index)
    )
  );
}

// local v3, server v4
add_task(async function test_local_v3_server_v4() {
  let { collection, profileStorage, server, engine } = await setup();

  const V3_RECORDS = getTestRecords(profileStorage, 3);
  const V4_RECORDS = getTestRecords(profileStorage, 4);

  await setupLocalProfilesAndRunMigration(profileStorage, V3_RECORDS);
  setupServerRecords(server, V4_RECORDS);

  await engine.setLastSync(0);
  await engine.sync();

  await expectServerProfiles(collection, V3_RECORDS);
  await expectLocalProfiles(profileStorage, V3_RECORDS);

  await cleanup(server);
});

// local v4, server empty
add_task(async function test_local_v4_server_empty() {
  let { collection, profileStorage, server, engine } = await setup();
  const V3_RECORDS = getTestRecords(profileStorage, 3);
  const V4_RECORDS = getTestRecords(profileStorage, 4);

  await setupLocalProfilesAndRunMigration(profileStorage, V4_RECORDS);

  await engine.setLastSync(0);
  await engine.sync();

  await expectServerProfiles(collection, V3_RECORDS);
  await expectLocalProfiles(profileStorage, V3_RECORDS);

  await cleanup(server);
});

// local v4, server v3
add_task(async function test_local_v4_server_v3() {
  let { collection, profileStorage, server, engine } = await setup();
  const V3_RECORDS = getTestRecords(profileStorage, 3);
  const V4_RECORDS = getTestRecords(profileStorage, 4);

  await setupLocalProfilesAndRunMigration(profileStorage, V4_RECORDS);
  setupServerRecords(server, V3_RECORDS);

  // local should be v3 before syncing.
  await expectLocalProfiles(profileStorage, V3_RECORDS);

  await engine.setLastSync(0);
  await engine.sync();

  await expectServerProfiles(collection, V3_RECORDS);
  await expectLocalProfiles(profileStorage, V3_RECORDS);

  await cleanup(server);
});

// local v4, server v4
add_task(async function test_local_v4_server_v4() {
  let { collection, profileStorage, server, engine } = await setup();
  const V3_RECORDS = getTestRecords(profileStorage, 3);
  const V4_RECORDS = getTestRecords(profileStorage, 4);

  await setupLocalProfilesAndRunMigration(profileStorage, V4_RECORDS);
  setupServerRecords(server, V4_RECORDS);

  // local should be v3 before syncing and then we ignore
  // incoming v4 from server
  await expectLocalProfiles(profileStorage, V3_RECORDS);

  await engine.setLastSync(0);
  await engine.sync();

  await expectServerProfiles(collection, V3_RECORDS);
  await expectLocalProfiles(profileStorage, V3_RECORDS);

  await cleanup(server);
});