<commit_before>require 'spec_helper'

# Should change this to no_sphinx for performance reasons when TS bug #914 is fixed.
feature 'responses form', js: true, sphinx: true do
  before do
    @user = create(:user)
  end

  describe 'general' do # This should be refactored to split into different scenarios.
    before do
      @form = create(:form, question_types: %w(select_one multi_level_select_one select_multiple integer decimal
        location text long_text datetime date time))
      @qings = @form.questionings
      @form.publish!
      login(@user)
    end

    scenario 'should work' do
      visit_submit_page_and_select_user

      # Fill in answers
      select('Dog', from: control_id(@qings[0], '_option_id'))

      select('Plant', from: control_id(@qings[1], '_0_option_id'))
      find('#' + control_id(@qings[1], '_1_option_id') + ' option', text: 'Oak')
      select('Oak', from: control_id(@qings[1], '_1_option_id'))

      check(control_id(@qings[2], '_choices_attributes_0_checked')) # Cat
      fill_in(control_id(@qings[3], '_value'), with: '10')
      fill_in(control_id(@qings[4], '_value'), with: '10.2')
      fill_in(control_id(@qings[5], '_value'), with: '42.277976 -83.817573')
      fill_in(control_id(@qings[6], '_value'), with: 'Foo')
      fill_in_ckeditor(control_id(@qings[7], '_value'), with: "Foo Bar\nBaz")

      select(Time.now.year, from: control_id(@qings[8], '_datetime_value_1i'))
      select('March', from: control_id(@qings[8], '_datetime_value_2i'))
      select('12', from: control_id(@qings[8], '_datetime_value_3i'))
      select('18', from: control_id(@qings[8], '_datetime_value_4i'))
      select('32', from: control_id(@qings[8], '_datetime_value_5i'))

      select(Time.now.year, from: control_id(@qings[9], '_date_value_1i'))
      select('October', from: control_id(@qings[9], '_date_value_2i'))
      select('26', from: control_id(@qings[9], '_date_value_3i'))

      select('03', from: control_id(@qings[10], '_time_value_4i'))
      select('08', from: control_id(@qings[10], '_time_value_5i'))

      # Save and check it worked.
      click_button('Save')
      expect(page).to have_selector('h1', text: 'Response')

      # Check show mode.
      click_link(Response.first.id.to_s)
      check_response_show_form('Dog', %w(Plant Oak), 'Cat', '10', '10.2', '42.277976 -83.817573', 'Foo',
        "Foo Bar Baz", "Mar 12 #{Time.now.year} 18:32", "Oct 26 #{Time.now.year}", "03:08")

      # Check edit mode.
      click_link('Edit Response')
      select('Animal', from: control_id(@qings[1], '_0_option_id'))
      find('#' + control_id(@qings[1], '_1_option_id') + ' option', text: 'Cat')
      select('Cat', from: control_id(@qings[1], '_1_option_id'))
      uncheck(control_id(@qings[2], '_choices_attributes_0_checked')) # Cat
      check(control_id(@qings[2], '_choices_attributes_1_checked')) # Dog
      click_button('Save')

      # Check that change occurred.
      click_link(Response.first.id.to_s)
      check_response_show_form('Dog', %w(Animal Cat), 'Dog', '10', '10.2', '42.277976 -83.817573', 'Foo',
        "Foo Bar Baz", "Mar 12 #{Time.now.year} 18:32", "Oct 26 #{Time.now.year}", "03:08")

      # Delete.
      handle_js_confirm{click_link('Delete Response')}
      expect(page).to have_selector('.alert-success', text: 'deleted')
    end
  end

  describe 'hidden questions' do
    before do
      @form = create(:form, question_types: %w(text text))
      @qing0, @qing1 = @form.questionings
      @qing1.update_attributes(hidden: true, required: true) # Being required shouldn't make a difference.
      @form.publish!
      login(@user)
    end

    scenario 'should be properly ignored' do
      visit_submit_page_and_select_user

      expect(page).not_to have_selector("div.form_field#qing_#{@qing1.id}")
      fill_in(control_id(@qing0, '_value'), with: 'Foo')
      click_button('Save')

      # Ensure response saved properly.
      click_link(Response.first.id.to_s)
      expect(page).not_to have_selector("div.form_field#qing_#{@qing1.id}")
      expect(page).to have_selector("div.form_field#qing_#{@qing0.id} .ro-val", text: 'Foo')
    end
  end

  describe 'integer constraints' do
    before do
      @form = create(:form, question_types: %w(integer))
      @form.questions[0].update_attributes!(minimum: 10)
      @qings = @form.questionings
      login(@user)
    end

    scenario 'should be enforced if appropriate' do
      # Should raise error if value filled in.
      visit_submit_page_and_select_user
      fill_in(control_id(@qings[0], '_value'), with: '9')
      click_button('Save')
      expect(page).to have_content('greater than or equal to 10')

      # Should not raise error if value is valid.
      fill_in(control_id(@qings[0], '_value'), with: '11')
      click_button('Save')
      expect(page).to have_content('Response created successfully')

      # Should not raise error if left blank.
      visit_submit_page_and_select_user
      fill_in(control_id(@qings[0], '_value'), with: '')
      click_button('Save')
      expect(page).to have_content('Response created successfully')
    end
  end

  def control_id(qing, suffix)
    "response_answers_attributes_#{qing.id}#{suffix}"
  end

  def visit_submit_page_and_select_user
    visit(new_response_path(locale: 'en', mode: 'm', mission_name: get_mission.compact_name, form_id: @form.id))
    select2(@user.name, from: 'response_user_id')
  end

  def check_response_show_form(*values)
    values.each_with_index{ |v,i| expect_answer(i, v) }
  end
<commit_msg>2397: Add spec to ensure that observers don't see reviewer notes<commit_after>require 'spec_helper'

# Should change this to no_sphinx for performance reasons when TS bug #914 is fixed.
feature 'responses form', js: true, sphinx: true do
  before do
    @user = create(:user)
  end

  describe 'general' do # This should be refactored to split into different scenarios.
    before do
      @form = create(:form, question_types: %w(select_one multi_level_select_one select_multiple integer decimal
        location text long_text datetime date time))
      @qings = @form.questionings
      @form.publish!
      login(@user)
    end

    scenario 'should work' do
      visit_submit_page_and_select_user

      # Fill in answers
      select('Dog', from: control_id(@qings[0], '_option_id'))

      select('Plant', from: control_id(@qings[1], '_0_option_id'))
      find('#' + control_id(@qings[1], '_1_option_id') + ' option', text: 'Oak')
      select('Oak', from: control_id(@qings[1], '_1_option_id'))

      check(control_id(@qings[2], '_choices_attributes_0_checked')) # Cat
      fill_in(control_id(@qings[3], '_value'), with: '10')
      fill_in(control_id(@qings[4], '_value'), with: '10.2')
      fill_in(control_id(@qings[5], '_value'), with: '42.277976 -83.817573')
      fill_in(control_id(@qings[6], '_value'), with: 'Foo')
      fill_in_ckeditor(control_id(@qings[7], '_value'), with: "Foo Bar\nBaz")

      select(Time.now.year, from: control_id(@qings[8], '_datetime_value_1i'))
      select('March', from: control_id(@qings[8], '_datetime_value_2i'))
      select('12', from: control_id(@qings[8], '_datetime_value_3i'))
      select('18', from: control_id(@qings[8], '_datetime_value_4i'))
      select('32', from: control_id(@qings[8], '_datetime_value_5i'))

      select(Time.now.year, from: control_id(@qings[9], '_date_value_1i'))
      select('October', from: control_id(@qings[9], '_date_value_2i'))
      select('26', from: control_id(@qings[9], '_date_value_3i'))

      select('03', from: control_id(@qings[10], '_time_value_4i'))
      select('08', from: control_id(@qings[10], '_time_value_5i'))

      # Save and check it worked.
      click_button('Save')
      expect(page).to have_selector('h1', text: 'Response')

      # Check show mode.
      click_link(Response.first.id.to_s)
      check_response_show_form('Dog', %w(Plant Oak), 'Cat', '10', '10.2', '42.277976 -83.817573', 'Foo',
        "Foo Bar Baz", "Mar 12 #{Time.now.year} 18:32", "Oct 26 #{Time.now.year}", "03:08")

      # Check edit mode.
      click_link('Edit Response')
      select('Animal', from: control_id(@qings[1], '_0_option_id'))
      find('#' + control_id(@qings[1], '_1_option_id') + ' option', text: 'Cat')
      select('Cat', from: control_id(@qings[1], '_1_option_id'))
      uncheck(control_id(@qings[2], '_choices_attributes_0_checked')) # Cat
      check(control_id(@qings[2], '_choices_attributes_1_checked')) # Dog
      click_button('Save')

      # Check that change occurred.
      click_link(Response.first.id.to_s)
      check_response_show_form('Dog', %w(Animal Cat), 'Dog', '10', '10.2', '42.277976 -83.817573', 'Foo',
        "Foo Bar Baz", "Mar 12 #{Time.now.year} 18:32", "Oct 26 #{Time.now.year}", "03:08")

      # Delete.
      handle_js_confirm{click_link('Delete Response')}
      expect(page).to have_selector('.alert-success', text: 'deleted')
    end
  end

  describe 'hidden questions' do
    before do
      @form = create(:form, question_types: %w(text text))
      @qing0, @qing1 = @form.questionings
      @qing1.update_attributes(hidden: true, required: true) # Being required shouldn't make a difference.
      @form.publish!
      login(@user)
    end

    scenario 'should be properly ignored' do
      visit_submit_page_and_select_user

      expect(page).not_to have_selector("div.form_field#qing_#{@qing1.id}")
      fill_in(control_id(@qing0, '_value'), with: 'Foo')
      click_button('Save')

      # Ensure response saved properly.
      click_link(Response.first.id.to_s)
      expect(page).not_to have_selector("div.form_field#qing_#{@qing1.id}")
      expect(page).to have_selector("div.form_field#qing_#{@qing0.id} .ro-val", text: 'Foo')
    end
  end

  describe 'integer constraints' do
    before do
      @form = create(:form, question_types: %w(integer))
      @form.questions[0].update_attributes!(minimum: 10)
      @qings = @form.questionings
      login(@user)
    end

    scenario 'should be enforced if appropriate' do
      # Should raise error if value filled in.
      visit_submit_page_and_select_user
      fill_in(control_id(@qings[0], '_value'), with: '9')
      click_button('Save')
      expect(page).to have_content('greater than or equal to 10')

      # Should not raise error if value is valid.
      fill_in(control_id(@qings[0], '_value'), with: '11')
      click_button('Save')
      expect(page).to have_content('Response created successfully')

      # Should not raise error if left blank.
      visit_submit_page_and_select_user
      fill_in(control_id(@qings[0], '_value'), with: '')
      click_button('Save')
      expect(page).to have_content('Response created successfully')
    end
  end

  describe 'reviewer notes' do

    before do
      @observer = create(:user, role_name: :observer)
      @form = create(:form, question_types: %w(integer))
      @notes = "Zero? (##{SecureRandom.hex})"
      @response = create(:response, form: @form, answer_values: [0], reviewer_notes: @notes, user: @observer)
    end

    scenario 'should not be visible to normal users' do
      login(@observer)
      visit(response_path(@response, locale: 'en', mode: 'm', mission_name: get_mission.compact_name))
      expect(page).not_to have_content(@notes)
    end

    scenario 'should be visible to admin' do
      login(create(:user, admin: true))
      visit(response_path(@response, locale: 'en', mode: 'm', mission_name: get_mission.compact_name))
      expect(page).to have_content(@notes)
    end

    scenario 'should be visible to staffer' do
      login(create(:user, role_name: :staffer))
      visit(response_path(@response, locale: 'en', mode: 'm', mission_name: get_mission.compact_name))
      expect(page).to have_content(@notes)
    end
  end

  def control_id(qing, suffix)
    "response_answers_attributes_#{qing.id}#{suffix}"
  end

  def visit_submit_page_and_select_user
    visit(new_response_path(locale: 'en', mode: 'm', mission_name: get_mission.compact_name, form_id: @form.id))
    select2(@user.name, from: 'response_user_id')
  end

  def check_response_show_form(*values)
    values.each_with_index{ |v,i| expect_answer(i, v) }
  end

{'fraction_non_alphanumeric': 0.1491508737386168, 'fraction_numerical': 0.027976043974074986, 'mean_word_length': 3.9312297734627832, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '349571', 'n_tokens_mistral': 4312, 'n_tokens_neox': 4090, 'n_words': 911}