Repository navigation
Expand file tree
/
Copy pathpush.test.js
More file actions
141 lines (112 loc) · 4.65 KB
/
Copy pathpush.test.js
File metadata and controls
141 lines (112 loc) · 4.65 KB
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
'use strict';
define(['ably', 'shared_helper', 'chai', 'push'], function (Ably, Helper, chai, PushPlugin) {
const expect = chai.expect;
const swUrl = '/push_sw.js';
let http;
const persistKeys = {
deviceId: 'ably.push.deviceId',
deviceSecret: 'ably.push.deviceSecret',
deviceIdentityToken: 'ably.push.deviceIdentityToken',
pushRecipient: 'ably.push.pushRecipient',
activationState: 'ably.push.activationState',
};
const messageChannel = new MessageChannel();
/**
* These tests don't work in CI for various reasons (below) but should work when running locally via `npm run test:webserver`, provided
* that you have enabled notification permissions for the origin serving the test ui.
*
* chromium, firefox - don't support push notifications in incognito
* webkit - doesn't have a way to launch programatically with notification permissions granted
*/
if (Notification.permission === 'granted') {
describe('browser/push', function () {
this.timeout(60 * 1000);
before(function (done) {
const helper = Helper.forHook(this);
helper.setupApp(function () {
http = helper.AblyHttp({
pushServiceWorkerUrl: swUrl,
plugins: { Push: PushPlugin },
});
done();
});
});
beforeEach(async function () {
Object.values(persistKeys).forEach((key) => {
Ably.DefaultPubSubRealtimeClient.Platform.Config.push.storage.remove(key);
});
const worker = await navigator.serviceWorker.getRegistration(swUrl);
if (worker) {
await worker.unregister();
}
});
afterEach(async function () {
await http.push.deactivate();
});
/** @spec RSH2a */
it('push_activation_succeeds', async function () {
await http.push.activate();
expect((await http.getDevice()).deviceIdentityToken).to.be.ok;
});
/** @nospec */
it('direct_publish_device_id', async function () {
await http.push.activate();
const pushRecipient = {
deviceId: (await http.getDevice()).id,
};
const pushPayload = {
notification: { title: 'Test message', body: 'Test message body' },
data: { foo: 'bar' },
};
const sw = await navigator.serviceWorker.getRegistration(swUrl);
sw.active.postMessage({ type: 'INIT_PORT' }, [messageChannel.port2]);
const receivedPushPayload = await new Promise((resolve, reject) => {
messageChannel.port1.onmessage = function (event) {
resolve(event.data.payload);
};
http.push.admin.publish(pushRecipient, pushPayload).catch(reject);
});
expect(receivedPushPayload.data).to.deep.equal(pushPayload.data);
expect(receivedPushPayload.notification.title).to.equal(pushPayload.notification.title);
expect(receivedPushPayload.notification.body).to.equal(pushPayload.notification.body);
});
/** @nospec */
it('device_list_subscriptions', async function () {
const helper = Helper.forHook(this);
const adminHttp = helper.AblyHttp({
pushServiceWorkerUrl: swUrl,
plugins: { Push: PushPlugin },
key: helper.getTestApp().keys[0].keyStr, // admin user, all capabilities
});
const subscriberHttp = helper.AblyHttp({
pushServiceWorkerUrl: swUrl,
plugins: { Push: PushPlugin },
key: helper.getTestApp().keys[1].keyStr, // subscriber user, push-subscribe capability but no admin
});
await subscriberHttp.push.activate();
const subscriberDevice = await subscriberHttp.getDevice();
expect(subscriberDevice.deviceIdentityToken).to.be.ok;
const channel1 = 'pushenabled:test1';
const channel2 = 'pushenabled:test2';
await adminHttp.push.admin.channelSubscriptions.save({
channel: channel1,
deviceId: subscriberDevice.id,
});
await adminHttp.push.admin.channelSubscriptions.save({
channel: channel2,
deviceId: subscriberDevice.id,
});
const subscriptions = await subscriberDevice.listSubscriptions();
expect(subscriptions).to.be.ok;
expect(Array.isArray(subscriptions.items)).to.be.true;
expect(subscriptions.items.length).to.equal(2);
const channels = subscriptions.items.map((sub) => sub.channel).sort();
expect(channels).to.deep.equal([channel1, channel2].sort());
subscriptions.items.forEach((sub) => {
expect(sub.deviceId).to.equal(subscriberDevice.id);
});
await subscriberHttp.push.deactivate();
});
});
}
});