Sha256: 9e8b7960840ce28b438aabc32303627a90010776b2bf32d8d6b24ea2554fda05

Contents?: true

Size: 1.96 KB

Versions: 8

Compression:

Stored size: 1.96 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

ActiveSupport::TestCase.extend(MuckControllerMacros)
Test::Unit::TestCase.extend(MuckControllerMacros)
ActionController::TestCase.extend(MuckControllerMacros)

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
muck-engine-0.2.22 lib/test/shoulda_macros/controller.rb
muck-engine-0.2.21 lib/test/shoulda_macros/controller.rb
muck-engine-0.2.20 lib/test/shoulda_macros/controller.rb
muck-engine-0.2.19 lib/test/shoulda_macros/controller.rb
muck-engine-0.2.18 lib/test/shoulda_macros/controller.rb
muck-engine-0.2.17 lib/test/shoulda_macros/controller.rb
muck-engine-0.2.16 lib/test/shoulda_macros/controller.rb
muck-engine-0.2.15 lib/test/shoulda_macros/controller.rb