Module: MuckControllerMacros
- Defined in:
- lib/test/shoulda_macros/controller.rb
Instance Method Summary collapse
- #should_allow(action, object, msg = nil) ⇒ Object
- #should_not_allow(action, object, url = "/login", msg = nil) ⇒ Object
-
#should_render_partial_text(text) ⇒ Object
look for the given text in the response body.
-
#should_render_text(text) ⇒ Object
make sure the response body matches the text exactly.
-
#should_require_login(*args) ⇒ Object
Ensure a login is required for the given actions Parameters: Pass the following as arguements :login_url => ‘/login’ – url the user should be redirected to if they aren’t logged in.
-
#should_require_role(role, *args) ⇒ Object
Ensure the user is in a given role for the given actions.
Instance Method Details
#should_allow(action, object, msg = nil) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/test/shoulda_macros/controller.rb', line 73 def should_allow action, object, msg=nil msg ||= "a #{object.class.to_s.downcase}" should "be able to #{action} #{msg}" do object = eval(object, self.send(:binding), __FILE__, __LINE__) get action, :id => object.id assert_response :success end end |
#should_not_allow(action, object, url = "/login", msg = nil) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/test/shoulda_macros/controller.rb', line 64 def should_not_allow action, object, url= "/login", msg=nil msg ||= "a #{object.class.to_s.downcase}" should "not be able to #{action} #{msg}" do object = eval(object, self.send(:binding), __FILE__, __LINE__) get action, :id => object.id assert_redirected_to url end end |
#should_render_partial_text(text) ⇒ Object
look for the given text in the response body
90 91 92 93 94 |
# File 'lib/test/shoulda_macros/controller.rb', line 90 def should_render_partial_text(text) should "contain text #{text}" do assert @response.body.include?(text), "Response did not contain the text '#{text}'" end end |
#should_render_text(text) ⇒ Object
make sure the response body matches the text exactly
83 84 85 86 87 |
# File 'lib/test/shoulda_macros/controller.rb', line 83 def should_render_text(text) should "render text #{text}" do assert_equal text, @response.body end end |
#should_require_login(*args) ⇒ Object
Ensure a login is required for the given actions Parameters:
Pass the following as arguements
:login_url => '/login' -- url the user should be redirected to if they aren't logged in. Defaults to '/login'
:get => 'index'
:post => 'create'
Example:
should_require_login :login_url => '/signup', :get => 'index'
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/test/shoulda_macros/controller.rb', line 12 def should_require_login(*args) args = Hash[*args] login_url = args.delete :login_url login_url ||= '/login' args.each do |action, verb| should "Require login for '#{action}' action" do if [:put, :delete].include?(verb) || [:edit, :show].include?(action) # edit, show, put and delete require an id even if it is a bogus one send(verb, action, :id => 1) else send(verb, action) end assert_redirected_to(login_url) end end end |
#should_require_role(role, *args) ⇒ Object
Ensure the user is in a given role for the given actions. The test user will need to be logged in. Parameters:
role: The role required for the user
Pass the following as arguements
:login_url => '/login' -- url the user should be redirected to if they aren't logged in. Defaults to '/login'
:get => 'index'
:post => 'create'
Example:
context "logged in not admin" do
setup do
@user = Factory(:user)
activate_authlogic
login_as @user
end
should_require_role('admin', :redirect_url => '/login', :index => :get)
end
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/test/shoulda_macros/controller.rb', line 46 def should_require_role(role, *args) args = Hash[*args] redirect_url = args.delete :redirect_url redirect_url ||= '/login' args.each do |action, verb| should "require role for '#{action}' action" do if [:put, :delete].include?(verb) # put and delete require an id even if it is a bogus one send(verb, action, :id => 1) else send(verb, action) end ensure_flash(/permission/i) assert_redirected_to(redirect_url) end end end |