casserver_spec.rb 6.4 KB

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