<commit_before>require "rails_helper"

feature "Voters" do

  let(:poll) { create(:poll, :current) }
  let(:booth) { create(:poll_booth) }
  let(:officer) { create(:poll_officer) }

  background do
    login_as(officer.user)
    create(:geozone, :in_census)
    create(:poll_shift, officer: officer, booth: booth, date: Date.current, task: :vote_collection)
    booth_assignment = create(:poll_booth_assignment, poll: poll, booth: booth)
    create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment)
    set_officing_booth(booth)
  end

  scenario "Can vote", :js do
    create(:poll_officer_assignment, officer: officer)

    visit new_officing_residence_path
    officing_verify_residence

    expect(page).to have_content "Polls"
    expect(page).to have_content poll.name

    click_button "Confirm vote"

    expect(page).to have_content "Vote introduced!"
    expect(page).not_to have_button "Confirm vote"

    page.evaluate_script("window.location.reload()")
    expect(page).to have_content "Has already participated in this poll"
    expect(page).not_to have_button "Confirm vote"

    expect(Poll::Voter.last.officer_id).to eq(officer.id)
  end

  scenario "Already voted", :js do
    poll2 = create(:poll, :current)
    booth_assignment = create(:poll_booth_assignment, poll: poll2, booth: booth)
    create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment)

    user = create(:user, :level_two)
    voter = create(:poll_voter, poll: poll, user: user)

    visit new_officing_voter_path(id: voter.user.id)

    within("#poll_#{poll.id}") do
      expect(page).to have_content "Has already participated in this poll"
      expect(page).not_to have_button "Confirm vote"
    end

    within("#poll_#{poll2.id}") do
      expect(page).to have_button "Confirm vote"
    end
  end

  scenario "Had already verified his residence, but is not level 2 yet", :js do
    user = create(:user, residence_verified_at: Time.current, document_type: "1", document_number: "12345678Z")
    expect(user).not_to be_level_two_verified

    visit root_path
    click_link "Sign out"
    login_through_form_as_officer(officer.user)

    visit new_officing_residence_path
    officing_verify_residence

    expect(page).to have_content "Polls"
    expect(page).to have_content poll.name
  end

  scenario "Display only current polls on which officer has a voting shift today, and user can answer", :js do
    poll_current = create(:poll, :current)
    second_booth = create(:poll_booth)
    booth_assignment = create(:poll_booth_assignment, poll: poll_current, booth: second_booth)
    create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment)
    create(:poll_shift, officer: officer, booth: second_booth, date: Date.current, task: :recount_scrutiny)
    create(:poll_shift, officer: officer, booth: second_booth, date: Date.tomorrow, task: :vote_collection)

    poll_expired = create(:poll, :expired)
    create(:poll_officer_assignment, officer: officer, booth_assignment: create(:poll_booth_assignment, poll: poll_expired, booth: booth))

    poll_geozone_restricted_in = create(:poll, :current, geozone_restricted: true, geozones: [Geozone.first])
    booth_assignment = create(:poll_booth_assignment, poll: poll_geozone_restricted_in, booth: booth)
    create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment)

    poll_geozone_restricted_out = create(:poll, :current, geozone_restricted: true, geozones: [create(:geozone, census_code: "02")])
    booth_assignment = create(:poll_booth_assignment, poll: poll_geozone_restricted_out, booth: booth)
    create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment)

    set_officing_booth(second_booth)
    visit new_officing_residence_path
    officing_verify_residence

    expect(page).to have_content "Polls"
    expect(page).to have_content poll.name
    expect(page).not_to have_content poll_current.name
    expect(page).not_to have_content poll_expired.name
    expect(page).to have_content poll_geozone_restricted_in.name
    expect(page).not_to have_content poll_geozone_restricted_out.name
  end

  scenario "Store officer and booth information", :js do
    create(:user, :in_census, id: rand(9999999))
    poll1 = create(:poll, name: "¿Quieres que XYZ sea aprobado?")
    poll2 = create(:poll, name: "Pregunta de votación de prueba")

    ba1 = create(:poll_booth_assignment, poll: poll1)
    ba2 = create(:poll_booth_assignment, poll: poll2)
    oa1 = create(:poll_officer_assignment, officer: officer, booth_assignment: ba1, date: Date.current)
    oa2 = create(:poll_officer_assignment, officer: officer, booth_assignment: ba2, date: Date.current)

    validate_officer
    visit new_officing_residence_path
    officing_verify_residence

    within("#poll_#{poll1.id}") do
      click_button "Confirm vote"

      expect(page).to have_content "Vote introduced!"
    end

    within("#poll_#{poll2.id}") do
      click_button "Confirm vote"

      expect(page).to have_content "Vote introduced!"
    end

    expect(Poll::Voter.count).to eq(2)

    voter1 = Poll::Voter.first

    expect(voter1.booth_assignment).to eq(ba1)
    expect(voter1.officer_assignment).to eq(oa1)

    voter2 = Poll::Voter.last
    expect(voter2.booth_assignment).to eq(ba2)
    expect(voter2.officer_assignment).to eq(oa2)
  end
end
<commit_msg>Fix store officer and booth info spec<commit_after>require "rails_helper"

feature "Voters" do

  let(:poll) { create(:poll, :current) }
  let(:booth) { create(:poll_booth) }
  let(:officer) { create(:poll_officer) }

  background do
    login_as(officer.user)
    create(:geozone, :in_census)
    create(:poll_shift, officer: officer, booth: booth, date: Date.current, task: :vote_collection)
    booth_assignment = create(:poll_booth_assignment, poll: poll, booth: booth)
    create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment)
    set_officing_booth(booth)
  end

  scenario "Can vote", :js do
    create(:poll_officer_assignment, officer: officer)

    visit new_officing_residence_path
    officing_verify_residence

    expect(page).to have_content "Polls"
    expect(page).to have_content poll.name

    click_button "Confirm vote"

    expect(page).to have_content "Vote introduced!"
    expect(page).not_to have_button "Confirm vote"

    page.evaluate_script("window.location.reload()")
    expect(page).to have_content "Has already participated in this poll"
    expect(page).not_to have_button "Confirm vote"

    expect(Poll::Voter.last.officer_id).to eq(officer.id)
  end

  scenario "Already voted", :js do
    poll2 = create(:poll, :current)
    booth_assignment = create(:poll_booth_assignment, poll: poll2, booth: booth)
    create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment)

    user = create(:user, :level_two)
    voter = create(:poll_voter, poll: poll, user: user)

    visit new_officing_voter_path(id: voter.user.id)

    within("#poll_#{poll.id}") do
      expect(page).to have_content "Has already participated in this poll"
      expect(page).not_to have_button "Confirm vote"
    end

    within("#poll_#{poll2.id}") do
      expect(page).to have_button "Confirm vote"
    end
  end

  scenario "Had already verified his residence, but is not level 2 yet", :js do
    user = create(:user, residence_verified_at: Time.current, document_type: "1", document_number: "12345678Z")
    expect(user).not_to be_level_two_verified

    visit root_path
    click_link "Sign out"
    login_through_form_as_officer(officer.user)

    visit new_officing_residence_path
    officing_verify_residence

    expect(page).to have_content "Polls"
    expect(page).to have_content poll.name
  end

  scenario "Display only current polls on which officer has a voting shift today, and user can answer", :js do
    poll_current = create(:poll, :current)
    second_booth = create(:poll_booth)
    booth_assignment = create(:poll_booth_assignment, poll: poll_current, booth: second_booth)
    create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment)
    create(:poll_shift, officer: officer, booth: second_booth, date: Date.current, task: :recount_scrutiny)
    create(:poll_shift, officer: officer, booth: second_booth, date: Date.tomorrow, task: :vote_collection)

    poll_expired = create(:poll, :expired)
    create(:poll_officer_assignment, officer: officer, booth_assignment: create(:poll_booth_assignment, poll: poll_expired, booth: booth))

    poll_geozone_restricted_in = create(:poll, :current, geozone_restricted: true, geozones: [Geozone.first])
    booth_assignment = create(:poll_booth_assignment, poll: poll_geozone_restricted_in, booth: booth)
    create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment)

    poll_geozone_restricted_out = create(:poll, :current, geozone_restricted: true, geozones: [create(:geozone, census_code: "02")])
    booth_assignment = create(:poll_booth_assignment, poll: poll_geozone_restricted_out, booth: booth)
    create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment)

    set_officing_booth(second_booth)
    visit new_officing_residence_path
    officing_verify_residence

    expect(page).to have_content "Polls"
    expect(page).to have_content poll.name
    expect(page).not_to have_content poll_current.name
    expect(page).not_to have_content poll_expired.name
    expect(page).to have_content poll_geozone_restricted_in.name
    expect(page).not_to have_content poll_geozone_restricted_out.name
  end

  scenario "Store officer and booth information", :js do
    create(:user, :in_census, id: rand(9999999))
    poll1 = create(:poll, name: "¿Quieres que XYZ sea aprobado?")
    poll2 = create(:poll, name: "Pregunta de votación de prueba")

    second_booth = create(:poll_booth)

    ba1 = create(:poll_booth_assignment, poll: poll1, booth: second_booth )
    ba2 = create(:poll_booth_assignment, poll: poll2, booth: second_booth )
    create(:poll_shift, officer: officer, booth: second_booth, date: Date.current, task: :vote_collection)

    validate_officer
    visit new_officing_residence_path
    set_officing_booth(second_booth)
    officing_verify_residence

    within("#poll_#{poll1.id}") do
      click_button "Confirm vote"

      expect(page).to have_content "Vote introduced!"
    end

    within("#poll_#{poll2.id}") do
      click_button "Confirm vote"

      expect(page).to have_content "Vote introduced!"
    end

    expect(Poll::Voter.count).to eq(2)

    voter1 = Poll::Voter.first

    expect(voter1.booth_assignment).to eq(ba1)
    expect(voter1.officer_assignment).not_to be_nil

    voter2 = Poll::Voter.last
    expect(voter2.booth_assignment).to eq(ba2)
    expect(voter2.officer_assignment).not_to be_nil
  end
end

{'fraction_non_alphanumeric': 0.10047715177096715, 'fraction_numerical': 0.007707836300238576, 'mean_word_length': 4.910520607375271, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '4902486', 'n_tokens_mistral': 3673, 'n_tokens_neox': 3563, 'n_words': 796}