summaryrefslogtreecommitdiffstats
path: root/dom/push/test/xpcshell/test_clear_origin_data.js
blob: fe42031df3f80504c125161c05c8484376377aaa (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const userAgentID = "bd744428-f125-436a-b6d0-dd0c9845837f";

let clearForPattern = async function (testRecords, pattern) {
  let patternString = JSON.stringify(pattern);
  await PushService._clearOriginData(patternString);

  for (let length = testRecords.length; length--; ) {
    let test = testRecords[length];
    let originSuffix = ChromeUtils.originAttributesToSuffix(
      test.originAttributes
    );

    let registration = await PushService.registration({
      scope: test.scope,
      originAttributes: originSuffix,
    });

    let url = test.scope + originSuffix;

    if (ObjectUtils.deepEqual(test.clearIf, pattern)) {
      ok(
        !registration,
        "Should clear registration " + url + " for pattern " + patternString
      );
      testRecords.splice(length, 1);
    } else {
      ok(
        registration,
        "Should not clear registration " + url + " for pattern " + patternString
      );
    }
  }
};

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

add_task(async function test_webapps_cleardata() {
  let db = PushServiceWebSocket.newPushDB();
  registerCleanupFunction(() => {
    return db.drop().then(_ => db.close());
  });

  let testRecords = [
    {
      scope: "https://example.org/1",
      originAttributes: {},
      clearIf: { inIsolatedMozBrowser: false },
    },
  ];

  let unregisterDone;
  let unregisterPromise = new Promise(
    resolve => (unregisterDone = after(testRecords.length, resolve))
  );

  PushService.init({
    serverURI: "wss://push.example.org",
    db,
    makeWebSocket(uri) {
      return new MockWebSocket(uri, {
        onHello(data) {
          equal(data.messageType, "hello", "Handshake: wrong message type");
          ok(
            !data.uaid,
            "Should not send UAID in handshake without local subscriptions"
          );
          this.serverSendMsg(
            JSON.stringify({
              messageType: "hello",
              status: 200,
              uaid: userAgentID,
            })
          );
        },
        onRegister(data) {
          equal(data.messageType, "register", "Register: wrong message type");
          this.serverSendMsg(
            JSON.stringify({
              messageType: "register",
              status: 200,
              channelID: data.channelID,
              uaid: userAgentID,
              pushEndpoint: "https://example.com/update/" + Math.random(),
            })
          );
        },
        onUnregister(data) {
          equal(data.code, 200, "Expected manual unregister reason");
          unregisterDone();
        },
      });
    },
  });

  await Promise.all(
    testRecords.map(test =>
      PushService.register({
        scope: test.scope,
        originAttributes: ChromeUtils.originAttributesToSuffix(
          test.originAttributes
        ),
      })
    )
  );

  // Removes all the records, Excluding where `inIsolatedMozBrowser` is true.
  await clearForPattern(testRecords, { inIsolatedMozBrowser: false });

  // Removes the all the remaining records where `inIsolatedMozBrowser` is true.
  await clearForPattern(testRecords, {});

  equal(testRecords.length, 0, "Should remove all test records");
  await unregisterPromise;
});