test_updateRecordNoEncryptionKeys_ws.js (2767B)
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 = "4dffd396-6582-471d-8c0c-84f394e9f7db"; 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_with_data_enabled() { 17 let db = PushServiceWebSocket.newPushDB(); 18 registerCleanupFunction(() => { 19 return db.drop().then(_ => db.close()); 20 }); 21 22 let [publicKey, privateKey] = await PushCrypto.generateKeys(); 23 let records = [ 24 { 25 channelID: "eb18f12a-cc42-4f14-accb-3bfc1227f1aa", 26 pushEndpoint: "https://example.org/push/no-key/1", 27 scope: "https://example.com/page/1", 28 originAttributes: "", 29 quota: Infinity, 30 }, 31 { 32 channelID: "0d8886b9-8da1-4778-8f5d-1cf93a877ed6", 33 pushEndpoint: "https://example.org/push/key", 34 scope: "https://example.com/page/2", 35 originAttributes: "", 36 p256dhPublicKey: publicKey, 37 p256dhPrivateKey: privateKey, 38 quota: Infinity, 39 }, 40 ]; 41 for (let record of records) { 42 await db.put(record); 43 } 44 45 PushService.init({ 46 serverURI: "wss://push.example.org/", 47 db, 48 makeWebSocket(uri) { 49 return new MockWebSocket(uri, { 50 onHello(request) { 51 ok( 52 request.use_webpush, 53 "Should use Web Push if data delivery is enabled" 54 ); 55 this.serverSendMsg( 56 JSON.stringify({ 57 messageType: "hello", 58 status: 200, 59 uaid: request.uaid, 60 use_webpush: true, 61 }) 62 ); 63 }, 64 onRegister(request) { 65 this.serverSendMsg( 66 JSON.stringify({ 67 messageType: "register", 68 status: 200, 69 uaid: userAgentID, 70 channelID: request.channelID, 71 pushEndpoint: "https://example.org/push/new", 72 }) 73 ); 74 }, 75 }); 76 }, 77 }); 78 79 let newRecord = await PushService.register({ 80 scope: "https://example.com/page/3", 81 originAttributes: ChromeUtils.originAttributesToSuffix({ 82 inIsolatedMozBrowser: false, 83 }), 84 }); 85 ok(newRecord.p256dhKey, "Should generate public keys for new records"); 86 87 let record = await db.getByKeyID("eb18f12a-cc42-4f14-accb-3bfc1227f1aa"); 88 ok(record.p256dhPublicKey, "Should add public key to partial record"); 89 ok(record.p256dhPrivateKey, "Should add private key to partial record"); 90 91 record = await db.getByKeyID("0d8886b9-8da1-4778-8f5d-1cf93a877ed6"); 92 deepEqual( 93 record.p256dhPublicKey, 94 publicKey, 95 "Should leave existing public key" 96 ); 97 deepEqual( 98 record.p256dhPrivateKey, 99 privateKey, 100 "Should leave existing private key" 101 ); 102 });