Guest User

Untitled

a guest
Jul 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. require 'test_helper'
  2.  
  3. class ApplicationControllerTest < ActionController::TestCase
  4. class FooController < ApplicationController
  5. before_filter :authenticate, :only => [:require_authenticated]
  6.  
  7. def show_current_session
  8. render :inline => "<p>#{current_session.inspect}</p>"
  9. end
  10.  
  11. def show_current_user
  12. render :inline => "<p>#{current_user.inspect}</p>"
  13. end
  14.  
  15. def show_signed_in
  16. render :inline => "<p>#{signed_in?.to_s}</p>"
  17. end
  18.  
  19. def denying_access
  20. self.deny_access
  21. end
  22.  
  23. def require_authenticated
  24. render :inline => '<p>Authenticated</p>'
  25. end
  26. end
  27.  
  28. tests FooController
  29.  
  30. ActionController::Routing::Routes.draw do |map|
  31. map.resource :foo, :controller => 'application_controller_test/foo',
  32. :member => { :show_current_session => :get,
  33. :show_current_user => :get,
  34. :show_signed_in => :get,
  35. :denying_access => :get,
  36. :require_authenticated => :get }
  37. end
  38.  
  39. context "when checking if signed in" do
  40. context "when signed in" do
  41. setup do
  42. activate_authlogic
  43. Session.create Factory(:user)
  44. get :show_signed_in
  45. end
  46.  
  47. should 'return true' do
  48. assert_select 'p', 'true'
  49. end
  50. end
  51. end
  52.  
  53. context "when retrieving the current session" do
  54. context "when signed in" do
  55. setup do
  56. activate_authlogic
  57. Session.create Factory(:user)
  58. get :show_current_session
  59. end
  60.  
  61. should 'retrieve the session' do
  62. assert_select 'p', Session.find.inspect
  63. end
  64. end
  65. end
  66.  
  67. context "when retrieving the current user" do
  68. context "when signed in" do
  69. setup do
  70. activate_authlogic
  71. Session.create Factory(:user)
  72. get :show_current_user
  73. end
  74.  
  75. should 'retrieve the user' do
  76. assert_select 'p', Session.find.user.inspect
  77. end
  78. end
  79. end
  80.  
  81. context "on actions requiring authentication" do
  82. context "when signed in" do
  83. setup do
  84. activate_authlogic
  85. Session.create Factory(:user)
  86. get :require_authenticated
  87. end
  88.  
  89. should_respond_with :success
  90.  
  91. should "perform the action" do
  92. assert_select 'p', 'Authenticated'
  93. end
  94. end
  95. end
  96. end
Add Comment
Please, Sign In to add comment