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
package/index.d.ts CHANGED
@@ -1,8 +1,13 @@
1
1
  export {
2
- Authorizer,
2
+ DeprecatedAuthOptions,
3
+ ChannelAuthorizerGenerator,
4
+ } from './types/src/core/auth/deprecated_channel_authorizer';
5
+ export {
3
6
  AuthOptions,
4
- AuthorizerGenerator,
5
- AuthorizerCallback,
7
+ ChannelAuthorizationHandler,
8
+ UserAuthenticationHandler,
9
+ ChannelAuthorizationCallback,
10
+ UserAuthenticationCallback,
6
11
  } from './types/src/core/auth/options';
7
12
  export { Options } from './types/src/core/options'
8
13
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pusher-js",
3
- "version": "7.0.6",
3
+ "version": "7.2.0",
4
4
  "description": "Pusher Channels JavaScript library for browsers, React Native, NodeJS and web workers",
5
5
  "main": "dist/node/pusher.js",
6
6
  "browser": "dist/web/pusher.js",
@@ -45,7 +45,7 @@
45
45
  "karma-chrome-launcher": "^3.1.0",
46
46
  "karma-firefox-launcher": "^2.1.1",
47
47
  "karma-jasmine": "^4.0.1",
48
- "karma-jasmine-web-worker": "^0.1.2",
48
+ "karma-jasmine-web-worker": "git+https://git@github.com/pusher/karma-jasmine-web-worker.git#jasmine_3",
49
49
  "karma-opera-launcher": "^1.0.0",
50
50
  "karma-safari-launcher": "^1.0.0",
51
51
  "karma-spec-reporter": "0.0.32",
@@ -26,6 +26,9 @@ module.exports = function(config, suite) {
26
26
  'src/runtimes',
27
27
  'spec/javascripts/helpers'
28
28
  ]
29
+ config.webpack.resolve.alias = {
30
+ 'dom/dependencies': 'worker/mock-dom-dependencies',
31
+ }
29
32
  config.webpack.externals.testenv = "'worker'";
30
33
  return config;
31
34
  }
@@ -10,8 +10,10 @@ if (process.env.CI) {
10
10
  if (process.env.WORKER === 'true') {
11
11
  config = require('./config.worker')(config, 'integration');
12
12
  config.webpack.resolve.alias = {
13
- pusher_integration: 'core/pusher',
14
- integration: 'node/integration'
13
+ pusher_integration: 'core/pusher.js',
14
+ integration: 'node/integration',
15
+ 'dom/dependencies': 'worker/mock-dom-dependencies',
16
+ 'dom/dependency_loader': 'worker/mock-dom-dependencies',
15
17
  };
16
18
  if (process.env.CI) config.browsers = ['bs_chrome_74'];
17
19
  }
@@ -112,6 +112,47 @@ var Mocks = {
112
112
  return transport;
113
113
  },
114
114
 
115
+ getWorkingTransport: function() {
116
+ var transport = new EventsDispatcher();
117
+
118
+ transport.handlesActivityChecks = jasmine
119
+ .createSpy('handlesActivityChecks')
120
+ .and.returnValue(false);
121
+ transport.supportsPing = jasmine
122
+ .createSpy('supportsPing')
123
+ .and.returnValue(false);
124
+ transport.initialize = jasmine
125
+ .createSpy('initialize')
126
+ .and.callFake(function() {
127
+ transport.state = 'initializing';
128
+ transport.emit('initializing');
129
+ setTimeout(() => {
130
+ transport.state = 'initialized';
131
+ transport.emit('initialized');
132
+ }, 0);
133
+ });
134
+ transport.connect = jasmine.createSpy('connect').and.callFake(function() {
135
+ transport.state = 'open';
136
+ transport.emit('open');
137
+ setTimeout(() => {
138
+ transport.emit('message', {
139
+ data: JSON.stringify({
140
+ event: 'pusher:connection_established',
141
+ data: {
142
+ socket_id: '123.456',
143
+ activity_timeout: 120
144
+ }
145
+ })
146
+ });
147
+ }, 0);
148
+ });
149
+ transport.send = jasmine.createSpy('send').and.returnValue(true);
150
+ transport.ping = jasmine.createSpy('ping');
151
+ transport.close = jasmine.createSpy('close');
152
+ transport.state = undefined;
153
+ return transport;
154
+ },
155
+
115
156
  getTransportManager: function(alive) {
116
157
  return {
117
158
  isAlive: jasmine.createSpy("isAlive").and.returnValue(alive !== false),
@@ -228,14 +269,6 @@ var Mocks = {
228
269
  return channel;
229
270
  },
230
271
 
231
- getAuthorizer: function() {
232
- var authorizer = {};
233
- authorizer._callback = null;
234
- authorizer.authorize = jasmine.createSpy("authorize").and.callFake(function(_, callback) {
235
- authorizer._callback = callback;
236
- });
237
- return authorizer;
238
- }
239
272
  };
240
273
 
241
274
  module.exports = Mocks;
@@ -0,0 +1 @@
1
+ exports.Dependencies = {}
@@ -141,5 +141,13 @@ module.exports = function(testConfigs) {
141
141
  for (testConfig of testConfigs) {
142
142
  describeClusterTest(testConfig)
143
143
  }
144
+
145
+
146
+ it("should restore the global config", function() {
147
+ Dependencies = _Dependencies;
148
+ Defaults.authEndpoint = _authEndpoint;
149
+ Defaults.authTransport = _authTransport;
150
+ Defaults.VERSION = _VERSION;
151
+ });
144
152
  });
145
153
  }
@@ -23,6 +23,7 @@ module.exports = function() {
23
23
  var pusher;
24
24
 
25
25
  beforeEach(function() {
26
+ jasmine.clock().uninstall();
26
27
  jasmine.clock().install();
27
28
 
28
29
  spyOn(Network, "isOnline").and.returnValue(true);
@@ -1,2 +1,13 @@
1
1
  var sharedTestsContext = require.context("./core", true, /_spec$/);
2
- sharedTestsContext.keys().forEach(sharedTestsContext);
2
+ var testConfigs = [{
3
+ transport: "ws",
4
+ forceTLS: true,
5
+ },{
6
+ transport: "ws",
7
+ forceTLS: false,
8
+ }];
9
+
10
+
11
+ sharedTestsContext.keys().forEach((key) => {
12
+ sharedTestsContext(key)(testConfigs);
13
+ })
@@ -170,6 +170,31 @@ describe("Channel", function() {
170
170
  });
171
171
  });
172
172
 
173
+ describe('on pusher_internal:subscription_count', function() {
174
+ it('should emit pusher:subscription_count', function() {
175
+ const testEvent = {
176
+ event: 'pusher_internal:subscription_count',
177
+ data: { subscription_count: 101 }
178
+ };
179
+
180
+ var callback = jasmine.createSpy('callback');
181
+ channel.bind('pusher:subscription_count', callback);
182
+
183
+ channel.handleEvent(testEvent);
184
+
185
+ expect(callback).toHaveBeenCalledWith(testEvent.data);
186
+ });
187
+
188
+ it('should set #subscriptionCount', function() {
189
+ channel.handleEvent({
190
+ event: 'pusher_internal:subscription_count',
191
+ data: { subscription_count: 101 }
192
+ });
193
+
194
+ expect(channel.subscriptionCount).toEqual(101);
195
+ });
196
+ });
197
+
173
198
  describe("on other events", function() {
174
199
  it("should emit the event", function() {
175
200
  var callback = jasmine.createSpy("callback");
@@ -1,7 +1,6 @@
1
1
  const Errors = require("core/errors");
2
2
  const Logger = require('core/logger').default;
3
3
  const EncryptedChannel = require("core/channels/encrypted_channel").default;
4
- const Factory = require("core/utils/factory").default;
5
4
  const Mocks = require("mocks");
6
5
  const nacl = require("tweetnacl");
7
6
  const utf8 = require("@stablelib/utf8");
@@ -10,8 +9,7 @@ const base64 = require("@stablelib/base64");
10
9
  describe("EncryptedChannel", function() {
11
10
  var pusher;
12
11
  var channel;
13
- var authorizer;
14
- var factorySpy;
12
+ var channelAuthorizer;
15
13
  const secretUTF8 = "It Must Be Thirty Two Characters";
16
14
  const secretBytes = utf8.encode(secretUTF8);
17
15
  const secretBase64 = base64.encode(secretBytes);
@@ -25,10 +23,9 @@ describe("EncryptedChannel", function() {
25
23
  };
26
24
 
27
25
  beforeEach(function() {
28
- pusher = Mocks.getPusher({ foo: "bar" });
26
+ channelAuthorizer = jasmine.createSpy("channelAuthorizer").and.callFake(function(params, callback) {})
27
+ pusher = Mocks.getPusher({ channelAuthorizer: channelAuthorizer });
29
28
  channel = new EncryptedChannel("private-encrypted-test", pusher, nacl);
30
- authorizer = Mocks.getAuthorizer();
31
- factorySpy = spyOn(Factory, "createAuthorizer").and.returnValue(authorizer);
32
29
  });
33
30
 
34
31
  describe("after construction", function() {
@@ -46,36 +43,38 @@ describe("EncryptedChannel", function() {
46
43
  });
47
44
 
48
45
  describe("#authorize", function() {
49
- it("should create and call an authorizer", function() {
50
- channel.authorize("1.23", function() {});
51
- expect(Factory.createAuthorizer.calls.count()).toEqual(1);
52
- expect(Factory.createAuthorizer).toHaveBeenCalledWith(channel, {
53
- foo: "bar"
54
- });
46
+ it("should call channelAuthorizer", function() {
47
+ const callback = function(){}
48
+ channel.authorize("1.23", callback);
49
+ expect(channelAuthorizer.calls.count()).toEqual(1);
50
+ expect(channelAuthorizer).toHaveBeenCalledWith(
51
+ { socketId: "1.23", channelName: "private-encrypted-test" }, jasmine.any(Function));
55
52
  });
56
53
 
57
- it("should call back with only authorization data", function() {
58
- let callback = jasmine.createSpy("callback");
54
+ it("should call the callback if an authorizaiton error is encountered", function() {
55
+ const callback = jasmine.createSpy("callback")
59
56
  channel.authorize("1.23", callback);
60
- expect(callback).not.toHaveBeenCalled();
61
- authorizer._callback(false, {
62
- shared_secret: secretBase64,
63
- foo: "bar"
64
- });
65
- expect(callback).toHaveBeenCalledWith(null, { foo: "bar" });
57
+ expect(channelAuthorizer.calls.count()).toEqual(1);
58
+ expect(channelAuthorizer).toHaveBeenCalledWith(
59
+ { socketId: "1.23", channelName: "private-encrypted-test" }, jasmine.any(Function));
60
+ const encryptedChannelCallback = channelAuthorizer.calls.mostRecent().args[1];
61
+
62
+ encryptedChannelCallback("error", {})
63
+ expect(callback).toHaveBeenCalledWith("error", {})
66
64
  });
67
65
 
68
- it("should callback an error if no shared_secret included in auth data", function() {
69
- let callback = jasmine.createSpy("callback");
66
+ it("should fail if AuthData doens't have a shared_secret", function() {
67
+ const callback = jasmine.createSpy("callback")
70
68
  channel.authorize("1.23", callback);
71
- authorizer._callback(null, {
72
- foo: "bar"
73
- });
74
- // For some reason comparing the Error types doesn't work properly in
75
- // Safari on Mojave. Manually check the arguments.
69
+ expect(channelAuthorizer.calls.count()).toEqual(1);
70
+ expect(channelAuthorizer).toHaveBeenCalledWith(
71
+ { socketId: "1.23", channelName: "private-encrypted-test" }, jasmine.any(Function));
72
+ const encryptedChannelCallback = channelAuthorizer.calls.mostRecent().args[1];
73
+
74
+ encryptedChannelCallback(null, {})
75
+
76
76
  expect(callback.calls.count()).toEqual(1)
77
77
  let args = callback.calls.first().args;
78
-
79
78
  expect(args.length).toEqual(2)
80
79
  expect(args[0]).toEqual(jasmine.any(Error))
81
80
  expect(args[0].message).toEqual(
@@ -83,27 +82,22 @@ describe("EncryptedChannel", function() {
83
82
  );
84
83
  expect(args[1]).toEqual(null);
85
84
  });
86
-
87
- describe("with custom authorizer", function() {
88
- beforeEach(function() {
89
- pusher = Mocks.getPusher({
90
- authorizer: function(channel, options) {
91
- return authorizer;
92
- }
93
- });
94
- channel = new EncryptedChannel("private-test-custom-auth", pusher, nacl);
95
- factorySpy.and.callThrough();
96
- });
97
-
98
- it("should call the authorizer", function() {
99
- let callback = jasmine.createSpy("callback");
100
- channel.authorize("1.23", callback);
101
- authorizer._callback(false, {
102
- shared_secret: secretBase64,
103
- foo: "bar"
104
- });
105
- expect(callback).toHaveBeenCalledWith(null, { foo: "bar" });
106
- });
85
+
86
+ it("should succeed if AuthData has a shared_secret", function() {
87
+ const callback = jasmine.createSpy("callback")
88
+ channel.authorize("1.23", callback);
89
+ expect(channelAuthorizer.calls.count()).toEqual(1);
90
+ expect(channelAuthorizer).toHaveBeenCalledWith(
91
+ { socketId: "1.23", channelName: "private-encrypted-test" }, jasmine.any(Function));
92
+ const encryptedChannelCallback = channelAuthorizer.calls.mostRecent().args[1];
93
+
94
+ encryptedChannelCallback(null, {
95
+ shared_secret: secretBase64,
96
+ foo: 'bar',
97
+ })
98
+ expect(callback).toHaveBeenCalledWith(null, {
99
+ foo: 'bar',
100
+ })
107
101
  });
108
102
  });
109
103
 
@@ -213,14 +207,14 @@ describe("EncryptedChannel", function() {
213
207
 
214
208
  describe("on other events", function() {
215
209
  beforeEach(function() {
216
- // in order to decrypt encrypted events, we need to get a shared secret
217
- // from the authorizer.
218
- let callback = function() {};
219
- channel.authorize("1.23", callback);
220
- authorizer._callback(false, {
221
- shared_secret: secretBase64,
222
- foo: "bar"
210
+ channelAuthorizer.and.callFake(function(params, callback) {
211
+ callback(null, {
212
+ shared_secret: secretBase64,
213
+ foo: 'bar',
214
+ });
223
215
  });
216
+
217
+ channel.authorize("1.23", function (){});
224
218
  });
225
219
  it("should decrypt the event payload and emit the event", function() {
226
220
  let payload = { test: "payload" };
@@ -281,10 +275,20 @@ describe("EncryptedChannel", function() {
281
275
  };
282
276
 
283
277
  beforeEach(function() {
278
+ // Channel has already been authorized with the old secret
279
+ channel.authorize("1.23", function (){});
280
+
284
281
  pusher.connection = {
285
282
  socket_id: "9.37"
286
283
  };
287
- authorizer._callback = null
284
+
285
+ // Next call to authorize will use the new secret
286
+ channelAuthorizer.and.callFake(function(params, callback) {
287
+ callback(null, {
288
+ shared_secret: newSecretBase64,
289
+ foo: 'bar',
290
+ });
291
+ });
288
292
  });
289
293
 
290
294
  it("should request new key from authorizer and decrypt event", function() {
@@ -299,10 +303,6 @@ describe("EncryptedChannel", function() {
299
303
  event: "something",
300
304
  data: encryptedPayload
301
305
  });
302
- authorizer._callback(false, {
303
- shared_secret: newSecretBase64,
304
- foo: "bar"
305
- });
306
306
  expect(boundCallback).toHaveBeenCalledWith(payload);
307
307
  });
308
308
  it("should log a warning if it fails to decrypt event after requesting a new key from the auth endpoint", function() {
@@ -315,10 +315,6 @@ describe("EncryptedChannel", function() {
315
315
  event: "something",
316
316
  data: encryptedPayload
317
317
  });
318
- authorizer._callback(false, {
319
- shared_secret: newSecretBase64,
320
- foo: "bar"
321
- });
322
318
  expect(Logger.error).toHaveBeenCalledWith(
323
319
  "Failed to decrypt event with new key. Dropping encrypted event"
324
320
  );
@@ -330,11 +326,13 @@ describe("EncryptedChannel", function() {
330
326
  ciphertext: newTestEncrypt(payload)
331
327
  };
332
328
  spyOn(Logger, "error");
329
+ channelAuthorizer.and.callFake(function(params, callback) {
330
+ callback(true, "ERROR");
331
+ });
333
332
  channel.handleEvent({
334
333
  event: "something",
335
334
  data: encryptedPayload
336
335
  });
337
- authorizer._callback(true, "ERROR");
338
336
  expect(Logger.error).toHaveBeenCalledWith(
339
337
  "Failed to make a request to the authEndpoint: ERROR. Unable to fetch new key, so dropping encrypted event"
340
338
  );
@@ -1,7 +1,6 @@
1
1
  var PresenceChannel = require('core/channels/presence_channel').default;
2
2
  var Channel = require('core/channels/channel').default;
3
3
  var Members = require('core/channels/members').default;
4
- var Authorizer = require('core/auth/pusher_authorizer').default;
5
4
  var Errors = require('core/errors');
6
5
  var Factory = require('core/utils/factory').default;
7
6
  var Mocks = require("mocks");
@@ -9,9 +8,11 @@ var Mocks = require("mocks");
9
8
  describe("PresenceChannel", function() {
10
9
  var pusher;
11
10
  var channel;
11
+ var channelAuthorizer;
12
12
 
13
13
  beforeEach(function() {
14
- pusher = Mocks.getPusher({ foo: "bar" });
14
+ channelAuthorizer = jasmine.createSpy("channelAuthorizer")
15
+ pusher = Mocks.getPusher({ channelAuthorizer: channelAuthorizer });
15
16
  channel = new PresenceChannel("presence-test", pusher);
16
17
  });
17
18
 
@@ -46,46 +47,57 @@ describe("PresenceChannel", function() {
46
47
  });
47
48
 
48
49
  describe("#authorize", function() {
49
- var authorizer;
50
-
51
- beforeEach(function() {
52
- authorizer = Mocks.getAuthorizer();
53
- spyOn(Factory, "createAuthorizer").and.returnValue(authorizer);
50
+
51
+ it("should call channelAuthorizer", function() {
52
+ const callback = function(){}
53
+ channel.authorize("1.23", callback);
54
+ expect(channelAuthorizer.calls.count()).toEqual(1);
55
+ expect(channelAuthorizer).toHaveBeenCalledWith(
56
+ { socketId: "1.23", channelName: "presence-test" }, jasmine.any(Function));
54
57
  });
55
58
 
56
- it("should create and call an authorizer", function() {
57
- channel.authorize("1.23", function() {});
58
- expect(Factory.createAuthorizer.calls.count()).toEqual(1);
59
- expect(Factory.createAuthorizer).toHaveBeenCalledWith(
60
- channel,
61
- { foo: "bar" }
62
- );
59
+ it("should call the callback if an authorizaiton error is encountered", function() {
60
+ const callback = jasmine.createSpy("callback")
61
+ channel.authorize("1.23", callback);
62
+ expect(channelAuthorizer.calls.count()).toEqual(1);
63
+ expect(channelAuthorizer).toHaveBeenCalledWith(
64
+ { socketId: "1.23", channelName: "presence-test" }, jasmine.any(Function));
65
+ const presenceChannelCallback = channelAuthorizer.calls.mostRecent().args[1];
66
+
67
+ presenceChannelCallback("error", {})
68
+ expect(callback).toHaveBeenCalledWith("error", {})
63
69
  });
64
70
 
65
- it("should call back on success with authorization data", function() {
66
- var callback = jasmine.createSpy("callback");
71
+ it("should call the callback with error if auth data doesn't have channel_data", function() {
72
+ const callback = jasmine.createSpy("callback")
67
73
  channel.authorize("1.23", callback);
68
-
69
- expect(callback).not.toHaveBeenCalled();
70
- authorizer._callback(false, {
71
- foo: "bar",
72
- channel_data: JSON.stringify({ user_id: "U" })
73
- });
74
-
75
- expect(callback).toHaveBeenCalledWith(false, {
76
- foo: "bar",
77
- channel_data: JSON.stringify({ user_id: "U" })
78
- });
74
+ expect(channelAuthorizer.calls.count()).toEqual(1);
75
+ expect(channelAuthorizer).toHaveBeenCalledWith(
76
+ { socketId: "1.23", channelName: "presence-test" }, jasmine.any(Function));
77
+ const presenceChannelCallback = channelAuthorizer.calls.mostRecent().args[1];
78
+
79
+ presenceChannelCallback(null, {
80
+ foo: 'bar'
81
+ })
82
+ expect(callback).toHaveBeenCalledWith("Invalid auth response")
79
83
  });
80
84
 
81
- it("should call back on failure", function() {
82
- var callback = jasmine.createSpy("callback");
85
+ it("should call the callback with auth data", function() {
86
+ const callback = jasmine.createSpy("callback")
83
87
  channel.authorize("1.23", callback);
84
-
85
- authorizer._callback("error!");
86
-
87
- expect(callback).toHaveBeenCalledWith("error!", undefined);
88
+ expect(channelAuthorizer.calls.count()).toEqual(1);
89
+ expect(channelAuthorizer).toHaveBeenCalledWith(
90
+ { socketId: "1.23", channelName: "presence-test" }, jasmine.any(Function));
91
+ const presenceChannelCallback = channelAuthorizer.calls.mostRecent().args[1];
92
+
93
+ const authdata = {
94
+ channel_data: "{\"user_id\":\"123\"}",
95
+ foo: 'bar'
96
+ };
97
+ presenceChannelCallback(null, authdata)
98
+ expect(callback).toHaveBeenCalledWith(null, authdata)
88
99
  });
100
+
89
101
  });
90
102
 
91
103
  describe("#trigger", function() {
@@ -114,16 +126,14 @@ describe("PresenceChannel", function() {
114
126
  });
115
127
 
116
128
  describe("after authorizing", function() {
117
- var authorizer;
118
-
119
129
  beforeEach(function() {
120
- authorizer = Mocks.getAuthorizer();
121
- spyOn(Factory, "createAuthorizer").and.returnValue(authorizer);
122
- channel.authorize("1.23", function() {});
123
- authorizer._callback(false, {
124
- foo: "bar",
125
- channel_data: JSON.stringify({ user_id: "U" })
130
+ channelAuthorizer.and.callFake(function(params, callback) {
131
+ callback(null, {
132
+ foo: "bar",
133
+ channel_data: JSON.stringify({ user_id: "U" })
134
+ });
126
135
  });
136
+ channel.authorize("1.23", function() {});
127
137
  });
128
138
 
129
139
  describe("#handleEvent", function() {
@@ -1,16 +1,15 @@
1
- var Authorizer = require('core/auth/pusher_authorizer').default;
2
1
  var Errors = require('core/errors');
3
2
  var PrivateChannel = require('core/channels/private_channel').default;
4
- var Factory = require('core/utils/factory').default;
5
3
  var Mocks = require("mocks");
6
4
 
7
5
  describe("PrivateChannel", function() {
8
6
  var pusher;
9
7
  var channel;
10
- var factorySpy;
8
+ var channelAuthorizer;
11
9
 
12
10
  beforeEach(function() {
13
- pusher = Mocks.getPusher({ foo: "bar" });
11
+ channelAuthorizer = jasmine.createSpy("channelAuthorizer")
12
+ pusher = Mocks.getPusher({ channelAuthorizer: channelAuthorizer });
14
13
  channel = new PrivateChannel("private-test", pusher);
15
14
  });
16
15
 
@@ -29,49 +28,12 @@ describe("PrivateChannel", function() {
29
28
  });
30
29
 
31
30
  describe("#authorize", function() {
32
- var authorizer;
33
-
34
- beforeEach(function() {
35
- authorizer = Mocks.getAuthorizer();
36
- factorySpy = spyOn(Factory, "createAuthorizer").and.returnValue(authorizer);
37
- });
38
-
39
- it("should create and call an authorizer", function() {
40
- channel.authorize("1.23", function() {});
41
- expect(Factory.createAuthorizer.calls.count()).toEqual(1);
42
- expect(Factory.createAuthorizer).toHaveBeenCalledWith(
43
- channel,
44
- { foo: "bar" }
45
- );
46
- });
47
-
48
- it("should call back with authorization data", function() {
49
- var callback = jasmine.createSpy("callback");
31
+ it("should call channelAuthorizer", function() {
32
+ const callback = function(){}
50
33
  channel.authorize("1.23", callback);
51
-
52
- expect(callback).not.toHaveBeenCalled();
53
- authorizer._callback(false, { foo: "bar" });
54
-
55
- expect(callback).toHaveBeenCalledWith(false, { foo: "bar" });
56
- });
57
-
58
- describe('with custom authorizer', function() {
59
- beforeEach(function() {
60
- pusher = Mocks.getPusher({
61
- authorizer: function(channel, options) {
62
- return authorizer;
63
- }
64
- });
65
- channel = new PrivateChannel("private-test-custom-auth", pusher);
66
- factorySpy.and.callThrough();
67
- });
68
-
69
- it("should call the authorizer", function() {
70
- var callback = jasmine.createSpy("callback");
71
- channel.authorize("1.23", callback);
72
- authorizer._callback(false, { foo: "bar" });
73
- expect(callback).toHaveBeenCalledWith(false, { foo: "bar" });
74
- });
34
+ expect(channelAuthorizer.calls.count()).toEqual(1);
35
+ expect(channelAuthorizer).toHaveBeenCalledWith(
36
+ { socketId: "1.23", channelName: "private-test" }, callback);
75
37
  });
76
38
  });
77
39