Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add session support for connection #35

Merged
merged 1 commit into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master

- Add session support for connection. ([@sponomarev][])

See https://github.com/palkan/action-cable-testing/pull/35

## 0.3.0

- Add connection unit-testing utilities. ([@palkan][])
Expand Down Expand Up @@ -34,3 +38,4 @@ See https://github.com/palkan/action-cable-testing/issues/4.

[@palkan]: https://github.com/palkan
[@thesmartnik]: https://github.com/thesmartnik
[@sponomarev]: https://github.com/sponomarev
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MyCableTest < ActionCable::TestCase
ActionCable.server.broadcast 'messages', { text: 'hello' }
assert_broadcasts 'messages', 1

# Check the number of messages broadcasted to the stream within a block
# Check the number of messages broadcasted to the stream within a block
assert_broadcasts('messages', 1) do
ActionCable.server.broadcast 'messages', { text: 'hello' }
end
Expand Down Expand Up @@ -184,6 +184,12 @@ def test_connect_with_headers_and_query_string

assert_equal connection.user_id, "1"
end

def test_connect_with_session
connect "/cable", session: { users[:john].id }

assert_equal connection.user_id, "1"
end
```

### RSpec Usage
Expand Down
17 changes: 16 additions & 1 deletion features/channel_specs/channel_spec.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: channel spec

Channel specs are marked by `:type => :channel` or if you have set
`config.infer_spec_type_from_file_location!` by placing them in `spec/channels`.

Expand Down Expand Up @@ -146,6 +146,21 @@ Feature: channel spec
When I run `rspec spec/channels/connection_spec.rb`
Then the example should pass

Scenario: successful connection with session
Given a file named "spec/channels/connection_spec.rb" with:
"""ruby
require "rails_helper"

RSpec.describe ApplicationCable::Connection, :type => :channel do
it "successfully connects" do
connect "/cable", session: { user_id: "324" }
expect(connection.user_id).to eq "324"
end
end
"""
When I run `rspec spec/channels/connection_spec.rb`
Then the example should pass

Scenario: successful connection with headers
Given a file named "spec/channels/connection_spec.rb" with:
"""ruby
Expand Down
8 changes: 5 additions & 3 deletions lib/action_cable/connection/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def assert_reject_connection(&block)

class TestRequest < ActionDispatch::TestRequest
attr_reader :cookie_jar
attr_accessor :session

module CookiesStub
# Stub signed cookies
Expand All @@ -59,7 +60,7 @@ def cookie_jar=(val)
module TestConnection
attr_reader :logger, :request

def initialize(path, cookies, headers)
def initialize(path, cookies, headers, session)
inner_logger = ActiveSupport::Logger.new(StringIO.new)
tagged_logging = ActiveSupport::TaggedLogging.new(inner_logger)
@logger = ActionCable::Connection::TaggedLoggerProxy.new(tagged_logging, tags: [])
Expand All @@ -72,6 +73,7 @@ def initialize(path, cookies, headers)

@request = TestRequest.create(env)
@request.cookie_jar = cookies.with_indifferent_access
@request.session = session.with_indifferent_access
end

def build_headers(headers)
Expand Down Expand Up @@ -174,10 +176,10 @@ def determine_default_connection(name)
# Performs connection attempt (i.e. calls #connect method).
#
# Accepts request path as the first argument and cookies and headers as options.
def connect(path = "/cable", cookies: {}, headers: {})
def connect(path = "/cable", cookies: {}, headers: {}, session: {})
connection = self.class.connection_class.allocate
connection.singleton_class.include(TestConnection)
connection.send(:initialize, path, cookies, headers)
connection.send(:initialize, path, cookies, headers, session)
connection.connect if connection.respond_to?(:connect)

# Only set instance variable if connected successfully
Expand Down
3 changes: 2 additions & 1 deletion spec/dummy/app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def disconnect
def verify_user
user_id = request.params[:user_id] ||
request.headers["x-user-id"] ||
cookies.signed[:user_id]
cookies.signed[:user_id] ||
request.session[:user_id]
reject_unauthorized_connection unless user_id.present?
user_id
end
Expand Down
30 changes: 30 additions & 0 deletions test/connection/test_case_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require_relative "../test_helper"

class SimpleConnection < ActionCable::Connection::Base
identified_by :user_id

Expand Down Expand Up @@ -110,3 +112,31 @@ def test_connection_rejected
assert_reject_connection { connect }
end
end

class SessionConnection < ActionCable::Connection::Base
identified_by :user_id

def connect
self.user_id = verify_user
end

private

def verify_user
reject_unauthorized_connection unless request.session[:user_id].present?
request.session[:user_id]
end
end

class SessionConnectionTest < ActionCable::Connection::TestCase
tests SessionConnection

def test_connected_with_encrypted_cookies
connect session: { user_id: "789" }
assert_equal "789", connection.user_id
end

def test_connection_rejected
assert_reject_connection { connect }
end
end