Skip to content

Commit

Permalink
Remove net-telnet dependency to work on Ruby 2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Nat Budin committed May 10, 2016
1 parent 10e4c7f commit 3487c8a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
14 changes: 11 additions & 3 deletions lib/devise_cas_authenticatable/memcache_checker.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'net/telnet'
require 'socket'
require 'timeout'

module DeviseCasAuthenticatable
class MemcacheChecker
Expand All @@ -17,14 +18,21 @@ def alive?
memcache_servers.each do |server|
host, port = server.split(":")
begin
Net::Telnet.new("Host" => host, "Port" => port, "Timeout" => 1)
try_connect host, port

return true
rescue Errno::ECONNREFUSED
rescue Errno::ECONNREFUSED, Timeout::Error
return false
end
end
end

def try_connect(host, port)
Timeout::timeout(1) do
TCPSocket.open(host, port)
end
end

private

def session_store_class
Expand Down
2 changes: 1 addition & 1 deletion spec/memcache_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
subject(:alive?) { described_class.new(conf_double).alive? }

before do
Net::Telnet.stubs(:new)
DeviseCasAuthenticatable::MemcacheChecker.any_instance.stubs(:try_connect)
end

it { expect(alive?).to eq true }
Expand Down
38 changes: 19 additions & 19 deletions spec/strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
describe Devise::Strategies::CasAuthenticatable, :type => "acceptance" do
include RSpec::Rails::RequestExampleGroup
include Capybara::DSL
before do

before do
Devise.cas_base_url = "http://www.example.com/cas_server"
TestAdapter.reset_valid_users!

Expand All @@ -13,31 +13,31 @@
u.username = "joeuser"
end
end

after do
visit destroy_user_session_url
end

def cas_login_url
@cas_login_url ||= begin
uri = URI.parse(Devise.cas_base_url + "/login")
uri.query = Rack::Utils.build_nested_query(:service => user_service_url)
uri.to_s
end
end

def cas_logout_url
@cas_logout_url ||= Devise.cas_base_url + "/logout?service"
end

def sign_into_cas(username, password)
visit root_url
current_url.should == cas_login_url
fill_in "Username", :with => username
fill_in "Password", :with => password
click_on "Login"
end

describe "GET /protected/resource" do
before { get '/' }

Expand All @@ -46,28 +46,28 @@ def sign_into_cas(username, password)
response.should redirect_to(new_user_session_url)
end
end

describe "GET /users/sign_in" do
before { get new_user_session_url }

it 'should redirect to CAS server' do
response.should be_redirect
response.should redirect_to(cas_login_url)
end
end

it "should sign in with valid user" do
sign_into_cas "joeuser", "joepassword"
current_url.should == root_url
end

it "should fail to sign in with an invalid user" do
sign_into_cas "invaliduser", "invalidpassword"
current_url.should_not == root_url
end

describe "with a deactivated user" do
before do
before do
@user = User.first
@user.deactivated = true
@user.save!
Expand All @@ -78,13 +78,13 @@ def sign_into_cas(username, password)
current_url.should == cas_logout_url
end
end

it "should register new CAS users if set up to do so" do
User.count.should == 1
TestAdapter.register_valid_user("newuser", "newpassword")
Devise.cas_create_user = true
sign_into_cas "newuser", "newpassword"

current_url.should == root_url
User.count.should == 2
User.find_by_username("newuser").should_not be_nil
Expand All @@ -102,7 +102,7 @@ def cas_create_user?
TestAdapter.register_valid_user("newuser", "newpassword")
Devise.cas_create_user = false
sign_into_cas "newuser", "newpassword"

current_url.should == root_url
User.count.should == 2
User.find_by_username("newuser").should_not be_nil
Expand All @@ -112,13 +112,13 @@ class << User
end
end
end

it "should fail CAS login if user is unregistered and cas_create_user is false" do
User.count.should == 1
TestAdapter.register_valid_user("newuser", "newpassword")
Devise.cas_create_user = false
sign_into_cas "newuser", "newpassword"

current_url.should_not == root_url
User.count.should == 1
User.find_by_username("newuser").should be_nil
Expand All @@ -129,12 +129,12 @@ class << User
click_on "Login"
current_url.should == root_url
end

it "should work correctly with Devise trackable" do
user = User.first
user.update_attributes!(:last_sign_in_at => 1.day.ago, :last_sign_in_ip => "1.2.3.4", :sign_in_count => 41)
sign_into_cas "joeuser", "joepassword"

user.reload
user.last_sign_in_at.should >= 1.hour.ago
user.sign_in_count.should == 42
Expand Down

0 comments on commit 3487c8a

Please sign in to comment.