<commit_before>require 'test_helper'

class PeopleControllerTest < ActionController::TestCase

  fixtures :people,:users, :projects, :work_groups, :group_memberships, :project_roles

  include AuthenticatedTestHelper
  include RestTestCases
  include ApplicationHelper

  def setup
    login_as(:quentin)
  end

  def rest_api_test_object
    @object=people(:quentin_person)
  end

  def test_title
    get :index
    assert_select "title", :text=>/The Sysmo SEEK People.*/, :count=>1
  end

  def test_xml_for_person_with_tools_and_expertise
    p=Factory :person
    Factory :expertise,:value=>"golf",:annotatable=>p
    Factory :expertise,:value=>"fishing",:annotatable=>p
    Factory :tool,:value=>"fishing rod",:annotatable=>p

    test_get_rest_api_xml p

    doc = LibXML::XML::Document.string(@response.body)
    doc.root.namespaces.default_prefix="s"
    assert_equal 2, doc.find("//s:tags/s:tag[@context='expertise']").count
    assert_equal 1,doc.find("//s:tags/s:tag[@context='tool']").count
    assert_equal "fishing rod",doc.find("//s:tags/s:tag[@context='tool']").first.content
  end

  def test_should_get_index
    get :index
    assert_response :success
    assert_not_nil assigns(:people)
  end

  def test_should_get_new
    get :new
    assert_response :success
  end

  def test_first_registered_person_is_admin

    Person.destroy_all
    assert_equal 0, Person.count, "There should be no people in the database"
    user = Factory(:activated_user)
    login_as user

    assert_difference('Person.count') do
      assert_difference('NotifieeInfo.count') do
        post :create, :person => {:first_name=>"test", :email=>"<EMAIL>"}
      end
    end
    assert assigns(:person)
    person = Person.find(assigns(:person).id)
    assert person.is_admin?
    assert person.only_first_admin_person?
    assert_redirected_to registration_form_path(:during_setup=>"true")
  end


  def test_second_registered_person_is_not_admin
      Person.delete_all
      person = Person.new(:first_name=>"fred", :email=>"<EMAIL>")
      person.save!
      assert_equal 1, Person.count, "There should be 1 person in the database"
      user = Factory(:activated_user)
      login_as user
      assert_difference('Person.count') do
        assert_difference('NotifieeInfo.count') do
          post :create, :person => {:first_name=>"test", :email=>"<EMAIL>"}
        end
      end
      assert assigns(:person)
      person = Person.find(assigns(:person).id)
      assert !person.is_admin?
      assert !person.only_first_admin_person?
      assert_redirected_to person_path(person)
  end

  def test_should_create_person
    assert_difference('Person.count') do
      assert_difference('NotifieeInfo.count') do
        post :create, :person => {:first_name=>"test", :email=>"<EMAIL>"}
      end
    end

    assert_redirected_to person_path(assigns(:person))
    assert_equal "T", assigns(:person).first_letter
    assert_not_nil Person.find(assigns(:person).id).notifiee_info
  end

  def test_should_create_person_with_project
    work_group_id = Factory(:work_group).id
    assert_difference('Person.count') do
      assert_difference('NotifieeInfo.count') do
        post :create, :person => {:first_name=>"test", :email=>"<EMAIL>"}
      end
    end

    assert_redirected_to person_path(assigns(:person))
    assert_equal "T", assigns(:person).first_letter

    put :administer_update, :id => assigns(:person), :person => {:work_group_ids => [work_group_id]}

    assert_redirected_to person_path(assigns(:person))
    assert_equal [work_group_id], assigns(:person).work_group_ids
    assert_not_nil Person.find(assigns(:person).id).notifiee_info
  end

  def test_created_person_should_receive_notifications
    post :create, :person => {:first_name=>"test", :email=>"<EMAIL>"}
    p=assigns(:person)
    assert_not_nil p.notifiee_info
    assert p.notifiee_info.receive_notifications?
  end

  test "non_admin_should_not_create_pal" do
    login_as(:pal_user)
    assert_difference('Person.count') do
      post :create, :person => {:first_name=>"test", :email=>"<EMAIL>"}
    end

    p=assigns(:person)
    assert_redirected_to person_path(p)

    put :administer_update, :id => assigns(:person), :person => {:roles_mask => Person::ROLES_MASK_FOR_PAL}

    p=assigns(:person)
    assert_redirected_to :root
    assert !p.is_pal?
    assert !Person.find(p.id).is_pal?
  end

  def test_should_show_person
    get :show, :id => people(:quentin_person)
    assert_response :success
  end

  as_virtualliver do
    test 'anonymous user cannot view people' do
      logout
      get :show, :id => people(:quentin_person)
      assert_not_nil flash[:error]
    end
  end

  test 'anonymous user doesnt see people in index' do
    Factory :person, :first_name => 'Invisible', :last_name => ''
    logout
    get :index
    assert_select 'a', :text => /Invisible/, :count => 0
  end
  
  def test_should_get_edit
    get :edit, :id => people(:quentin_person)
    assert_response :success
  end

  def test_non_admin_cant_edit_someone_else
    login_as(Factory(:user))
    get :edit, :id=> people(:aaron_person)
    assert_redirected_to people(:aaron_person)
  end

  def test_project_manager_can_edit_others_inside_their_projects
    login_as(:project_manager)
    assert !(users(:project_manager).person.projects & people(:aaron_person).projects).empty?
    get :edit, :id=> people(:aaron_person)
    assert_response :success
  end

  def test_admin_can_edit_others
    get :edit, :id=>people(:aaron_person)
    assert_response :success
  end

  def test_change_notification_settings
    login_as(:quentin)
    p=people(:fred)
    assert p.notifiee_info.receive_notifications?, "should receive noticiations by default in fixtures"

    put :update, :id=>p.id, :person=>{:id=>p.id}
    assert !Person.find(p.id).notifiee_info.receive_notifications?<commit_msg>as_virtualliver does not work before test begins --> check is_virtualliver instead<commit_after>require 'test_helper'

class PeopleControllerTest < ActionController::TestCase

  fixtures :people,:users, :projects, :work_groups, :group_memberships, :project_roles

  include AuthenticatedTestHelper
  include RestTestCases
  include ApplicationHelper

  def setup
    login_as(:quentin)
  end

  def rest_api_test_object
    @object=people(:quentin_person)
  end

  def test_title
    get :index
    assert_select "title", :text=>/The Sysmo SEEK People.*/, :count=>1
  end

  def test_xml_for_person_with_tools_and_expertise
    p=Factory :person
    Factory :expertise,:value=>"golf",:annotatable=>p
    Factory :expertise,:value=>"fishing",:annotatable=>p
    Factory :tool,:value=>"fishing rod",:annotatable=>p

    test_get_rest_api_xml p

    doc = LibXML::XML::Document.string(@response.body)
    doc.root.namespaces.default_prefix="s"
    assert_equal 2, doc.find("//s:tags/s:tag[@context='expertise']").count
    assert_equal 1,doc.find("//s:tags/s:tag[@context='tool']").count
    assert_equal "fishing rod",doc.find("//s:tags/s:tag[@context='tool']").first.content
  end

  def test_should_get_index
    get :index
    assert_response :success
    assert_not_nil assigns(:people)
  end

  def test_should_get_new
    get :new
    assert_response :success
  end

  def test_first_registered_person_is_admin

    Person.destroy_all
    assert_equal 0, Person.count, "There should be no people in the database"
    user = Factory(:activated_user)
    login_as user

    assert_difference('Person.count') do
      assert_difference('NotifieeInfo.count') do
        post :create, :person => {:first_name=>"test", :email=>"<EMAIL>"}
      end
    end
    assert assigns(:person)
    person = Person.find(assigns(:person).id)
    assert person.is_admin?
    assert person.only_first_admin_person?
    assert_redirected_to registration_form_path(:during_setup=>"true")
  end


  def test_second_registered_person_is_not_admin
      Person.delete_all
      person = Person.new(:first_name=>"fred", :email=>"<EMAIL>")
      person.save!
      assert_equal 1, Person.count, "There should be 1 person in the database"
      user = Factory(:activated_user)
      login_as user
      assert_difference('Person.count') do
        assert_difference('NotifieeInfo.count') do
          post :create, :person => {:first_name=>"test", :email=>"<EMAIL>"}
        end
      end
      assert assigns(:person)
      person = Person.find(assigns(:person).id)
      assert !person.is_admin?
      assert !person.only_first_admin_person?
      assert_redirected_to person_path(person)
  end

  def test_should_create_person
    assert_difference('Person.count') do
      assert_difference('NotifieeInfo.count') do
        post :create, :person => {:first_name=>"test", :email=>"<EMAIL>"}
      end
    end

    assert_redirected_to person_path(assigns(:person))
    assert_equal "T", assigns(:person).first_letter
    assert_not_nil Person.find(assigns(:person).id).notifiee_info
  end

  def test_should_create_person_with_project
    work_group_id = Factory(:work_group).id
    assert_difference('Person.count') do
      assert_difference('NotifieeInfo.count') do
        post :create, :person => {:first_name=>"test", :email=>"<EMAIL>"}
      end
    end

    assert_redirected_to person_path(assigns(:person))
    assert_equal "T", assigns(:person).first_letter

    put :administer_update, :id => assigns(:person), :person => {:work_group_ids => [work_group_id]}

    assert_redirected_to person_path(assigns(:person))
    assert_equal [work_group_id], assigns(:person).work_group_ids
    assert_not_nil Person.find(assigns(:person).id).notifiee_info
  end

  def test_created_person_should_receive_notifications
    post :create, :person => {:first_name=>"test", :email=>"<EMAIL>"}
    p=assigns(:person)
    assert_not_nil p.notifiee_info
    assert p.notifiee_info.receive_notifications?
  end

  test "non_admin_should_not_create_pal" do
    login_as(:pal_user)
    assert_difference('Person.count') do
      post :create, :person => {:first_name=>"test", :email=>"<EMAIL>"}
    end

    p=assigns(:person)
    assert_redirected_to person_path(p)

    put :administer_update, :id => assigns(:person), :person => {:roles_mask => Person::ROLES_MASK_FOR_PAL}

    p=assigns(:person)
    assert_redirected_to :root
    assert !p.is_pal?
    assert !Person.find(p.id).is_pal?
  end

  def test_should_show_person
    get :show, :id => people(:quentin_person)
    assert_response :success
  end

  if Seek::Config.is_virtualliver
    test 'anonymous user cannot view people' do
        logout
        get :show, :id => people(:quentin_person)
        assert_not_nil flash[:error]
      end


    test 'anonymous user doesnt see people in index' do
      Factory :person, :first_name => 'Invisible', :last_name => ''
      logout
      get :index
      assert_select 'a', :text => /Invisible/, :count => 0
    end
  end

  def test_should_get_edit
    get :edit, :id => people(:quentin_person)
    assert_response :success
  end

  def test_non_admin_cant_edit_someone_else
    login_as(Factory(:user))
    get :edit, :id=> people(:aaron_person)
    assert_redirected_to people(:aaron_person)
  end

  def test_project_manager_can_edit_others_inside_their_projects
    login_as(:project_manager)
    assert !(users(:project_manager).person.projects & people(:aaron_person).projects).empty?
    get :edit, :id=> people(:aaron_person)
    assert_response :success
  end

  def test_admin_can_edit_others
    get :edit, :id=>people(:aaron_person)
    assert_response :success
  end

  def test_change_notification_settings
    login_as(:quentin)
    p=people(:fred)
    assert p.notifiee_info.receive_notifications?, "should receive noticiations by default in fixtures"

    put :update, :id=>p.id, :person=>{:id=>p.id}
    assert !Person.find(p.id).notifiee_info.receive_notifications?
{'fraction_non_alphanumeric': 0.11527085270007606, 'fraction_numerical': 0.0011831319192089917, 'mean_word_length': 4.220114689016321, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 114, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2222646', 'n_tokens_mistral': 4014, 'n_tokens_neox': 3851, 'n_words': 752}