ably-rest 0.8.15 → 0.9.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 (28) hide show
  1. checksums.yaml +4 -4
  2. data/lib/submodules/ably-ruby/CHANGELOG.md +61 -48
  3. data/lib/submodules/ably-ruby/README.md +0 -6
  4. data/lib/submodules/ably-ruby/ably.gemspec +1 -1
  5. data/lib/submodules/ably-ruby/lib/ably/auth.rb +37 -32
  6. data/lib/submodules/ably-ruby/lib/ably/modules/event_emitter.rb +15 -30
  7. data/lib/submodules/ably-ruby/lib/ably/modules/model_common.rb +0 -25
  8. data/lib/submodules/ably-ruby/lib/ably/realtime/auth.rb +30 -18
  9. data/lib/submodules/ably-ruby/lib/ably/realtime/channel/channel_manager.rb +3 -3
  10. data/lib/submodules/ably-ruby/lib/ably/realtime/client.rb +0 -3
  11. data/lib/submodules/ably-ruby/lib/ably/realtime/client/incoming_message_dispatcher.rb +2 -2
  12. data/lib/submodules/ably-ruby/lib/ably/realtime/connection/connection_manager.rb +3 -3
  13. data/lib/submodules/ably-ruby/lib/ably/realtime/presence/members_map.rb +3 -3
  14. data/lib/submodules/ably-ruby/lib/ably/rest/client.rb +6 -6
  15. data/lib/submodules/ably-ruby/lib/ably/version.rb +2 -2
  16. data/lib/submodules/ably-ruby/spec/acceptance/realtime/auth_spec.rb +66 -53
  17. data/lib/submodules/ably-ruby/spec/acceptance/realtime/channel_spec.rb +8 -10
  18. data/lib/submodules/ably-ruby/spec/acceptance/realtime/client_spec.rb +3 -3
  19. data/lib/submodules/ably-ruby/spec/acceptance/realtime/connection_spec.rb +12 -48
  20. data/lib/submodules/ably-ruby/spec/acceptance/realtime/presence_spec.rb +38 -79
  21. data/lib/submodules/ably-ruby/spec/acceptance/rest/auth_spec.rb +74 -40
  22. data/lib/submodules/ably-ruby/spec/acceptance/rest/base_spec.rb +1 -1
  23. data/lib/submodules/ably-ruby/spec/acceptance/rest/channel_spec.rb +4 -2
  24. data/lib/submodules/ably-ruby/spec/acceptance/rest/client_spec.rb +21 -26
  25. data/lib/submodules/ably-ruby/spec/acceptance/rest/presence_spec.rb +12 -10
  26. data/lib/submodules/ably-ruby/spec/unit/modules/event_emitter_spec.rb +51 -109
  27. data/lib/submodules/ably-ruby/spec/unit/realtime/presence_spec.rb +3 -3
  28. metadata +4 -3
@@ -50,14 +50,18 @@ module Ably
50
50
  #
51
51
  # @return [void]
52
52
  def on(*event_names, &block)
53
- add_callback event_names, proc_for_block(block)
53
+ event_names.each do |event_name|
54
+ callbacks[callbacks_event_coerced(event_name)] << proc_for_block(block)
55
+ end
54
56
  end
55
57
 
56
58
  # Equivalent of {#on} but any exception raised in a block will bubble up and cause this client library to fail.
57
59
  # This method should only be used internally by the client library.
58
60
  # @api private
59
61
  def unsafe_on(*event_names, &block)
60
- add_callback event_names, proc_for_block(block, unsafe: true)
62
+ event_names.each do |event_name|
63
+ callbacks[callbacks_event_coerced(event_name)] << proc_for_block(block, unsafe: true)
64
+ end
61
65
  end
62
66
 
63
67
  # On receiving an event maching the event_name, call the provided block only once and remove the registered callback
@@ -66,20 +70,24 @@ module Ably
66
70
  #
67
71
  # @return [void]
68
72
  def once(*event_names, &block)
69
- add_callback event_names, proc_for_block(block, delete_once_run: true)
73
+ event_names.each do |event_name|
74
+ callbacks[callbacks_event_coerced(event_name)] << proc_for_block(block, delete_once_run: true)
75
+ end
70
76
  end
71
77
 
72
78
  # Equivalent of {#once} but any exception raised in a block will bubble up and cause this client library to fail.
73
79
  # This method should only be used internally by the client library.
74
80
  # @api private
75
81
  def unsafe_once(*event_names, &block)
76
- add_callback event_names, proc_for_block(block, delete_once_run: true, unsafe: true)
82
+ event_names.each do |event_name|
83
+ callbacks[callbacks_event_coerced(event_name)] << proc_for_block(block, delete_once_run: true, unsafe: true)
84
+ end
77
85
  end
78
86
 
79
87
  # Emit an event with event_name that will in turn call all matching callbacks setup with `on`
80
88
  def emit(event_name, *args)
81
- [callbacks_any, callbacks[callbacks_event_coerced(event_name)]].each do |callback_arr|
82
- callback_arr.clone.
89
+ callbacks[callbacks_event_coerced(event_name)].
90
+ clone.
83
91
  select do |proc_hash|
84
92
  if proc_hash[:unsafe]
85
93
  proc_hash[:emit_proc].call(*args)
@@ -87,9 +95,8 @@ module Ably
87
95
  safe_yield proc_hash[:emit_proc], *args
88
96
  end
89
97
  end.each do |callback|
90
- callback_arr.delete callback
98
+ callbacks[callbacks_event_coerced(event_name)].delete callback
91
99
  end
92
- end
93
100
  end
94
101
 
95
102
  # Remove all callbacks for event_name.
@@ -114,14 +121,6 @@ module Ably
114
121
  callbacks[callbacks_event_coerced(event_name)].clear
115
122
  end
116
123
  end
117
-
118
- if event_names.empty?
119
- if block_given?
120
- callbacks_any.delete_if { |proc_hash| proc_hash[:block] == block }
121
- else
122
- callbacks_any.clear
123
- end
124
- end
125
124
  end
126
125
 
127
126
  private
@@ -129,16 +128,6 @@ module Ably
129
128
  klass.extend ClassMethods
130
129
  end
131
130
 
132
- def add_callback(event_names, proc_block)
133
- if event_names.empty?
134
- callbacks_any << proc_block
135
- else
136
- event_names.each do |event_name|
137
- callbacks[callbacks_event_coerced(event_name)] << proc_block
138
- end
139
- end
140
- end
141
-
142
131
  # Create a Hash with a proc that calls the provided block and returns true if option :delete_once_run is set to true.
143
132
  # #emit automatically deletes any blocks that return true thus allowing a block to be run once
144
133
  def proc_for_block(block, options = {})
@@ -156,10 +145,6 @@ module Ably
156
145
  @callbacks ||= Hash.new { |hash, key| hash[key] = [] }
157
146
  end
158
147
 
159
- def callbacks_any
160
- @callbacks_any ||= []
161
- end
162
-
163
148
  def callbacks_event_coerced(event_name)
164
149
  if self.class.event_emitter_coerce_proc
165
150
  self.class.event_emitter_coerce_proc.call(event_name)
@@ -32,14 +32,6 @@ module Ably::Modules
32
32
  as_json.to_json(*args)
33
33
  end
34
34
 
35
- # Like to_json but encodes all binary fields to hex
36
- def to_safe_json(*args)
37
- as_json.
38
- each_with_object({}) do |(key, val), obj|
39
- obj[key] = to_safe_jsonable_val(val)
40
- end.to_json(*args)
41
- end
42
-
43
35
  # @!attribute [r] hash
44
36
  # @return [Integer] Compute a hash-code for this hash. Two hashes with the same content will have the same hash code
45
37
  def hash
@@ -47,23 +39,6 @@ module Ably::Modules
47
39
  end
48
40
 
49
41
  private
50
- def to_safe_jsonable_val(val)
51
- case val
52
- when Array
53
- val.map { |array_val| to_safe_jsonable_val(array_val) }
54
- when Hash
55
- val.each_with_object({}) { |(key, hash_val), obj| obj[key] = to_safe_jsonable_val(hash_val) }
56
- when String
57
- if val.encoding == Encoding::ASCII_8BIT
58
- val.unpack("H*").first
59
- else
60
- val
61
- end
62
- else
63
- val
64
- end
65
- end
66
-
67
42
  def ensure_utf8_string_for(attribute, value)
68
43
  if value
69
44
  raise ArgumentError, "#{attribute} must be a String" unless value.kind_of?(String)
@@ -54,8 +54,8 @@ module Ably
54
54
  #
55
55
  # In the event that a new token request is made, the provided options are used
56
56
  #
57
- # @param (see Ably::Auth#authorise)
58
- # @option (see Ably::Auth#authorise)
57
+ # @param (see Ably::Auth#authorize)
58
+ # @option (see Ably::Auth#authorize)
59
59
  #
60
60
  # @return [Ably::Util::SafeDeferrable]
61
61
  # @yield [Ably::Models::TokenDetails]
@@ -63,13 +63,13 @@ module Ably
63
63
  # @example
64
64
  # # will issue a simple token request using basic auth
65
65
  # client = Ably::Rest::Client.new(key: 'key.id:secret')
66
- # client.auth.authorise do |token_details|
66
+ # client.auth.authorize do |token_details|
67
67
  # token_details #=> Ably::Models::TokenDetails
68
68
  # end
69
69
  #
70
- def authorise(token_params = nil, auth_options = nil, &success_callback)
70
+ def authorize(token_params = nil, auth_options = nil, &success_callback)
71
71
  async_wrap(success_callback) do
72
- auth_sync.authorise(token_params, auth_options, &method(:upgrade_authentication_block).to_proc)
72
+ auth_sync.authorize(token_params, auth_options, &method(:upgrade_authentication_block).to_proc)
73
73
  end.tap do |deferrable|
74
74
  deferrable.errback do |error|
75
75
  client.connection.transition_state_machine :failed, reason: error if error.kind_of?(Ably::Exceptions::IncompatibleClientId)
@@ -77,13 +77,25 @@ module Ably
77
77
  end
78
78
  end
79
79
 
80
- # Synchronous version of {#authorise}. See {Ably::Auth#authorise} for method definition
81
- # @param (see Ably::Auth#authorise)
82
- # @option (see Ably::Auth#authorise)
80
+ # @deprecated Use {#authorize} instead
81
+ def authorise(*args, &block)
82
+ logger.warn "Auth#authorise is deprecated and will be removed in 1.0. Please use Auth#authorize instead"
83
+ authorize(*args, &block)
84
+ end
85
+
86
+ # Synchronous version of {#authorize}. See {Ably::Auth#authorize} for method definition
87
+ # @param (see Ably::Auth#authorize)
88
+ # @option (see Ably::Auth#authorize)
83
89
  # @return [Ably::Models::TokenDetails]
84
90
  #
85
- def authorise_sync(token_params = nil, auth_options = nil)
86
- auth_sync.authorise(token_params, auth_options, &method(:upgrade_authentication_block).to_proc)
91
+ def authorize_sync(token_params = nil, auth_options = nil)
92
+ auth_sync.authorize(token_params, auth_options, &method(:upgrade_authentication_block).to_proc)
93
+ end
94
+
95
+ # @deprecated Use {#authorize_sync} instead
96
+ def authorise_sync(*args)
97
+ logger.warn "Auth#authorise_sync is deprecated and will be removed in 1.0. Please use Auth#authorize_sync instead"
98
+ authorize_sync(*args)
87
99
  end
88
100
 
89
101
  # def_delegator :auth_sync, :request_token, :request_token_sync
@@ -113,8 +125,8 @@ module Ably
113
125
  end
114
126
 
115
127
  # Synchronous version of {#request_token}. See {Ably::Auth#request_token} for method definition
116
- # @param (see Ably::Auth#authorise)
117
- # @option (see Ably::Auth#authorise)
128
+ # @param (see Ably::Auth#authorize)
129
+ # @option (see Ably::Auth#authorize)
118
130
  # @return [Ably::Models::TokenDetails]
119
131
  #
120
132
  def request_token_sync(token_params = {}, auth_options = {})
@@ -140,8 +152,8 @@ module Ably
140
152
  end
141
153
 
142
154
  # Synchronous version of {#create_token_request}. See {Ably::Auth#create_token_request} for method definition
143
- # @param (see Ably::Auth#authorise)
144
- # @option (see Ably::Auth#authorise)
155
+ # @param (see Ably::Auth#authorize)
156
+ # @option (see Ably::Auth#authorize)
145
157
  # @return [Ably::Models::TokenRequest]
146
158
  #
147
159
  def create_token_request_sync(token_params = {}, auth_options = {})
@@ -149,7 +161,7 @@ module Ably
149
161
  end
150
162
 
151
163
  # Auth header string used in HTTP requests to Ably
152
- # Will reauthorise implicitly if required and capable
164
+ # Will reauthorize implicitly if required and capable
153
165
  #
154
166
  # @return [Ably::Util::SafeDeferrable]
155
167
  # @yield [String] HTTP authentication value used in HTTP_AUTHORIZATION header
@@ -168,7 +180,7 @@ module Ably
168
180
  end
169
181
 
170
182
  # Auth params used in URI endpoint for Realtime connections
171
- # Will reauthorise implicitly if required and capable
183
+ # Will reauthorize implicitly if required and capable
172
184
  #
173
185
  # @return [Ably::Util::SafeDeferrable]
174
186
  # @yield [Hash] Auth params for a new Realtime connection
@@ -197,12 +209,12 @@ module Ably
197
209
  @client
198
210
  end
199
211
 
200
- # If authorise is called with true, this block is executed so that it
212
+ # If authorize is called with true, this block is executed so that it
201
213
  # can perform the authentication upgrade
202
214
  def upgrade_authentication_block(new_token)
203
215
  # This block is called if the authorisation was forced
204
216
  if client.connection.connected? || client.connection.connecting?
205
- logger.debug "Realtime::Auth - authorise called with { force: true } so forcibly disconnecting transport to initiate auth upgrade"
217
+ logger.debug "Realtime::Auth - authorize was called so forcibly disconnecting transport to initiate auth upgrade"
206
218
  block = Proc.new do
207
219
  if client.connection.transport
208
220
  logger.debug "Realtime::Auth - current transport disconnected"
@@ -58,7 +58,7 @@ module Ably::Realtime
58
58
  # all messages awaiting an ACK response should fail immediately
59
59
  def fail_messages_awaiting_ack(error)
60
60
  # Allow a short time for other queued operations to complete before failing all messages
61
- EventMachine.next_tick do
61
+ EventMachine.add_timer(0.1) do
62
62
  error = Ably::Exceptions::MessageDeliveryFailed.new("Channel cannot publish messages whilst state is '#{channel.state}'") unless error
63
63
  fail_messages_in_queue connection.__pending_message_ack_queue__, error
64
64
  fail_messages_in_queue connection.__outgoing_message_queue__, error
@@ -76,10 +76,10 @@ module Ably::Realtime
76
76
 
77
77
  def nack_messages(protocol_message, error)
78
78
  (protocol_message.messages + protocol_message.presence).each do |message|
79
- logger.debug "Calling NACK failure callbacks for #{message.class.name} - #{message.to_safe_json}, protocol message: #{protocol_message}"
79
+ logger.debug "Calling NACK failure callbacks for #{message.class.name} - #{message.to_json}, protocol message: #{protocol_message}"
80
80
  message.fail error
81
81
  end
82
- logger.debug "Calling NACK failure callbacks for #{protocol_message.class.name} - #{protocol_message.to_safe_json}"
82
+ logger.debug "Calling NACK failure callbacks for #{protocol_message.class.name} - #{protocol_message.to_json}"
83
83
  protocol_message.fail error
84
84
  end
85
85
 
@@ -68,9 +68,6 @@ module Ably
68
68
  #
69
69
  # @param (see Ably::Rest::Client#initialize)
70
70
  # @option options (see Ably::Rest::Client#initialize)
71
- # @option options [Proc] :auth_callback when provided, the Proc will be called with the token params hash as the first argument, whenever a new token is required.
72
- # Whilst the proc is called synchronously, it does not block the EventMachine reactor as it is run in a separate thread.
73
- # The Proc should return a token string, {Ably::Models::TokenDetails} or JSON equivalent, {Ably::Models::TokenRequest} or JSON equivalent
74
71
  # @option options [Boolean] :queue_messages If false, this disables the default behaviour whereby the library queues messages on a connection in the disconnected or connecting states
75
72
  # @option options [Boolean] :echo_messages If false, prevents messages originating from this connection being echoed back on the same connection
76
73
  # @option options [String] :recover When a recover option is specified a connection inherits the state of a previous connection that may have existed under a different instance of the Realtime library, please refer to the API documentation for further information on connection state recovery
@@ -178,14 +178,14 @@ module Ably::Realtime
178
178
 
179
179
  def ack_messages(messages)
180
180
  messages.each do |message|
181
- logger.debug "Calling ACK success callbacks for #{message.class.name} - #{message.to_safe_json}"
181
+ logger.debug "Calling ACK success callbacks for #{message.class.name} - #{message.to_json}"
182
182
  message.succeed message
183
183
  end
184
184
  end
185
185
 
186
186
  def nack_messages(messages, protocol_message)
187
187
  messages.each do |message|
188
- logger.debug "Calling NACK failure callbacks for #{message.class.name} - #{message.to_safe_json}, protocol message: #{protocol_message}"
188
+ logger.debug "Calling NACK failure callbacks for #{message.class.name} - #{message.to_json}, protocol message: #{protocol_message}"
189
189
  message.fail protocol_message.error
190
190
  end
191
191
  end
@@ -402,8 +402,8 @@ module Ably::Realtime
402
402
  @renewing_token = true
403
403
  logger.info "ConnectionManager: Token has expired and is renewable, renewing token now"
404
404
 
405
- client.auth.authorise(nil, force: true).tap do |authorise_deferrable|
406
- authorise_deferrable.callback do |token_details|
405
+ client.auth.authorize.tap do |authorize_deferrable|
406
+ authorize_deferrable.callback do |token_details|
407
407
  logger.info 'ConnectionManager: Token renewed succesfully following expiration'
408
408
 
409
409
  connection.once_state_changed { @renewing_token = false }
@@ -415,7 +415,7 @@ module Ably::Realtime
415
415
  end
416
416
  end
417
417
 
418
- authorise_deferrable.errback do |auth_error|
418
+ authorize_deferrable.errback do |auth_error|
419
419
  @renewing_token = false
420
420
  logger.error "ConnectionManager: Error authorising following token expiry: #{auth_error}"
421
421
  connection.transition_state_machine :failed, reason: auth_error
@@ -200,7 +200,7 @@ module Ably::Realtime
200
200
  return unless ensure_presence_message_is_valid(presence_message)
201
201
 
202
202
  unless should_update_member?(presence_message)
203
- logger.debug "#{self.class.name}: Skipped presence member #{presence_message.action} on channel #{presence.channel.name}.\n#{presence_message.to_safe_json}"
203
+ logger.debug "#{self.class.name}: Skipped presence member #{presence_message.action} on channel #{presence.channel.name}.\n#{presence_message.to_json}"
204
204
  return
205
205
  end
206
206
 
@@ -239,13 +239,13 @@ module Ably::Realtime
239
239
  end
240
240
 
241
241
  def add_presence_member(presence_message)
242
- logger.debug "#{self.class.name}: Member '#{presence_message.member_key}' for event '#{presence_message.action}' #{members.has_key?(presence_message.member_key) ? 'updated' : 'added'}.\n#{presence_message.to_safe_json}"
242
+ logger.debug "#{self.class.name}: Member '#{presence_message.member_key}' for event '#{presence_message.action}' #{members.has_key?(presence_message.member_key) ? 'updated' : 'added'}.\n#{presence_message.to_json}"
243
243
  members[presence_message.member_key] = { present: true, message: presence_message }
244
244
  presence.emit_message presence_message.action, presence_message
245
245
  end
246
246
 
247
247
  def remove_presence_member(presence_message)
248
- logger.debug "#{self.class.name}: Member '#{presence_message.member_key}' removed.\n#{presence_message.to_safe_json}"
248
+ logger.debug "#{self.class.name}: Member '#{presence_message.member_key}' removed.\n#{presence_message.to_json}"
249
249
 
250
250
  if in_sync?
251
251
  members.delete presence_message.member_key
@@ -285,14 +285,14 @@ module Ably
285
285
 
286
286
  response = case method.to_sym
287
287
  when :get
288
- reauthorise_on_authorisation_failure do
288
+ reauthorize_on_authorisation_failure do
289
289
  send_request(method, path, params, headers: headers)
290
290
  end
291
291
  when :post
292
292
  path_with_params = Addressable::URI.new
293
293
  path_with_params.query_values = params || {}
294
294
  query = path_with_params.query
295
- reauthorise_on_authorisation_failure do
295
+ reauthorize_on_authorisation_failure do
296
296
  send_request(method, "#{path}#{"?#{query}" unless query.nil? || query.empty?}", body, headers: headers)
297
297
  end
298
298
  end
@@ -414,10 +414,10 @@ module Ably
414
414
  private
415
415
  def raw_request(method, path, params = {}, options = {})
416
416
  options = options.clone
417
- if options.delete(:disable_automatic_reauthorise) == true
417
+ if options.delete(:disable_automatic_reauthorize) == true
418
418
  send_request(method, path, params, options)
419
419
  else
420
- reauthorise_on_authorisation_failure do
420
+ reauthorize_on_authorisation_failure do
421
421
  send_request(method, path, params, options)
422
422
  end
423
423
  end
@@ -464,11 +464,11 @@ module Ably
464
464
  end
465
465
  end
466
466
 
467
- def reauthorise_on_authorisation_failure
467
+ def reauthorize_on_authorisation_failure
468
468
  yield
469
469
  rescue Ably::Exceptions::TokenExpired => e
470
470
  if auth.token_renewable?
471
- auth.authorise(nil, force: true)
471
+ auth.authorize
472
472
  yield
473
473
  else
474
474
  raise e
@@ -1,6 +1,6 @@
1
1
  module Ably
2
- VERSION = '0.8.15'
3
- PROTOCOL_VERSION = '0.8'
2
+ VERSION = '0.9.0'
3
+ PROTOCOL_VERSION = '0.9'
4
4
 
5
5
  # Allow a variant to be configured for all instances of this client library
6
6
  # such as ruby-rest-[VERSION]
@@ -73,7 +73,7 @@ describe Ably::Realtime::Auth, :event_machine do
73
73
  context '#current_token_details' do
74
74
  it 'contains the current token after auth' do
75
75
  expect(auth.current_token_details).to be_nil
76
- auth.authorise do
76
+ auth.authorize do
77
77
  expect(auth.current_token_details).to be_a(Ably::Models::TokenDetails)
78
78
  stop_reactor
79
79
  end
@@ -93,7 +93,7 @@ describe Ably::Realtime::Auth, :event_machine do
93
93
  let(:client_options) { default_options.merge(auto_connect: false) }
94
94
 
95
95
  it 'contains the configured auth options' do
96
- auth.authorise({}, auth_url: auth_url, auth_params: auth_params) do
96
+ auth.authorize({}, auth_url: auth_url, auth_params: auth_params) do
97
97
  expect(auth.options[:auth_url]).to eql(auth_url)
98
98
  stop_reactor
99
99
  end
@@ -104,7 +104,7 @@ describe Ably::Realtime::Auth, :event_machine do
104
104
  let(:custom_ttl) { 33 }
105
105
 
106
106
  it 'contains the configured auth options' do
107
- auth.authorise(ttl: custom_ttl) do
107
+ auth.authorize(ttl: custom_ttl) do
108
108
  expect(auth.token_params[:ttl]).to eql(custom_ttl)
109
109
  stop_reactor
110
110
  end
@@ -113,7 +113,7 @@ describe Ably::Realtime::Auth, :event_machine do
113
113
 
114
114
  context '#using_basic_auth?' do
115
115
  it 'is false when using Token Auth' do
116
- auth.authorise do
116
+ auth.authorize do
117
117
  expect(auth).to_not be_using_basic_auth
118
118
  stop_reactor
119
119
  end
@@ -122,7 +122,7 @@ describe Ably::Realtime::Auth, :event_machine do
122
122
 
123
123
  context '#using_token_auth?' do
124
124
  it 'is true when using Token Auth' do
125
- auth.authorise do
125
+ auth.authorize do
126
126
  expect(auth).to be_using_token_auth
127
127
  stop_reactor
128
128
  end
@@ -176,9 +176,9 @@ describe Ably::Realtime::Auth, :event_machine do
176
176
  end
177
177
  end
178
178
 
179
- context '#authorise' do
179
+ context '#authorize' do
180
180
  it 'returns a token asynchronously' do
181
- auth.authorise(ttl: custom_ttl, client_id: custom_client_id) do |token_details|
181
+ auth.authorize(ttl: custom_ttl, client_id: custom_client_id) do |token_details|
182
182
  expect(token_details).to be_a(Ably::Models::TokenDetails)
183
183
  expect(token_details.expires.to_i).to be_within(3).of(Time.now.to_i + custom_ttl)
184
184
  expect(token_details.client_id).to eql(custom_client_id)
@@ -186,35 +186,6 @@ describe Ably::Realtime::Auth, :event_machine do
186
186
  end
187
187
  end
188
188
 
189
- context 'with auth_callback blocking' do
190
- let(:rest_auth_client) { Ably::Rest::Client.new(default_options.merge(key: api_key)) }
191
- let(:client_options) { default_options.merge(auth_callback: auth_callback) }
192
- let(:pause) { 5 }
193
-
194
- context 'with a slow auth callback response' do
195
- let(:auth_callback) do
196
- Proc.new do
197
- sleep pause
198
- rest_auth_client.auth.request_token
199
- end
200
- end
201
-
202
- it 'asynchronously authenticates' do
203
- timers_called = 0
204
- block = Proc.new do
205
- timers_called += 1
206
- EventMachine.add_timer(0.5, &block)
207
- end
208
- block.call
209
- client.connect
210
- client.connection.on(:connected) do
211
- expect(timers_called).to be >= (pause-1) / 0.5
212
- stop_reactor
213
- end
214
- end
215
- end
216
- end
217
-
218
189
  context 'when implicitly called, with an explicit ClientOptions client_id' do
219
190
  let(:client_id) { random_str }
220
191
  let(:client_options) { default_options.merge(auth_callback: Proc.new { auth_token_object }, client_id: client_id, log_level: :none) }
@@ -271,7 +242,7 @@ describe Ably::Realtime::Auth, :event_machine do
271
242
  context 'and an incompatible client_id in a TokenDetails object passed to the auth callback' do
272
243
  it 'rejects a TokenDetails object with an incompatible client_id and raises an exception' do
273
244
  client.connection.once(:connected) do
274
- client.auth.authorise({}, force: true)
245
+ client.auth.authorize({})
275
246
  client.connection.on(:error) do |error|
276
247
  expect(error).to be_a(Ably::Exceptions::IncompatibleClientId)
277
248
  EventMachine.add_timer(0.1) do
@@ -284,7 +255,7 @@ describe Ably::Realtime::Auth, :event_machine do
284
255
  end
285
256
  end
286
257
 
287
- context 'with force: true to trigger an authentication upgrade' do
258
+ context 'when already authenticated with a valid token' do
288
259
  let(:rest_client) { Ably::Rest::Client.new(default_options) }
289
260
  let(:client_publisher) { auto_close Ably::Realtime::Client.new(default_options) }
290
261
  let(:basic_capability) { JSON.dump("foo" => ["subscribe"]) }
@@ -308,7 +279,7 @@ describe Ably::Realtime::Auth, :event_machine do
308
279
  it 'forces the connection to disconnect and reconnect with a new token when in the CONNECTED state' do
309
280
  client.connection.once(:connected) do
310
281
  existing_token = client.auth.current_token_details
311
- client.auth.authorise(nil, force: true)
282
+ client.auth.authorize(nil)
312
283
  client.connection.once(:disconnected) do
313
284
  client.connection.once(:connected) do
314
285
  expect(existing_token).to_not eql(client.auth.current_token_details)
@@ -321,7 +292,7 @@ describe Ably::Realtime::Auth, :event_machine do
321
292
  it 'forces the connection to disconnect and reconnect with a new token when in the CONNECTING state' do
322
293
  client.connection.once(:connecting) do
323
294
  existing_token = client.auth.current_token_details
324
- client.auth.authorise(nil, force: true)
295
+ client.auth.authorize(nil)
325
296
  client.connection.once(:disconnected) do
326
297
  client.connection.once(:connected) do
327
298
  expect(existing_token).to_not eql(client.auth.current_token_details)
@@ -340,7 +311,7 @@ describe Ably::Realtime::Auth, :event_machine do
340
311
 
341
312
  it 'transisitions the connection state to FAILED if the client_id changes' do
342
313
  client.connection.once(:connected) do
343
- client.auth.authorise(nil, auth_callback: identified_token_cb, force: true)
314
+ client.auth.authorize(nil, auth_callback: identified_token_cb)
344
315
  client.connection.once(:failed) do
345
316
  expect(client.connection.error_reason.message).to match(/incompatible.*client ID/)
346
317
  stop_reactor
@@ -358,7 +329,7 @@ describe Ably::Realtime::Auth, :event_machine do
358
329
  channel.publish('not-allowed').errback do |error|
359
330
  expect(error.code).to eql(40160)
360
331
  expect(error.message).to match(/permission denied/)
361
- client.auth.authorise(nil, auth_callback: upgraded_token_cb, force: true)
332
+ client.auth.authorize(nil, auth_callback: upgraded_token_cb)
362
333
  client.connection.once(:connected) do
363
334
  expect(client.connection.error_reason).to be_nil
364
335
  channel.subscribe('allowed') do |message|
@@ -378,7 +349,7 @@ describe Ably::Realtime::Auth, :event_machine do
378
349
  client.connection.once(:connected) do
379
350
  channel = client.channels.get('foo')
380
351
  channel.attach do
381
- client.auth.authorise(nil, auth_callback: downgraded_token_cb, force: true)
352
+ client.auth.authorize(nil, auth_callback: downgraded_token_cb)
382
353
  channel.once(:failed) do
383
354
  expect(channel.error_reason.code).to eql(40160)
384
355
  expect(channel.error_reason.message).to match(/Channel denied access/)
@@ -401,7 +372,7 @@ describe Ably::Realtime::Auth, :event_machine do
401
372
  publisher_channel.publish('foo') do
402
373
  EventMachine.add_timer(2) do
403
374
  expect(received_messages.length).to eql(1)
404
- client.auth.authorise(nil, force: true)
375
+ client.auth.authorize(nil)
405
376
  client.connection.once(:disconnected) do
406
377
  publisher_channel.publish('bar') do
407
378
  expect(received_messages.length).to eql(1)
@@ -422,9 +393,9 @@ describe Ably::Realtime::Auth, :event_machine do
422
393
  it 'does not change the connection state if current connection state is closing' do
423
394
  client.connection.once(:connected) do
424
395
  client.connection.once(:closing) do
425
- client.auth.authorise(nil, force: true)
396
+ client.auth.authorize(nil)
426
397
  client.connection.once(:connected) do
427
- raise "Should not reconnect following auth force: true"
398
+ raise "Should not reconnect following #authorize"
428
399
  end
429
400
  EventMachine.add_timer(4) do
430
401
  expect(client.connection).to be_closed
@@ -438,9 +409,9 @@ describe Ably::Realtime::Auth, :event_machine do
438
409
  it 'does not change the connection state if current connection state is closed' do
439
410
  client.connection.once(:connected) do
440
411
  client.connection.once(:closed) do
441
- client.auth.authorise(nil, force: true)
412
+ client.auth.authorize(nil)
442
413
  client.connection.once(:connected) do
443
- raise "Should not reconnect following auth force: true"
414
+ raise "Should not reconnect following #authorize"
444
415
  end
445
416
  EventMachine.add_timer(4) do
446
417
  expect(client.connection).to be_closed
@@ -457,9 +428,9 @@ describe Ably::Realtime::Auth, :event_machine do
457
428
  it 'does not change the connection state' do
458
429
  client.connection.once(:connected) do
459
430
  client.connection.once(:failed) do
460
- client.auth.authorise(nil, force: true)
431
+ client.auth.authorize(nil)
461
432
  client.connection.once(:connected) do
462
- raise "Should not reconnect following auth force: true"
433
+ raise "Should not reconnect following #authorize"
463
434
  end
464
435
  EventMachine.add_timer(4) do
465
436
  expect(client.connection).to be_failed
@@ -474,10 +445,10 @@ describe Ably::Realtime::Auth, :event_machine do
474
445
  end
475
446
  end
476
447
 
477
- context '#authorise_async' do
448
+ context '#authorize_async' do
478
449
  it 'returns a token synchronously' do
479
- auth.authorise_sync(ttl: custom_ttl, client_id: custom_client_id).tap do |token_details|
480
- expect(auth.authorise_sync).to be_a(Ably::Models::TokenDetails)
450
+ auth.authorize_sync(ttl: custom_ttl, client_id: custom_client_id).tap do |token_details|
451
+ expect(auth.authorize_sync).to be_a(Ably::Models::TokenDetails)
481
452
  expect(token_details.expires.to_i).to be_within(3).of(Time.now.to_i + custom_ttl)
482
453
  expect(token_details.client_id).to eql(custom_client_id)
483
454
  stop_reactor
@@ -696,5 +667,47 @@ describe Ably::Realtime::Auth, :event_machine do
696
667
  end
697
668
  end
698
669
  end
670
+
671
+ context 'deprecated #authorise' do
672
+ let(:client_options) { default_options.merge(key: api_key, logger: custom_logger_object) }
673
+ let(:custom_logger) do
674
+ Class.new do
675
+ def initialize
676
+ @messages = []
677
+ end
678
+
679
+ [:fatal, :error, :warn, :info, :debug].each do |severity|
680
+ define_method severity do |message|
681
+ @messages << [severity, message]
682
+ end
683
+ end
684
+
685
+ def logs
686
+ @messages
687
+ end
688
+
689
+ def level
690
+ 1
691
+ end
692
+
693
+ def level=(new_level)
694
+ end
695
+ end
696
+ end
697
+ let(:custom_logger_object) { custom_logger.new }
698
+
699
+ it 'logs a deprecation warning (#RSA10l)' do
700
+ client.auth.authorise
701
+ expect(custom_logger_object.logs.find { |severity, message| message.match(/authorise.*deprecated/i)} ).to_not be_nil
702
+ stop_reactor
703
+ end
704
+
705
+ it 'returns a valid token (#RSA10l)' do
706
+ client.auth.authorise do |response|
707
+ expect(response).to be_a(Ably::Models::TokenDetails)
708
+ stop_reactor
709
+ end
710
+ end
711
+ end
699
712
  end
700
713
  end