public
Description: A library for safe evaluation of Ruby code based on ParseTree/RubyParser and Ruby2Ruby. Provides Rails ActionView template handlers for ERB and Haml.
Homepage: http://www.artweb-design.de
Clone URL: git://github.com/svenfuchs/safemode.git
safemode / test / test_jail.rb
100644 51 lines (39 sloc) 1.564 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require File.join(File.dirname(__FILE__), 'test_helper')
 
class TestJail < Test::Unit::TestCase
  def setup
    @article = Article.new.to_jail
    @comment = @article.comments.first
  end
  
  def test_explicitly_allowed_methods_should_be_accessible
    assert_nothing_raised { @article.title }
  end
  
  def test_jail_instance_methods_should_be_accessible
    assert_nothing_raised { @article.author_name }
  end
  
  def test_sending_to_jail_to_an_object_should_return_a_jail
    assert_equal "Article::Jail", @article.class.name
  end
  
  def test_jail_instances_should_have_limited_methods
    expected = ["class", "inspect", "method_missing", "methods", "respond_to?", "to_jail", "to_s", "instance_variable_get"]
    objects.each do |object|
      assert_equal expected.sort, reject_pretty_methods(object.to_jail.methods.sort)
    end
  end
  
  def test_jail_classes_should_have_limited_methods
    expected = ["new", "methods", "name", "inherited", "method_added", "inspect",
                "allow", "allowed?", "allowed_methods", "init_allowed_methods"]
    objects.each do |object|
      assert_equal expected.sort, reject_pretty_methods(object.to_jail.class.methods.sort)
    end
  end
  
  def test_allowed_methods_should_be_propagated_to_subclasses
    assert_equal Article::Jail.allowed_methods, Article::ExtendedJail.allowed_methods
  end
  
  private
  
  def objects
    [[], {}, 1..2, "a", :a, Time.now, 1, 1.0, nil, false, true]
  end
  
  def reject_pretty_methods(methods)
    methods.reject{ |method| method =~ /^pretty_/ }
  end
  
end