PageRenderTime 69ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/test/ssl_requirement_test.rb

https://github.com/technicalpickles/ssl_requirement
Ruby | 220 lines | 179 code | 41 blank | 0 comment | 1 complexity | 03f55293213fb6205a9eb904de1ef439 MD5 | raw file
  1. require 'set'
  2. require 'rubygems'
  3. require 'activesupport'
  4. begin
  5. require 'action_controller'
  6. rescue LoadError
  7. if ENV['ACTIONCONTROLLER_PATH'].nil?
  8. abort <<MSG
  9. Please set the ACTIONCONTROLLER_PATH environment variable to the directory
  10. containing the action_controller.rb file.
  11. MSG
  12. else
  13. $LOAD_PATH.unshift ENV['ACTIONCONTROLLER_PATH']
  14. begin
  15. require 'action_controller'
  16. rescue LoadError
  17. abort "ActionController could not be found."
  18. end
  19. end
  20. end
  21. require 'action_controller/test_process'
  22. require 'test/unit'
  23. require "#{File.dirname(__FILE__)}/../lib/ssl_requirement"
  24. ActionController::Base.logger = nil
  25. ActionController::Routing::Routes.reload rescue nil
  26. class SslRequirementController < ActionController::Base
  27. include SslRequirement
  28. ssl_required :a, :b
  29. ssl_allowed :c
  30. def a
  31. render :nothing => true
  32. end
  33. def b
  34. render :nothing => true
  35. end
  36. def c
  37. render :nothing => true
  38. end
  39. def d
  40. render :nothing => true
  41. end
  42. def set_flash
  43. flash[:foo] = "bar"
  44. end
  45. end
  46. class SslExceptionController < ActionController::Base
  47. include SslRequirement
  48. ssl_required :a
  49. ssl_exceptions :b
  50. ssl_allowed :d
  51. def a
  52. render :nothing => true
  53. end
  54. def b
  55. render :nothing => true
  56. end
  57. def c
  58. render :nothing => true
  59. end
  60. def d
  61. render :nothing => true
  62. end
  63. def set_flash
  64. flash[:foo] = "bar"
  65. end
  66. end
  67. class SslAllActionsController < ActionController::Base
  68. include SslRequirement
  69. ssl_exceptions
  70. def a
  71. render :nothing => true
  72. end
  73. end
  74. class SslRequirementTest < Test::Unit::TestCase
  75. def setup
  76. @controller = SslRequirementController.new
  77. @request = ActionController::TestRequest.new
  78. @response = ActionController::TestResponse.new
  79. end
  80. def test_redirect_to_https_preserves_flash
  81. get :set_flash
  82. get :b
  83. assert_response :redirect
  84. assert_equal "bar", flash[:foo]
  85. end
  86. def test_not_redirecting_to_https_does_not_preserve_the_flash
  87. get :set_flash
  88. get :d
  89. assert_response :success
  90. assert_nil flash[:foo]
  91. end
  92. def test_redirect_to_http_preserves_flash
  93. get :set_flash
  94. @request.env['HTTPS'] = "on"
  95. get :d
  96. assert_response :redirect
  97. assert_equal "bar", flash[:foo]
  98. end
  99. def test_not_redirecting_to_http_does_not_preserve_the_flash
  100. get :set_flash
  101. @request.env['HTTPS'] = "on"
  102. get :a
  103. assert_response :success
  104. assert_nil flash[:foo]
  105. end
  106. def test_required_without_ssl
  107. assert_not_equal "on", @request.env["HTTPS"]
  108. get :a
  109. assert_response :redirect
  110. assert_match %r{^https://}, @response.headers['Location']
  111. get :b
  112. assert_response :redirect
  113. assert_match %r{^https://}, @response.headers['Location']
  114. end
  115. def test_required_with_ssl
  116. @request.env['HTTPS'] = "on"
  117. get :a
  118. assert_response :success
  119. get :b
  120. assert_response :success
  121. end
  122. def test_disallowed_without_ssl
  123. assert_not_equal "on", @request.env["HTTPS"]
  124. get :d
  125. assert_response :success
  126. end
  127. def test_ssl_exceptions_without_ssl
  128. @controller = SslExceptionController.new
  129. get :a
  130. assert_response :redirect
  131. assert_match %r{^https://}, @response.headers['Location']
  132. get :b
  133. assert_response :success
  134. get :c # c is not explicity in ssl_required, but it is not listed in ssl_exceptions
  135. assert_response :redirect
  136. assert_match %r{^https://}, @response.headers['Location']
  137. end
  138. def test_ssl_exceptions_with_ssl
  139. @controller = SslExceptionController.new
  140. @request.env['HTTPS'] = "on"
  141. get :a
  142. assert_response :success
  143. @request.env['HTTPS'] = "on"
  144. get :c
  145. assert_response :success
  146. end
  147. def test_ssl_all_actions_without_ssl
  148. @controller = SslAllActionsController.new
  149. get :a
  150. assert_response :redirect
  151. assert_match %r{^https://}, @response.headers['Location']
  152. end
  153. def test_disallowed_with_ssl
  154. @request.env['HTTPS'] = "on"
  155. get :d
  156. assert_response :redirect
  157. assert_match %r{^http://}, @response.headers['Location']
  158. end
  159. def test_allowed_without_ssl
  160. assert_not_equal "on", @request.env["HTTPS"]
  161. get :c
  162. assert_response :success
  163. end
  164. def test_allowed_with_ssl
  165. @request.env['HTTPS'] = "on"
  166. get :c
  167. assert_response :success
  168. end
  169. def test_disable_ssl_check
  170. SslRequirement.disable_ssl_check = true
  171. assert_not_equal "on", @request.env["HTTPS"]
  172. get :a
  173. assert_response :success
  174. get :b
  175. assert_response :success
  176. ensure
  177. SslRequirement.disable_ssl_check = false
  178. end
  179. end