summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/browser/address/browser_address_doorhanger_invalid_fields.js
blob: 0f0d3026849b1d8156fae9207b5de536eccc9c05 (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
"use strict";

const VALID_ADDRESS = {
  "given-name": "John",
  "street-address": "32 Vassar Street",
  "address-level1": "California",
  "address-level2": "Cambridge",
  country: "US",
};

const INVALID_ADDRESS = {
  "address-level1": "ZZ", // Invalid state
  organization: "???", // Invalid: only contains punctuation
  email: "john.doe@work@mozilla.org", // Invalid email format
  tel: "2-800-555-1234", // Invalid: wrong country code
  "postal-code": "1234", // Invalid: too short
};

async function expectSavedAddresses(expectedCount) {
  const addresses = await getAddresses();
  is(
    addresses.length,
    expectedCount,
    `${addresses.length} address in the storage`
  );
  return addresses;
}

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["extensions.formautofill.addresses.capture.enabled", true],
      ["extensions.formautofill.addresses.supported", "on"],
    ],
  });
});

/**
 * Submit a form with both valid and invalid fields, we should only
 * save address fields that are valid
 */
add_task(async function test_do_not_save_invalid_fields() {
  let addresses = await expectSavedAddresses(0);

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_URL },
    async browser => {
      const onSavePopupShown = waitForPopupShown();
      await focusUpdateSubmitForm(browser, {
        focusSelector: "#given-name",
        newValues: {
          "#given-name": VALID_ADDRESS["given-name"],
          "#family-name": VALID_ADDRESS["family-name"],
          "#street-address": VALID_ADDRESS["street-address"],
          "#address-level1": VALID_ADDRESS["address-level1"],
          "#address-level2": VALID_ADDRESS["address-level2"],

          // Invalid
          "#organization": INVALID_ADDRESS.organization,
          "#email": INVALID_ADDRESS.email,
          "#tel": INVALID_ADDRESS.tel,
          "#postal-code": INVALID_ADDRESS["postal-code"],
        },
      });
      await onSavePopupShown;
      await clickAddressDoorhangerButton(MAIN_BUTTON);
    }
  );

  addresses = await expectSavedAddresses(1);
  for (const [key, value] of Object.entries(VALID_ADDRESS)) {
    Assert.equal(addresses[0][key] ?? "", value, `${key} field is saved`);
  }
  for (const [key, value] of Object.entries(INVALID_ADDRESS)) {
    Assert.notEqual(
      addresses[0][key] ?? "",
      value,
      `${key} field is not saved`
    );
  }
  await removeAllRecords();
});

/**
 * Submit a form with both valid and invalid fields, we should only
 * update address fields that are valid
 */
add_task(async function test_do_not_update_invalid_fields() {
  await setStorage(VALID_ADDRESS);

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_URL },
    async browser => {
      const onUpdatePopupShown = waitForPopupShown();
      await focusUpdateSubmitForm(browser, {
        focusSelector: "#given-name",
        newValues: {
          "#given-name": VALID_ADDRESS["given-name"],
          "#family-name": "Doe", // To trigger an update
          "#street-address": VALID_ADDRESS["street-address"],

          // Invalid
          "#address-level1": INVALID_ADDRESS["address-level1"],
          "#organization": INVALID_ADDRESS.organization,
          "#email": INVALID_ADDRESS.email,
          "#tel": INVALID_ADDRESS.tel,
          "#postal-code": INVALID_ADDRESS["postal-code"],
        },
      });
      await onUpdatePopupShown;

      await clickAddressDoorhangerButton(MAIN_BUTTON);
    }
  );

  const addresses = await expectSavedAddresses(1);

  Assert.equal(
    addresses[0]["family-name"],
    "Doe",
    `family-name field is update`
  );
  for (const [key, value] of Object.entries(VALID_ADDRESS)) {
    Assert.equal(addresses[0][key] ?? "", value, `${key} field is saved`);
  }
  for (const [key, value] of Object.entries(INVALID_ADDRESS)) {
    Assert.notEqual(
      addresses[0][key] ?? "",
      value,
      `${key} field is not saved`
    );
  }
  await removeAllRecords();
});

/**
 * it is possibile that existing records contain invalid fields (Users add those
 * fields via preference page). When updating a record with invalid fields, we
 * should still keep those invalid fields in the existing records
 */
add_task(async function test_do_not_remove_invalid_fields_of_exising_address() {
  const STORED_ADDRESS = {
    ...VALID_ADDRESS,
    ...INVALID_ADDRESS,
  };

  // address-level1 in US cannot be invalid, remove it from the storage
  delete STORED_ADDRESS["address-level1"];
  await setStorage(STORED_ADDRESS);

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_URL },
    async browser => {
      const onUpdatePopupShown = waitForPopupShown();
      await focusUpdateSubmitForm(browser, {
        focusSelector: "#given-name",
        newValues: {
          "#given-name": VALID_ADDRESS["given-name"],
          "#family-name": "Doe", // To trigger an update
          "#street-address": VALID_ADDRESS["street-address"],
          "#address-level2": VALID_ADDRESS["address-level2"],
        },
      });
      await onUpdatePopupShown;
      await clickAddressDoorhangerButton(MAIN_BUTTON);
    }
  );

  const addresses = await expectSavedAddresses(1);
  Assert.equal(
    addresses[0]["family-name"],
    "Doe",
    `family-name field is update`
  );
  for (const [key, value] of Object.entries(STORED_ADDRESS)) {
    Assert.equal(addresses[0][key] ?? "", value, `${key} field is saved`);
  }
  await removeAllRecords();
});

/**
 * Ensure the edit address doorhanger show the invalid fields of the existing record
 */
add_task(async function test_do_not_show_invalid_fields_in_edit_doorhanger() {
  const STORED_ADDRESS = {
    ...INVALID_ADDRESS,
    ...VALID_ADDRESS, // valid address fields will overwrite invalid address fields
  };
  await setStorage(STORED_ADDRESS);

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_URL },
    async browser => {
      const onUpdatePopupShown = waitForPopupShown();
      await focusUpdateSubmitForm(browser, {
        focusSelector: "#given-name",
        newValues: {
          "#given-name": VALID_ADDRESS["given-name"],
          "#family-name": "Doe", // To trigger an update
          "#street-address": VALID_ADDRESS["street-address"],
          "#address-level2": VALID_ADDRESS["address-level2"],
        },
      });
      await onUpdatePopupShown;

      const onEditPopupShown = waitForPopupShown();
      await clickAddressDoorhangerButton(EDIT_ADDRESS_BUTTON);
      await onEditPopupShown;

      await clickAddressDoorhangerButton(MAIN_BUTTON);
    }
  );

  const addresses = await expectSavedAddresses(1);
  Assert.equal(
    addresses[0]["family-name"],
    "Doe",
    `family-name field is update`
  );
  for (const [key, value] of Object.entries(STORED_ADDRESS)) {
    Assert.equal(addresses[0][key] ?? "", value, `${key} field is saved`);
  }
  await removeAllRecords();
});