test/test_canhaz.rb in rails-canhaz-1.0.5 vs test/test_canhaz.rb in rails-canhaz-1.0.6
- old
+ new
@@ -268,7 +268,39 @@
s.cannot!(:foo)
assert_equal false, s.can?(:foo)
end
+ def test_objects_subjects_permission
+
+ s1 = SubjectModel.create!
+ s2 = SubjectModel.create!
+ o1 = ObjectModel.create!
+ o2 = ObjectModel.create!
+ o3 = ObjectModel.create!
+
+ s1.can!(:foo_osp, o1)
+ s1.can!(:foo_osp, o3)
+
+ s2.can!(:foo_osp, o1)
+ s2.can!(:foo_osp, o2)
+
+ res = CanHazPermission.objects_and_subjects_from_permission(ObjectModel, SubjectModel, :foo_osp)
+
+ assert_equal 2, res.count
+
+ subjects = Set.new(res.collect { |row| row[:subject] }) # Sets are unsorted arrays
+
+ assert_equal Set.new([s1, s2]), subjects # Both arrays contain the same values, independently of the order
+
+ objects = Set.new(res.find { |row| row[:subject] == s1 }[:objects])
+
+ assert_equal Set.new([o1, o3]), objects
+
+ objects = Set.new(res.find { |row| row[:subject] == s2 }[:objects])
+
+ assert_equal Set.new([o1, o2]), objects
+
+ end
+
end