test_clearAll_successful.js (3712B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 var db; 7 var unregisterDefers = {}; 8 var userAgentID = "4ce480ef-55b2-4f83-924c-dcd35ab978b4"; 9 10 function promiseUnregister(keyID) { 11 return new Promise(r => (unregisterDefers[keyID] = r)); 12 } 13 14 function run_test() { 15 do_get_profile(); 16 setPrefs({ 17 userAgentID, 18 }); 19 run_next_test(); 20 } 21 22 add_task(async function setup() { 23 Services.fog.testResetFOG(); 24 25 db = PushServiceWebSocket.newPushDB(); 26 registerCleanupFunction(() => db.drop().then(() => db.close())); 27 28 // Active subscriptions; should be expired then dropped. 29 await putTestRecord(db, "active-1", "https://example.info/some-page", 8); 30 await putTestRecord(db, "active-2", "https://example.com/another-page", 16); 31 32 // Expired subscription; should be dropped. 33 await putTestRecord(db, "expired", "https://example.net/yet-another-page", 0); 34 35 // A privileged subscription that should not be affected by sanitizing data 36 // because its quota is set to `Infinity`. 37 await putTestRecord(db, "privileged", "app://chrome/only", Infinity); 38 39 let handshakeDone; 40 let handshakePromise = new Promise(r => (handshakeDone = r)); 41 PushService.init({ 42 serverURI: "wss://push.example.org/", 43 db, 44 makeWebSocket(uri) { 45 return new MockWebSocket(uri, { 46 onHello() { 47 this.serverSendMsg( 48 JSON.stringify({ 49 messageType: "hello", 50 uaid: userAgentID, 51 status: 200, 52 use_webpush: true, 53 }) 54 ); 55 handshakeDone(); 56 }, 57 onUnregister(request) { 58 let resolve = unregisterDefers[request.channelID]; 59 equal( 60 typeof resolve, 61 "function", 62 "Dropped unexpected channel ID " + request.channelID 63 ); 64 delete unregisterDefers[request.channelID]; 65 equal(request.code, 200, "Expected manual unregister reason"); 66 this.serverSendMsg( 67 JSON.stringify({ 68 messageType: "unregister", 69 channelID: request.channelID, 70 status: 200, 71 }) 72 ); 73 resolve(); 74 }, 75 }); 76 }, 77 }); 78 await handshakePromise; 79 }); 80 81 add_task(async function test_sanitize() { 82 let modifiedScopes = []; 83 let changeScopes = []; 84 85 let promiseCleared = Promise.all([ 86 // Active subscriptions should be unregistered. 87 promiseUnregister("active-1"), 88 promiseUnregister("active-2"), 89 promiseObserverNotification( 90 PushServiceComponent.subscriptionModifiedTopic, 91 (subject, data) => { 92 modifiedScopes.push(data); 93 return modifiedScopes.length == 3; 94 } 95 ), 96 97 // Privileged should be recreated. 98 promiseUnregister("privileged"), 99 promiseObserverNotification( 100 PushServiceComponent.subscriptionChangeTopic, 101 (subject, data) => { 102 changeScopes.push(data); 103 return changeScopes.length == 1; 104 } 105 ), 106 ]); 107 108 await PushService.clear({ 109 domain: "*", 110 }); 111 112 await promiseCleared; 113 114 equal( 115 Glean.webPush.unsubscribedByClearingData.testGetValue(), 116 2, 117 "Should increment the Glean counter for the removed records" 118 ); 119 120 deepEqual( 121 modifiedScopes.sort(compareAscending), 122 [ 123 "app://chrome/only", 124 "https://example.com/another-page", 125 "https://example.info/some-page", 126 ], 127 "Should modify active subscription scopes" 128 ); 129 130 deepEqual( 131 changeScopes, 132 ["app://chrome/only"], 133 "Should fire change notification for privileged scope" 134 ); 135 136 let remainingIDs = await getAllKeyIDs(db); 137 deepEqual(remainingIDs, [], "Should drop all subscriptions"); 138 });