ably 0.8.8 → 0.8.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -2
  3. data/LICENSE +2 -2
  4. data/README.md +81 -20
  5. data/SPEC.md +235 -178
  6. data/lib/ably/auth.rb +1 -1
  7. data/lib/ably/exceptions.rb +10 -1
  8. data/lib/ably/models/cipher_params.rb +114 -0
  9. data/lib/ably/models/connection_details.rb +8 -6
  10. data/lib/ably/models/error_info.rb +3 -3
  11. data/lib/ably/models/idiomatic_ruby_wrapper.rb +27 -20
  12. data/lib/ably/models/message.rb +15 -15
  13. data/lib/ably/models/message_encoders/cipher.rb +8 -7
  14. data/lib/ably/models/presence_message.rb +17 -17
  15. data/lib/ably/models/protocol_message.rb +26 -19
  16. data/lib/ably/models/stats.rb +15 -15
  17. data/lib/ably/models/token_details.rb +14 -12
  18. data/lib/ably/models/token_request.rb +16 -14
  19. data/lib/ably/modules/async_wrapper.rb +1 -1
  20. data/lib/ably/modules/encodeable.rb +10 -10
  21. data/lib/ably/modules/model_common.rb +13 -5
  22. data/lib/ably/realtime/channel.rb +1 -2
  23. data/lib/ably/realtime/presence.rb +29 -58
  24. data/lib/ably/realtime/presence/members_map.rb +2 -2
  25. data/lib/ably/rest/channel.rb +1 -2
  26. data/lib/ably/rest/middleware/exceptions.rb +14 -4
  27. data/lib/ably/rest/presence.rb +3 -1
  28. data/lib/ably/util/crypto.rb +50 -40
  29. data/lib/ably/version.rb +1 -1
  30. data/spec/acceptance/realtime/message_spec.rb +20 -20
  31. data/spec/acceptance/realtime/presence_history_spec.rb +7 -7
  32. data/spec/acceptance/realtime/presence_spec.rb +65 -77
  33. data/spec/acceptance/rest/auth_spec.rb +8 -8
  34. data/spec/acceptance/rest/base_spec.rb +4 -4
  35. data/spec/acceptance/rest/channel_spec.rb +1 -1
  36. data/spec/acceptance/rest/client_spec.rb +1 -1
  37. data/spec/acceptance/rest/encoders_spec.rb +4 -4
  38. data/spec/acceptance/rest/message_spec.rb +15 -15
  39. data/spec/acceptance/rest/presence_spec.rb +4 -4
  40. data/spec/shared/model_behaviour.rb +7 -7
  41. data/spec/unit/models/cipher_params_spec.rb +140 -0
  42. data/spec/unit/models/idiomatic_ruby_wrapper_spec.rb +15 -8
  43. data/spec/unit/models/message_encoders/cipher_spec.rb +28 -22
  44. data/spec/unit/models/message_encoders/json_spec.rb +24 -0
  45. data/spec/unit/models/protocol_message_spec.rb +3 -3
  46. data/spec/unit/util/crypto_spec.rb +50 -17
  47. metadata +5 -2
@@ -1,3 +1,3 @@
1
1
  module Ably
2
- VERSION = '0.8.8'
2
+ VERSION = '0.8.9'
3
3
  end
@@ -345,7 +345,7 @@ describe 'Ably::Realtime::Channel Message', :event_machine do
345
345
  let(:secret_key) { Base64.decode64(data['key']) }
346
346
  let(:iv) { Base64.decode64(data['iv']) }
347
347
 
348
- let(:cipher_options) { { key: secret_key, iv: iv, algorithm: algorithm, mode: mode, key_length: key_length } }
348
+ let(:cipher_options) { { key: secret_key, fixed_iv: iv, algorithm: algorithm, mode: mode, key_length: key_length } }
349
349
 
350
350
  context 'with #publish and #subscribe' do
351
351
  let(:encoded) { item['encoded'] }
@@ -372,7 +372,7 @@ describe 'Ably::Realtime::Channel Message', :event_machine do
372
372
  end
373
373
  end
374
374
 
375
- let(:encrypted_channel) { client.channel(channel_name, encrypted: true, cipher_params: cipher_options) }
375
+ let(:encrypted_channel) { client.channel(channel_name, cipher: cipher_options) }
376
376
 
377
377
  it 'encrypts message automatically before they are pushed to the server' do
378
378
  encrypted_channel.__incoming_msgbus__.unsubscribe # remove all subscribe callbacks that could decrypt the message
@@ -424,9 +424,9 @@ describe 'Ably::Realtime::Channel Message', :event_machine do
424
424
  end
425
425
 
426
426
  context 'with multiple sends from one client to another' do
427
- let(:cipher_options) { { key: random_str(32) } }
428
- let(:encrypted_channel_client1) { client.channel(channel_name, encrypted: true, cipher_params: cipher_options) }
429
- let(:encrypted_channel_client2) { other_client.channel(channel_name, encrypted: true, cipher_params: cipher_options) }
427
+ let(:cipher_options) { { key: Ably::Util::Crypto.generate_random_key } }
428
+ let(:encrypted_channel_client1) { client.channel(channel_name, cipher: cipher_options) }
429
+ let(:encrypted_channel_client2) { other_client.channel(channel_name, cipher: cipher_options) }
430
430
 
431
431
  let(:data) { { 'key' => random_str } }
432
432
  let(:message_count) { 50 }
@@ -471,9 +471,9 @@ describe 'Ably::Realtime::Channel Message', :event_machine do
471
471
  auto_close Ably::Realtime::Client.new(default_options.merge(protocol: other_protocol))
472
472
  end
473
473
 
474
- let(:cipher_options) { { key: random_str(32), algorithm: 'aes', mode: 'cbc', key_length: 256 } }
475
- let(:encrypted_channel_client1) { client.channel(channel_name, encrypted: true, cipher_params: cipher_options) }
476
- let(:encrypted_channel_client2) { other_client.channel(channel_name, encrypted: true, cipher_params: cipher_options) }
474
+ let(:cipher_options) { { key: Ably::Util::Crypto.generate_random_key, algorithm: 'aes', mode: 'cbc', key_length: 256 } }
475
+ let(:encrypted_channel_client1) { client.channel(channel_name, cipher: cipher_options) }
476
+ let(:encrypted_channel_client2) { other_client.channel(channel_name, cipher: cipher_options) }
477
477
 
478
478
  before do
479
479
  expect(other_client.protocol_binary?).to_not eql(client.protocol_binary?)
@@ -495,9 +495,9 @@ describe 'Ably::Realtime::Channel Message', :event_machine do
495
495
 
496
496
  context 'publishing on an unencrypted channel and subscribing on an encrypted channel with another client' do
497
497
  let(:client_options) { default_options.merge(log_level: :fatal) }
498
- let(:cipher_options) { { key: random_str(32), algorithm: 'aes', mode: 'cbc', key_length: 256 } }
498
+ let(:cipher_options) { { key: Ably::Util::Crypto.generate_random_key, algorithm: 'aes', mode: 'cbc', key_length: 256 } }
499
499
  let(:unencrypted_channel_client1) { client.channel(channel_name) }
500
- let(:encrypted_channel_client2) { other_client.channel(channel_name, encrypted: true, cipher_params: cipher_options) }
500
+ let(:encrypted_channel_client2) { other_client.channel(channel_name, cipher: cipher_options) }
501
501
 
502
502
  let(:payload) { MessagePack.pack({ 'key' => random_str }) }
503
503
 
@@ -513,8 +513,8 @@ describe 'Ably::Realtime::Channel Message', :event_machine do
513
513
 
514
514
  context 'publishing on an encrypted channel and subscribing on an unencrypted channel with another client' do
515
515
  let(:client_options) { default_options.merge(log_level: :fatal) }
516
- let(:cipher_options) { { key: random_str(32), algorithm: 'aes', mode: 'cbc', key_length: 256 } }
517
- let(:encrypted_channel_client1) { client.channel(channel_name, encrypted: true, cipher_params: cipher_options) }
516
+ let(:cipher_options) { { key: Ably::Util::Crypto.generate_random_key, algorithm: 'aes', mode: 'cbc', key_length: 256 } }
517
+ let(:encrypted_channel_client1) { client.channel(channel_name, cipher: Ably::Util::Crypto.get_default_params(cipher_options)) }
518
518
  let(:unencrypted_channel_client2) { other_client.channel(channel_name) }
519
519
 
520
520
  let(:payload) { MessagePack.pack({ 'key' => random_str }) }
@@ -543,10 +543,10 @@ describe 'Ably::Realtime::Channel Message', :event_machine do
543
543
 
544
544
  context 'publishing on an encrypted channel and subscribing with a different algorithm on another client' do
545
545
  let(:client_options) { default_options.merge(log_level: :fatal) }
546
- let(:cipher_options_client1) { { key: random_str(32), algorithm: 'aes', mode: 'cbc', key_length: 256 } }
547
- let(:encrypted_channel_client1) { client.channel(channel_name, encrypted: true, cipher_params: cipher_options_client1) }
548
- let(:cipher_options_client2) { { key: random_str(32), algorithm: 'aes', mode: 'cbc', key_length: 128 } }
549
- let(:encrypted_channel_client2) { other_client.channel(channel_name, encrypted: true, cipher_params: cipher_options_client2) }
546
+ let(:cipher_options_client1) { { key: Ably::Util::Crypto.generate_random_key, algorithm: 'aes', mode: 'cbc', key_length: 256 } }
547
+ let(:encrypted_channel_client1) { client.channel(channel_name, cipher: Ably::Util::Crypto.get_default_params(cipher_options_client1)) }
548
+ let(:cipher_options_client2) { { key: Ably::Util::Crypto.generate_random_key(128), algorithm: 'aes', mode: 'cbc', key_length: 128 } }
549
+ let(:encrypted_channel_client2) { other_client.channel(channel_name, cipher: Ably::Util::Crypto.get_default_params(cipher_options_client2)) }
550
550
 
551
551
  let(:payload) { MessagePack.pack({ 'key' => random_str }) }
552
552
 
@@ -574,10 +574,10 @@ describe 'Ably::Realtime::Channel Message', :event_machine do
574
574
 
575
575
  context 'publishing on an encrypted channel and subscribing with a different key on another client' do
576
576
  let(:client_options) { default_options.merge(log_level: :fatal) }
577
- let(:cipher_options_client1) { { key: random_str(32), algorithm: 'aes', mode: 'cbc', key_length: 256 } }
578
- let(:encrypted_channel_client1) { client.channel(channel_name, encrypted: true, cipher_params: cipher_options_client1) }
579
- let(:cipher_options_client2) { { key: random_str(32), algorithm: 'aes', mode: 'cbc', key_length: 256 } }
580
- let(:encrypted_channel_client2) { other_client.channel(channel_name, encrypted: true, cipher_params: cipher_options_client2) }
577
+ let(:cipher_options_client1) { { key: Ably::Util::Crypto.generate_random_key, algorithm: 'aes', mode: 'cbc', key_length: 256 } }
578
+ let(:encrypted_channel_client1) { client.channel(channel_name, cipher: cipher_options_client1) }
579
+ let(:cipher_options_client2) { { key: Ably::Util::Crypto.generate_random_key, algorithm: 'aes', mode: 'cbc', key_length: 256 } }
580
+ let(:encrypted_channel_client2) { other_client.channel(channel_name, cipher: cipher_options_client2) }
581
581
 
582
582
  let(:payload) { MessagePack.pack({ 'key' => random_str }) }
583
583
 
@@ -19,8 +19,8 @@ describe Ably::Realtime::Presence, 'history', :event_machine do
19
19
  let(:leave_data) { random_str }
20
20
 
21
21
  it 'provides up to the moment presence history' do
22
- presence_client_one.enter(data: data) do
23
- presence_client_one.leave(data: leave_data) do
22
+ presence_client_one.enter(data) do
23
+ presence_client_one.leave(leave_data) do
24
24
  presence_client_one.history do |history_page|
25
25
  expect(history_page).to be_a(Ably::Models::PaginatedResult)
26
26
  expect(history_page.items.count).to eql(2)
@@ -50,7 +50,7 @@ describe Ably::Realtime::Presence, 'history', :event_machine do
50
50
  end
51
51
  end
52
52
 
53
- presence_client_one.enter(data: data)
53
+ presence_client_one.enter(data)
54
54
  end
55
55
 
56
56
  context 'with option until_attach: true' do
@@ -59,8 +59,8 @@ describe Ably::Realtime::Presence, 'history', :event_machine do
59
59
  let(:presence_data_after_attach) { random_str }
60
60
 
61
61
  it 'retrieves all presence messages before channel was attached' do
62
- presence_client_two.enter(data: presence_data_before_attach) do
63
- presence_client_one.enter(data: presence_data_after_attach) do
62
+ presence_client_two.enter(presence_data_before_attach) do
63
+ presence_client_one.enter(presence_data_after_attach) do
64
64
  presence_client_one.history(until_attach: true) do |presence_page|
65
65
  expect(presence_page.items.count).to eql(1)
66
66
  expect(presence_page.items.first.data).to eql(presence_data_before_attach)
@@ -76,8 +76,8 @@ describe Ably::Realtime::Presence, 'history', :event_machine do
76
76
  let(:client_two) { auto_close Ably::Realtime::Client.new(default_options.merge(auth_callback: wildcard_token)) }
77
77
 
78
78
  it 'retrieves two pages of messages before channel was attached' do
79
- when_all(*10.times.map { |i| presence_client_two.enter_client("client:#{i}", data: presence_data_before_attach) }) do
80
- when_all(*10.times.map { |i| presence_client_one.enter_client("client:#{i}", data: presence_data_after_attach) }) do
79
+ when_all(*10.times.map { |i| presence_client_two.enter_client("client:#{i}", presence_data_before_attach) }) do
80
+ when_all(*10.times.map { |i| presence_client_one.enter_client("client:#{i}", presence_data_after_attach) }) do
81
81
  presence_client_one.history(until_attach: true, limit: 5) do |presence_page|
82
82
  expect(presence_page.items.count).to eql(5)
83
83
  expect(presence_page.items.map(&:data).uniq.first).to eql(presence_data_before_attach)
@@ -133,9 +133,9 @@ describe Ably::Realtime::Presence, :event_machine do
133
133
  context 'with supported data payload content type' do
134
134
  def register_presence_and_check_data(method_name, data)
135
135
  if method_name.to_s.match(/_client/)
136
- presence_client_one.public_send(method_name, client_id, data: data)
136
+ presence_client_one.public_send(method_name, client_id, data)
137
137
  else
138
- presence_client_one.public_send(method_name, data: data)
138
+ presence_client_one.public_send(method_name, data)
139
139
  end
140
140
 
141
141
  presence_client_one.subscribe do |presence_message|
@@ -188,9 +188,9 @@ describe Ably::Realtime::Presence, :event_machine do
188
188
  context 'with unsupported data payload content type' do
189
189
  def presence_action(method_name, data)
190
190
  if method_name.to_s.match(/_client/)
191
- presence_client_one.public_send(method_name, client_id, data: data)
191
+ presence_client_one.public_send(method_name, client_id, data)
192
192
  else
193
- presence_client_one.public_send(method_name, data: data)
193
+ presence_client_one.public_send(method_name, data)
194
194
  end
195
195
  end
196
196
 
@@ -677,7 +677,7 @@ describe Ably::Realtime::Presence, :event_machine do
677
677
 
678
678
  context '#get' do
679
679
  context 'with :wait_for_sync option set to true' do
680
- it 'waits until sync is complete', em_timeout: 15 do
680
+ it 'waits until sync is complete', em_timeout: 30 do # allow for slow connections and lots of messages
681
681
  enter_expected_count.times do |index|
682
682
  EventMachine.add_timer(index / 10) do
683
683
  presence_client_one.enter_client("client:#{index}") do |message|
@@ -696,7 +696,7 @@ describe Ably::Realtime::Presence, :event_machine do
696
696
  end
697
697
 
698
698
  context 'by default' do
699
- it 'it does not wait for sync', em_timeout: 15 do
699
+ it 'it does not wait for sync', em_timeout: 30 do # allow for slow connections and lots of messages
700
700
  enter_expected_count.times do |index|
701
701
  EventMachine.add_timer(index / 10) do
702
702
  presence_client_one.enter_client("client:#{index}") do |message|
@@ -745,21 +745,12 @@ describe Ably::Realtime::Presence, :event_machine do
745
745
  end
746
746
 
747
747
  context '#enter' do
748
- it 'allows client_id to be set on enter for anonymous clients' do
749
- channel_anonymous_client.presence.enter client_id: "123"
750
-
751
- channel_anonymous_client.presence.subscribe do |presence|
752
- expect(presence.client_id).to eq("123")
753
- stop_reactor
754
- end
755
- end
756
-
757
748
  context 'data attribute' do
758
749
  context 'when provided as argument option to #enter' do
759
- it 'remains intact following #leave' do
750
+ it 'changes to value provided in #leave' do
760
751
  leave_callback_called = false
761
752
 
762
- presence_client_one.enter(data: 'stored') do
753
+ presence_client_one.enter('stored') do
763
754
  expect(presence_client_one.data).to eql('stored')
764
755
 
765
756
  presence_client_one.leave do |presence|
@@ -767,7 +758,7 @@ describe Ably::Realtime::Presence, :event_machine do
767
758
  end
768
759
 
769
760
  presence_client_one.on(:left) do
770
- expect(presence_client_one.data).to eql('stored')
761
+ expect(presence_client_one.data).to eql(nil)
771
762
 
772
763
  EventMachine.next_tick do
773
764
  expect(leave_callback_called).to eql(true)
@@ -792,11 +783,6 @@ describe Ably::Realtime::Presence, :event_machine do
792
783
  end
793
784
  end
794
785
 
795
- it 'raises an exception if client_id is not set' do
796
- expect { channel_anonymous_client.presence.enter }.to raise_error(Ably::Exceptions::IncompatibleClientId, /without a client_id/)
797
- stop_reactor
798
- end
799
-
800
786
  context 'without necessary capabilities to join presence' do
801
787
  let(:restricted_client) do
802
788
  auto_close Ably::Realtime::Client.new(default_options.merge(key: restricted_api_key, log_level: :fatal))
@@ -805,7 +791,7 @@ describe Ably::Realtime::Presence, :event_machine do
805
791
  let(:restricted_presence) { restricted_channel.presence }
806
792
 
807
793
  it 'calls the Deferrable errback on capabilities failure' do
808
- restricted_presence.enter(client_id: 'clientId').tap do |deferrable|
794
+ restricted_presence.enter_client('bob').tap do |deferrable|
809
795
  deferrable.callback { raise "Should not succeed" }
810
796
  deferrable.errback { stop_reactor }
811
797
  end
@@ -817,7 +803,7 @@ describe Ably::Realtime::Presence, :event_machine do
817
803
 
818
804
  context '#update' do
819
805
  it 'without previous #enter automatically enters' do
820
- presence_client_one.update(data: data_payload) do
806
+ presence_client_one.update(data_payload) do
821
807
  EventMachine.add_timer(1) do
822
808
  expect(presence_client_one.state).to eq(:entered)
823
809
  stop_reactor
@@ -830,7 +816,7 @@ describe Ably::Realtime::Presence, :event_machine do
830
816
  presence_client_one.enter do
831
817
  presence_client_one.once_state_changed { fail 'State should not have changed ' }
832
818
 
833
- presence_client_one.update(data: data_payload) do
819
+ presence_client_one.update(data_payload) do
834
820
  EventMachine.add_timer(1) do
835
821
  expect(presence_client_one.state).to eq(:entered)
836
822
  presence_client_one.off
@@ -842,8 +828,8 @@ describe Ably::Realtime::Presence, :event_machine do
842
828
  end
843
829
 
844
830
  it 'updates the data if :data argument provided' do
845
- presence_client_one.enter(data: 'prior') do
846
- presence_client_one.update(data: data_payload)
831
+ presence_client_one.enter('prior') do
832
+ presence_client_one.update(data_payload)
847
833
  end
848
834
  presence_client_one.subscribe(:update) do |message|
849
835
  expect(message.data).to eql(data_payload)
@@ -852,7 +838,7 @@ describe Ably::Realtime::Presence, :event_machine do
852
838
  end
853
839
 
854
840
  it 'updates the data to nil if :data argument is not provided (assumes nil value)' do
855
- presence_client_one.enter(data: 'prior') do
841
+ presence_client_one.enter('prior') do
856
842
  presence_client_one.update
857
843
  end
858
844
  presence_client_one.subscribe(:update) do |message|
@@ -871,8 +857,8 @@ describe Ably::Realtime::Presence, :event_machine do
871
857
 
872
858
  context 'when set to a string' do
873
859
  it 'emits the new data for the leave event' do
874
- presence_client_one.enter data: enter_data do
875
- presence_client_one.leave data: data
860
+ presence_client_one.enter enter_data do
861
+ presence_client_one.leave data
876
862
  end
877
863
 
878
864
  presence_client_one.subscribe(:leave) do |presence_message|
@@ -883,9 +869,9 @@ describe Ably::Realtime::Presence, :event_machine do
883
869
  end
884
870
 
885
871
  context 'when set to nil' do
886
- it 'emits a nil value for the data attribute when leaving' do
887
- presence_client_one.enter data: enter_data do
888
- presence_client_one.leave data: nil
872
+ it 'emits the last value for the data attribute when leaving' do
873
+ presence_client_one.enter enter_data do
874
+ presence_client_one.leave nil
889
875
  end
890
876
 
891
877
  presence_client_one.subscribe(:leave) do |presence_message|
@@ -895,9 +881,9 @@ describe Ably::Realtime::Presence, :event_machine do
895
881
  end
896
882
  end
897
883
 
898
- context 'when not passed as an argument' do
899
- it 'emits the previously defined value as a convenience' do
900
- presence_client_one.enter data: enter_data do
884
+ context 'when not passed as an argument (i.e. nil)' do
885
+ it 'emits the previous value for the data attribute when leaving' do
886
+ presence_client_one.enter enter_data do
901
887
  presence_client_one.leave
902
888
  end
903
889
 
@@ -910,10 +896,10 @@ describe Ably::Realtime::Presence, :event_machine do
910
896
 
911
897
  context 'and sync is complete' do
912
898
  it 'does not cache members that have left' do
913
- presence_client_one.enter data: enter_data do
899
+ presence_client_one.enter enter_data do
914
900
  expect(presence_client_one.members).to be_in_sync
915
901
  expect(presence_client_one.members.send(:members).count).to eql(1)
916
- presence_client_one.leave data: data
902
+ presence_client_one.leave data
917
903
  end
918
904
 
919
905
  presence_client_one.subscribe(:leave) do |presence_message|
@@ -935,7 +921,7 @@ describe Ably::Realtime::Presence, :event_machine do
935
921
 
936
922
  context ':left event' do
937
923
  it 'emits the data defined in enter' do
938
- channel_client_one.presence.enter(data: 'data') do
924
+ channel_client_one.presence.enter('data') do
939
925
  channel_client_one.presence.leave
940
926
  end
941
927
 
@@ -946,8 +932,8 @@ describe Ably::Realtime::Presence, :event_machine do
946
932
  end
947
933
 
948
934
  it 'emits the data defined in update' do
949
- channel_client_one.presence.enter(data: 'something else') do
950
- channel_client_one.presence.update(data: 'data') do
935
+ channel_client_one.presence.enter('something else') do
936
+ channel_client_one.presence.update('data') do
951
937
  channel_client_one.presence.leave
952
938
  end
953
939
  end
@@ -984,7 +970,7 @@ describe Ably::Realtime::Presence, :event_machine do
984
970
 
985
971
  it 'enters a channel and sets the data based on the provided :data option' do
986
972
  client_count.times do |client_id|
987
- presence_client_one.enter_client("client:#{client_id}", data: data)
973
+ presence_client_one.enter_client("client:#{client_id}", data)
988
974
  end
989
975
 
990
976
  presence_anonymous_client.subscribe(:enter) do |presence|
@@ -1040,7 +1026,7 @@ describe Ably::Realtime::Presence, :event_machine do
1040
1026
 
1041
1027
  client_count.times do |client_id|
1042
1028
  presence_client_one.enter_client("client:#{client_id}") do
1043
- presence_client_one.update_client("client:#{client_id}", data: data) do
1029
+ presence_client_one.update_client("client:#{client_id}", data) do
1044
1030
  updated_callback_count += 1
1045
1031
  end
1046
1032
  end
@@ -1075,7 +1061,7 @@ describe Ably::Realtime::Presence, :event_machine do
1075
1061
  updated_callback_count = 0
1076
1062
 
1077
1063
  client_count.times do |client_id|
1078
- presence_client_one.update_client("client:#{client_id}", data: data) do
1064
+ presence_client_one.update_client("client:#{client_id}", data) do
1079
1065
  updated_callback_count += 1
1080
1066
  end
1081
1067
  end
@@ -1105,8 +1091,8 @@ describe Ably::Realtime::Presence, :event_machine do
1105
1091
  left_callback_count = 0
1106
1092
 
1107
1093
  client_count.times do |client_id|
1108
- presence_client_one.enter_client("client:#{client_id}", data: random_str) do
1109
- presence_client_one.leave_client("client:#{client_id}", data: data) do
1094
+ presence_client_one.enter_client("client:#{client_id}", random_str) do
1095
+ presence_client_one.leave_client("client:#{client_id}", data) do
1110
1096
  left_callback_count += 1
1111
1097
  end
1112
1098
  end
@@ -1150,8 +1136,8 @@ describe Ably::Realtime::Presence, :event_machine do
1150
1136
 
1151
1137
  context 'with a new value in :data option' do
1152
1138
  it 'emits the leave event with the new data value' do
1153
- presence_client_one.enter_client("client:unique", data: random_str) do
1154
- presence_client_one.leave_client("client:unique", data: data)
1139
+ presence_client_one.enter_client("client:unique", random_str) do
1140
+ presence_client_one.leave_client("client:unique", data)
1155
1141
  end
1156
1142
 
1157
1143
  presence_client_one.subscribe(:leave) do |presence_message|
@@ -1163,8 +1149,8 @@ describe Ably::Realtime::Presence, :event_machine do
1163
1149
 
1164
1150
  context 'with a nil value in :data option' do
1165
1151
  it 'emits the leave event with the previous value as a convenience' do
1166
- presence_client_one.enter_client("client:unique", data: data) do
1167
- presence_client_one.leave_client("client:unique", data: nil)
1152
+ presence_client_one.enter_client("client:unique", data) do
1153
+ presence_client_one.leave_client("client:unique", nil)
1168
1154
  end
1169
1155
 
1170
1156
  presence_client_one.subscribe(:leave) do |presence_message|
@@ -1176,7 +1162,7 @@ describe Ably::Realtime::Presence, :event_machine do
1176
1162
 
1177
1163
  context 'with no :data option' do
1178
1164
  it 'emits the leave event with the previous value as a convenience' do
1179
- presence_client_one.enter_client("client:unique", data: data) do
1165
+ presence_client_one.enter_client("client:unique", data) do
1180
1166
  presence_client_one.leave_client("client:unique")
1181
1167
  end
1182
1168
 
@@ -1519,7 +1505,7 @@ describe Ably::Realtime::Presence, :event_machine do
1519
1505
 
1520
1506
  context 'REST #get' do
1521
1507
  it 'returns current members' do
1522
- presence_client_one.enter(data: data_payload) do
1508
+ presence_client_one.enter(data_payload) do
1523
1509
  members_page = channel_rest_client_one.presence.get
1524
1510
  this_member = members_page.items.first
1525
1511
 
@@ -1532,7 +1518,7 @@ describe Ably::Realtime::Presence, :event_machine do
1532
1518
  end
1533
1519
 
1534
1520
  it 'returns no members once left' do
1535
- presence_client_one.enter(data: data_payload) do
1521
+ presence_client_one.enter(data_payload) do
1536
1522
  presence_client_one.leave do
1537
1523
  members_page = channel_rest_client_one.presence.get
1538
1524
  expect(members_page.items.count).to eql(0)
@@ -1562,32 +1548,34 @@ describe Ably::Realtime::Presence, :event_machine do
1562
1548
  let(:client_one) { auto_close Ably::Realtime::Client.new(default_options) }
1563
1549
 
1564
1550
  it 'is converted into UTF_8' do
1565
- presence_client_one.enter(client_id: client_id)
1566
- presence_client_one.on(:entered) do |presence|
1567
- expect(presence.client_id.encoding).to eql(Encoding::UTF_8)
1568
- expect(presence.client_id.encode(Encoding::ASCII_8BIT)).to eql(client_id)
1569
- stop_reactor
1551
+ channel_client_one.attach do
1552
+ presence_client_one.subscribe(:enter) do |presence|
1553
+ expect(presence.client_id.encoding).to eql(Encoding::UTF_8)
1554
+ expect(presence.client_id.encode(Encoding::ASCII_8BIT)).to eql(client_id)
1555
+ stop_reactor
1556
+ end
1557
+ presence_anonymous_client.enter_client(client_id)
1570
1558
  end
1571
1559
  end
1572
1560
  end
1573
1561
  end
1574
1562
 
1575
1563
  context 'encoding and decoding of presence message data' do
1576
- let(:secret_key) { random_str }
1564
+ let(:secret_key) { Ably::Util::Crypto.generate_random_key(256) }
1577
1565
  let(:cipher_options) { { key: secret_key, algorithm: 'aes', mode: 'cbc', key_length: 256 } }
1578
1566
  let(:channel_name) { random_str }
1579
- let(:encrypted_channel) { client_one.channel(channel_name, encrypted: true, cipher_params: cipher_options) }
1580
- let(:channel_rest_client_one) { client_one.rest_client.channel(channel_name, encrypted: true, cipher_params: cipher_options) }
1567
+ let(:encrypted_channel) { client_one.channel(channel_name, cipher: cipher_options) }
1568
+ let(:channel_rest_client_one) { client_one.rest_client.channel(channel_name, cipher: cipher_options) }
1581
1569
 
1582
1570
  let(:crypto) { Ably::Util::Crypto.new(cipher_options) }
1583
1571
 
1584
- let(:data) { { 'key' => random_str } }
1572
+ let(:data) { { 'hash_id' => random_str } }
1585
1573
  let(:data_as_json) { data.to_json }
1586
1574
  let(:data_as_cipher) { crypto.encrypt(data.to_json) }
1587
1575
 
1588
1576
  it 'encrypts presence message data' do
1589
1577
  encrypted_channel.attach do
1590
- encrypted_channel.presence.enter data: data
1578
+ encrypted_channel.presence.enter data
1591
1579
  end
1592
1580
 
1593
1581
  encrypted_channel.presence.__incoming_msgbus__.unsubscribe(:presence) # remove all subscribe callbacks that could decrypt the message
@@ -1606,7 +1594,7 @@ describe Ably::Realtime::Presence, :event_machine do
1606
1594
  context '#subscribe' do
1607
1595
  it 'emits decrypted enter events' do
1608
1596
  encrypted_channel.attach do
1609
- encrypted_channel.presence.enter data: data
1597
+ encrypted_channel.presence.enter data
1610
1598
  end
1611
1599
 
1612
1600
  encrypted_channel.presence.subscribe(:enter) do |presence_message|
@@ -1618,8 +1606,8 @@ describe Ably::Realtime::Presence, :event_machine do
1618
1606
 
1619
1607
  it 'emits decrypted update events' do
1620
1608
  encrypted_channel.attach do
1621
- encrypted_channel.presence.enter(data: 'to be updated') do
1622
- encrypted_channel.presence.update data: data
1609
+ encrypted_channel.presence.enter('to be updated') do
1610
+ encrypted_channel.presence.update data
1623
1611
  end
1624
1612
  end
1625
1613
 
@@ -1632,7 +1620,7 @@ describe Ably::Realtime::Presence, :event_machine do
1632
1620
 
1633
1621
  it 'emits previously set data for leave events' do
1634
1622
  encrypted_channel.attach do
1635
- encrypted_channel.presence.enter(data: data) do
1623
+ encrypted_channel.presence.enter(data) do
1636
1624
  encrypted_channel.presence.leave
1637
1625
  end
1638
1626
  end
@@ -1647,7 +1635,7 @@ describe Ably::Realtime::Presence, :event_machine do
1647
1635
 
1648
1636
  context '#get' do
1649
1637
  it 'returns a list of members with decrypted data' do
1650
- encrypted_channel.presence.enter(data: data) do
1638
+ encrypted_channel.presence.enter(data) do
1651
1639
  encrypted_channel.presence.get do |members|
1652
1640
  member = members.first
1653
1641
  expect(member.encoding).to be_nil
@@ -1660,7 +1648,7 @@ describe Ably::Realtime::Presence, :event_machine do
1660
1648
 
1661
1649
  context 'REST #get' do
1662
1650
  it 'returns a list of members with decrypted data' do
1663
- encrypted_channel.presence.enter(data: data) do
1651
+ encrypted_channel.presence.enter(data) do
1664
1652
  member = channel_rest_client_one.presence.get.items.first
1665
1653
  expect(member.encoding).to be_nil
1666
1654
  expect(member.data).to eql(data)
@@ -1671,11 +1659,11 @@ describe Ably::Realtime::Presence, :event_machine do
1671
1659
 
1672
1660
  context 'when cipher settings do not match publisher' do
1673
1661
  let(:client_options) { default_options.merge(log_level: :fatal) }
1674
- let(:incompatible_cipher_options) { { key: secret_key, algorithm: 'aes', mode: 'cbc', key_length: 128 } }
1675
- let(:incompatible_encrypted_channel) { client_two.channel(channel_name, encrypted: true, cipher_params: incompatible_cipher_options) }
1662
+ let(:incompatible_cipher_options) { { key: Ably::Util::Crypto.generate_random_key(128), algorithm: 'aes', mode: 'cbc', key_length: 128 } }
1663
+ let(:incompatible_encrypted_channel) { client_two.channel(channel_name, cipher: incompatible_cipher_options) }
1676
1664
 
1677
1665
  it 'delivers an unencoded presence message left with encoding value' do
1678
- encrypted_channel.presence.enter data: data
1666
+ encrypted_channel.presence.enter data
1679
1667
 
1680
1668
  incompatible_encrypted_channel.presence.subscribe(:enter) do
1681
1669
  incompatible_encrypted_channel.presence.get do |members|
@@ -1696,7 +1684,7 @@ describe Ably::Realtime::Presence, :event_machine do
1696
1684
  end
1697
1685
 
1698
1686
  encrypted_channel.attach do
1699
- encrypted_channel.presence.enter data: data
1687
+ encrypted_channel.presence.enter data
1700
1688
  end
1701
1689
  end
1702
1690
  end
@@ -1722,14 +1710,14 @@ describe Ably::Realtime::Presence, :event_machine do
1722
1710
  stop_reactor
1723
1711
  end
1724
1712
  end
1725
- presence_client_one.enter(data: data_payload) do
1713
+ presence_client_one.enter(data_payload) do
1726
1714
  presence_client_one.leave
1727
1715
  end
1728
1716
  end
1729
1717
  end
1730
1718
 
1731
1719
  context 'connection failure mid-way through a large member sync' do
1732
- let(:members_count) { 400 }
1720
+ let(:members_count) { 250 }
1733
1721
  let(:sync_pages_received) { [] }
1734
1722
  let(:client_options) { default_options.merge(log_level: :error) }
1735
1723
 
@@ -1741,7 +1729,7 @@ describe Ably::Realtime::Presence, :event_machine do
1741
1729
  client_two.connection.transport.__incoming_protocol_msgbus__.subscribe(:protocol_message) do |protocol_message|
1742
1730
  if protocol_message.action == :sync
1743
1731
  sync_pages_received << protocol_message
1744
- force_connection_failure client_two if sync_pages_received.count == 2
1732
+ force_connection_failure client_two if sync_pages_received.count == 1
1745
1733
  end
1746
1734
  end
1747
1735
  end