garufa 0.0.1.beta.0 → 0.0.1.beta.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10052debad06b41d1ebb56145493aea97987d5e8
4
- data.tar.gz: 9f6fbb6b447fd706042339f0b5c3d046b442a32d
3
+ metadata.gz: ed1fb1125ed40c4f3cc0a19dcae61dff59691948
4
+ data.tar.gz: 30e670e78a6ecdf3c678a8a32523bce7ad768ee9
5
5
  SHA512:
6
- metadata.gz: de5feab7a14ac3ac6101398e681423f5657218da0e1e8e46777027a1e29b7578f659b4b068f892f744e30834d007c086251e7d0cdb3e92c56db1de5a6cc270b4
7
- data.tar.gz: 7d05306034b82221c97e8f8d06e654c2e31f09981b953a1c0493e8a4afd02571989c580b9ad480c30754eed4e0101b923ce82abfe6a672d1cbc3a9aeee534992
6
+ metadata.gz: c92cfa90de87f3256e6b9e7888438d6ae2c014458ea4fd630a9f44e516861dece549eb9a47ee08c3b9384d7f1b962a7734bf8a26b3bb90dacb031b123790c567
7
+ data.tar.gz: f294190202d0cc16e2a7bd95e9d34d213ffaa003bcc473c67800e166ae6a9d409d91331ead19d5c02e77889e5f891250266520e2ece44291999897282f8db2ba
data/README.md CHANGED
@@ -5,7 +5,7 @@ Garufa
5
5
 
6
6
  An open source server implementation of the [Pusher][pusher] protocol.
7
7
 
8
- **IMPORTANT:** Garufa is currently in alpha version, which means it is not
8
+ **IMPORTANT:** Garufa is currently in beta version, which means it is not
9
9
  production ready, but you are free to use it and test it. Any feedback is
10
10
  welcome.
11
11
 
@@ -84,7 +84,7 @@ module Garufa
84
84
 
85
85
  if subscription.success?
86
86
  @subscriptions[subscription.channel] = subscription
87
- send_subscription_succeeded(subscription) unless subscription.public_channel?
87
+ send_subscription_succeeded(subscription)
88
88
  else
89
89
  error(subscription.error.code, subscription.error.message)
90
90
  end
@@ -32,11 +32,11 @@ module Garufa
32
32
  end
33
33
 
34
34
  def self.subscription_succeeded(channel)
35
- new(event: 'pusher_internal:subscription_succeeded', channel: channel)
35
+ new(event: 'pusher_internal:subscription_succeeded', channel: channel, data: {})
36
36
  end
37
37
 
38
38
  def self.pong
39
- new(event: 'pusher:pong')
39
+ new(event: 'pusher:pong', data: {})
40
40
  end
41
41
 
42
42
  def self.error(code, message)
@@ -52,10 +52,12 @@ module Garufa
52
52
 
53
53
  def subscribe
54
54
  case true
55
- when invalid_channel?
56
- set_error(nil, 'Invalid channnel or not present')
57
- when invalid_signature?
55
+ when !valid_channel?
56
+ set_error(nil, 'Invalid channel')
57
+ when !public_channel? && !valid_signature?
58
58
  set_error(nil, 'Invalid signature')
59
+ when !public_channel? && !valid_app_key?
60
+ set_error(nil, 'Invalid key')
59
61
  when already_subscribed?
60
62
  set_error(nil, "Already subscribed to channel: #{channel}")
61
63
  else
@@ -97,17 +99,17 @@ module Garufa
97
99
 
98
100
  private
99
101
 
100
- def invalid_channel?
101
- !channel.is_a?(String) || channel.empty?
102
+ def valid_channel?
103
+ channel.is_a?(String) && !channel.empty?
102
104
  end
103
105
 
104
- def invalid_signature?
105
- return false if public_channel?
106
+ def valid_app_key?
107
+ app_key && app_key == Config[:app_key]
108
+ end
106
109
 
107
- app_key, signature = @data["auth"].split(':')
110
+ def valid_signature?
108
111
  string_to_sign = [@connection.socket_id, channel].compact.join(':')
109
-
110
- app_key != Config[:app_key] || token(string_to_sign) != signature
112
+ token(string_to_sign) == signature
111
113
  end
112
114
 
113
115
  def token(string_to_sign)
@@ -118,6 +120,14 @@ module Garufa
118
120
  def already_subscribed?
119
121
  Subscriptions.include? self
120
122
  end
123
+
124
+ def app_key
125
+ @data['auth'].split(':').first if @data['auth']
126
+ end
127
+
128
+ def signature
129
+ @data['auth'].split(':').last if @data['auth']
130
+ end
121
131
  end
122
132
 
123
133
  class SubscriptionError < Struct.new(:code, :message)
@@ -1,4 +1,4 @@
1
1
  module Garufa
2
- VERSION = '0.0.1.beta.0'
2
+ VERSION = '0.0.1.beta.1'
3
3
  end
4
4
 
@@ -0,0 +1,75 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+ require 'logger'
3
+
4
+ module Garufa
5
+ describe Connection do
6
+
7
+ before do
8
+ @socket = MiniTest::Mock.new
9
+ @connection = Connection.new(@socket, Logger.new('/dev/null'))
10
+ end
11
+
12
+ describe '.handle_incomming_data' do
13
+
14
+ describe 'pusher:subscribe' do
15
+
16
+ let(:data) { { event: 'pusher:subscribe', data: { channel: 'ch1' } } }
17
+
18
+ it 'should add a new Subscription to Subscriptions' do
19
+ @socket.expect :send, true, [String]
20
+ @connection.handle_incomming_data data.to_json
21
+ Subscriptions.all['ch1'].first.class.must_equal Subscription
22
+ Subscriptions.all['ch1'].count.must_equal 1
23
+ end
24
+
25
+ describe 'public channels' do
26
+ it 'should response with subscription_succeeded' do
27
+ message = Message.subscription_succeeded(data[:data][:channel])
28
+ @socket.expect :send, true, [message.to_json]
29
+ @connection.handle_incomming_data data.to_json
30
+ @socket.verify
31
+ end
32
+ end
33
+
34
+ describe 'private channels' do
35
+
36
+ let(:channel) { 'private-ch1' }
37
+ let(:app_key) { Config[:app_key] }
38
+ let(:signature) { sign(@connection.socket_id, channel) }
39
+ let(:data) { { event: 'pusher:subscribe', data: { channel: channel, auth: "#{app_key}:#{signature}" } } }
40
+
41
+ it 'should response with subscription_succeeded' do
42
+ message = Message.subscription_succeeded(channel)
43
+ @socket.expect :send, true, [message.to_json]
44
+ @connection.handle_incomming_data data.to_json
45
+ @socket.verify
46
+ end
47
+
48
+ it 'should response with invalid channel' do
49
+ data[:data][:channel] = ''
50
+ message = Message.error(nil, 'Invalid channel')
51
+ @socket.expect :send, true, [message.to_json]
52
+ @connection.handle_incomming_data data.to_json
53
+ @socket.verify
54
+ end
55
+
56
+ it 'should response with invalid signature' do
57
+ data[:data][:auth] = "#{app_key}:invalid-signature"
58
+ message = Message.error(nil, 'Invalid signature')
59
+ @socket.expect :send, true, [message.to_json]
60
+ @connection.handle_incomming_data data.to_json
61
+ @socket.verify
62
+ end
63
+
64
+ it 'should response with invalid key' do
65
+ data[:data][:auth] = "invalid-app-key:#{signature}"
66
+ message = Message.error(nil, 'Invalid key')
67
+ @socket.expect :send, true, [message.to_json]
68
+ @connection.handle_incomming_data data.to_json
69
+ @socket.verify
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -2,3 +2,13 @@ $:.unshift(File.expand_path("../lib", File.dirname(__FILE__)))
2
2
 
3
3
  require "garufa"
4
4
  require 'minitest/autorun'
5
+ require 'minitest/mock'
6
+
7
+ Garufa::Config[:app_key] = '123123-123123'
8
+ Garufa::Config[:secret] = '456456-456456'
9
+
10
+ def sign(socket_id, custom_string = nil)
11
+ string_to_sign = [socket_id, custom_string].join(':')
12
+ digest = OpenSSL::Digest::SHA256.new
13
+ return OpenSSL::HMAC.hexdigest(digest, Garufa::Config[:secret], string_to_sign)
14
+ end
@@ -3,9 +3,11 @@ require File.expand_path("helper", File.dirname(__FILE__))
3
3
  module Garufa
4
4
  describe Message do
5
5
  describe '.connection_established' do
6
+
7
+ let(:socket_id) { '123123-123123' }
8
+
6
9
  before do
7
- @socket_id = '123123-123123'
8
- @message = Message.connection_established @socket_id
10
+ @message = Message.connection_established socket_id
9
11
  end
10
12
 
11
13
  it 'should response with data attribute as string' do
@@ -18,36 +20,40 @@ module Garufa
18
20
 
19
21
  it 'should response with expected data' do
20
22
  data = JSON.parse(@message.data)
21
- data["socket_id"].must_equal @socket_id
23
+ data["socket_id"].must_equal socket_id
22
24
  data["activity_timeout"].must_equal 120
23
25
  end
24
26
  end
25
27
 
26
28
  describe '.channel_event' do
29
+
30
+ let(:channel) { 'channel-123' }
31
+ let(:event) { 'my-event' }
32
+ let(:data) { { itemId: 1, value: 'Sample Item' } }
33
+
27
34
  before do
28
- @channel = 'channel-123'
29
- @event = 'my-event'
30
- @data = { itemId: 1, value: 'Sample Item' }
31
- @message = Message.channel_event @channel, @event, @data
35
+ @message = Message.channel_event channel, event, data
32
36
  end
33
37
 
34
38
  it 'should response with expected event' do
35
- @message.event.must_equal @event
39
+ @message.event.must_equal event
36
40
  end
37
41
 
38
42
  it 'should response with expected data' do
39
- @message.data.must_equal @data
43
+ @message.data.must_equal data
40
44
  end
41
45
 
42
46
  it 'should response with expected channel' do
43
- @message.channel.must_equal @channel
47
+ @message.channel.must_equal channel
44
48
  end
45
49
  end
46
50
 
47
51
  describe '.subscription_succeeded' do
52
+
53
+ let(:channel) { 'channel-123' }
54
+
48
55
  before do
49
- @channel = 'channel-123'
50
- @message = Message.subscription_succeeded @channel
56
+ @message = Message.subscription_succeeded channel
51
57
  end
52
58
 
53
59
  it 'should response with expected event' do
@@ -55,20 +61,36 @@ module Garufa
55
61
  end
56
62
 
57
63
  it 'should response with expected channel' do
58
- @message.channel.must_equal @channel
64
+ @message.channel.must_equal channel
65
+ end
66
+
67
+ it 'should response with empty data' do
68
+ @message.data.must_equal({})
59
69
  end
60
70
  end
61
71
 
62
72
  describe '.pong' do
63
- it 'should response with a pong event' do
64
- Message.pong.event.must_equal 'pusher:pong'
73
+
74
+ before do
75
+ @message = Message.pong
76
+ end
77
+
78
+ it 'should response with expected event' do
79
+ @message.event.must_equal 'pusher:pong'
80
+ end
81
+
82
+ it 'should response with empty data' do
83
+ @message.data.must_equal({})
65
84
  end
66
85
  end
67
86
 
68
87
  describe '.error' do
88
+
89
+ let(:code) { 4000 }
90
+ let(:error_message) { 'There was an error!' }
91
+
69
92
  before do
70
- @code, @error_message = 4000, 'There was an error!'
71
- @message = Message.error @code, @error_message
93
+ @message = Message.error code, error_message
72
94
  end
73
95
 
74
96
  it 'should response with expected event' do
@@ -77,8 +99,8 @@ module Garufa
77
99
 
78
100
  it 'should response with expected data' do
79
101
  data = JSON.parse(@message.data)
80
- data["code"].must_equal @code
81
- data["message"].must_equal @error_message
102
+ data["code"].must_equal code
103
+ data["message"].must_equal error_message
82
104
  end
83
105
  end
84
106
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garufa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.beta.0
4
+ version: 0.0.1.beta.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Manuel Cuello
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-15 00:00:00.000000000 Z
11
+ date: 2014-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: goliath
@@ -103,11 +103,11 @@ files:
103
103
  - lib/garufa/garufa_app.rb
104
104
  - lib/garufa/connection.rb
105
105
  - lib/garufa.rb
106
- - bin/garufa.pid
107
106
  - bin/garufa
108
107
  - garufa.gemspec
109
108
  - test/helper.rb
110
109
  - test/message.rb
110
+ - test/connection.rb
111
111
  homepage: http://github.com/Juanmcuello/garufa
112
112
  licenses: []
113
113
  metadata: {}
@@ -1 +0,0 @@
1
- 16262