devise_bushido_authenticatable 1.0.0.alpha10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/.project +12 -0
  2. data/Gemfile +22 -0
  3. data/Gemfile.lock +169 -0
  4. data/README.md +114 -0
  5. data/Rakefile +46 -0
  6. data/VERSION +1 -0
  7. data/app/controllers/devise/cas_sessions_controller.rb +52 -0
  8. data/app/views/devise/cas_sessions/new.html.erb +1 -0
  9. data/app/views/devise/cas_sessions/unregistered.html.erb +2 -0
  10. data/devise_cas_authenticatable.gemspec +118 -0
  11. data/lib/devise_cas_authenticatable/exceptions.rb +10 -0
  12. data/lib/devise_cas_authenticatable/model.rb +56 -0
  13. data/lib/devise_cas_authenticatable/routes.rb +35 -0
  14. data/lib/devise_cas_authenticatable/schema.rb +15 -0
  15. data/lib/devise_cas_authenticatable/strategy.rb +49 -0
  16. data/lib/devise_cas_authenticatable.rb +93 -0
  17. data/rails/init.rb +1 -0
  18. data/spec/routes_spec.rb +20 -0
  19. data/spec/scenario/.gitignore +4 -0
  20. data/spec/scenario/app/controllers/application_controller.rb +3 -0
  21. data/spec/scenario/app/controllers/home_controller.rb +7 -0
  22. data/spec/scenario/app/models/user.rb +3 -0
  23. data/spec/scenario/app/views/layouts/application.html.erb +17 -0
  24. data/spec/scenario/config/application.rb +38 -0
  25. data/spec/scenario/config/boot.rb +13 -0
  26. data/spec/scenario/config/castronaut.yml +32 -0
  27. data/spec/scenario/config/database.yml +22 -0
  28. data/spec/scenario/config/environment.rb +5 -0
  29. data/spec/scenario/config/environments/development.rb +26 -0
  30. data/spec/scenario/config/environments/production.rb +49 -0
  31. data/spec/scenario/config/environments/test.rb +35 -0
  32. data/spec/scenario/config/initializers/backtrace_silencers.rb +7 -0
  33. data/spec/scenario/config/initializers/devise.rb +3 -0
  34. data/spec/scenario/config/initializers/inflections.rb +10 -0
  35. data/spec/scenario/config/initializers/mime_types.rb +5 -0
  36. data/spec/scenario/config/initializers/secret_token.rb +7 -0
  37. data/spec/scenario/config/initializers/session_store.rb +8 -0
  38. data/spec/scenario/config/locales/en.yml +5 -0
  39. data/spec/scenario/config/routes.rb +8 -0
  40. data/spec/scenario/config/rubycas-server.yml +13 -0
  41. data/spec/scenario/config.ru +4 -0
  42. data/spec/scenario/db/migrate/20100401102949_create_tables.rb +14 -0
  43. data/spec/scenario/public/.gitkeep +0 -0
  44. data/spec/spec_helper.rb +22 -0
  45. data/spec/strategy_spec.rb +96 -0
  46. data/spec/support/migrations.rb +4 -0
  47. metadata +202 -0
@@ -0,0 +1,49 @@
1
+ require 'devise/strategies/base'
2
+
3
+ module Devise
4
+ module Strategies
5
+ class CasAuthenticatable < Base
6
+ # True if the mapping supports authenticate_with_cas_ticket.
7
+ def valid?
8
+ mapping.to.respond_to?(:authenticate_with_cas_ticket) && params[:ticket]
9
+ end
10
+
11
+ # Try to authenticate a user using the CAS ticket passed in params.
12
+ # If the ticket is valid and the model's authenticate_with_cas_ticket method
13
+ # returns a user, then return success. If the ticket is invalid, then either
14
+ # fail (if we're just returning from the CAS server, based on the referrer)
15
+ # or attempt to redirect to the CAS server's login URL.
16
+ def authenticate!
17
+ ticket = read_ticket(params)
18
+ if ticket
19
+ if resource = mapping.to.authenticate_with_cas_ticket(ticket)
20
+ success!(resource)
21
+ elsif ticket.is_valid?
22
+ redirect!(::Devise.cas_unregistered_url(request.url, mapping), :username => ticket.response.user)
23
+ #fail!("The user #{ticket.response.user} is not registered with this site. Please use a different account.")
24
+ else
25
+ fail!(:invalid)
26
+ end
27
+ else
28
+ fail!(:invalid)
29
+ end
30
+ end
31
+
32
+ protected
33
+
34
+ def read_ticket(params)
35
+ ticket = params[:ticket]
36
+ return nil unless ticket
37
+
38
+ service_url = ::Devise.cas_service_url(request.url, mapping)
39
+ if ticket =~ /^PT-/
40
+ ::CASClient::ProxyTicket.new(ticket, service_url, params[:renew])
41
+ else
42
+ ::CASClient::ServiceTicket.new(ticket, service_url, params[:renew])
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ Warden::Strategies.add(:cas_authenticatable, Devise::Strategies::CasAuthenticatable)
@@ -0,0 +1,93 @@
1
+ require 'devise'
2
+
3
+ require 'devise_cas_authenticatable/schema'
4
+ require 'devise_cas_authenticatable/routes'
5
+ require 'devise_cas_authenticatable/strategy'
6
+ require 'devise_cas_authenticatable/exceptions'
7
+
8
+ require 'rubycas-client'
9
+
10
+ # Register as a Rails engine if Rails::Engine exists
11
+ begin
12
+ Rails::Engine
13
+ rescue
14
+ else
15
+ module DeviseCasAuthenticatable
16
+ class Engine < Rails::Engine
17
+ end
18
+ end
19
+ end
20
+
21
+ module Devise
22
+ # The base URL of the CAS server. For example, http://cas.example.com. Specifying this
23
+ # is mandatory.
24
+ @@cas_base_url = nil
25
+
26
+ # The login URL of the CAS server. If undefined, will default based on cas_base_url.
27
+ @@cas_login_url = nil
28
+
29
+ # The login URL of the CAS server. If undefined, will default based on cas_base_url.
30
+ @@cas_logout_url = nil
31
+
32
+ # The login URL of the CAS server. If undefined, will default based on cas_base_url.
33
+ @@cas_validate_url = nil
34
+
35
+ # Should devise_cas_authenticatable attempt to create new user records for
36
+ # unknown usernames? True by default.
37
+ @@cas_create_user = true
38
+
39
+ # The model attribute used for query conditions. Should be the same as
40
+ # the rubycas-server username_column. :username by default
41
+ @@cas_username_column = :username
42
+
43
+ # Name of the parameter passed in the logout query
44
+ @@cas_destination_logout_param_name = nil
45
+
46
+ mattr_accessor :cas_base_url, :cas_login_url, :cas_logout_url, :cas_validate_url, :cas_create_user, :cas_destination_logout_param_name, :cas_username_column
47
+
48
+ def self.cas_create_user?
49
+ cas_create_user
50
+ end
51
+
52
+ # Return a CASClient::Client instance based on configuration parameters.
53
+ def self.cas_client
54
+ @@cas_client ||= CASClient::Client.new(
55
+ :cas_destination_logout_param_name => @@cas_destination_logout_param_name,
56
+ :cas_base_url => @@cas_base_url,
57
+ :login_url => @@cas_login_url,
58
+ :logout_url => @@cas_logout_url,
59
+ :validate_url => @@cas_validate_url
60
+ )
61
+ end
62
+
63
+ def self.cas_service_url(base_url, mapping)
64
+ cas_action_url(base_url, mapping, "service")
65
+ end
66
+
67
+ def self.cas_unregistered_url(base_url, mapping)
68
+ cas_action_url(base_url, mapping, "unregistered")
69
+ end
70
+
71
+ private
72
+ def self.cas_action_url(base_url, mapping, action)
73
+ u = URI.parse(base_url)
74
+ u.query = nil
75
+ u.path = if mapping.respond_to?(:fullpath)
76
+ mapping.fullpath
77
+ else
78
+ mapping.raw_path
79
+ end
80
+ u.path << "/"
81
+ u.path << action
82
+ u.to_s
83
+
84
+ return u.to_s
85
+ end
86
+
87
+ end
88
+
89
+ Devise.add_module(:cas_authenticatable,
90
+ :strategy => true,
91
+ :controller => :cas_sessions,
92
+ :route => :cas_authenticatable,
93
+ :model => 'devise_cas_authenticatable/model')
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "devise_cas_authenticatable"
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Devise::CasSessionsController do
4
+ include RSpec::Rails::ControllerExampleGroup
5
+
6
+ it { should route(:get, "/users/service").to(:action => "service") }
7
+ it { should route(:get, "/users/sign_in").to(:action => "new") }
8
+ it { should route(:post, "/users/sign_in").to(:action => "create") }
9
+ it { should route(:get, "/users/sign_out").to(:action => "destroy") }
10
+ it { should route(:get, "/users/unregistered").to(:action => "unregistered") }
11
+
12
+ it "should have the right route names" do
13
+ controller.should respond_to("user_service_path", "new_user_session_path", "user_session_path", "destroy_user_session_path")
14
+ controller.user_service_path.should == "/users/service"
15
+ controller.new_user_session_path.should == "/users/sign_in"
16
+ controller.user_session_path.should == "/users/sign_in"
17
+ controller.destroy_user_session_path.should == "/users/sign_out"
18
+ controller.unregistered_user_session_path.should == "/users/unregistered"
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ .bundle
2
+ db/*.sqlite3
3
+ log/*.log
4
+ tmp/**/*
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,7 @@
1
+ class HomeController < ApplicationController
2
+ before_filter :authenticate_user!
3
+
4
+ def index
5
+ head(:ok)
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ devise :cas_authenticatable, :rememberable
3
+ end
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Scenario</title>
5
+ <%= stylesheet_link_tag :all %>
6
+ <%= javascript_include_tag :defaults %>
7
+ <%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <p class="alert"><%= alert %></p>
12
+ <p class="notice"><%= notice %></p>
13
+
14
+ <%= yield %>
15
+
16
+ </body>
17
+ </html>
@@ -0,0 +1,38 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require(:default, Rails.env) if defined?(Bundler)
6
+
7
+ require 'castronaut'
8
+ class TestAdapter
9
+ def self.reset_valid_users!
10
+ @@valid_users = {
11
+ "joeuser" => "joepassword"
12
+ }
13
+ end
14
+ reset_valid_users!
15
+
16
+ def self.register_valid_user(username, password)
17
+ @@valid_users[username] = password
18
+ end
19
+
20
+ def self.authenticate(username, password)
21
+ error_message = if @@valid_users[username] == password
22
+ nil
23
+ else
24
+ "Invalid password"
25
+ end
26
+
27
+ Castronaut::AuthenticationResult.new(username, error_message)
28
+ end
29
+ end
30
+
31
+ Castronaut::Adapters.register("test_adapter", TestAdapter)
32
+ Castronaut.config = Castronaut::Configuration.load(File.expand_path(File.join(File.dirname(__FILE__), "castronaut.yml")))
33
+
34
+ module Scenario
35
+ class Application < Rails::Application
36
+ config.active_support.deprecation = :stderr
37
+ end
38
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+
3
+ # Set up gems listed in the Gemfile.
4
+ gemfile = File.expand_path('../../Gemfile', __FILE__)
5
+ begin
6
+ ENV['BUNDLE_GEMFILE'] = gemfile
7
+ require 'bundler'
8
+ Bundler.setup
9
+ rescue Bundler::GemNotFound => e
10
+ STDERR.puts e.message
11
+ STDERR.puts "Try running `bundle install`."
12
+ exit!
13
+ end if File.exist?(gemfile)
@@ -0,0 +1,32 @@
1
+ organization_name: Foo Bar Baz Industries, LLC Inc. A division of Holdings Co.
2
+
3
+ environment: development
4
+ # The port the CAS webserver will start on
5
+ server_port: 4567
6
+
7
+ log_directory: log
8
+
9
+ log_level: Logger::DEBUG
10
+
11
+ ssl_enabled: false
12
+
13
+ cas_database:
14
+ adapter: sqlite3
15
+ database: db/cas.sqlite3
16
+ timeout: 5000
17
+
18
+ cas_adapter:
19
+ adapter: test_adapter
20
+
21
+ # Use this example if you are using LDAP as your authentication source
22
+ # cas_adapter:
23
+ # adapter: ldap
24
+ # host: localhost
25
+ # port: 389
26
+ # prefix: cn=
27
+ # base: dc=example, dc=com
28
+
29
+ # Uncomment these to enable authentication callbacks
30
+ # callbacks:
31
+ # on_authentication_success: http://example.com/authentication/success
32
+ # on_authentication_failed: http://example.com/authentication/failed
@@ -0,0 +1,22 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3-ruby (not necessary on OS X Leopard)
3
+ development:
4
+ adapter: sqlite3
5
+ database: db/development.sqlite3
6
+ pool: 5
7
+ timeout: 5000
8
+
9
+ # Warning: The database defined as "test" will be erased and
10
+ # re-generated from your development database when you run "rake".
11
+ # Do not set this db to the same as development or production.
12
+ test:
13
+ adapter: sqlite3
14
+ database: db/test.sqlite3
15
+ pool: 5
16
+ timeout: 5000
17
+
18
+ production:
19
+ adapter: sqlite3
20
+ database: db/production.sqlite3
21
+ pool: 5
22
+ timeout: 5000
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Scenario::Application.initialize!
@@ -0,0 +1,26 @@
1
+ Scenario::Application.configure do
2
+ # Settings specified here will take precedence over those in config/environment.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the webserver when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_view.debug_rjs = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Don't care if the mailer can't send
18
+ config.action_mailer.raise_delivery_errors = false
19
+
20
+ # Print deprecation notices to the Rails logger
21
+ config.active_support.deprecation = :log
22
+
23
+ # Only use best-standards-support built into browsers
24
+ config.action_dispatch.best_standards_support = :builtin
25
+ end
26
+
@@ -0,0 +1,49 @@
1
+ Scenario::Application.configure do
2
+ # Settings specified here will take precedence over those in config/environment.rb
3
+
4
+ # The production environment is meant for finished, "live" apps.
5
+ # Code is not reloaded between requests
6
+ config.cache_classes = true
7
+
8
+ # Full error reports are disabled and caching is turned on
9
+ config.consider_all_requests_local = false
10
+ config.action_controller.perform_caching = true
11
+
12
+ # Specifies the header that your server uses for sending files
13
+ config.action_dispatch.x_sendfile_header = "X-Sendfile"
14
+
15
+ # For nginx:
16
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
17
+
18
+ # If you have no front-end server that supports something like X-Sendfile,
19
+ # just comment this out and Rails will serve the files
20
+
21
+ # See everything in the log (default is :info)
22
+ # config.log_level = :debug
23
+
24
+ # Use a different logger for distributed setups
25
+ # config.logger = SyslogLogger.new
26
+
27
+ # Use a different cache store in production
28
+ # config.cache_store = :mem_cache_store
29
+
30
+ # Disable Rails's static asset server
31
+ # In production, Apache or nginx will already do this
32
+ config.serve_static_assets = false
33
+
34
+ # Enable serving of images, stylesheets, and javascripts from an asset server
35
+ # config.action_controller.asset_host = "http://assets.example.com"
36
+
37
+ # Disable delivery errors, bad email addresses will be ignored
38
+ # config.action_mailer.raise_delivery_errors = false
39
+
40
+ # Enable threaded mode
41
+ # config.threadsafe!
42
+
43
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
44
+ # the I18n.default_locale when a translation can not be found)
45
+ config.i18n.fallbacks = true
46
+
47
+ # Send deprecation notices to registered listeners
48
+ config.active_support.deprecation = :notify
49
+ end
@@ -0,0 +1,35 @@
1
+ Scenario::Application.configure do
2
+ # Settings specified here will take precedence over those in config/environment.rb
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Log error messages when you accidentally call methods on nil.
11
+ config.whiny_nils = true
12
+
13
+ # Show full error reports and disable caching
14
+ config.consider_all_requests_local = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Raise exceptions instead of rendering exception templates
18
+ config.action_dispatch.show_exceptions = false
19
+
20
+ # Disable request forgery protection in test environment
21
+ config.action_controller.allow_forgery_protection = false
22
+
23
+ # Tell Action Mailer not to deliver emails to the real world.
24
+ # The :test delivery method accumulates sent emails in the
25
+ # ActionMailer::Base.deliveries array.
26
+ config.action_mailer.delivery_method = :test
27
+
28
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
29
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
30
+ # like if you have constraints or database-specific column types
31
+ # config.active_record.schema_format = :sql
32
+
33
+ # Print deprecation notices to the stderr
34
+ config.active_support.deprecation = :stderr
35
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,3 @@
1
+ Devise.setup do |config|
2
+ require "devise/orm/active_record"
3
+ end
@@ -0,0 +1,10 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
5
+ # Mime::Type.register_alias "text/html", :iphone
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ Scenario::Application.config.secret_token = '70d2ec936ec5a91e883a9dc74bfeadd5a96cc242d3fd0857aa0151112ac71721475e01ae788e5c976a09ab62dd20240678cdc393c37cb777e872e59ea74adaad'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Scenario::Application.config.session_store :cookie_store, :key => '_scenario_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rake db:sessions:create")
8
+ # Scenario::Application.config.session_store :active_record_store
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: "Hello world"
@@ -0,0 +1,8 @@
1
+ require 'castronaut/application'
2
+ Castronaut::Application.set(:path, "/cas_server")
3
+
4
+ Scenario::Application.routes.draw do
5
+ devise_for :users
6
+ mount Castronaut::Application, :at => "/cas_server"
7
+ root :to => "home#index"
8
+ end
@@ -0,0 +1,13 @@
1
+ url_path: /cas_server
2
+
3
+ log:
4
+ level: DEBUG
5
+
6
+ database:
7
+ adapter: sqlite3
8
+ database: db/cas.sqlite3
9
+ pool: 5
10
+ timeout: 5000
11
+
12
+ authenticator:
13
+ class: TestAuthenticator
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Scenario::Application
@@ -0,0 +1,14 @@
1
+ class CreateTables < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users do |t|
4
+ t.cas_authenticatable
5
+ t.rememberable
6
+ t.string :email
7
+ t.timestamps
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :users
13
+ end
14
+ end
File without changes
@@ -0,0 +1,22 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ $:.unshift File.dirname(__FILE__)
3
+ $:.unshift File.expand_path('../../lib', __FILE__)
4
+
5
+ require "scenario/config/environment"
6
+ require "rails/test_help"
7
+ require 'rspec/rails'
8
+ require 'sham_rack'
9
+ require 'capybara/rspec'
10
+
11
+ RSpec.configure do |config|
12
+ config.mock_with :mocha
13
+ end
14
+
15
+ ShamRack.at('www.example.com') do |env|
16
+ request = Rack::Request.new(env)
17
+ request.path_info = request.path_info.sub(/^\/cas_server/, '')
18
+
19
+ Castronaut::Application.call(request.env)
20
+ end
21
+
22
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+
3
+ describe Devise::Strategies::CasAuthenticatable, :type => "acceptance" do
4
+ include RSpec::Rails::RequestExampleGroup
5
+
6
+ before do
7
+ Devise.cas_base_url = "http://www.example.com/cas_server"
8
+ TestAdapter.reset_valid_users!
9
+
10
+ User.delete_all
11
+ User.create! do |u|
12
+ u.username = "joeuser"
13
+ end
14
+ end
15
+
16
+ after do
17
+ visit destroy_user_session_url
18
+ end
19
+
20
+ def cas_login_url
21
+ @cas_login_url ||= begin
22
+ uri = URI.parse(Devise.cas_base_url + "/login")
23
+ uri.query = Rack::Utils.build_nested_query(:service => user_service_url)
24
+ uri.to_s
25
+ end
26
+ end
27
+
28
+ def cas_logout_url
29
+ @cas_logout_url ||= Devise.cas_base_url + "/logout"
30
+ end
31
+
32
+ def sign_into_cas(username, password)
33
+ visit root_url
34
+ current_url.should == cas_login_url
35
+ fill_in "Username", :with => username
36
+ fill_in "Password", :with => password
37
+ click_on "Login"
38
+ end
39
+
40
+ describe "GET /protected/resource" do
41
+ before { get '/' }
42
+
43
+ it 'should redirect to sign-in' do
44
+ response.should be_redirect
45
+ response.should redirect_to(new_user_session_url)
46
+ end
47
+ end
48
+
49
+ describe "GET /users/sign_in" do
50
+ before { get new_user_session_url }
51
+
52
+ it 'should redirect to CAS server' do
53
+ response.should be_redirect
54
+ response.should redirect_to(cas_login_url)
55
+ end
56
+ end
57
+
58
+ it "should sign in with valid user" do
59
+ sign_into_cas "joeuser", "joepassword"
60
+ current_url.should == root_url
61
+ end
62
+
63
+ it "should fail to sign in with an invalid user" do
64
+ sign_into_cas "invaliduser", "invalidpassword"
65
+ current_url.should_not == root_url
66
+ end
67
+
68
+ it "should register new CAS users if set up to do so" do
69
+ User.count.should == 1
70
+ TestAdapter.register_valid_user("newuser", "newpassword")
71
+ Devise.cas_create_user = true
72
+ sign_into_cas "newuser", "newpassword"
73
+
74
+ current_url.should == root_url
75
+ User.count.should == 2
76
+ User.find_by_username("newuser").should_not be_nil
77
+ end
78
+
79
+ it "should fail CAS login if user is unregistered and cas_create_user is false" do
80
+ User.count.should == 1
81
+ TestAdapter.register_valid_user("newuser", "newpassword")
82
+ Devise.cas_create_user = false
83
+ sign_into_cas "newuser", "newpassword"
84
+
85
+ current_url.should_not == root_url
86
+ User.count.should == 1
87
+ User.find_by_username("newuser").should be_nil
88
+
89
+ click_on "sign in using a different account"
90
+ current_url.should == cas_login_url
91
+ fill_in "Username", :with => "joeuser"
92
+ fill_in "Password", :with => "joepassword"
93
+ click_on "Login"
94
+ current_url.should == root_url
95
+ end
96
+ end
@@ -0,0 +1,4 @@
1
+ FileUtils.rm File.expand_path("../../scenario/db/*.sqlite3", __FILE__), :force => true
2
+ ActiveRecord::Base.logger = Logger.new(nil)
3
+ ActiveRecord::Migration.verbose = false
4
+ ActiveRecord::Migrator.migrate(File.expand_path("../../scenario/db/migrate/", __FILE__))