-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathcasserver_spec.rb
More file actions
221 lines (166 loc) · 6.47 KB
/
casserver_spec.rb
File metadata and controls
221 lines (166 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# encoding: UTF-8
require File.dirname(__FILE__) + '/spec_helper'
$LOG = Logger.new(File.basename(__FILE__).gsub('.rb','.log'))
RSpec.configure do |config|
config.include Capybara::DSL
end
VALID_USERNAME = 'spec_user'
VALID_PASSWORD = 'spec_password'
ATTACK_USERNAME = '%3E%22%27%3E%3Cscript%3Ealert%2826%29%3C%2Fscript%3E&password=%3E%22%27%3E%3Cscript%3Ealert%2826%29%3C%2Fscript%3E<=%3E%22%27%3E%3Cscript%3Ealert%2826%29%3C%2Fscript%3E&service=%3E%22%27%3E%3Cscript%3Ealert%2826%29%3C%2Fscript%3E'
INVALID_PASSWORD = 'invalid_password'
describe 'CASServer' do
include Rack::Test::Methods
before do
@target_service = 'http://my.app.test'
end
describe "/login" do
before do
load_server("default_config")
reset_spec_database
end
it "logs in successfully with valid username and password without a target service" do
visit "/login"
fill_in 'username', :with => VALID_USERNAME
fill_in 'password', :with => VALID_PASSWORD
click_button 'login-submit'
page.should have_content("You have successfully logged in")
Capybara.current_session.driver.response.headers['Set-Cookie'].should match 'path=/'
end
it "fails to log in with invalid password" do
visit "/login"
fill_in 'username', :with => VALID_USERNAME
fill_in 'password', :with => INVALID_PASSWORD
click_button 'login-submit'
page.should have_content("Incorrect username or password")
end
it "logs in successfully with valid username and password and redirects to target service" do
visit "/login?service="+CGI.escape(@target_service)
fill_in 'username', :with => VALID_USERNAME
fill_in 'password', :with => VALID_PASSWORD
click_button 'login-submit'
page.current_url.should =~ /^#{Regexp.escape(@target_service)}\/?\?ticket=ST\-[1-9rA-Z]+/
end
it "preserves target service after invalid login" do
visit "/login?service="+CGI.escape(@target_service)
fill_in 'username', :with => VALID_USERNAME
fill_in 'password', :with => INVALID_PASSWORD
click_button 'login-submit'
page.should have_content("Incorrect username or password")
page.should have_xpath('//input[@id="service"]', :value => @target_service)
end
it "uses appropriate localization based on Accept-Language header" do
page.driver.options[:headers] = {'HTTP_ACCEPT_LANGUAGE' => 'pl'}
#visit "/login?lang=pl"
visit "/login"
page.should have_content("Użytkownik")
page.driver.options[:headers] = {'HTTP_ACCEPT_LANGUAGE' => 'pt_BR'}
#visit "/login?lang=pt_BR"
visit "/login"
page.should have_content("Usuário")
page.driver.options[:headers] = {'HTTP_ACCEPT_LANGUAGE' => 'en'}
#visit "/login?lang=en"
visit "/login"
page.should have_content("Username")
end
it "is not vunerable to Cross Site Scripting" do
visit '/login?service=%22%2F%3E%3cscript%3ealert%2832%29%3c%2fscript%3e'
page.should_not have_content("alert(32)")
page.should_not have_xpath("//script")
#page.should have_xpath("<script>alert(32)</script>")
end
end # describe '/login'
describe '/logout' do
describe 'user logged in' do
before do
load_server("default_config")
reset_spec_database
visit "/login"
fill_in 'username', :with => VALID_USERNAME
fill_in 'password', :with => VALID_PASSWORD
click_button 'login-submit'
page.should have_content("You have successfully logged in")
end
it "logs out user who is looged in" do
visit "/logout"
page.should have_content("You have successfully logged out")
end
it "logs out successfully and redirects to target service" do
visit "/logout?gateway=true&service="+CGI.escape(@target_service)
page.current_url.should =~ /^#{Regexp.escape(@target_service)}\/?/
end
end
describe "user not logged in" do
it "try logs out user which is not logged in" do
visit "/logout"
page.should have_content("You have successfully logged out")
end
end
end # describe '/logout'
describe 'Configuration' do
it "uri_path value changes prefix of routes" do
load_server("alt_config")
@target_service = 'http://my.app.test'
visit "/test/login"
page.status_code.should_not == 404
visit "/test/logout"
page.status_code.should_not == 404
end
end
describe 'validation' do
let(:allowed_ip) { '127.0.0.1' }
let(:unallowed_ip) { '10.0.0.1' }
let(:service) { @target_service }
before do
load_server('default_config') # 127.0.0.0/24 is allowed here
reset_spec_database
ticket = get_ticket_for(service)
Rack::Request.any_instance.stub(:ip).and_return(request_ip)
get "/#{path}?service=#{CGI.escape(service)}&ticket=#{CGI.escape(ticket)}"
end
subject { last_response }
describe 'validate' do
let(:path) { 'validate' }
context 'from allowed IP' do
let(:request_ip) { allowed_ip }
it { should be_ok }
its(:body) { should match 'yes' }
end
context 'from unallowed IP' do
let(:request_ip) { unallowed_ip }
its(:status) { should eql 422 }
its(:body) { should match 'no' }
end
end
describe 'serviceValidate' do
let(:path) { 'serviceValidate' }
context 'from allowed IP' do
let(:request_ip) { allowed_ip }
it { should be_ok }
its(:content_type) { should match 'text/xml' }
its(:body) { should match /cas:authenticationSuccess/i }
its(:body) { should match '<test_utf_string>Ютф</test_utf_string>' }
end
context 'from unallowed IP' do
let(:request_ip) { unallowed_ip }
its(:status) { should eql 422 }
its(:content_type) { should match 'text/xml' }
its(:body) { should match /cas:authenticationFailure.*INVALID_REQUEST/i }
end
end
describe 'proxyValidate' do
let(:path) { 'proxyValidate' }
context 'from allowed IP' do
let(:request_ip) { allowed_ip }
it { should be_ok }
its(:content_type) { should match 'text/xml' }
its(:body) { should match /cas:authenticationSuccess/i }
end
context 'from unallowed IP' do
let(:request_ip) { unallowed_ip }
its(:status) { should eql 422 }
its(:content_type) { should match 'text/xml' }
its(:body) { should match /cas:authenticationFailure.*INVALID_REQUEST/i }
end
end
end
end