test_quota_exceeded.js (4644B)
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 = "7eb873f9-8d47-4218-804b-fff78dc04e88"; 7 8 function run_test() { 9 do_get_profile(); 10 setPrefs({ 11 userAgentID, 12 "testing.ignorePermission": true, 13 }); 14 run_next_test(); 15 } 16 17 add_task(async function test_expiration_origin_threshold() { 18 let db = PushServiceWebSocket.newPushDB(); 19 registerCleanupFunction(() => db.drop().then(_ => db.close())); 20 21 await db.put({ 22 channelID: "eb33fc90-c883-4267-b5cb-613969e8e349", 23 pushEndpoint: "https://example.org/push/1", 24 scope: "https://example.com/auctions", 25 pushCount: 0, 26 lastPush: 0, 27 version: null, 28 originAttributes: "", 29 quota: 16, 30 }); 31 await db.put({ 32 channelID: "46cc6f6a-c106-4ffa-bb7c-55c60bd50c41", 33 pushEndpoint: "https://example.org/push/2", 34 scope: "https://example.com/deals", 35 pushCount: 0, 36 lastPush: 0, 37 version: null, 38 originAttributes: "", 39 quota: 16, 40 }); 41 42 // The notification threshold is per-origin, even with multiple service 43 // workers for different scopes. 44 await PlacesTestUtils.addVisits([ 45 { 46 uri: "https://example.com/login", 47 title: "Sign in to see your auctions", 48 visitDate: (Date.now() - 7 * 24 * 60 * 60 * 1000) * 1000, 49 transition: Ci.nsINavHistoryService.TRANSITION_LINK, 50 }, 51 // We'll always use your most recent visit to an origin. 52 { 53 uri: "https://example.com/auctions", 54 title: "Your auctions", 55 visitDate: (Date.now() - 2 * 24 * 60 * 60 * 1000) * 1000, 56 transition: Ci.nsINavHistoryService.TRANSITION_LINK, 57 }, 58 // ...But we won't count downloads or embeds. 59 { 60 uri: "https://example.com/invoices/invoice.pdf", 61 title: "Invoice #123", 62 visitDate: (Date.now() - 1 * 24 * 60 * 60 * 1000) * 1000, 63 transition: Ci.nsINavHistoryService.TRANSITION_EMBED, 64 }, 65 { 66 uri: "https://example.com/invoices/invoice.pdf", 67 title: "Invoice #123", 68 visitDate: Date.now() * 1000, 69 transition: Ci.nsINavHistoryService.TRANSITION_DOWNLOAD, 70 }, 71 ]); 72 73 // We expect to receive 6 notifications: 5 on the `auctions` channel, 74 // and 1 on the `deals` channel. They're from the same origin, but 75 // different scopes, so each can send 5 notifications before we remove 76 // their subscription. 77 let updates = 0; 78 let notifyPromise = promiseObserverNotification( 79 PushServiceComponent.pushTopic, 80 () => { 81 updates++; 82 return updates == 6; 83 } 84 ); 85 86 let unregisterDone; 87 let unregisterPromise = new Promise(resolve => (unregisterDone = resolve)); 88 89 PushService.init({ 90 serverURI: "wss://push.example.org/", 91 db, 92 makeWebSocket(uri) { 93 return new MockWebSocket(uri, { 94 onHello() { 95 this.serverSendMsg( 96 JSON.stringify({ 97 messageType: "hello", 98 status: 200, 99 uaid: userAgentID, 100 }) 101 ); 102 // We last visited the site 2 days ago, so we can send 5 103 // notifications without throttling. Sending a 6th should 104 // drop the registration. 105 for (let version = 1; version <= 6; version++) { 106 this.serverSendMsg( 107 JSON.stringify({ 108 messageType: "notification", 109 updates: [ 110 { 111 channelID: "eb33fc90-c883-4267-b5cb-613969e8e349", 112 version, 113 }, 114 ], 115 }) 116 ); 117 } 118 // But the limits are per-channel, so we can send 5 more 119 // notifications on a different channel. 120 this.serverSendMsg( 121 JSON.stringify({ 122 messageType: "notification", 123 updates: [ 124 { 125 channelID: "46cc6f6a-c106-4ffa-bb7c-55c60bd50c41", 126 version: 1, 127 }, 128 ], 129 }) 130 ); 131 }, 132 onUnregister(request) { 133 equal( 134 request.channelID, 135 "eb33fc90-c883-4267-b5cb-613969e8e349", 136 "Unregistered wrong channel ID" 137 ); 138 equal(request.code, 201, "Expected quota exceeded unregister reason"); 139 unregisterDone(); 140 }, 141 // We expect to receive acks, but don't care about their 142 // contents. 143 onACK() {}, 144 }); 145 }, 146 }); 147 148 await unregisterPromise; 149 150 await notifyPromise; 151 152 let expiredRecord = await db.getByKeyID( 153 "eb33fc90-c883-4267-b5cb-613969e8e349" 154 ); 155 strictEqual(expiredRecord.quota, 0, "Expired record not updated"); 156 });