<commit_before>defmodule SessionIntegrationTest do
  use MogoChat.TestCase
  use Hound.Helpers
  import TestUtils

  hound_session
  truncate_db_after_test


  def wait_until(element, func \\ nil) do
    if until_element(element) do
      if func do
        apply(func, [])
      else
        true
      end
    else
      IO.inspect "The following element wasn't found:"
      throw element
    end
  end


  def until_element(element, wait_time \\ 10000) do
    new_wait_time = wait_time - 1000
    :timer.sleep(1000)
    {strategy, identifier} = element

    if new_wait_time > 0 do
      try do
        find_element(strategy, identifier)
      catch
        _ ->
          until_element(element, new_wait_time)
      else
        _ ->
          true
      end
    else
      find_element(strategy, identifier)
    end
  end


  test "should redirect to login if I visit a page without logging in" do
    # navigate_to app_path()
    # current_url = 
    # Regex.match?(%r/(login)/, app_path())
  end


  test "member should be able to login and logout" do
    user = create_member("Test", "<EMAIL>")

    navigate_to app_path()
    url_at_login = current_url()

    fill_field {:name, "email"}, user.email
    fill_field {:name, "password"}, "password"
    click({:name, "login"})

    wait_until({:class, "left-panel-wrapper"})
    assert url_at_login != current_url()

    click({:id, "logout-btn"})
    assert url_at_login == current_url()
  end


  test "admin should be able to login and logout" do
    user = create_admin("Test", "<EMAIL>")

    navigate_to app_path()
    url_at_login = current_url()

    fill_field {:name, "email"}, user.email
    fill_field {:name, "password"}, "password"
    click({:name, "login"})

    wait_until({:class, "left-panel-wrapper"})
    assert url_at_login != current_url()

    click({:id, "logout-btn"})
    assert url_at_login == current_url()
  end

end
<commit_msg>Tests for logging in<commit_after>defmodule SessionIntegrationTest do
  use MogoChat.TestCase
  use Hound.Helpers
  import TestUtils

  hound_session
  truncate_db_after_test


  def wait_until(element, func \\ nil) do
    if until_element(element) do
      if func do
        apply(func, [])
      else
        true
      end
    else
      IO.inspect "The following element wasn't found:"
      throw element
    end
  end


  def until_element(element, wait_time \\ 10) do
    new_wait_time = wait_time - 1
    :timer.sleep(1000)
    {strategy, identifier} = element

    if new_wait_time > 0 do
      try do
        find_element(strategy, identifier)
      catch
        _ ->
          until_element(element, new_wait_time)
      else
        _ ->
          true
      end
    else
      find_element(strategy, identifier)
    end
  end


  test "should redirect to login if I visit a page without logging in" do
    navigate_to app_path()
    wait_until({:name, "login"})
    assert Regex.match?(%r/(login)/, current_url)
  end


  test "member should be able to login and logout" do
    user = create_member("Test", "<EMAIL>")

    navigate_to app_path()
    url_at_login = current_url()

    fill_field {:name, "email"}, user.email
    fill_field {:name, "password"}, "password"
    click({:name, "login"})

    wait_until({:class, "left-panel-wrapper"})
    assert url_at_login != current_url()

    click({:id, "logout-btn"})
    assert url_at_login == current_url()
  end


  test "admin should be able to login and logout" do
    user = create_admin("Test", "<EMAIL>")

    navigate_to app_path()
    url_at_login = current_url()

    fill_field {:name, "email"}, user.email
    fill_field {:name, "password"}, "password"
    click({:name, "login"})

    wait_until({:class, "left-panel-wrapper"})
    assert url_at_login != current_url()

    click({:id, "logout-btn"})
    assert url_at_login == current_url()
  end

end

{'fraction_non_alphanumeric': 0.10906258114775383, 'fraction_numerical': 0.005712801869644248, 'mean_word_length': 2.772771792360431, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '6030161', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1226, 'n_words': 335}