require 'spec_helper'
require 'ruby-debug'


describe AppAuth do 
  
  before  do
    User.all.destroy
    UserRole.all.destroy
    AppAuth.all.destroy
    ModuleApp.all.destroy
    
    #Create some fixtures of Main Role
    main_role_key = ["Stud","Teacher","Staff"]
    @new_main_role_list = main_role_key.each do |role|
      new_role = UserRole.new :key => role
      
      new_role.save
    end
    #Create some users of User
    user_emails = ["a_good_stud","a_bad_stud","a_teacher","a_staff"]
    user_emails.each do |user_email|
      email=user_email+"@rulingcom.com"
      new_user = User.new :email=> email
      new_user.save
    end
     
    @stud_MRK = UserRole.first(conditions:{key:"Stud"})
    @teacher_MRK = UserRole.first(conditions:{key:"Teacher"})
    @staff_MRK = UserRole.first(conditions:{key:"Staff"})

    @good_stu = User.first(conditions:{email:"a_good_stud@rulingcom.com"})
    @bad_stu = User.first(conditions:{email:"a_bad_stud@rulingcom.com"})
    @teacher = User.first(conditions:{email:"a_teacher@rulingcom.com"})
    @staff =  User.first(conditions:{email:"a_staff@rulingcom.com"})
    
    #setting Roles for users
    @good_stu.user_role = @stud_MRK
    @bad_stu.user_role = @stud_MRK
    @teacher.user_role = @teacher_MRK
    @staff.user_role = @staff_MRK

    @good_stu.save!
    @bad_stu.save!
    @teacher.save!
    @staff.save!

  end
  describe "Testing basic structure" do
    before do
      @app_auth = AppAuth.new()
      #all stud has access right
      @app_auth.user_roles << @stud_MRK

      #a_bad_stud add to block to app_auth 
      @app_auth.blocked_users << @bad_stu

      #all teacher has access right
      @app_auth.user_roles << @teacher_MRK

      @app_auth.privilege_users << @staff
      @app_auth.save!
    end
    context "Should just initialize all obj that is needed" do

      it "Testing @app_auth init result" do
        @app_auth.user_roles.should have(2).item
      end

      it "@app_auth should have UserRoles: Stud , Teacher " do
        key_ary = @app_auth.user_roles.collect do |role|
          role.key
        end
        key_ary.should == ["Stud","Teacher"]
      end
      
      it "@app_auth should have one Privialage user which is belongs to Staff" do
        p_user_ary = @app_auth.privilege_users.collect do |p_user|
          p_user.user_role.key
        end
        p_user_ary.should include("Staff")
      end
      
      it "@app_auth should have one student listed at blocklist" do
        @bad_stu = User.first(conditions:{email:"a_bad_stud@rulingcom.com"})
        @app_auth.blocked_users.should have(1).item
        @app_auth.blocked_users.should include(@bad_stu)
      end
          
      it "[Development #1]-1.Authorizing roles: roles + blocklist" do
        @good_stu = User.first(conditions:{email:"a_good_stud@rulingcom.com"})
        @teacher = User.first(conditions:{email:"a_teacher@rulingcom.com"})
        @staff =  User.first(conditions:{email:"a_staff@rulingcom.com"})
        ary = [@good_stu,@teacher,@staff]
        @app_auth.auth_users_after_block_list.should == ary
      end
      
      it "[Development #1]-2.Authorizing single users: list of users [new_user1~2]" do
        user_emails = ["new_user1","new_user2","new_user3","new_user4"]
        user_emails.each do |user_email|
          email=user_email+"@rulingcom.com"
          new_user = User.new :email=> email
          new_user.save
        end
        user1= User.first(conditions:{email:"new_user1@rulingcom.com"})
        user2= User.first(conditions:{email:"new_user2@rulingcom.com"})
        user3= User.first(conditions:{email:"new_user3@rulingcom.com"})
        user4= User.first(conditions:{email:"new_user4@rulingcom.com"})
        
        @app_auth.privilege_users << user1
        @app_auth.privilege_users << user2
        
        @app_auth.auth_users_after_block_list.should include(user1,user2)
        @app_auth.auth_users_after_block_list.should_not include(user3,user4)
        
      end
      
      it "[Development #1]-3.Authorizing roles and single users: roles + blocklist + list of users" do
        @app_auth.auth_users.should have(4).item 
      end
      
      it "[Development #1]-4.Authorizing all: blocklist" do
        @bad_stu = User.first(conditions:{email:"a_bad_stud@rulingcom.com"})
        @new_app_auth = (AppAuth.new :all => true)
        @new_app_auth.blocked_users << @bad_stu 
        
        @new_app_auth.auth_users.should == User.all.entries
        @new_app_auth.auth_users_after_block_list.should_not include(@bad_stu)
        @new_app_auth.save!
      end
    end
    
    
    
  end

end