test_clear_origin_data.js (3610B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const userAgentID = "bd744428-f125-436a-b6d0-dd0c9845837f"; 7 8 let clearForPattern = async function (testRecords, pattern) { 9 let patternString = JSON.stringify(pattern); 10 await PushService._clearOriginData(patternString); 11 12 for (let length = testRecords.length; length--; ) { 13 let test = testRecords[length]; 14 let originSuffix = ChromeUtils.originAttributesToSuffix( 15 test.originAttributes 16 ); 17 18 let registration = await PushService.registration({ 19 scope: test.scope, 20 originAttributes: originSuffix, 21 }); 22 23 let url = test.scope + originSuffix; 24 25 if (ObjectUtils.deepEqual(test.clearIf, pattern)) { 26 ok( 27 !registration, 28 "Should clear registration " + url + " for pattern " + patternString 29 ); 30 testRecords.splice(length, 1); 31 } else { 32 ok( 33 registration, 34 "Should not clear registration " + url + " for pattern " + patternString 35 ); 36 } 37 } 38 }; 39 40 function run_test() { 41 do_get_profile(); 42 setPrefs({ 43 userAgentID, 44 requestTimeout: 1000, 45 retryBaseInterval: 150, 46 }); 47 run_next_test(); 48 } 49 50 add_task(async function test_webapps_cleardata() { 51 Services.fog.testResetFOG(); 52 53 let db = PushServiceWebSocket.newPushDB(); 54 registerCleanupFunction(() => { 55 return db.drop().then(_ => db.close()); 56 }); 57 58 let testRecords = [ 59 { 60 scope: "https://example.org/1", 61 originAttributes: {}, 62 clearIf: { inIsolatedMozBrowser: false }, 63 }, 64 ]; 65 66 let unregisterDone; 67 let unregisterPromise = new Promise( 68 resolve => (unregisterDone = after(testRecords.length, resolve)) 69 ); 70 71 PushService.init({ 72 serverURI: "wss://push.example.org", 73 db, 74 makeWebSocket(uri) { 75 return new MockWebSocket(uri, { 76 onHello(data) { 77 equal(data.messageType, "hello", "Handshake: wrong message type"); 78 ok( 79 !data.uaid, 80 "Should not send UAID in handshake without local subscriptions" 81 ); 82 this.serverSendMsg( 83 JSON.stringify({ 84 messageType: "hello", 85 status: 200, 86 uaid: userAgentID, 87 }) 88 ); 89 }, 90 onRegister(data) { 91 equal(data.messageType, "register", "Register: wrong message type"); 92 this.serverSendMsg( 93 JSON.stringify({ 94 messageType: "register", 95 status: 200, 96 channelID: data.channelID, 97 uaid: userAgentID, 98 pushEndpoint: "https://example.com/update/" + Math.random(), 99 }) 100 ); 101 }, 102 onUnregister(data) { 103 equal(data.code, 200, "Expected manual unregister reason"); 104 unregisterDone(); 105 }, 106 }); 107 }, 108 }); 109 110 await Promise.all( 111 testRecords.map(test => 112 PushService.register({ 113 scope: test.scope, 114 originAttributes: ChromeUtils.originAttributesToSuffix( 115 test.originAttributes 116 ), 117 }) 118 ) 119 ); 120 121 // Removes all the records, Excluding where `inIsolatedMozBrowser` is true. 122 await clearForPattern(testRecords, { inIsolatedMozBrowser: false }); 123 124 // Removes the all the remaining records where `inIsolatedMozBrowser` is true. 125 await clearForPattern(testRecords, {}); 126 127 equal(testRecords.length, 0, "Should remove all test records"); 128 equal( 129 Glean.webPush.unsubscribedByClearingData.testGetValue(), 130 1, 131 "Should increment the Glean counter for the removed record" 132 ); 133 await unregisterPromise; 134 });