test/status_test.rb in stasi-0.1.0 vs test/status_test.rb in stasi-0.1.2

- old
+ new

@@ -1,10 +1,18 @@ require 'test_helper' class StatusTest < ActiveSupport::TestCase Book = Class.new + Book.class_eval do + def method_returning_true + true + end + def method_returning_false + false + end + end def setup @status = Robotnik::Authorization::Status.new end @@ -30,11 +38,11 @@ @status.can :read, Book assert @status.can? :read, Book.new assert_equal true, @status.instance_variable_get('@rules')[Book][:read] end - test "it defines authorization with if and unless options" do + test "it defines authorization with if and unless options and a Proc" do Post = Struct.new :name assertions = [true, false, false, true, false, true, false, false] [[true, nil], [nil, true], [false, nil], [nil, false], [true, true], [true, false], [false, true], [false, false]].each_with_index do |conditions, i| conditions_hash = {} [:if, :unless].each_with_index do |operator, j| @@ -49,10 +57,28 @@ @status.can :read, Post, conditions_hash assert_equal assertions[i], @status.can?(:read, Post.new('test')) end end + test "it defines authorization with if and unless options and a symbol" do + assertions = [true, false, false, true, false, true, false, false] + [[true, nil], [nil, true], [false, nil], [nil, false], [true, true], [true, false], [false, true], [false, false]].each_with_index do |conditions, i| + conditions_hash = {} + [:if, :unless].each_with_index do |operator, j| + unless conditions[j].nil? + if conditions[j] + conditions_hash[operator] = :method_returning_true + else + conditions_hash[operator] = :method_returning_false + end + end + end + @status.can :read, Book, conditions_hash + assert_equal assertions[i], @status.can?(:read, Book.new) + end + end + test "it defines authorizations with a block" do Book.class_eval do attr_accessor :collection def self.all Book.new.tap do |book| @@ -107,8 +133,14 @@ end end @status.cannot :read, Object @status.can :read, :taggable assert @status.can? :read, o + end + + test "it overrides the matching condition when :as option is present" do + @status.can :read, Fixnum + refute @status.can? :read, Object.new + assert @status.can? :read, Object.new, as: Fixnum end end