pusher-js 7.0.6 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +27 -87
  3. package/dist/node/pusher.js +325 -73
  4. package/dist/node/pusher.js.map +1 -1
  5. package/dist/react-native/pusher.js +2 -2
  6. package/dist/react-native/pusher.js.map +1 -1
  7. package/dist/web/pusher-with-encryption.js +330 -78
  8. package/dist/web/pusher-with-encryption.js.map +1 -1
  9. package/dist/web/pusher-with-encryption.min.js +2 -2
  10. package/dist/web/pusher-with-encryption.min.js.map +1 -1
  11. package/dist/web/pusher.js +330 -78
  12. package/dist/web/pusher.js.map +1 -1
  13. package/dist/web/pusher.min.js +2 -2
  14. package/dist/web/pusher.min.js.map +1 -1
  15. package/dist/worker/pusher-with-encryption.worker.js +314 -69
  16. package/dist/worker/pusher-with-encryption.worker.js.map +1 -1
  17. package/dist/worker/pusher-with-encryption.worker.min.js +2 -2
  18. package/dist/worker/pusher-with-encryption.worker.min.js.map +1 -1
  19. package/dist/worker/pusher.worker.js +314 -69
  20. package/dist/worker/pusher.worker.js.map +1 -1
  21. package/dist/worker/pusher.worker.min.js +2 -2
  22. package/dist/worker/pusher.worker.min.js.map +1 -1
  23. package/index.d.ts +8 -3
  24. package/package.json +2 -2
  25. package/spec/config/karma/config.worker.js +3 -0
  26. package/spec/config/karma/integration.js +4 -2
  27. package/spec/javascripts/helpers/mocks.js +41 -8
  28. package/spec/javascripts/helpers/worker/mock-dom-dependencies.js +1 -0
  29. package/spec/javascripts/integration/core/cluster_config_spec.js +8 -0
  30. package/spec/javascripts/integration/core/timeout_configuration_spec.js +1 -0
  31. package/spec/javascripts/integration/index.worker.js +12 -1
  32. package/spec/javascripts/unit/core/channels/channel_spec.js +25 -0
  33. package/spec/javascripts/unit/core/channels/encrypted_channel_spec.js +64 -66
  34. package/spec/javascripts/unit/core/channels/presence_channel_spec.js +51 -41
  35. package/spec/javascripts/unit/core/channels/private_channel_spec.js +8 -46
  36. package/spec/javascripts/unit/core/config_spec.js +307 -7
  37. package/spec/javascripts/unit/core/connection/connection_manager_spec.js +1 -0
  38. package/spec/javascripts/unit/core/http/http_socket_spec.js +1 -0
  39. package/spec/javascripts/unit/core/logger_spec.js +21 -20
  40. package/spec/javascripts/unit/core/pusher_spec.js +67 -39
  41. package/spec/javascripts/unit/core/pusher_with_encryption_spec.js +2 -0
  42. package/spec/javascripts/unit/core/strategies/cached_strategy_spec.js +1 -0
  43. package/spec/javascripts/unit/core/strategies/delayed_strategy_spec.js +1 -0
  44. package/spec/javascripts/unit/core/strategies/sequential_strategy_spec.js +1 -0
  45. package/spec/javascripts/unit/core/strategies/transport_strategy_spec.js +1 -0
  46. package/spec/javascripts/unit/core/transports/assistant_to_the_transport_manager_spec.js +1 -0
  47. package/spec/javascripts/unit/core/user_spec.js +295 -0
  48. package/spec/javascripts/unit/core/utils/periodic_timer_spec.js +4 -1
  49. package/spec/javascripts/unit/core/utils/timers_spec.js +6 -0
  50. package/spec/javascripts/unit/core/utils/url_store_spec.js +1 -1
  51. package/spec/javascripts/unit/core_with_runtime/auth/channel_authorizer_spec.js +55 -0
  52. package/spec/javascripts/unit/core_with_runtime/auth/deprecated_channel_authorizer_spec.js +48 -0
  53. package/spec/javascripts/unit/core_with_runtime/auth/user_authorizer_spec.js +52 -0
  54. package/spec/javascripts/unit/core_with_runtime/readme.md +5 -0
  55. package/spec/javascripts/unit/index.node.js +3 -0
  56. package/spec/javascripts/unit/index.web.js +3 -0
  57. package/spec/javascripts/unit/index.worker.js +3 -0
  58. package/spec/javascripts/unit/web/pusher_authorizer_spec.js +15 -16
  59. package/spec/javascripts/unit/web/transports/hosts_and_ports_spec.js +1 -0
  60. package/spec/javascripts/unit/worker/channel_authorizer_spec.js +110 -0
  61. package/src/core/auth/auth_transports.ts +8 -1
  62. package/src/core/auth/channel_authorizer.ts +53 -0
  63. package/src/core/auth/deprecated_channel_authorizer.ts +58 -0
  64. package/src/core/auth/options.ts +52 -17
  65. package/src/core/auth/user_authenticator.ts +51 -0
  66. package/src/core/channels/channel.ts +17 -3
  67. package/src/core/channels/channels.ts +4 -0
  68. package/src/core/channels/encrypted_channel.ts +26 -20
  69. package/src/core/channels/presence_channel.ts +5 -2
  70. package/src/core/channels/private_channel.ts +9 -4
  71. package/src/core/config.ts +76 -11
  72. package/src/core/defaults.ts +15 -0
  73. package/src/core/errors.ts +9 -0
  74. package/src/core/options.ts +18 -5
  75. package/src/core/pusher.ts +9 -1
  76. package/src/core/user.ts +143 -0
  77. package/src/core/utils/factory.ts +1 -10
  78. package/src/core/utils/url_store.ts +4 -1
  79. package/src/runtimes/isomorphic/auth/xhr_auth.ts +32 -19
  80. package/src/runtimes/web/auth/jsonp_auth.ts +13 -7
  81. package/src/runtimes/worker/auth/fetch_auth.ts +17 -12
  82. package/types/src/core/auth/auth_transports.d.ts +2 -1
  83. package/types/src/core/auth/channel_authorizer.d.ts +3 -0
  84. package/types/src/core/auth/deprecated_channel_authorizer.d.ts +18 -0
  85. package/types/src/core/auth/options.d.ts +34 -15
  86. package/types/src/core/auth/user_authenticator.d.ts +3 -0
  87. package/types/src/core/channels/channel.d.ts +4 -2
  88. package/types/src/core/channels/encrypted_channel.d.ts +2 -2
  89. package/types/src/core/channels/private_channel.d.ts +2 -2
  90. package/types/src/core/config.d.ts +4 -6
  91. package/types/src/core/defaults.d.ts +3 -0
  92. package/types/src/core/errors.d.ts +3 -0
  93. package/types/src/core/options.d.ts +6 -3
  94. package/types/src/core/pusher.d.ts +3 -0
  95. package/types/src/core/user.d.ts +15 -0
  96. package/types/src/core/utils/factory.d.ts +0 -2
  97. package/types/src/runtimes/isomorphic/auth/xhr_auth.d.ts +1 -1
  98. package/worker/with-encryption/index.js +1 -1
  99. package/spec/javascripts/unit/core/pusher_authorizer_spec.js +0 -160
  100. package/spec/javascripts/unit/worker/pusher_authorizer_spec.js +0 -111
  101. package/src/core/auth/pusher_authorizer.ts +0 -64
  102. package/types/index.d.ts +0 -15
  103. package/types/src/core/auth/pusher_authorizer.d.ts +0 -13
  104. package/types/src/core/index.d.ts +0 -6
  105. package/types/src/runtimes/react-native/tweetnacl-dummy.d.ts +0 -5
  106. package/types/src/runtimes/react-native/tweetnacl-util-dummy.d.ts +0 -7
@@ -5,6 +5,7 @@ var Util = require('core/util').default;
5
5
 
6
6
  describe("CachedStrategy", function() {
7
7
  beforeEach(function() {
8
+ jasmine.clock().uninstall();
8
9
  jasmine.clock().install();
9
10
  });
10
11
 
@@ -7,6 +7,7 @@ describe("DelayedStrategy", function() {
7
7
  this.strategy = new DelayedStrategy(this.substrategy, { delay: 0 });
8
8
  this.callback = jasmine.createSpy();
9
9
 
10
+ jasmine.clock().uninstall();
10
11
  jasmine.clock().install();
11
12
  });
12
13
 
@@ -7,6 +7,7 @@ describe("SequentialStrategy", function() {
7
7
  this.substrategies = Mocks.getStrategies([true, true]);
8
8
  this.strategy = new SequentialStrategy(this.substrategies, {});
9
9
 
10
+ jasmine.clock().uninstall();
10
11
  jasmine.clock().install();
11
12
  });
12
13
 
@@ -43,6 +43,7 @@ describe("TransportStrategy", function() {
43
43
 
44
44
  describe("#connect", function() {
45
45
  beforeEach(() => {
46
+ jasmine.clock().uninstall();
46
47
  jasmine.clock().install();
47
48
  });
48
49
 
@@ -7,6 +7,7 @@ describe("AssistantToTheTransportManager", function() {
7
7
  var manager;
8
8
 
9
9
  beforeEach(function() {
10
+ jasmine.clock().uninstall();
10
11
  jasmine.clock().install();
11
12
  spyOn(Util, "now").and.returnValue(1);
12
13
 
@@ -0,0 +1,295 @@
1
+ var TestEnv = require("testenv");
2
+ var Util = require("core/util").default;
3
+ var Collections = require("core/utils/collections");
4
+ var Logger = require("core/logger").default;
5
+ var Defaults = require("core/defaults").default;
6
+ var DefaultConfig = require("core/config");
7
+ var TimelineSender = require("core/timeline/timeline_sender").default;
8
+ var Pusher = require("core/pusher").default;
9
+ var Mocks = require("../../helpers/mocks");
10
+ var Factory = require("core/utils/factory").default;
11
+ var Runtime = require("runtime").default;
12
+ const transports = Runtime.Transports;
13
+ const Network = require("net_info").Network;
14
+ const waitsFor = require("../../helpers/waitsFor");
15
+ var NetInfo = require("net_info").NetInfo;
16
+
17
+ describe("Pusher (User)", function () {
18
+
19
+ describe("#signin", function () {
20
+ var pusher;
21
+ beforeEach(function () {
22
+ pusher = new Pusher("foo");
23
+ spyOn(pusher.config, "userAuthenticator");
24
+ spyOn(pusher, "send_event");
25
+ pusher.connection.state = "connected";
26
+ pusher.connection.socket_id = "1.23";
27
+ });
28
+
29
+ it("should not call userAuthenticator if the connection is not connected", function () {
30
+ pusher.connection.state = "connecting";
31
+ pusher.signin();
32
+ expect(pusher.config.userAuthenticator).not.toHaveBeenCalled();
33
+ });
34
+
35
+
36
+ it("should fail if userAuthenticator fails", function () {
37
+ pusher.config.userAuthenticator.and.callFake(function (params, callback) {
38
+ callback("this error", {});
39
+ });
40
+ spyOn(Logger, "warn");
41
+ pusher.signin();
42
+ expect(pusher.config.userAuthenticator).toHaveBeenCalledWith(
43
+ { socketId: "1.23" },
44
+ jasmine.any(Function)
45
+ );
46
+ expect(Logger.warn).toHaveBeenCalledWith(
47
+ "Error during signin: this error"
48
+ );
49
+ });
50
+
51
+ it("should send pusher:signin event", function () {
52
+ pusher.config.userAuthenticator.and.callFake(function (params, callback) {
53
+ callback(null, {
54
+ auth: "auth",
55
+ user_data: JSON.stringify({ id: "1" }),
56
+ foo: "bar"
57
+ });
58
+ });
59
+ spyOn(Logger, "warn");
60
+ pusher.signin();
61
+ expect(pusher.config.userAuthenticator).toHaveBeenCalledWith(
62
+ { socketId: "1.23" },
63
+ jasmine.any(Function)
64
+ );
65
+ expect(pusher.send_event).toHaveBeenCalledWith("pusher:signin", {
66
+ auth: "auth",
67
+ user_data: JSON.stringify({ id: "1" })
68
+ });
69
+ });
70
+
71
+ it("should signin when the connection becomes connected", function () {
72
+ pusher.connection.state = "connecting";
73
+ pusher.signin();
74
+ expect(pusher.config.userAuthenticator).not.toHaveBeenCalled();
75
+
76
+ pusher.config.userAuthenticator.and.callFake(function (params, callback) {
77
+ callback(null, {
78
+ auth: "auth",
79
+ user_data: JSON.stringify({ id: "1" }),
80
+ foo: "bar"
81
+ });
82
+ });
83
+
84
+ pusher.connection.state = "connected";
85
+ pusher.connection.emit('connected');
86
+
87
+ expect(pusher.config.userAuthenticator).toHaveBeenCalledWith(
88
+ { socketId: "1.23" },
89
+ jasmine.any(Function)
90
+ );
91
+ expect(pusher.send_event).toHaveBeenCalledWith("pusher:signin", {
92
+ auth: "auth",
93
+ user_data: JSON.stringify({ id: "1" })
94
+ });
95
+ });
96
+
97
+ it("should re-signin when the connection reconnects!", function () {
98
+ pusher.config.userAuthenticator.and.callFake(function (params, callback) {
99
+ callback(null, {
100
+ auth: "auth",
101
+ user_data: JSON.stringify({ id: "1" }),
102
+ foo: "bar"
103
+ });
104
+ });
105
+
106
+ pusher.signin();
107
+ expect(pusher.config.userAuthenticator).toHaveBeenCalledWith(
108
+ { socketId: "1.23" },
109
+ jasmine.any(Function)
110
+ );
111
+ expect(pusher.send_event).toHaveBeenCalledWith("pusher:signin", {
112
+ auth: "auth",
113
+ user_data: JSON.stringify({ id: "1" })
114
+ });
115
+ pusher.send_event.calls.reset()
116
+ pusher.config.userAuthenticator.calls.reset()
117
+
118
+ pusher.connection.state == "disconnected";
119
+ pusher.connection.emit("disconnected");
120
+ pusher.connection.state == "connecting";
121
+ pusher.connection.emit("connecting");
122
+ pusher.connection.state == "connected";
123
+ pusher.connection.emit("connected");
124
+
125
+ expect(pusher.config.userAuthenticator).toHaveBeenCalledWith(
126
+ { socketId: "1.23" },
127
+ jasmine.any(Function)
128
+ );
129
+ expect(pusher.send_event).toHaveBeenCalledWith("pusher:signin", {
130
+ auth: "auth",
131
+ user_data: JSON.stringify({ id: "1" })
132
+ });
133
+ });
134
+
135
+ it("should not signin when the connection is connected if signin() was never called", function () {
136
+ pusher.connection.state = "connected";
137
+ pusher.connection.emit('connected');
138
+ expect(pusher.config.userAuthenticator).not.toHaveBeenCalled();
139
+ })
140
+
141
+ });
142
+
143
+
144
+ describe('pusher:signin_success', function () {
145
+ var pusher;
146
+ var transport;
147
+
148
+ beforeEach(async function () {
149
+ spyOn(Network, 'isOnline').and.returnValue(true);
150
+ spyOn(Runtime, 'getLocalStorage').and.returnValue({});
151
+
152
+ var Transports = Runtime.Transports;
153
+ function createConnection() {
154
+ transport = Mocks.getWorkingTransport();
155
+ return transport;
156
+ }
157
+ spyOn(Transports.xhr_polling, 'createConnection').and.callFake(
158
+ createConnection
159
+ );
160
+ spyOn(Transports.xhr_polling, 'isSupported').and.returnValue(true);
161
+ pusher = new Pusher('foobar', {
162
+ enabledTransports: ['xhr_polling']
163
+ });
164
+ pusher.connect();
165
+ await waitsFor(
166
+ function () {
167
+ return pusher.connection.state === 'connected';
168
+ },
169
+ 'pusher.connection.state to be connected',
170
+ 500
171
+ );
172
+ });
173
+
174
+ it('should process pusher:signin_success', async function () {
175
+ transport.emit('message', {
176
+ data: JSON.stringify({
177
+ event: 'pusher:signin_success',
178
+ data: {
179
+ user_data: JSON.stringify({ id: '1', name: 'test' })
180
+ }
181
+ })
182
+ });
183
+
184
+ expect(pusher.user.user_data).toEqual({ id: '1', name: 'test' });
185
+ expect(pusher.user.serverToUserChannel.subscriptionPending).toBe(true);
186
+ });
187
+
188
+ it('should log warning if user_data is not JSON', async function () {
189
+ spyOn(Logger, 'error');
190
+ transport.emit('message', {
191
+ data: JSON.stringify({
192
+ event: 'pusher:signin_success',
193
+ data: {
194
+ user_data: "I'm not JSON"
195
+ }
196
+ })
197
+ });
198
+ expect(Logger.error).toHaveBeenCalledWith(
199
+ "Failed parsing user data after signin: I'm not JSON"
200
+ );
201
+ expect(pusher.user.user_data).toEqual(null);
202
+ });
203
+
204
+ it('should bind to servetToUser channel events after sign in', async function () {
205
+ const fooCallback = jasmine.createSpy('fooCallback');
206
+ const barCallback = jasmine.createSpy('barCallback');
207
+ pusher.user.bind('foo', fooCallback);
208
+ pusher.user.bind('bar', barCallback);
209
+
210
+ // Send events on channel without being signed in
211
+ transport.emit('message', {
212
+ data: JSON.stringify({
213
+ channel: '#server-to-user-1',
214
+ event: 'foo',
215
+ data: { 'something': 'another' }
216
+ })
217
+ });
218
+
219
+ expect(fooCallback).not.toHaveBeenCalled();
220
+ expect(barCallback).not.toHaveBeenCalled();
221
+
222
+ // Sign in successfully
223
+ transport.emit('message', {
224
+ data: JSON.stringify({
225
+ event: 'pusher:signin_success',
226
+ data: {
227
+ user_data: JSON.stringify({ id: '1', name: 'test' })
228
+ }
229
+ })
230
+ });
231
+ transport.emit('message', {
232
+ data: JSON.stringify({
233
+ channel: '#server-to-user-1',
234
+ event: 'pusher_internal:subscription_succeeded',
235
+ data: {}
236
+ })
237
+ });
238
+ await waitsFor(
239
+ function () {
240
+ return pusher.user.serverToUserChannel.subscribed === true;
241
+ },
242
+ 'pusher.user.serverToUserChannel.subscribed to be true',
243
+ 500
244
+ );
245
+
246
+ // Send events on channel
247
+ transport.emit('message', {
248
+ data: JSON.stringify({
249
+ channel: '#server-to-user-1',
250
+ event: 'foo',
251
+ data: { 'something': 'another' }
252
+ })
253
+ });
254
+
255
+ expect(fooCallback).toHaveBeenCalledWith({ 'something': 'another' });
256
+ expect(barCallback).not.toHaveBeenCalled();
257
+ });
258
+
259
+
260
+ it('should cleanup the signed in state when disconnected', async function () {
261
+ // Sign in successfully
262
+ transport.emit('message', {
263
+ data: JSON.stringify({
264
+ event: 'pusher:signin_success',
265
+ data: {
266
+ user_data: JSON.stringify({ id: '1', name: 'test' })
267
+ }
268
+ })
269
+ });
270
+ transport.emit('message', {
271
+ data: JSON.stringify({
272
+ channel: '#server-to-user-1',
273
+ event: 'pusher_internal:subscription_succeeded',
274
+ data: {}
275
+ })
276
+ });
277
+ await waitsFor(
278
+ function () {
279
+ return pusher.user.serverToUserChannel.subscribed === true;
280
+ },
281
+ 'pusher.user.serverToUserChannel.subscribed to be true',
282
+ 500
283
+ );
284
+ expect(pusher.user.user_data).toEqual({ id: '1', name: 'test' });
285
+ expect(pusher.user.serverToUserChannel.subscribed).toBe(true);
286
+
287
+ // Disconnect
288
+ pusher.connection.emit('disconnected');
289
+
290
+ expect(pusher.user.user_data).toEqual(null);
291
+ expect(pusher.user.serverToUserChannel).toEqual(null);
292
+ });
293
+ });
294
+
295
+ });
@@ -6,6 +6,7 @@ describe("PeriodicTimer", function() {
6
6
  var timer;
7
7
 
8
8
  beforeEach(function() {
9
+ jasmine.clock().uninstall();
9
10
  jasmine.clock().install();
10
11
 
11
12
  callback = jasmine.createSpy("callback");
@@ -62,10 +63,12 @@ describe("PeriodicTimer", function() {
62
63
 
63
64
  it("should stop callback from being called even if clearInterval is broken", function() {
64
65
  // IE has some edge-case with clearInterval not working, let's simulate it
65
- spyOn(global, "clearInterval");
66
+ let _clearInterval = global.clearInterval;
67
+ spyOn(global, "clearInterval").and.callThrough();
66
68
  timer.ensureAborted();
67
69
  jasmine.clock().tick(1000);
68
70
  expect(callback).not.toHaveBeenCalled();
71
+ global.clearInterval = _clearInterval;
69
72
  });
70
73
  });
71
74
  });
@@ -19,6 +19,7 @@ describe("timers", function() {
19
19
 
20
20
  afterEach(function() {
21
21
  timer.ensureAborted();
22
+ jasmine.clock().uninstall();
22
23
  });
23
24
 
24
25
  it("should execute the callback with the specified delay", function() {
@@ -65,10 +66,12 @@ describe("timers", function() {
65
66
 
66
67
  it("should stop callback from being called even if clearTimeout is broken", function() {
67
68
  // IE has some edge-case with clearTimeout not working, let's simulate it
69
+ let _clearTimeout = global.clearTimeout;
68
70
  spyOn(global, "clearTimeout");
69
71
  timer.ensureAborted();
70
72
  jasmine.clock().tick(1000);
71
73
  expect(callback).not.toHaveBeenCalled();
74
+ global.clearTimeout = _clearTimeout;
72
75
  });
73
76
  });
74
77
  });
@@ -87,6 +90,7 @@ describe("timers", function() {
87
90
 
88
91
  afterEach(function() {
89
92
  timer.ensureAborted();
93
+ jasmine.clock().uninstall();
90
94
  });
91
95
 
92
96
  it("should execute the callback with the specified delay", function() {
@@ -145,10 +149,12 @@ describe("timers", function() {
145
149
 
146
150
  it("should stop callback from being called even if clearTimeout is broken", function() {
147
151
  // IE has some edge-case with clearTimeout not working, let's simulate it
152
+ let _clearTimeout = global.clearTimeout;
148
153
  spyOn(global, "clearTimeout");
149
154
  timer.ensureAborted();
150
155
  jasmine.clock().tick(1000);
151
156
  expect(callback).not.toHaveBeenCalled();
157
+ global.clearTimeout = _clearTimeout;
152
158
  });
153
159
  });
154
160
  });
@@ -4,7 +4,7 @@ describe('url_store', function(){
4
4
  describe('buildLogSuffix', function(){
5
5
  it('should build a log suffix for known keys', function(){
6
6
  var suffix = url_store.buildLogSuffix('authenticationEndpoint');
7
- expect(suffix).toEqual('See: https://pusher.com/docs/authenticating_users');
7
+ expect(suffix).toEqual('See: https://pusher.com/docs/channels/server_api/authenticating_users');
8
8
  });
9
9
  it('should return a blank suffix for unknown keys', function(){
10
10
  var suffix = url_store.buildLogSuffix('somethingUnknown');
@@ -0,0 +1,55 @@
1
+ var Runtime = require('runtime').default;
2
+ var ChannelAuthorizer = require('core/auth/channel_authorizer').default;
3
+
4
+ describe("ChannelAuthorizer", function() {
5
+
6
+ describe("initialization", function(){
7
+ it("should throw an error if the specified transport is unrecognized", function(){
8
+ const authOptions = {
9
+ transport: "bad-transport",
10
+ };
11
+ expect(function(){
12
+ ChannelAuthorizer(authOptions)
13
+ }).toThrow("'bad-transport' is not a recognized auth transport");
14
+ });
15
+ });
16
+
17
+ describe("channel AuthHandler", function(){
18
+ let _getAuthorizers;
19
+
20
+ beforeAll(function() {
21
+ _getAuthorizers = Runtime.getAuthorizers;
22
+ });
23
+
24
+ afterAll(function() {
25
+ Runtime.getAuthorizers = _getAuthorizers;
26
+ });
27
+
28
+ it("should call the specified transport authorizer", function(){
29
+ const authOptions = {
30
+ transport: "ajax",
31
+ endpoint: "http://example.com/auth",
32
+ params: { foo: "bar" },
33
+ headers: { "X-Foo": "my-bar" }
34
+ };
35
+ channelAuthorizer = ChannelAuthorizer(authOptions);
36
+
37
+ transportAuthorizer = jasmine.createSpy("ajax")
38
+ Runtime.getAuthorizers = jasmine.createSpy("getAuthorizers").and.returnValue({
39
+ ajax: transportAuthorizer
40
+ });
41
+
42
+ const params = { socketId: '1.23', channelName: 'private-test' };
43
+ const callback = function(){};
44
+ const query = 'socket_id=1.23&channel_name=private-test&foo=bar';
45
+ channelAuthorizer(params, callback);
46
+ expect(Runtime.getAuthorizers.calls.count()).toEqual(1);
47
+ expect(transportAuthorizer).toHaveBeenCalledWith(
48
+ Runtime,
49
+ query,
50
+ authOptions,
51
+ 'channel-authorization',
52
+ callback);
53
+ });
54
+ });
55
+ });
@@ -0,0 +1,48 @@
1
+ var ChannelAuthorizerProxy = require('core/auth/deprecated_channel_authorizer').ChannelAuthorizerProxy;
2
+
3
+ describe("ChannelAuthorizerProxy", function() {
4
+
5
+ describe("initialization", function(){
6
+ it("should call the deprecated authorizer properly", function(){
7
+ const channel = {};
8
+ const pusher = {
9
+ channel: jasmine.createSpy('channel').and.returnValue(channel)
10
+ }
11
+
12
+ const authOptions = {
13
+ transport: "bad-transport",
14
+ endpoint: "http://example.com/auth",
15
+ params: { foo: "bar" },
16
+ headers: { "X-Foo": "my-bar" }
17
+ };
18
+ const deprecatedAuthorizerParams = {
19
+ authTransport: "bad-transport",
20
+ authEndpoint: "http://example.com/auth",
21
+ auth: {
22
+ params: { foo: "bar" },
23
+ headers: { "X-Foo": "my-bar" }
24
+ }
25
+ }
26
+ const deprecatedAuthorizer = {
27
+ authorize:jasmine.createSpy('deprecatedAuthorizer')
28
+ };
29
+ const deprecatedAuthorizerGenerator = jasmine.createSpy("deprecatedAuthorizerGenerator").and.returnValue(deprecatedAuthorizer);
30
+
31
+ channelAuthorizer = ChannelAuthorizerProxy(pusher, authOptions, deprecatedAuthorizerGenerator)
32
+
33
+ const params = { socketId: "1.23", channelName: "private-test-channel"};
34
+ const callback = function() {};
35
+ channelAuthorizer(params, callback);
36
+
37
+ expect(pusher.channel.calls.count()).toEqual(1);
38
+ expect(pusher.channel).toHaveBeenCalledWith('private-test-channel');
39
+
40
+ expect(deprecatedAuthorizerGenerator.calls.count()).toEqual(1);
41
+ expect(deprecatedAuthorizerGenerator).toHaveBeenCalledWith(channel, deprecatedAuthorizerParams);
42
+
43
+ expect(deprecatedAuthorizer.authorize.calls.count()).toEqual(1);
44
+ expect(deprecatedAuthorizer.authorize).toHaveBeenCalledWith("1.23", callback);
45
+ });
46
+ });
47
+
48
+ });
@@ -0,0 +1,52 @@
1
+ var Runtime = require('runtime').default;
2
+ var UserAuthenticator = require('core/auth/user_authenticator').default;
3
+
4
+ describe("UserAuthenticator", function() {
5
+
6
+ describe("initialization", function(){
7
+ it("should throw an error if the specified transport is unrecognized", function(){
8
+ const userAuthentication = {
9
+ transport: "bad-transport",
10
+ };
11
+ expect(function(){
12
+ UserAuthenticator(userAuthentication)
13
+ }).toThrow("'bad-transport' is not a recognized auth transport");
14
+ });
15
+ });
16
+
17
+ describe("user AuthHandler", function(){
18
+ let _getAuthorizers;
19
+
20
+ beforeAll(function() {
21
+ _getAuthorizers = Runtime.getAuthorizers;
22
+ });
23
+
24
+ afterAll(function() {
25
+ Runtime.getAuthorizers = _getAuthorizers;
26
+ });
27
+
28
+ it("should call the specified transport authorizer", function(){
29
+ const userAuthentication = {
30
+ transport: "ajax",
31
+ };
32
+ userAuthenticator = UserAuthenticator(userAuthentication);
33
+
34
+ transportAuthorizer = jasmine.createSpy("ajax")
35
+ Runtime.getAuthorizers = jasmine.createSpy("getAuthorizers").and.returnValue({
36
+ ajax: transportAuthorizer
37
+ });
38
+
39
+ const params = { socketId: '1.23' };
40
+ const callback = function(){};
41
+ const query = 'socket_id=1.23';
42
+ userAuthenticator(params, callback);
43
+ expect(Runtime.getAuthorizers.calls.count()).toEqual(1);
44
+ expect(transportAuthorizer).toHaveBeenCalledWith(
45
+ Runtime,
46
+ query,
47
+ userAuthentication,
48
+ 'user-authentication',
49
+ callback);
50
+ });
51
+ });
52
+ });
@@ -0,0 +1,5 @@
1
+ # core with runtime
2
+
3
+ This directory contains unit tests for core features that requires access to a `Runtime`. These tests will be included in the unit tests for each runtime and the proper runtime will be loaded before calling those tests.
4
+
5
+ In order for this to work, this directory `core_with_runtime` need to be included in `unit/index.node.js` and similar files after the proper runtime directory is included (ex. `unit/node`)
@@ -6,3 +6,6 @@ nodeTestsContext.keys().forEach(nodeTestsContext);
6
6
 
7
7
  var nodeTestsContext = require.context("./node", true, /_spec$/);
8
8
  nodeTestsContext.keys().forEach(nodeTestsContext)
9
+
10
+ var coreWithRuntimeTestsContext = require.context("./core_with_runtime", true, /_spec$/);
11
+ coreWithRuntimeTestsContext.keys().forEach(coreWithRuntimeTestsContext)
@@ -7,3 +7,6 @@ sharedTestsContext.keys().forEach(sharedTestsContext);
7
7
 
8
8
  var nodeTestsContext = require.context("./web", true, /_spec$/);
9
9
  nodeTestsContext.keys().forEach(nodeTestsContext);
10
+
11
+ var coreWithRuntimeTestsContext = require.context("./core_with_runtime", true, /_spec$/);
12
+ coreWithRuntimeTestsContext.keys().forEach(coreWithRuntimeTestsContext)
@@ -6,3 +6,6 @@ nodeTestsContext.keys().forEach(nodeTestsContext);
6
6
 
7
7
  var nodeTestsContext = require.context("./worker", true, /_spec$/);
8
8
  nodeTestsContext.keys().forEach(nodeTestsContext);
9
+
10
+ var coreWithRuntimeTestsContext = require.context("./core_with_runtime", true, /_spec$/);
11
+ coreWithRuntimeTestsContext.keys().forEach(coreWithRuntimeTestsContext)
@@ -1,4 +1,4 @@
1
- var Authorizer = require('core/auth/pusher_authorizer').default;
1
+ var ChannelAuthorizer = require('core/auth/channel_authorizer').default;
2
2
  var Logger = require('core/logger');
3
3
  var Mocks = require('mocks');
4
4
  var Util = require('core/util').default;
@@ -6,17 +6,13 @@ var Factory = require('core/utils/factory').default;
6
6
  var Logger = require('core/logger').default;
7
7
  var Runtime = require('runtime').default;
8
8
 
9
- describe("JSONP Authorizer", function() {
10
- it("should raise a warning if headers are passed", function() {
11
- var headers = { "foo": "bar", "n": 42 };
12
- var authorizer = new Authorizer(
13
- { name: "chan" },
14
- { authTransport: "jsonp",
15
- auth: {
16
- headers: headers
17
- }
18
- }
19
- );
9
+ describe('JSONP Authorizer', function() {
10
+ it('should raise a warning if headers are passed', function() {
11
+ var headers = { foo: 'bar', n: 42 };
12
+ var channelAuthorizer = ChannelAuthorizer({
13
+ transport: 'jsonp',
14
+ headers: headers
15
+ })
20
16
 
21
17
  var document = Mocks.getDocument();
22
18
  var script = Mocks.getDocumentElement();
@@ -25,13 +21,16 @@ describe("JSONP Authorizer", function() {
25
21
  document.createElement.and.returnValue(script);
26
22
  document.getElementsByTagName.and.returnValue([]);
27
23
  document.documentElement = documentElement;
28
- spyOn(Runtime, "getDocument").and.returnValue(document);
24
+ spyOn(Runtime, 'getDocument').and.returnValue(document);
29
25
 
30
- spyOn(Logger, "warn");
31
- authorizer.authorize("1.23", function() {});
26
+ spyOn(Logger, 'warn');
27
+ channelAuthorizer({
28
+ socketId: '1.23',
29
+ channelName: 'chan',
30
+ }, function() {})
32
31
 
33
32
  expect(Logger.warn).toHaveBeenCalledWith(
34
- "To send headers with the auth request, you must use AJAX, rather than JSONP."
33
+ 'To send headers with the channel-authorization request, you must use AJAX, rather than JSONP.'
35
34
  );
36
35
  });
37
36
  });
@@ -31,6 +31,7 @@ describe("Host/Port Configuration", function() {
31
31
 
32
32
  afterEach(function() {
33
33
  pusher.disconnect();
34
+ jasmine.clock().uninstall();
34
35
  });
35
36
 
36
37
  describe("SockJS", function() {