test_notification_error.js (3824B)
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 = "3c7462fc-270f-45be-a459-b9d631b0d093"; 7 8 function run_test() { 9 do_get_profile(); 10 setPrefs({ 11 userAgentID, 12 }); 13 run_next_test(); 14 } 15 16 add_task(async function test_notification_error() { 17 let db = PushServiceWebSocket.newPushDB(); 18 registerCleanupFunction(() => { 19 return db.drop().then(_ => db.close()); 20 }); 21 22 let originAttributes = ""; 23 let records = [ 24 { 25 channelID: "f04f1e46-9139-4826-b2d1-9411b0821283", 26 pushEndpoint: "https://example.org/update/success-1", 27 scope: "https://example.com/a", 28 originAttributes, 29 version: 1, 30 quota: Infinity, 31 systemRecord: true, 32 }, 33 { 34 channelID: "3c3930ba-44de-40dc-a7ca-8a133ec1a866", 35 pushEndpoint: "https://example.org/update/error", 36 scope: "https://example.com/b", 37 originAttributes, 38 version: 2, 39 quota: Infinity, 40 systemRecord: true, 41 }, 42 { 43 channelID: "b63f7bef-0a0d-4236-b41e-086a69dfd316", 44 pushEndpoint: "https://example.org/update/success-2", 45 scope: "https://example.com/c", 46 originAttributes, 47 version: 3, 48 quota: Infinity, 49 systemRecord: true, 50 }, 51 ]; 52 for (let record of records) { 53 await db.put(record); 54 } 55 56 let scopes = []; 57 let notifyPromise = promiseObserverNotification( 58 PushServiceComponent.pushTopic, 59 (subject, data) => scopes.push(data) == 2 60 ); 61 62 let ackDone; 63 let ackPromise = new Promise( 64 resolve => (ackDone = after(records.length, resolve)) 65 ); 66 PushService.init({ 67 serverURI: "wss://push.example.org/", 68 db: makeStub(db, { 69 getByKeyID(prev, channelID) { 70 if (channelID == "3c3930ba-44de-40dc-a7ca-8a133ec1a866") { 71 return Promise.reject("splines not reticulated"); 72 } 73 return prev.call(this, channelID); 74 }, 75 }), 76 makeWebSocket(uri) { 77 return new MockWebSocket(uri, { 78 onHello() { 79 this.serverSendMsg( 80 JSON.stringify({ 81 messageType: "hello", 82 status: 200, 83 uaid: userAgentID, 84 }) 85 ); 86 this.serverSendMsg( 87 JSON.stringify({ 88 messageType: "notification", 89 updates: records.map(({ channelID, version }) => ({ 90 channelID, 91 version: ++version, 92 })), 93 }) 94 ); 95 }, 96 // Should acknowledge all received updates, even if updating 97 // IndexedDB fails. 98 onACK: ackDone, 99 }); 100 }, 101 }); 102 103 await notifyPromise; 104 ok( 105 scopes.includes("https://example.com/a"), 106 "Missing scope for notification A" 107 ); 108 ok( 109 scopes.includes("https://example.com/c"), 110 "Missing scope for notification C" 111 ); 112 113 await ackPromise; 114 115 let aRecord = await db.getByIdentifiers({ 116 scope: "https://example.com/a", 117 originAttributes, 118 }); 119 equal( 120 aRecord.channelID, 121 "f04f1e46-9139-4826-b2d1-9411b0821283", 122 "Wrong channel ID for record A" 123 ); 124 strictEqual(aRecord.version, 2, "Should return the new version for record A"); 125 126 let bRecord = await db.getByIdentifiers({ 127 scope: "https://example.com/b", 128 originAttributes, 129 }); 130 equal( 131 bRecord.channelID, 132 "3c3930ba-44de-40dc-a7ca-8a133ec1a866", 133 "Wrong channel ID for record B" 134 ); 135 strictEqual( 136 bRecord.version, 137 2, 138 "Should return the previous version for record B" 139 ); 140 141 let cRecord = await db.getByIdentifiers({ 142 scope: "https://example.com/c", 143 originAttributes, 144 }); 145 equal( 146 cRecord.channelID, 147 "b63f7bef-0a0d-4236-b41e-086a69dfd316", 148 "Wrong channel ID for record C" 149 ); 150 strictEqual(cRecord.version, 4, "Should return the new version for record C"); 151 });