<commit_before>require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))

class ThreadedCommentsControllerTest < ActionController::TestCase

  def setup
    @test_book = Book.create!(Factory.attributes_for(:book))
    ThreadedComment.create!(Factory.attributes_for(:threaded_comment))
  end
  
  test "should get show" do
    @test_comment = ThreadedComment.new(Factory.attributes_for(:threaded_comment, :parent_id => 0))
    @test_comment.save
    get :show, :id => @test_comment.id
    assert_response :success, @response.body
    assert_not_nil assigns(:comment)
    assert @response.body.index(@test_comment.name), "Did not include comment name"
    assert @response.body.index(@test_comment.body), "Did not include comment body"
    assert @response.body.index(upmod_threaded_comment_path(@test_comment)), "Did not include link to upmod"
    assert @response.body.index(downmod_threaded_comment_path(@test_comment)), "Did not include link to downmod"
    assert @response.body.index(flag_threaded_comment_path(@test_comment)), "Did not include link to flag"
    assert @response.body.index(new_threaded_comment_path), "Did not include link to new"
  end
  
  test "show should not display threaded comments with flags greater than flag_threshold" do
    @test_comment = ThreadedComment.new(Factory.attributes_for(:threaded_comment, :name => "Flagged Commenter"))
    @test_comment.flags = 99999999
    @test_comment.save
    get :show, :id => @test_comment.id
    assert_response :success, @response.body
    assert_not_nil assigns(:comment)
    assert_nil @response.body.index(@test_comment.name), "Should not include comment name"
    assert_nil @response.body.index(@test_comment.body), "Should not include comment body"
  end
  
  test "should create comment" do
    assert_difference('ThreadedComment.count') do
      @test_comment = Factory.attributes_for(:threaded_comment)
      put :create, :threaded_comment => @test_comment
      assert_response :success
      assert @response.body.index(@test_comment[:name]), "Did not include comment name"
      assert @response.body.index(@test_comment[:body]), "Did not include comment body"
    end
  end
  
  test "should create sub-comment" do
    @test_parent_comment = @test_book.comments.create!(Factory.attributes_for(:threaded_comment))
    @test_comment = Factory.attributes_for(:threaded_comment, :parent_id => @test_parent_comment.id.to_s)
    assert_difference('ThreadedComment.count') do
      put :create, :threaded_comment => @test_comment
      assert_response :success
      assert @response.body.index(@test_comment[:name]), "Did not include comment name"
      assert @response.body.index(@test_comment[:body]), "Did not include comment body"
    end
  end
  
  test "should not create comment if negative captcha is filled" do
    assert_no_difference('ThreadedComment.count') do
      put :create, :threaded_comment => Factory.attributes_for(:threaded_comment, :confirm_email => "<EMAIL>")
    end
    assert_response :bad_request
  end
  
  test "should get new" do
    session[:name] = "<NAME>"
    session[:email] = "Test Name"
    @test_comment = Factory.attributes_for(:threaded_comment, :name => nil, :email => nil, :parent_id => "2")
    get :new, :threaded_comment => @test_comment
    assert_response :success
    assert_not_nil assigns(:comment)
    assert @response.body.include?(session[:name]), "Response body did not include commenter name"
    assert @response.body.include?(session[:email]), "Response body did not include commenter email"
    assert @response.body.include?(@test_comment[:body]), "Response body did not include body"
    assert @response.body.include?(@test_comment[:threaded_comment_polymorphic_id].to_s), "Response body did not include threaded_comment_polymorphic_id"
    assert @response.body.include?(@test_comment[:threaded_comment_polymorphic_type]), "Response body did not include threaded_comment_polymorphic_type"
    assert @response.body.include?(@test_comment[:parent_id]), "Response body did not include parent_id"
    assert @response.body.include?("threaded_comment[name]"), "Response body did not include form for name"
    assert @response.body.include?("threaded_comment[body]"), "Response body did not include form for body"
    assert @response.body.include?("threaded_comment[email]"), "Response body did not include form for email"
    assert @response.body.include?("threaded_comment[threaded_comment_polymorphic_id]"), "Response body did not include form for threaded_comment_polymorphic_id"
    assert @response.body.include?("threaded_comment[threaded_comment_polymorphic_type]"), "Response body did not include form for threaded_comment_polymorphic_type"
    assert @response.body.include?("threaded_comment[parent_id]"), "Response body did not include form for parent_id"
    assert @response.body.include?("threaded_comment[#{THREADED_COMMENTS_CONFIG[:render_comment_form][:honeypot_name]}]"), "Response body did not include honeypot form"
    assert @response.body.include?(THREADED_COMMENTS_CONFIG[:render_comment_form][:name_label]), "Response body did not include name label"
    assert @response.body.include?(THREADED_COMMENTS_CONFIG[:render_comment_form][:email_label]), "Response body did not include email label"
    assert @response.body.include?(THREADED_COMMENTS_CONFIG[:render_comment_form][:body_label]), "Response body did not include body label"
    assert @response.body.include?(THREADED_COMMENTS_CONFIG[:render_comment_form][:submit_title]), "Response body did not include submit title"
    assert @response.body.include?('removeChild(message)'), "Response body did not include javascript callback for removing no_comments_message" 
  end
  
  test "should upmod comment" do
    assert_difference('ThreadedComment.find(1).rating') do
      post :upmod, :id => 1
      assert_response :success
      assert @response.body.index(@expected_rating.to_s), "Response body did not include new rating"
    end
  end
  
  test "should downmod comment" do
    assert_difference('ThreadedComment.find(1).rating', -1) do
      post :downmod, :id => 1
      assert_response :success
      assert @response.body.index(@expected_rating.to_s), "Response body did not include new rating"
    end
  end
  
  test "should flag comment" do
    assert_difference('ThreadedComment.find(1).flags') do
      post :flag, :id => 1
      assert_response :success
    end
  end
  
  test "should only allow rating or flagging once per action per session" do
    @actions = [
      { :action => 'flag', :field => 'flags', :difference => 1},
      { :action => 'upmod', :field => 'rating', :difference => 1},
      { :action => 'downmod', :field => 'rating', :difference => -1}
    ]
    @actions.each do |action|
      assert_difference("ThreadedComment.find(1).#{action[:field]}", action[:difference], "Action failed first time: #{action[:action]}") do
        put action[:action], :id => 1
        assert_response :success
      end 
      assert_no_difference( "ThreadedComment.find(1).#{action[:field]}", "Action succeeded when it should have failed: #{action[:action]}") do
        put action[:action], :id => 1
        assert_response :bad_request
      end
    end
  end
  
  test "should remove email notifications if hash matches" do
    test_comment = ThreadedComment.find(1)
    assert !test_comment.email.empty?<commit_msg>Changed vote_once tests to be fixtureless<commit_after>require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))

class ThreadedCommentsControllerTest < ActionController::TestCase

  def setup
    @test_book = Book.create!(Factory.attributes_for(:book))
    ThreadedComment.create!(Factory.attributes_for(:threaded_comment))
  end
  
  test "should get show" do
    @test_comment = ThreadedComment.new(Factory.attributes_for(:threaded_comment, :parent_id => 0))
    @test_comment.save
    get :show, :id => @test_comment.id
    assert_response :success, @response.body
    assert_not_nil assigns(:comment)
    assert @response.body.index(@test_comment.name), "Did not include comment name"
    assert @response.body.index(@test_comment.body), "Did not include comment body"
    assert @response.body.index(upmod_threaded_comment_path(@test_comment)), "Did not include link to upmod"
    assert @response.body.index(downmod_threaded_comment_path(@test_comment)), "Did not include link to downmod"
    assert @response.body.index(flag_threaded_comment_path(@test_comment)), "Did not include link to flag"
    assert @response.body.index(new_threaded_comment_path), "Did not include link to new"
  end
  
  test "show should not display threaded comments with flags greater than flag_threshold" do
    @test_comment = ThreadedComment.new(Factory.attributes_for(:threaded_comment, :name => "Flagged Commenter"))
    @test_comment.flags = 99999999
    @test_comment.save
    get :show, :id => @test_comment.id
    assert_response :success, @response.body
    assert_not_nil assigns(:comment)
    assert_nil @response.body.index(@test_comment.name), "Should not include comment name"
    assert_nil @response.body.index(@test_comment.body), "Should not include comment body"
  end
  
  test "should create comment" do
    assert_difference('ThreadedComment.count') do
      @test_comment = Factory.attributes_for(:threaded_comment)
      put :create, :threaded_comment => @test_comment
      assert_response :success
      assert @response.body.index(@test_comment[:name]), "Did not include comment name"
      assert @response.body.index(@test_comment[:body]), "Did not include comment body"
    end
  end
  
  test "should create sub-comment" do
    @test_parent_comment = @test_book.comments.create!(Factory.attributes_for(:threaded_comment))
    @test_comment = Factory.attributes_for(:threaded_comment, :parent_id => @test_parent_comment.id.to_s)
    assert_difference('ThreadedComment.count') do
      put :create, :threaded_comment => @test_comment
      assert_response :success
      assert @response.body.index(@test_comment[:name]), "Did not include comment name"
      assert @response.body.index(@test_comment[:body]), "Did not include comment body"
    end
  end
  
  test "should not create comment if negative captcha is filled" do
    assert_no_difference('ThreadedComment.count') do
      put :create, :threaded_comment => Factory.attributes_for(:threaded_comment, :confirm_email => "<EMAIL>")
    end
    assert_response :bad_request
  end
  
  test "should get new" do
    session[:name] = "<NAME>"
    session[:email] = "Test Name"
    @test_comment = Factory.attributes_for(:threaded_comment, :name => nil, :email => nil, :parent_id => "2")
    get :new, :threaded_comment => @test_comment
    assert_response :success
    assert_not_nil assigns(:comment)
    assert @response.body.include?(session[:name]), "Response body did not include commenter name"
    assert @response.body.include?(session[:email]), "Response body did not include commenter email"
    assert @response.body.include?(@test_comment[:body]), "Response body did not include body"
    assert @response.body.include?(@test_comment[:threaded_comment_polymorphic_id].to_s), "Response body did not include threaded_comment_polymorphic_id"
    assert @response.body.include?(@test_comment[:threaded_comment_polymorphic_type]), "Response body did not include threaded_comment_polymorphic_type"
    assert @response.body.include?(@test_comment[:parent_id]), "Response body did not include parent_id"
    assert @response.body.include?("threaded_comment[name]"), "Response body did not include form for name"
    assert @response.body.include?("threaded_comment[body]"), "Response body did not include form for body"
    assert @response.body.include?("threaded_comment[email]"), "Response body did not include form for email"
    assert @response.body.include?("threaded_comment[threaded_comment_polymorphic_id]"), "Response body did not include form for threaded_comment_polymorphic_id"
    assert @response.body.include?("threaded_comment[threaded_comment_polymorphic_type]"), "Response body did not include form for threaded_comment_polymorphic_type"
    assert @response.body.include?("threaded_comment[parent_id]"), "Response body did not include form for parent_id"
    assert @response.body.include?("threaded_comment[#{THREADED_COMMENTS_CONFIG[:render_comment_form][:honeypot_name]}]"), "Response body did not include honeypot form"
    assert @response.body.include?(THREADED_COMMENTS_CONFIG[:render_comment_form][:name_label]), "Response body did not include name label"
    assert @response.body.include?(THREADED_COMMENTS_CONFIG[:render_comment_form][:email_label]), "Response body did not include email label"
    assert @response.body.include?(THREADED_COMMENTS_CONFIG[:render_comment_form][:body_label]), "Response body did not include body label"
    assert @response.body.include?(THREADED_COMMENTS_CONFIG[:render_comment_form][:submit_title]), "Response body did not include submit title"
    assert @response.body.include?('removeChild(message)'), "Response body did not include javascript callback for removing no_comments_message" 
  end
  
  test "should upmod comment" do
    assert_difference('ThreadedComment.find(1).rating') do
      post :upmod, :id => 1
      assert_response :success
      assert @response.body.index(@expected_rating.to_s), "Response body did not include new rating"
    end
  end
  
  test "should downmod comment" do
    assert_difference('ThreadedComment.find(1).rating', -1) do
      post :downmod, :id => 1
      assert_response :success
      assert @response.body.index(@expected_rating.to_s), "Response body did not include new rating"
    end
  end
  
  test "should flag comment" do
    assert_difference('ThreadedComment.find(1).flags') do
      post :flag, :id => 1
      assert_response :success
    end
  end
  
  test "should only allow rating or flagging once per action per session" do
    @actions = [
      { :action => 'flag', :field => 'flags', :difference => 1},
      { :action => 'upmod', :field => 'rating', :difference => 1},
      { :action => 'downmod', :field => 'rating', :difference => -1}
    ]
    @actions.each do |action|
      test_comment = ThreadedComment.create!(Factory.attributes_for(:threaded_comment))
      assert_difference("test_comment.#{action[:field]}", action[:difference], "Action failed first time: #{action[:action]}") do
        put action[:action], :id => test_comment.id
        assert_response :success
        test_comment.reload
      end 
      assert_no_difference( "test_comment.#{action[:field]}", "Action succeeded when it should have failed: #{action[:action]}") do
        put action[:action], :id => test_comment.id
        assert_response :bad_request
        test_comment.reload
      end
    end
  end
  
  test "should remove email notifications if hash matches" do
    test_comment = ThreadedComment.find(1)
    assert !test_comment.email.empty?
{'fraction_non_alphanumeric': 0.11156417112299465, 'fraction_numerical': 0.0030748663101604276, 'mean_word_length': 4.906435057244375, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 61, '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': '5446357', 'n_tokens_mistral': 4394, 'n_tokens_neox': 4281, 'n_words': 1114}