Sha256: 00b1a2f51128da2027fcc785a3e3a36b9020c12487780825ee617c791a2ff56b

Contents?: true

Size: 1.93 KB

Versions: 5

Compression:

Stored size: 1.93 KB

Contents

require 'spec_helper' 
use_roles_strategy :many_roles

class User 
  include Mongoid::Document  
  include Roles::Mongoid 
  
  strategy :many_roles, :default
  role_class :role

  valid_roles_are :admin, :guest   

  field :name, :type => String 
end

describe "Roles for Mongoid" do  
  
  after :each do
    Database.teardown
  end

  context "default setup" do

    before do              
      @user = User.create(:name => 'Kristian')
      @user.add_roles :guest      
      @user.save     

      @admin_user = User.create(:name => 'Admin user')
      @admin_user.roles = :admin            
      @admin_user.save
    end
    
    describe '#in_role' do
      it "should return first user matching role" do        
        User.in_role(:guest).first.name.should == 'Kristian'      
        User.in_role(:admin).first.name.should == 'Admin user'
      end
    end

    describe "Role API" do
      it "should have admin user role to :admin" do      
        @admin_user.roles_list.first.should == :admin      
        @admin_user.admin?.should be_true

        @admin_user.has_role?(:guest).should be_false
        
        @admin_user.has_role?(:admin).should be_true
        @admin_user.is?(:admin).should be_true
        @admin_user.has_roles?(:admin).should be_true
        @admin_user.has?(:admin).should be_true      
      end

      it "should have user role to :guest" do
        @user.roles_list.first.should == :guest
        @user.admin?.should be_false
      
        @user.has_role?(:guest).should be_true    
        @user.has_role?(:admin).should be_false
        @user.is?(:admin).should be_false
      
        @user.has_roles?(:admin).should be_false
        @user.has?(:admin).should be_false
      end
      
      it "should set user role to :admin using roles=" do
        @user.roles = :admin      
        @user.roles_list.first.should == :admin           
        @user.has_role?(:admin).should be_true      
      end  
    end  
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
roles_mongoid-0.2.4 spec/roles_mongoid/many_roles_spec.rb
roles_for_mongoid-0.2.3 spec/roles_mongoid/many_roles_spec.rb
roles_for_mongoid-0.2.2 spec/roles_mongoid/many_roles_spec.rb
roles_for_mongoid-0.2.1 spec/roles_mongoid/many_roles_spec.rb
roles_for_mongoid-0.2.0 spec/roles_mongoid/many_roles_spec.rb