test_drop_expired.js (4004B)
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 = "2c43af06-ab6e-476a-adc4-16cbda54fb89"; 7 8 var db; 9 var quotaURI; 10 var permURI; 11 12 function visitURI(uri, timestamp) { 13 return PlacesTestUtils.addVisits({ 14 uri, 15 title: uri.spec, 16 visitDate: timestamp * 1000, 17 transition: Ci.nsINavHistoryService.TRANSITION_LINK, 18 }); 19 } 20 21 var putRecord = async function ({ scope, perm, quota, lastPush, lastVisit }) { 22 let uri = Services.io.newURI(scope); 23 24 PermissionTestUtils.add( 25 uri, 26 "desktop-notification", 27 Ci.nsIPermissionManager[perm] 28 ); 29 registerCleanupFunction(() => { 30 PermissionTestUtils.remove(uri, "desktop-notification"); 31 }); 32 33 await visitURI(uri, lastVisit); 34 35 await db.put({ 36 channelID: uri.pathQueryRef, 37 pushEndpoint: "https://example.org/push" + uri.pathQueryRef, 38 scope: uri.spec, 39 pushCount: 0, 40 lastPush, 41 version: null, 42 originAttributes: "", 43 quota, 44 }); 45 46 return uri; 47 }; 48 49 function run_test() { 50 do_get_profile(); 51 setPrefs({ 52 userAgentID, 53 }); 54 55 db = PushServiceWebSocket.newPushDB(); 56 registerCleanupFunction(() => { 57 return db.drop().then(_ => db.close()); 58 }); 59 60 run_next_test(); 61 } 62 63 add_task(async function setUp() { 64 // An expired registration that should be evicted on startup. Permission is 65 // granted for this origin, and the last visit is more recent than the last 66 // push message. 67 await putRecord({ 68 scope: "https://example.com/expired-quota-restored", 69 perm: "ALLOW_ACTION", 70 quota: 0, 71 lastPush: Date.now() - 10, 72 lastVisit: Date.now(), 73 }); 74 75 // An expired registration that we should evict when the origin is visited 76 // again. 77 quotaURI = await putRecord({ 78 scope: "https://example.xyz/expired-quota-exceeded", 79 perm: "ALLOW_ACTION", 80 quota: 0, 81 lastPush: Date.now() - 10, 82 lastVisit: Date.now() - 20, 83 }); 84 85 // An expired registration that we should evict when permission is granted 86 // again. 87 permURI = await putRecord({ 88 scope: "https://example.info/expired-perm-revoked", 89 perm: "DENY_ACTION", 90 quota: 0, 91 lastPush: Date.now() - 10, 92 lastVisit: Date.now(), 93 }); 94 95 // An active registration that we should leave alone. 96 await putRecord({ 97 scope: "https://example.ninja/active", 98 perm: "ALLOW_ACTION", 99 quota: 16, 100 lastPush: Date.now() - 10, 101 lastVisit: Date.now() - 20, 102 }); 103 104 let subChangePromise = promiseObserverNotification( 105 PushServiceComponent.subscriptionChangeTopic, 106 (subject, data) => data == "https://example.com/expired-quota-restored" 107 ); 108 109 PushService.init({ 110 serverURI: "wss://push.example.org/", 111 db, 112 makeWebSocket(uri) { 113 return new MockWebSocket(uri, { 114 onHello() { 115 this.serverSendMsg( 116 JSON.stringify({ 117 messageType: "hello", 118 status: 200, 119 uaid: userAgentID, 120 }) 121 ); 122 }, 123 onUnregister(request) { 124 this.serverSendMsg( 125 JSON.stringify({ 126 messageType: "unregister", 127 channelID: request.channelID, 128 status: 200, 129 }) 130 ); 131 }, 132 }); 133 }, 134 }); 135 136 await subChangePromise; 137 }); 138 139 add_task(async function test_site_visited() { 140 let subChangePromise = promiseObserverNotification( 141 PushServiceComponent.subscriptionChangeTopic, 142 (subject, data) => data == "https://example.xyz/expired-quota-exceeded" 143 ); 144 145 await visitURI(quotaURI, Date.now()); 146 PushService.observe(null, "idle-daily", ""); 147 148 await subChangePromise; 149 }); 150 151 add_task(async function test_perm_restored() { 152 let subChangePromise = promiseObserverNotification( 153 PushServiceComponent.subscriptionChangeTopic, 154 (subject, data) => data == "https://example.info/expired-perm-revoked" 155 ); 156 157 PermissionTestUtils.add( 158 permURI, 159 "desktop-notification", 160 Ci.nsIPermissionManager.ALLOW_ACTION 161 ); 162 163 await subChangePromise; 164 });