<commit_before>require 'test_helper'
require 'helpers/passphrase_support'

class EventLogCreationIntegrationTest < ActionDispatch::IntegrationTest
  include PassPhraseSupport

  setup do
    @admin = create(:admin_user, name: "Admin User")
    @user = create(:user, name: "Normal User")
  end

  test "record successful login" do
    visit root_path
    signin_with(@user)

    assert_equal 1, @user.event_logs.count
    assert_equal EventLog::SUCCESSFUL_LOGIN, @user.event_logs.last.entry
  end

  context "recording unsuccessful login" do
    should "record unsuccessful login for a valid email" do
      visit root_path
      signin_with(email: @user.email, password: :<PASSWORD>)

      assert_equal 1, @user.event_logs.count
      assert_equal EventLog::UNSUCCESSFUL_LOGIN, @user.event_logs.last.entry
    end

    should "log nothing for an invalid email" do
      visit root_path
      signin_with(email: "<EMAIL>", password: "<PASSWORD>")

      assert_equal 0, EventLog.count
    end

    should "not blow up if not given a string for the email" do
      # Assert we don't blow up when looking up the attempted user
      # when people have been messing with the posted params.
      post "/users/sign_in", params: { "user" => { "email" => { "foo" => "bar" }, :password => "<PASSWORD>" } }

      assert response.success?
    end
  end

  test "record passphrase reset request" do
    visit root_path
    click_on "Forgot your passphrase?"
    fill_in "Email", with: @user.email
    click_on "Send me passphrase reset instructions"
<commit_msg>Fix a test which doesn't send a CSRF token<commit_after>require 'test_helper'
require 'helpers/passphrase_support'

class EventLogCreationIntegrationTest < ActionDispatch::IntegrationTest
  include PassPhraseSupport

  setup do
    @admin = create(:admin_user, name: "Admin User")
    @user = create(:user, name: "Normal User")
  end

  test "record successful login" do
    visit root_path
    signin_with(@user)

    assert_equal 1, @user.event_logs.count
    assert_equal EventLog::SUCCESSFUL_LOGIN, @user.event_logs.last.entry
  end

  context "recording unsuccessful login" do
    should "record unsuccessful login for a valid email" do
      visit root_path
      signin_with(email: @user.email, password: :<PASSWORD>)

      assert_equal 1, @user.event_logs.count
      assert_equal EventLog::UNSUCCESSFUL_LOGIN, @user.event_logs.last.entry
    end

    should "log nothing for an invalid email" do
      visit root_path
      signin_with(email: "<EMAIL>", password: "<PASSWORD>")

      assert_equal 0, EventLog.count
    end

    should "raise an error when missing CSRF token" do
      assert_raises ActionController::InvalidAuthenticityToken do
        post "/users/sign_in", params: { "user" => { "email" => { "foo" => "bar" }, :password => "<PASSWORD>" } }
      end
    end
  end

  test "record passphrase reset request" do
    visit root_path
    click_on "Forgot your passphrase?"
    fill_in "Email", with: @user.email
    click_on "Send me passphrase reset instructions"

{'fraction_non_alphanumeric': 0.09234308248439041, 'fraction_numerical': 0.001971738416036806, 'mean_word_length': 3.6261398176291793, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, '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': '3860069', 'n_tokens_mistral': 935, 'n_tokens_neox': 903, 'n_words': 286}