summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/browser/creditCard/browser_creditCard_capture_multiple_cc_number.js
blob: 5668e88e747bc549f536b574d0bbfe4b0e88f4ef (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
"use strict";

const TESTCASES = [
  {
    description: "Trigger credit card saving using multiple cc-number fields",
    document: `<form id="form">
      <input id="cc-name" autocomplete="cc-name">
      <input id="cc-number1" autocomplete="cc-number" maxlength="4">
      <input id="cc-number2" autocomplete="cc-number" maxlength="4">
      <input id="cc-number3" autocomplete="cc-number" maxlength="4">
      <input id="cc-number4" autocomplete="cc-number" maxlength="4">
      <input id="cc-exp-month" autocomplete="cc-exp-month">
      <input id="cc-exp-year" autocomplete="cc-exp-year">
      <input id="submit" type="submit">
    </form>`,
    targetElementId: "cc-number1",
    formValue: {
      "#cc-name": "John Doe",
      "#cc-number1": "3714",
      "#cc-number2": "4963",
      "#cc-number3": "5398",
      "#cc-number4": "431",
      "#cc-exp-month": 12,
      "#cc-exp-year": 2000,
    },
    expected: [
      {
        "cc-name": "John Doe",
        "cc-number": "371449635398431",
        "cc-exp-month": 12,
        "cc-exp-year": 2000,
        "cc-type": "amex",
      },
    ],
  },
];

async function expectSavedCreditCards(expectedCreditCards) {
  const creditcards = await getCreditCards();
  is(
    creditcards.length,
    expectedCreditCards.length,
    `${creditcards.length} credit card in the storage`
  );

  for (let i = 0; i < expectedCreditCards.length; i++) {
    for (const [key, value] of Object.entries(expectedCreditCards[i])) {
      if (key == "cc-number") {
        creditcards[i]["cc-number"] = await OSKeyStore.decrypt(
          creditcards[i]["cc-number-encrypted"]
        );
      }
      is(creditcards[i][key] ?? "", value, `field ${key} should be equal`);
    }
  }
  return creditcards;
}

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

add_task(async function test_capture_multiple_cc_number() {
  if (!OSKeyStoreTestUtils.canTestOSKeyStoreLogin()) {
    todo(
      OSKeyStoreTestUtils.canTestOSKeyStoreLogin(),
      "Cannot test OS key store login on official builds."
    );
    return;
  }

  for (const TEST of TESTCASES) {
    info(`Test ${TEST.description}`);

    let onChanged = waitForStorageChangedEvents("add");
    await BrowserTestUtils.withNewTab(EMPTY_URL, async function (browser) {
      await SpecialPowers.spawn(browser, [TEST.document], doc => {
        content.document.body.innerHTML = doc;
      });

      await SimpleTest.promiseFocus(browser);

      const onPopupShown = waitForPopupShown();
      await focusUpdateSubmitForm(browser, {
        focusSelector: `#${TEST.targetElementId}`,
        newValues: TEST.formValue,
      });

      await onPopupShown;
      await clickDoorhangerButton(MAIN_BUTTON, 0);
    });
    await onChanged;

    await expectSavedCreditCards(TEST.expected);
    await removeAllRecords();
  }
});