factorylabs-casrack_the_authenticator 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,85 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+ require 'net/http'
3
+
4
+ class ServiceTicketValidatorTest < Test::Unit::TestCase
5
+
6
+ context 'a service-ticket validator' do
7
+
8
+ setup do
9
+ config = Object.new
10
+ config.stubs(:service_validate_url).returns('http://cas.example.org/cas/serviceValidate?service=foo&ticket=bar')
11
+ @validator = CasrackTheAuthenticator::ServiceTicketValidator.new(config, nil, nil)
12
+ end
13
+
14
+ context 'validating a ticket' do
15
+
16
+ setup do
17
+ @server = Object.new
18
+ @connection = Object.new
19
+ @response = Object.new
20
+ @body = Object.new
21
+ Net::HTTP.stubs(:new).returns(@server)
22
+ @server.stubs(:start).yields(@connection)
23
+ @connection.stubs(:get).returns(@response)
24
+ @response.stubs(:body).returns(@body)
25
+ end
26
+
27
+ should 'return the body from the service-validate URL' do
28
+ assert_equal @body, @validator.send(:get_validation_response_body)
29
+ assert_received(Net::HTTP, :new) do |expects|
30
+ expects.with('cas.example.org', 80)
31
+ end
32
+ assert_received(@server, :start)
33
+ assert_received(@connection, :get) do |expects|
34
+ expects.with('/cas/serviceValidate?service=foo&ticket=bar', { 'Accept' => '*/*' })
35
+ end
36
+ assert_received(@response, :body)
37
+ end
38
+
39
+ context "but a connection error gets in the way" do
40
+
41
+ setup do
42
+ @server.stubs(:start).raises(SocketError)
43
+ end
44
+
45
+ should 'let the error percolate' do
46
+ assert_raises(SocketError) do
47
+ @validator.send(:get_validation_response_body)
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+
55
+ context 'parsing a successful response' do
56
+
57
+ setup do
58
+ @body = <<-EOX
59
+ <cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
60
+ <cas:authenticationSuccess>
61
+ <cas:user>beatrice</cas:user>
62
+ </cas:authenticationSuccess>
63
+ </cas:serviceResponse>
64
+ EOX
65
+ end
66
+
67
+ should 'get the user' do
68
+ assert_equal 'beatrice', @validator.send(:parse_user, @body)
69
+ end
70
+
71
+ end
72
+
73
+ context 'parsing an unsuccessful response' do
74
+ setup do
75
+ @body = ''
76
+ end
77
+
78
+ should 'return nil' do
79
+ assert_equal nil, @validator.send(:parse_user, @body)
80
+ end
81
+ end
82
+
83
+ end
84
+
85
+ end
@@ -0,0 +1,131 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class SimpleTest < Test::Unit::TestCase
4
+
5
+ context 'creating a Simple authenticator' do
6
+ should 'require a :cas_server' do
7
+ assert_raises(ArgumentError) do
8
+ CasrackTheAuthenticator::Simple.new(:anything, {})
9
+ end
10
+ end
11
+ end
12
+
13
+ def self.should_pass_the_request_on_down
14
+ should "pass the request to the underyling app" do
15
+ assert_received(@app, :call)
16
+ end
17
+ end
18
+
19
+ def self.should_set_the_cas_user_in_the_session_to(username)
20
+ should "set the CAS user in the session to #{username || '<nil>'}" do
21
+ assert_equal username, @session[:cas_user]
22
+ end
23
+ end
24
+
25
+ def get(url)
26
+ env = Rack::MockRequest.env_for(url)
27
+ if @session
28
+ env['rack.session'] = @session
29
+ else
30
+ @session = Rack::Request.new(env).session
31
+ end
32
+ Rack::MockRequest.stubs(:env_for).returns(env)
33
+ @response = @request.get url
34
+ end
35
+
36
+ def param_from_url(param, url)
37
+ uri = URI.parse(url)
38
+ Rack::Utils.parse_nested_query(uri.query)[param]
39
+ end
40
+
41
+ def return_to_url(response)
42
+ param_from_url 'service', response.headers['Location']
43
+ end
44
+
45
+ context 'a Simple authenticator' do
46
+
47
+ setup do
48
+ @app = Object.new
49
+ @authenticator = CasrackTheAuthenticator::Simple.new(@app, {:cas_server => 'http://cas.test/'})
50
+ @request = Rack::MockRequest.new(@authenticator)
51
+ end
52
+
53
+ context 'when receiving a 200 from below' do
54
+
55
+ setup do
56
+ @response_from_below = [ 200, {}, 'Success!' ]
57
+ @app.stubs(:call).returns(@response_from_below)
58
+ get '/'
59
+ end
60
+
61
+ should_pass_the_request_on_down
62
+
63
+ should 'do nothing to the response' do
64
+ assert_equal @response_from_below[0], @response.status
65
+ assert_equal @response_from_below[2], @response.body
66
+ end
67
+
68
+ end
69
+
70
+ context 'when receiving a 401 from below' do
71
+
72
+ setup do
73
+ response = [401, {}, 'Unauthorized!']
74
+ @app.stubs(:call).returns(response)
75
+ @url = "http://foo.bar/baz?yoo=hoo"
76
+ get @url
77
+ end
78
+
79
+ should_pass_the_request_on_down
80
+
81
+ should 'redirect to CAS' do
82
+ assert((300..399).include?(@response.status))
83
+ assert @response.headers['Location'] =~ /cas/i
84
+ end
85
+
86
+ should 'set the content-type to text/plain' do
87
+ assert_equal 'text/plain', @response.headers['Content-Type']
88
+ end
89
+
90
+ should 'use the requested URL for the return-to' do
91
+ assert_equal @url, return_to_url(@response)
92
+ end
93
+
94
+ context "and the request URL includes a 'ticket' param" do
95
+
96
+ setup do
97
+ @url = "http://foo.bar/baz?ticket=12345"
98
+ get @url
99
+ end
100
+
101
+ should 'strip the ticket from the return-to URL' do
102
+ return_to = return_to_url(@response)
103
+ assert_equal nil, param_from_url('ticket', return_to)
104
+ end
105
+
106
+ end
107
+
108
+ end
109
+
110
+ context 'when receiving a valid result from CAS' do
111
+
112
+ setup do
113
+ validator = Object.new
114
+ validator.stubs(:user).returns 'timmy'
115
+ CasrackTheAuthenticator::ServiceTicketValidator.stubs(:new).returns(validator)
116
+ @response_from_below = [ 200, {}, 'Success!' ]
117
+ @app.stubs(:call).returns(@response_from_below)
118
+ get '/?ticket=ST-77889'
119
+ end
120
+
121
+ should_set_the_cas_user_in_the_session_to 'timmy'
122
+
123
+ should 'build a service-ticket validator' do
124
+ assert_received(CasrackTheAuthenticator::ServiceTicketValidator, :new)
125
+ end
126
+
127
+ end
128
+
129
+ end
130
+
131
+ end
@@ -0,0 +1,16 @@
1
+ require 'test/unit'
2
+ require 'test/unit/testcase'
3
+ require 'rubygems'
4
+ require 'shoulda'
5
+ gem 'jferris-mocha'
6
+ require 'mocha'
7
+ require 'redgreen'
8
+
9
+ I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2 = 1
10
+
11
+ lib_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
12
+ $: << lib_path unless $:.include?(lib_path)
13
+
14
+ require 'casrack_the_authenticator'
15
+ require 'rack'
16
+ require 'rack/mock'
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factorylabs-casrack_the_authenticator
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 6
8
+ - 0
9
+ version: 1.6.0
10
+ platform: ruby
11
+ authors:
12
+ - James Rosen
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-07 00:00:00 -06:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: nokogiri
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 4
30
+ - 1
31
+ version: 1.4.1
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: thoughtbot-shoulda
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 10
44
+ - 2
45
+ version: 2.10.2
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: jferris-mocha
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ - 9
58
+ - 7
59
+ version: 0.9.7
60
+ type: :development
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: redgreen
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 1
71
+ - 2
72
+ - 2
73
+ version: 1.2.2
74
+ type: :development
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: rack
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 1
85
+ - 0
86
+ - 0
87
+ version: 1.0.0
88
+ type: :development
89
+ version_requirements: *id005
90
+ description: CAS Authentication via Rack Middleware
91
+ email: james.a.rosen@gmail.com
92
+ executables: []
93
+
94
+ extensions: []
95
+
96
+ extra_rdoc_files:
97
+ - README.rdoc
98
+ files:
99
+ - .gitignore
100
+ - README.rdoc
101
+ - Rakefile
102
+ - VERSION
103
+ - casrack_the_authenticator.gemspec
104
+ - developer_tasks/doc.rake
105
+ - developer_tasks/gem.rake
106
+ - developer_tasks/test.rake
107
+ - features/fake.feature
108
+ - features/require_cas.feature
109
+ - features/simple.feature
110
+ - features/step_definitions/fake_cas_steps.rb
111
+ - features/step_definitions/rack_steps.rb
112
+ - features/support/assertions.rb
113
+ - features/support/rack_support.rb
114
+ - lib/casrack_the_authenticator.rb
115
+ - lib/casrack_the_authenticator/configuration.rb
116
+ - lib/casrack_the_authenticator/fake.rb
117
+ - lib/casrack_the_authenticator/require_cas.rb
118
+ - lib/casrack_the_authenticator/service_ticket_validator.rb
119
+ - lib/casrack_the_authenticator/simple.rb
120
+ - test/configuration_test.rb
121
+ - test/fake_test.rb
122
+ - test/service_ticket_validator_test.rb
123
+ - test/simple_test.rb
124
+ - test/test_helper.rb
125
+ has_rdoc: true
126
+ homepage: http://github.com/gcnovus/casrack_the_authenticator
127
+ licenses: []
128
+
129
+ post_install_message:
130
+ rdoc_options:
131
+ - --line-numbers
132
+ - --inline-source
133
+ - --title
134
+ - "Casrack the Authenticator: RDoc"
135
+ - --charset
136
+ - utf-8
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ segments:
151
+ - 0
152
+ version: "0"
153
+ requirements: []
154
+
155
+ rubyforge_project:
156
+ rubygems_version: 1.3.6
157
+ signing_key:
158
+ specification_version: 3
159
+ summary: CAS Authentication via Rack Middleware
160
+ test_files:
161
+ - test/configuration_test.rb
162
+ - test/fake_test.rb
163
+ - test/service_ticket_validator_test.rb
164
+ - test/simple_test.rb
165
+ - test/test_helper.rb