require 'spec_helper'
require 'ruby-debug'

describe AppAuth do 
  
  before(:all)  do
     @app_ary = [
                {"name" => "G_app_1" , "all" => false,"blockusers" => ["a_bad_stud@rulingcom.com"]},
                {"name" => "G_app_2" , "all" => true,"blockusers" => ["a_bad_stud@rulingcom.com"]},
                {"name" => "G_app_3_for_bad" , "all" => true,"blockusers" => []}
      ]
      
      @app_ary.each do |t|
        a = ModuleApp.new :title => t["name"]
        blocked_users = t["blockusers"].collect do |b_user|
          User.first(conditions:{email:b_user})
        end
        a.create_app_auth :all=> t["all"],:blocked_users => blocked_users
        a.save!
      end      
      @good_stud_become_bad = User.first(conditions:{email:"a_good_stud@rulingcom.com"})
      @bad_stud_become_good = User.first(conditions:{email:"a_bad_stud@rulingcom.com"})
      @g_app_1 = ModuleApp.first(conditions:{title:"G_app_1"})
      @g_app_2 = ModuleApp.first(conditions:{title:"G_app_2"})
      @g_app_3 = ModuleApp.first(conditions:{title:"G_app_3_for_bad"})
  end
  
  describe "[Init status check]" do
    it "bad_stud_become_good status " do
      @bad_stud_become_good.avb_apps.should_not include @g_app_1.app_auth
      @bad_stud_become_good.avb_apps.should_not include @g_app_2.app_auth
      @bad_stud_become_good.avb_apps.should include @g_app_3.app_auth
    end
    
    it "good_stud_become_bad status " do
      @good_stud_become_bad.avb_apps.should_not include @g_app_1.app_auth
      @good_stud_become_bad.avb_apps.should include @g_app_2.app_auth
      @good_stud_become_bad.avb_apps.should include @g_app_3.app_auth
    end
  end
  
  describe ".add_user_to_black_list sutd" do
    it "When good stud is blocked with app1 and app2" do
      @g_app_1.app_auth.add_user_to_black_list @good_stud_become_bad
      @g_app_2.app_auth.add_user_to_black_list @good_stud_become_bad
      @good_stud_become_bad.avb_apps.should_not include @g_app_1.app_auth
      @good_stud_become_bad.avb_apps.should_not include @g_app_2.app_auth
    end
  end
  
  describe ".remove_user_from_black_list sutd" do
    it "When bad stud is authed with app1 and app2" do
      @g_app_1.app_auth.remove_user_from_black_list @bad_stud_become_good
      @g_app_2.app_auth.remove_user_from_black_list @bad_stud_become_good
      #@bad_stud_become_good.avb_apps.should include @g_app_1.app_auth
      @bad_stud_become_good.avb_apps.should include @g_app_2.app_auth
    end
  end
  
  describe ".add_user_to_privilege_list sutd" do
    it "When bad stud is auth for one more app" do
      @bad_stud_become_good.avb_apps.should_not include @g_app_1.app_auth   #shouldn't have at first time since app1 is not sat for all
      @g_app_1.app_auth.add_user_to_privilege_list @bad_stud_become_good  #change status
      @bad_stud_become_good.avb_apps.should include @g_app_1.app_auth  #check
    end
  end
  
  describe ".remove_user_from_privilege_list sutd" do
    it "admin mistake. remove user from privilege list" do
      @g_app_1.app_auth.remove_user_from_privilege_list @bad_stud_become_good
      @bad_stud_become_good.avb_apps.should_not include @g_app_1.app_auth
    end
  end
  
  after(:all) do
    @app_ary.each do |item|
      ModuleApp.first(conditions: {title: item["name"]}).destroy
    end
  end
  
end