summaryrefslogtreecommitdiffstats
path: root/browser/components/tests/unit/test_browserGlue_migration_osauth.js
blob: 5676ea2fa97397a6770595d1aaab1e3f8d79e62e (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TOPIC_BROWSERGLUE_TEST = "browser-glue-test";
const TOPICDATA_BROWSERGLUE_TEST = "force-ui-migration";
const gBrowserGlue = Cc["@mozilla.org/browser/browserglue;1"].getService(
  Ci.nsIObserver
);
const UI_VERSION = 147;

const { LoginHelper } = ChromeUtils.importESModule(
  "resource://gre/modules/LoginHelper.sys.mjs"
);
const { FormAutofillUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/shared/FormAutofillUtils.sys.mjs"
);

const CC_OLD_PREF = "extensions.formautofill.reauth.enabled";
const CC_TYPO_PREF = "extensions.formautofill.creditcards.reauth.optout";
const CC_NEW_PREF = FormAutofillUtils.AUTOFILL_CREDITCARDS_REAUTH_PREF;

const PASSWORDS_OLD_PREF = "signon.management.page.os-auth.enabled";
const PASSWORDS_NEW_PREF = LoginHelper.OS_AUTH_FOR_PASSWORDS_PREF;

function clearPrefs() {
  Services.prefs.clearUserPref("browser.migration.version");
  Services.prefs.clearUserPref(CC_OLD_PREF);
  Services.prefs.clearUserPref(CC_TYPO_PREF);
  Services.prefs.clearUserPref(CC_NEW_PREF);
  Services.prefs.clearUserPref(PASSWORDS_OLD_PREF);
  Services.prefs.clearUserPref(PASSWORDS_NEW_PREF);
  Services.prefs.clearUserPref("browser.startup.homepage_override.mstone");
}

function simulateUIMigration() {
  gBrowserGlue.observe(
    null,
    TOPIC_BROWSERGLUE_TEST,
    TOPICDATA_BROWSERGLUE_TEST
  );
}

add_task(async function setup() {
  registerCleanupFunction(clearPrefs);
});

add_task(async function test_pref_migration_old_pref_os_auth_disabled() {
  Services.prefs.setIntPref("browser.migration.version", UI_VERSION - 1);
  Services.prefs.setBoolPref(CC_OLD_PREF, false);
  Services.prefs.setBoolPref(PASSWORDS_OLD_PREF, false);

  simulateUIMigration();

  Assert.ok(
    !FormAutofillUtils.getOSAuthEnabled(CC_NEW_PREF),
    "OS Auth should be disabled for credit cards since it was disabled before migration."
  );
  Assert.ok(
    !LoginHelper.getOSAuthEnabled(PASSWORDS_NEW_PREF),
    "OS Auth should be disabled for passwords since it was disabled before migration."
  );
  clearPrefs();
});

add_task(async function test_pref_migration_old_pref_os_auth_enabled() {
  Services.prefs.setIntPref("browser.migration.version", UI_VERSION - 1);
  Services.prefs.setBoolPref(CC_OLD_PREF, true);
  Services.prefs.setBoolPref(PASSWORDS_OLD_PREF, true);

  simulateUIMigration();

  Assert.ok(
    FormAutofillUtils.getOSAuthEnabled(CC_NEW_PREF),
    "OS Auth should be enabled for credit cards since it was enabled before migration."
  );
  Assert.ok(
    LoginHelper.getOSAuthEnabled(PASSWORDS_NEW_PREF),
    "OS Auth should be enabled for passwords since it was enabled before migration."
  );
  clearPrefs();
});

add_task(
  async function test_creditCards_pref_migration_typo_pref_os_auth_disabled() {
    Services.prefs.setIntPref("browser.migration.version", UI_VERSION - 1);
    Services.prefs.setCharPref(
      "browser.startup.homepage_override.mstone",
      "127.0"
    );
    FormAutofillUtils.setOSAuthEnabled(CC_TYPO_PREF, false);

    simulateUIMigration();

    Assert.ok(
      !FormAutofillUtils.getOSAuthEnabled(CC_NEW_PREF),
      "OS Auth should be disabled for credit cards since it was disabled before migration."
    );
    clearPrefs();
  }
);

add_task(
  async function test_creditCards_pref_migration_typo_pref_os_auth_enabled() {
    Services.prefs.setIntPref("browser.migration.version", UI_VERSION - 1);
    Services.prefs.setCharPref(
      "browser.startup.homepage_override.mstone",
      "127.0"
    );
    FormAutofillUtils.setOSAuthEnabled(CC_TYPO_PREF, true);

    simulateUIMigration();

    Assert.ok(
      FormAutofillUtils.getOSAuthEnabled(CC_NEW_PREF),
      "OS Auth should be enabled for credit cards since it was enabled before migration."
    );
    clearPrefs();
  }
);

add_task(
  async function test_creditCards_pref_migration_real_pref_os_auth_disabled() {
    Services.prefs.setIntPref("browser.migration.version", UI_VERSION - 1);
    Services.prefs.setCharPref(
      "browser.startup.homepage_override.mstone",
      "127.0"
    );
    FormAutofillUtils.setOSAuthEnabled(CC_NEW_PREF, false);

    simulateUIMigration();

    Assert.ok(
      !FormAutofillUtils.getOSAuthEnabled(CC_NEW_PREF),
      "OS Auth should be disabled for credit cards since it was disabled before migration."
    );
    clearPrefs();
  }
);

add_task(
  async function test_creditCards_pref_migration_real_pref_os_auth_enabled() {
    Services.prefs.setIntPref("browser.migration.version", UI_VERSION - 1);
    Services.prefs.setCharPref(
      "browser.startup.homepage_override.mstone",
      "127.0"
    );
    FormAutofillUtils.setOSAuthEnabled(CC_NEW_PREF, true);

    simulateUIMigration();

    Assert.ok(
      FormAutofillUtils.getOSAuthEnabled(CC_NEW_PREF),
      "OS Auth should be enabled for credit cards since it was enabled before migration."
    );
    clearPrefs();
  }
);