Sha256: 35a5f0f2d3edd4b5ad50f79271cefa69f0cb025544b6e766b3c4160c0ae66297

Contents?: true

Size: 1.69 KB

Versions: 37

Compression:

Stored size: 1.69 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
        send(verb, action)
        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

37 entries across 37 versions & 2 rubygems

Version Path
muck-engine-0.2.13 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.2.12 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.2.11 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.2.10 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.2.9 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.2.8 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.2.7 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.2.6 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.2.5 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.2.4 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.2.3 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.2.2 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.2.1 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.2.0 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.1.32 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.1.31 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.1.24 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.1.25 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.1.26 test/rails_root/test/shoulda_macros/controller.rb
muck-engine-0.1.27 test/rails_root/test/shoulda_macros/controller.rb