test/law_test.rb in stasi-0.0.1.alpha vs test/law_test.rb in stasi-0.1.0
- old
+ new
@@ -6,11 +6,11 @@
@law = Robotnik::Authorization::Law.new
end
test "it has rules" do
rules = @law.send :rules
- assert_instance_of ActiveSupport::HashWithIndifferentAccess, rules
+ assert_instance_of Hash, rules
end
test "it initiates rules for role" do
@law.send :init_role_for, :role
rules = @law.instance_variable_get('@rules')
@@ -18,65 +18,91 @@
assert_instance_of Robotnik::Authorization::Status, rules[:role]
end
test "it yields status for authorization definition" do
@law.status :admin do
- can :read, :book
+ can :read, Object
end
- assert @law.instance_variable_get('@rules')[:admin].can? :read, :book, nil
+ assert @law.instance_variable_get('@rules')[:admin].can? :read, Object.new
end
test "it has statuses" do
@law.status :admin do
end
- assert_equal ['admin'], @law.statuses
+ assert_equal [:admin], @law.statuses
end
test "it tests status on given object" do
- object = Object.new
- class << object
+ user = Object.new
+ class << user
def admin
true
end
def not_admin
false
end
end
@law.status :admin do
- can :read, :book
+ can :read, Object
end
@law.status :not_admin do
- cannot :read, :book
+ cannot :read, Object
end
- assert @law.can? object, :read, :book, nil
- class << object
+ assert @law.can? :read, Object.new, {agent: user}
+ class << user
def admin
false
end
def not_admin
true
end
end
- refute @law.can? object, :read, :book, nil
+ refute @law.can? :read, Object.new, {agent: user}
end
test "it returns false if no status returns true" do
- object = Object.new
- class << object
+ user = Object.new
+ class << user
def admin
false
end
end
@law.status :admin do
end
- refute @law.can? object, :do, :something, nil
+ refute @law.can? :do, :something, {agent: user}
end
test "it defines a new law at class level" do
Robotnik::Authorization::Law.define do
status :admin do
end
end
- assert_equal ['admin'], Robotnik::Authorization::Law.law.statuses
+ assert_equal [:admin], Robotnik::Authorization::Law.law.statuses
+ end
+
+ test "it parses all stauses" do
+ user = Object.new
+ class << user
+ def is_a_a
+ true
+ end
+ def is_a_b
+ true
+ end
+ end
+ @law.status :is_a_a do
+ can :read, Object
+ end
+ @law.status :is_a_b do
+ cannot :read, Object
+ end
+ refute @law.can? :read, Object.new, {agent: user}
+ end
+
+ test "it accepts defaults" do
+ @law.default do
+ can :read, Object
+ end
+ assert @law.can? :read, Object.new, {agent: Object.new}
end
end