PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/test/functional/test_protected.rb

http://github.com/jnunemaker/mongomapper
Ruby | 201 lines | 180 code | 21 blank | 0 comment | 12 complexity | 8f4acde1f641eda433b7b4ddb3e0a441 MD5 | raw file
  1. require 'test_helper'
  2. class ProtectedTest < Test::Unit::TestCase
  3. context 'A document with protected attributes' do
  4. setup do
  5. @doc_class = Doc do
  6. key :name, String
  7. key :admin, Boolean, :default => false
  8. attr_protected :admin
  9. end
  10. @doc = @doc_class.create(:name => 'Steve Sloan')
  11. end
  12. should "have protected attributes class method" do
  13. @doc_class.protected_attributes.should == [:admin].to_set
  14. end
  15. should "default protected attributes to nil" do
  16. Doc().protected_attributes.should be_nil
  17. end
  18. should "have protected attributes instance method" do
  19. @doc.protected_attributes.should equal(@doc_class.protected_attributes)
  20. end
  21. should "raise error if there are accessible attributes" do
  22. doc = Doc('Post')
  23. doc.attr_accessible :name
  24. lambda { doc.attr_protected :admin }.
  25. should raise_error(/Declare either attr_protected or attr_accessible for Post/)
  26. end
  27. should "know if using protected attributes" do
  28. @doc_class.protected_attributes?.should be(true)
  29. Doc().protected_attributes?.should be(false)
  30. end
  31. should "work with :protected shortcut when defining key" do
  32. Doc() do
  33. key :user_id, ObjectId, :protected => true
  34. end.protected_attributes.should == [:user_id].to_set
  35. end
  36. should "assign protected attribute through accessor" do
  37. @doc.admin = true
  38. @doc.admin.should be_true
  39. end
  40. should "ignore protected attribute on #initialize" do
  41. doc = @doc_class.new(:name => 'John', :admin => true)
  42. doc.admin.should be_false
  43. doc.name.should == 'John'
  44. end
  45. should "not ignore protected attributes on #initialize from the database" do
  46. doc = @doc_class.new(:name => 'John')
  47. doc.admin = true
  48. doc.save!
  49. doc = @doc_class.first(:name => 'John')
  50. doc.admin.should be_true
  51. doc.name.should == 'John'
  52. end
  53. should "not ignore protected attributes on #reload" do
  54. doc = @doc_class.new(:name => 'John')
  55. doc.admin = true
  56. doc.save!
  57. doc.reload
  58. doc.admin.should be_true
  59. doc.name.should == 'John'
  60. end
  61. should "ignore protected attribute on #update_attribute" do
  62. @doc.update_attribute('admin', true)
  63. @doc.admin.should be_true
  64. end
  65. should "ignore protected attribute on #update_attributes" do
  66. @doc.update_attributes(:name => 'Ren Hoek', :admin => true)
  67. @doc.name.should == 'Ren Hoek'
  68. @doc.admin.should be_false
  69. end
  70. should "ignore protected attribute on #update_attributes!" do
  71. @doc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
  72. @doc.name.should == 'Stimpson J. Cat'
  73. @doc.admin.should be_false
  74. end
  75. should "ignore protecteds attribute on #attributes=" do
  76. @doc.attributes = {:name => 'Stimpson J. Cat', :admin => true}
  77. @doc.name.should == 'Stimpson J. Cat'
  78. @doc.admin.should be_false
  79. end
  80. should "be indifferent to whether the protected keys are strings or symbols" do
  81. @doc.update_attributes!("name" => 'Stimpson J. Cat', "admin" => true)
  82. @doc.name.should == 'Stimpson J. Cat'
  83. @doc.admin.should be_false
  84. end
  85. should "accept nil as constructor's argument without raising exception" do
  86. lambda { @doc_class.new(nil) }.should_not raise_error
  87. end
  88. end
  89. context "Single collection inherited protected attributes" do
  90. setup do
  91. class ::GrandParent
  92. include MongoMapper::Document
  93. key :site_id, ObjectId
  94. attr_protected :site_id
  95. end
  96. GrandParent.collection.remove
  97. class ::Child < ::GrandParent
  98. key :position, Integer
  99. attr_protected :position
  100. end
  101. class ::GrandChild < ::Child; end
  102. class ::OtherChild < ::GrandParent
  103. key :blog_id, ObjectId
  104. attr_protected :blog_id
  105. end
  106. end
  107. teardown do
  108. Object.send :remove_const, 'GrandParent' if defined?(::GrandParent)
  109. Object.send :remove_const, 'Child' if defined?(::Child)
  110. Object.send :remove_const, 'GrandChild' if defined?(::GrandChild)
  111. Object.send :remove_const, 'OtherChild' if defined?(::OtherChild)
  112. end
  113. should "share keys down the inheritance trail" do
  114. GrandParent.protected_attributes.should == [:site_id].to_set
  115. Child.protected_attributes.should == [:site_id, :position].to_set
  116. GrandChild.protected_attributes.should == [:site_id, :position].to_set
  117. OtherChild.protected_attributes.should == [:site_id, :blog_id].to_set
  118. end
  119. end
  120. context 'An embedded document with protected attributes' do
  121. setup do
  122. @doc_class = Doc('Project')
  123. @edoc_class = EDoc('Person') do
  124. key :name, String
  125. key :admin, Boolean, :default => false
  126. attr_protected :admin
  127. end
  128. @doc_class.many :people, :class => @edoc_class
  129. @doc = @doc_class.create(:title => 'MongoMapper')
  130. @edoc = @edoc_class.new(:name => 'Steve Sloan')
  131. @doc.people << @edoc
  132. end
  133. should "have protected attributes class method" do
  134. @edoc_class.protected_attributes.should == [:admin].to_set
  135. end
  136. should "default protected attributes to nil" do
  137. EDoc().protected_attributes.should be_nil
  138. end
  139. should "have protected attributes instance method" do
  140. @edoc.protected_attributes.should equal(@edoc_class.protected_attributes)
  141. end
  142. should "assign protected attribute through accessor" do
  143. @edoc.admin = true
  144. @edoc.admin.should be_true
  145. end
  146. should "not ignore protected attribute on #update_attribute" do
  147. @edoc.update_attribute('admin', true)
  148. @edoc.admin.should be_true
  149. end
  150. should "ignore protected attribute on #update_attributes" do
  151. @edoc.update_attributes(:name => 'Ren Hoek', :admin => true)
  152. @edoc.name.should == 'Ren Hoek'
  153. @edoc.admin.should be_false
  154. end
  155. should "ignore protected attribute on #update_attributes!" do
  156. @edoc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
  157. @edoc.name.should == 'Stimpson J. Cat'
  158. @edoc.admin.should be_false
  159. end
  160. end
  161. end