Sha256: edaa1b83a9222e230511e8c11450effcb7757a394dcea48884dd655b03ef22e5

Contents?: true

Size: 1.85 KB

Versions: 11

Compression:

Stored size: 1.85 KB

Contents

module MuckControllerMacros

  def should_require_login(*args)
    args = Hash[*args]
    login_url = args.delete :login_url
    args.each do |action, verb|
      should "Require login 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
        assert_redirected_to(login_url)
      end
    end
  end

  def should_require_role(role, redirect_url, *actions)
    actions.each do |action|
      should "require role for '#{action}' action" do
        get(action)
        ensure_flash(/permission/i)
        assert_response :redirect
      end
    end
  end
  
  #from: http://blog.internautdesign.com/2008/9/11/more-on-custom-shoulda-macros-scoping-of-instance-variables
  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

  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

  # make sure the response body matches the text exactly
  def should_render_text(text)
    should "render text #{text}" do
      assert_equal text, @response.body
    end
  end

  # look for the given text in the response body
  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
  
end

ActionController::TestCase.extend(MuckControllerMacros)

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
muck-users-0.2.16 test/rails_root/test/shoulda_macros/controller.rb
muck-users-0.2.15 test/rails_root/test/shoulda_macros/controller.rb
muck-users-0.2.14 test/rails_root/test/shoulda_macros/controller.rb
muck-users-0.2.13 test/rails_root/test/shoulda_macros/controller.rb
muck-users-0.2.12 test/rails_root/test/shoulda_macros/controller.rb
muck-users-0.2.11 test/rails_root/test/shoulda_macros/controller.rb
muck-users-0.2.10 test/rails_root/test/shoulda_macros/controller.rb
muck-users-0.2.9 test/rails_root/test/shoulda_macros/controller.rb
muck-users-0.2.6 test/rails_root/test/shoulda_macros/controller.rb
muck-users-0.2.7 test/rails_root/test/shoulda_macros/controller.rb
muck-users-0.2.8 test/rails_root/test/shoulda_macros/controller.rb