test_IPProtectionStates.js (5276B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { IPPNimbusHelper } = ChromeUtils.importESModule( 7 "moz-src:///browser/components/ipprotection/IPPNimbusHelper.sys.mjs" 8 ); 9 const { IPPEnrollAndEntitleManager } = ChromeUtils.importESModule( 10 "moz-src:///browser/components/ipprotection/IPPEnrollAndEntitleManager.sys.mjs" 11 ); 12 13 do_get_profile(); 14 15 add_setup(async function () { 16 await putServerInRemoteSettings(); 17 IPProtectionService.uninit(); 18 19 registerCleanupFunction(async () => { 20 await IPProtectionService.init(); 21 }); 22 }); 23 24 /** 25 * Tests the uninitialized state. 26 */ 27 add_task(async function test_IPProtectionStates_uninitialized() { 28 Assert.equal( 29 IPProtectionService.state, 30 IPProtectionStates.UNINITIALIZED, 31 "IP Protection service should not be initialized yet" 32 ); 33 34 await IPProtectionService.init(); 35 36 Assert.equal( 37 IPProtectionService.state, 38 IPProtectionStates.UNAVAILABLE, 39 "IP Protection service should be initialized" 40 ); 41 42 IPProtectionService.uninit(); 43 44 Assert.equal( 45 IPProtectionService.state, 46 IPProtectionStates.UNINITIALIZED, 47 "IP Protection service should not be uninitialized" 48 ); 49 }); 50 51 /** 52 * Tests the unavailable state. 53 */ 54 add_task(async function test_IPProtectionStates_uninitialized() { 55 let sandbox = sinon.createSandbox(); 56 sandbox.stub(IPPSignInWatcher, "isSignedIn").get(() => false); 57 sandbox 58 .stub(IPProtectionService.guardian, "isLinkedToGuardian") 59 .resolves(false); 60 61 await IPProtectionService.init(); 62 63 Assert.equal( 64 IPProtectionService.state, 65 IPProtectionStates.UNAVAILABLE, 66 "IP Protection service should be unavailable" 67 ); 68 69 sandbox.stub(IPPNimbusHelper, "isEligible").get(() => true); 70 71 IPProtectionService.updateState(); 72 73 Assert.notStrictEqual( 74 IPProtectionService.state, 75 IPProtectionStates.UNAVAILABLE, 76 "IP Protection service should be available" 77 ); 78 79 IPProtectionService.uninit(); 80 sandbox.restore(); 81 }); 82 83 /** 84 * Tests the unauthenticated state. 85 */ 86 add_task(async function test_IPProtectionStates_unauthenticated() { 87 let sandbox = sinon.createSandbox(); 88 sandbox.stub(IPPSignInWatcher, "isSignedIn").get(() => true); 89 sandbox 90 .stub(IPProtectionService.guardian, "isLinkedToGuardian") 91 .resolves(false); 92 sandbox.stub(IPProtectionService.guardian, "enroll").resolves({ ok: true }); 93 sandbox.stub(IPPNimbusHelper, "isEligible").get(() => false); 94 95 await IPProtectionService.init(); 96 97 Assert.equal( 98 IPProtectionService.state, 99 IPProtectionStates.UNAVAILABLE, 100 "IP Protection service should be unavailable" 101 ); 102 103 sandbox.stub(IPPNimbusHelper, "isEligible").get(() => true); 104 105 IPProtectionService.updateState(); 106 107 Assert.equal( 108 IPProtectionService.state, 109 IPProtectionStates.READY, 110 "IP Protection service should no longer be unauthenticated" 111 ); 112 113 sandbox.stub(IPPSignInWatcher, "isSignedIn").get(() => false); 114 115 IPProtectionService.updateState(); 116 117 Assert.equal( 118 IPProtectionService.state, 119 IPProtectionStates.UNAUTHENTICATED, 120 "IP Protection service should be unauthenticated" 121 ); 122 123 IPProtectionService.uninit(); 124 sandbox.restore(); 125 }); 126 127 /** 128 * Tests the enrolling state. 129 */ 130 add_task(async function test_IPProtectionStates_enrolling() { 131 let sandbox = sinon.createSandbox(); 132 sandbox.stub(IPPSignInWatcher, "isSignedIn").get(() => true); 133 sandbox 134 .stub(IPProtectionService.guardian, "isLinkedToGuardian") 135 .resolves(false); 136 sandbox.stub(IPPNimbusHelper, "isEligible").get(() => true); 137 sandbox.stub(IPProtectionService.guardian, "enroll").resolves({ ok: true }); 138 sandbox.stub(IPProtectionService.guardian, "fetchUserInfo").resolves({ 139 status: 200, 140 error: null, 141 entitlement: { uid: 42 }, 142 }); 143 144 await IPProtectionService.init(); 145 146 Assert.equal( 147 IPProtectionService.state, 148 IPProtectionStates.READY, 149 "IP Protection service should be ready" 150 ); 151 152 IPProtectionService.guardian.isLinkedToGuardian.resolves(true); 153 154 const enrollData = await IPPEnrollAndEntitleManager.maybeEnrollAndEntitle(); 155 Assert.ok(enrollData.isEnrolledAndEntitled, "Fully enrolled and entitled"); 156 157 Assert.equal( 158 IPProtectionService.state, 159 IPProtectionStates.READY, 160 "IP Protection service should have enrolled and be ready" 161 ); 162 163 IPProtectionService.uninit(); 164 sandbox.restore(); 165 }); 166 167 /** 168 * Tests the ready state. 169 */ 170 add_task(async function test_IPProtectionStates_ready() { 171 let sandbox = sinon.createSandbox(); 172 sandbox.stub(IPPSignInWatcher, "isSignedIn").get(() => true); 173 sandbox 174 .stub(IPProtectionService.guardian, "isLinkedToGuardian") 175 .resolves(true); 176 sandbox.stub(IPProtectionService.guardian, "fetchUserInfo").resolves({ 177 status: 200, 178 error: null, 179 entitlement: { uid: 42 }, 180 }); 181 182 await IPProtectionService.init(); 183 184 Assert.equal( 185 IPProtectionService.state, 186 IPProtectionStates.READY, 187 "IP Protection service should be ready" 188 ); 189 190 sandbox.stub(IPPSignInWatcher, "isSignedIn").get(() => false); 191 192 IPProtectionService.updateState(); 193 194 Assert.notStrictEqual( 195 IPProtectionService.state, 196 IPProtectionStates.READY, 197 "IP Protection service should not be ready" 198 ); 199 200 IPProtectionService.uninit(); 201 sandbox.restore(); 202 });