summaryrefslogtreecommitdiffstats
path: root/dom/push/test/xpcshell/test_register_rollback.js
blob: 5a316257b314451d908307d3065d0e6e00abfac4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

'use strict';

const {PushDB, PushService, PushServiceWebSocket} = serviceExports;

const userAgentID = 'b2546987-4f63-49b1-99f7-739cd3c40e44';
const channelID = '35a820f7-d7dd-43b3-af21-d65352212ae3';

function run_test() {
  do_get_profile();
  setPrefs({
    userAgentID,
    requestTimeout: 1000,
    retryBaseInterval: 150
  });
  run_next_test();
}

add_task(function* test_register_rollback() {
  let db = PushServiceWebSocket.newPushDB();
  do_register_cleanup(() => {return db.drop().then(_ => db.close());});

  let handshakes = 0;
  let registers = 0;
  let unregisterDone;
  let unregisterPromise = new Promise(resolve => unregisterDone = resolve);
  PushServiceWebSocket._generateID = () => channelID;
  PushService.init({
    serverURI: "wss://push.example.org/",
    db: makeStub(db, {
      put(prev, record) {
        return Promise.reject('universe has imploded');
      }
    }),
    makeWebSocket(uri) {
      return new MockWebSocket(uri, {
        onHello(request) {
          handshakes++;
          equal(request.uaid, userAgentID, 'Handshake: wrong device ID');
          this.serverSendMsg(JSON.stringify({
            messageType: 'hello',
            status: 200,
            uaid: userAgentID
          }));
        },
        onRegister(request) {
          equal(request.channelID, channelID, 'Register: wrong channel ID');
          registers++;
          this.serverSendMsg(JSON.stringify({
            messageType: 'register',
            status: 200,
            uaid: userAgentID,
            channelID,
            pushEndpoint: 'https://example.com/update/rollback'
          }));
        },
        onUnregister(request) {
          equal(request.channelID, channelID, 'Unregister: wrong channel ID');
          equal(request.code, 200, 'Expected manual unregister reason');
          this.serverSendMsg(JSON.stringify({
            messageType: 'unregister',
            status: 200,
            channelID
          }));
          unregisterDone();
        }
      });
    }
  });

  // Should return a rejected promise if storage fails.
  yield rejects(
    PushService.register({
      scope: 'https://example.com/storage-error',
      originAttributes: ChromeUtils.originAttributesToSuffix(
        { appId: Ci.nsIScriptSecurityManager.NO_APP_ID, inIsolatedMozBrowser: false }),
    }),
    'Expected error for unregister database failure'
  );

  // Should send an out-of-band unregister request.
  yield unregisterPromise;
  equal(handshakes, 1, 'Wrong handshake count');
  equal(registers, 1, 'Wrong register count');
});