summaryrefslogtreecommitdiffstats
path: root/dom/push/test/xpcshell/test_drop_expired.js
blob: 4444753e8f161fbe993a5c123142ff6852b997e7 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

'use strict';

const {PushDB, PushService, PushServiceWebSocket} = serviceExports;

const userAgentID = '2c43af06-ab6e-476a-adc4-16cbda54fb89';

var db;
var quotaURI;
var permURI;

function visitURI(uri, timestamp) {
  return PlacesTestUtils.addVisits({
    uri: uri,
    title: uri.spec,
    visitDate: timestamp * 1000,
    transition: Ci.nsINavHistoryService.TRANSITION_LINK
  });
}

var putRecord = Task.async(function* ({scope, perm, quota, lastPush, lastVisit}) {
  let uri = Services.io.newURI(scope, null, null);

  Services.perms.add(uri, 'desktop-notification',
    Ci.nsIPermissionManager[perm]);
  do_register_cleanup(() => {
    Services.perms.remove(uri, 'desktop-notification');
  });

  yield visitURI(uri, lastVisit);

  yield db.put({
    channelID: uri.path,
    pushEndpoint: 'https://example.org/push' + uri.path,
    scope: uri.spec,
    pushCount: 0,
    lastPush: lastPush,
    version: null,
    originAttributes: '',
    quota: quota,
  });

  return uri;
});

function run_test() {
  do_get_profile();
  setPrefs({
    userAgentID: userAgentID,
  });

  db = PushServiceWebSocket.newPushDB();
  do_register_cleanup(() => {return db.drop().then(_ => db.close());});

  run_next_test();
}

add_task(function* setUp() {
  // An expired registration that should be evicted on startup. Permission is
  // granted for this origin, and the last visit is more recent than the last
  // push message.
  yield putRecord({
    scope: 'https://example.com/expired-quota-restored',
    perm: 'ALLOW_ACTION',
    quota: 0,
    lastPush: Date.now() - 10,
    lastVisit: Date.now(),
  });

  // An expired registration that we should evict when the origin is visited
  // again.
  quotaURI = yield putRecord({
    scope: 'https://example.xyz/expired-quota-exceeded',
    perm: 'ALLOW_ACTION',
    quota: 0,
    lastPush: Date.now() - 10,
    lastVisit: Date.now() - 20,
  });

  // An expired registration that we should evict when permission is granted
  // again.
  permURI = yield putRecord({
    scope: 'https://example.info/expired-perm-revoked',
    perm: 'DENY_ACTION',
    quota: 0,
    lastPush: Date.now() - 10,
    lastVisit: Date.now(),
  });

  // An active registration that we should leave alone.
  yield putRecord({
    scope: 'https://example.ninja/active',
    perm: 'ALLOW_ACTION',
    quota: 16,
    lastPush: Date.now() - 10,
    lastVisit: Date.now() - 20,
  });

  let subChangePromise = promiseObserverNotification(
    PushServiceComponent.subscriptionChangeTopic,
    (subject, data) => data == 'https://example.com/expired-quota-restored'
  );

  PushService.init({
    serverURI: 'wss://push.example.org/',
    db,
    makeWebSocket(uri) {
      return new MockWebSocket(uri, {
        onHello(request) {
          this.serverSendMsg(JSON.stringify({
            messageType: 'hello',
            status: 200,
            uaid: userAgentID,
          }));
        },
        onUnregister(request) {
          this.serverSendMsg(JSON.stringify({
            messageType: 'unregister',
            channelID: request.channelID,
            status: 200,
          }));
        },
      });
    },
  });

  yield subChangePromise;
});

add_task(function* test_site_visited() {
  let subChangePromise = promiseObserverNotification(
    PushServiceComponent.subscriptionChangeTopic,
    (subject, data) => data == 'https://example.xyz/expired-quota-exceeded'
  );

  yield visitURI(quotaURI, Date.now());
  PushService.observe(null, 'idle-daily', '');

  yield subChangePromise;
});

add_task(function* test_perm_restored() {
  let subChangePromise = promiseObserverNotification(
    PushServiceComponent.subscriptionChangeTopic,
    (subject, data) => data == 'https://example.info/expired-perm-revoked'
  );

  Services.perms.add(permURI, 'desktop-notification',
    Ci.nsIPermissionManager.ALLOW_ACTION);

  yield subChangePromise;
});