forked from shibao/cannery
		
	use atom keys in tests
This commit is contained in:
		@@ -4,7 +4,6 @@ defmodule CanneryWeb.UserAuthTest do
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use CanneryWeb.ConnCase, async: true
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  alias Cannery.Accounts
 | 
			
		||||
  alias CanneryWeb.UserAuth
 | 
			
		||||
 | 
			
		||||
@@ -148,7 +147,7 @@ defmodule CanneryWeb.UserAuthTest do
 | 
			
		||||
      assert redirected_to(conn) == Routes.user_session_path(conn, :new)
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :error) ==
 | 
			
		||||
               dgettext("errors", "You must confirm your account and log in to access this page.")
 | 
			
		||||
               "You must confirm your account and log in to access this page."
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "stores the path to redirect to on GET", %{conn: conn} do
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,6 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use CanneryWeb.ConnCase, async: true
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  alias Cannery.{Accounts, Repo}
 | 
			
		||||
 | 
			
		||||
  @moduletag :user_confirmation_controller_test
 | 
			
		||||
@@ -17,7 +16,7 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
 | 
			
		||||
    test "renders the confirmation page", %{conn: conn} do
 | 
			
		||||
      conn = get(conn, Routes.user_confirmation_path(conn, :new))
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ dgettext("actions", "Resend confirmation instructions")
 | 
			
		||||
      assert response =~ "Resend confirmation instructions"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@@ -25,18 +24,12 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
 | 
			
		||||
    @tag :capture_log
 | 
			
		||||
    test "sends a new confirmation token", %{conn: conn, user: user} do
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_confirmation_path(conn, :create), %{
 | 
			
		||||
          "user" => %{"email" => user.email}
 | 
			
		||||
        })
 | 
			
		||||
        post(conn, Routes.user_confirmation_path(conn, :create), %{user: %{email: user.email}})
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :info) =~
 | 
			
		||||
               dgettext(
 | 
			
		||||
                 "prompts",
 | 
			
		||||
                 "If your email is in our system and it has not been confirmed yet, " <>
 | 
			
		||||
                   "you will receive an email with instructions shortly."
 | 
			
		||||
               )
 | 
			
		||||
               "If your email is in our system and it has not been confirmed yet, you will receive an email with instructions shortly."
 | 
			
		||||
 | 
			
		||||
      assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context == "confirm"
 | 
			
		||||
    end
 | 
			
		||||
@@ -45,34 +38,24 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
 | 
			
		||||
      Repo.update!(Accounts.User.confirm_changeset(user))
 | 
			
		||||
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_confirmation_path(conn, :create), %{
 | 
			
		||||
          "user" => %{"email" => user.email}
 | 
			
		||||
        })
 | 
			
		||||
        post(conn, Routes.user_confirmation_path(conn, :create), %{user: %{email: user.email}})
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :info) =~
 | 
			
		||||
               dgettext(
 | 
			
		||||
                 "prompts",
 | 
			
		||||
                 "If your email is in our system and it has not been confirmed yet, " <>
 | 
			
		||||
                   "you will receive an email with instructions shortly."
 | 
			
		||||
               )
 | 
			
		||||
               "If your email is in our system and it has not been confirmed yet, you will receive an email with instructions shortly."
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "does not send confirmation token if email is invalid", %{conn: conn} do
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_confirmation_path(conn, :create), %{
 | 
			
		||||
          "user" => %{"email" => "unknown@example.com"}
 | 
			
		||||
          user: %{email: "unknown@example.com"}
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :info) =~
 | 
			
		||||
               dgettext(
 | 
			
		||||
                 "prompts",
 | 
			
		||||
                 "If your email is in our system and it has not been confirmed yet, " <>
 | 
			
		||||
                   "you will receive an email with instructions shortly."
 | 
			
		||||
               )
 | 
			
		||||
               "If your email is in our system and it has not been confirmed yet, you will receive an email with instructions shortly."
 | 
			
		||||
 | 
			
		||||
      assert Repo.all(Accounts.UserToken) == []
 | 
			
		||||
    end
 | 
			
		||||
@@ -87,10 +70,7 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
 | 
			
		||||
 | 
			
		||||
      conn = get(conn, Routes.user_confirmation_path(conn, :confirm, token))
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :info) =~
 | 
			
		||||
               dgettext("prompts", "%{email} confirmed successfully", email: user.email)
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :info) =~ "#{user.email} confirmed successfully"
 | 
			
		||||
      assert Accounts.get_user!(user.id).confirmed_at
 | 
			
		||||
      refute get_session(conn, :user_token)
 | 
			
		||||
      assert Repo.all(Accounts.UserToken) == []
 | 
			
		||||
@@ -99,8 +79,7 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
 | 
			
		||||
      conn = get(conn, Routes.user_confirmation_path(conn, :confirm, token))
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :error) =~
 | 
			
		||||
               dgettext("errors", "User confirmation link is invalid or it has expired")
 | 
			
		||||
      assert get_flash(conn, :error) =~ "User confirmation link is invalid or it has expired"
 | 
			
		||||
 | 
			
		||||
      # When logged in
 | 
			
		||||
      conn =
 | 
			
		||||
@@ -115,10 +94,7 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
 | 
			
		||||
    test "does not confirm email with invalid token", %{conn: conn, user: user} do
 | 
			
		||||
      conn = get(conn, Routes.user_confirmation_path(conn, :confirm, "oops"))
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :error) =~
 | 
			
		||||
               dgettext("errors", "User confirmation link is invalid or it has expired")
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :error) =~ "User confirmation link is invalid or it has expired"
 | 
			
		||||
      refute Accounts.get_user!(user.id).confirmed_at
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,6 @@ defmodule CanneryWeb.UserRegistrationControllerTest do
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use CanneryWeb.ConnCase, async: true
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
 | 
			
		||||
  @moduletag :user_registration_controller_test
 | 
			
		||||
 | 
			
		||||
@@ -12,8 +11,8 @@ defmodule CanneryWeb.UserRegistrationControllerTest do
 | 
			
		||||
    test "renders registration page", %{conn: conn} do
 | 
			
		||||
      conn = get(conn, Routes.user_registration_path(conn, :new))
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ dgettext("actions", "Register")
 | 
			
		||||
      assert response =~ dgettext("actions", "Log in")
 | 
			
		||||
      assert response =~ "Register"
 | 
			
		||||
      assert response =~ "Log in"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "redirects if already logged in", %{conn: conn} do
 | 
			
		||||
@@ -29,11 +28,11 @@ defmodule CanneryWeb.UserRegistrationControllerTest do
 | 
			
		||||
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_registration_path(conn, :create), %{
 | 
			
		||||
          "user" => valid_user_attributes(email: email)
 | 
			
		||||
          user: valid_user_attributes(email: email)
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      assert get_session(conn, :phoenix_flash) == %{
 | 
			
		||||
               "info" => dgettext("prompts", "Please check your email to verify your account")
 | 
			
		||||
               "info" => "Please check your email to verify your account"
 | 
			
		||||
             }
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) =~ "/"
 | 
			
		||||
@@ -48,11 +47,11 @@ defmodule CanneryWeb.UserRegistrationControllerTest do
 | 
			
		||||
    test "render errors for invalid data", %{conn: conn} do
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_registration_path(conn, :create), %{
 | 
			
		||||
          "user" => %{"email" => "with spaces", "password" => "too short"}
 | 
			
		||||
          user: %{email: "with spaces", password: "too short"}
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ gettext("Register")
 | 
			
		||||
      assert response =~ "Register"
 | 
			
		||||
      assert response =~ "must have the @ sign and no spaces"
 | 
			
		||||
      assert response =~ "should be at least 12 character"
 | 
			
		||||
    end
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,6 @@ defmodule CanneryWeb.UserResetPasswordControllerTest do
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use CanneryWeb.ConnCase, async: true
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  alias Cannery.{Accounts, Repo}
 | 
			
		||||
 | 
			
		||||
  @moduletag :user_reset_password_controller_test
 | 
			
		||||
@@ -17,7 +16,7 @@ defmodule CanneryWeb.UserResetPasswordControllerTest do
 | 
			
		||||
    test "renders the reset password page", %{conn: conn} do
 | 
			
		||||
      conn = get(conn, Routes.user_reset_password_path(conn, :new))
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ dgettext("actions", "Forgot your password?")
 | 
			
		||||
      assert response =~ "Forgot your password?"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@@ -25,17 +24,12 @@ defmodule CanneryWeb.UserResetPasswordControllerTest do
 | 
			
		||||
    @tag :capture_log
 | 
			
		||||
    test "sends a new reset password token", %{conn: conn, user: user} do
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_reset_password_path(conn, :create), %{
 | 
			
		||||
          "user" => %{"email" => user.email}
 | 
			
		||||
        })
 | 
			
		||||
        post(conn, Routes.user_reset_password_path(conn, :create), %{user: %{email: user.email}})
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :info) =~
 | 
			
		||||
               dgettext(
 | 
			
		||||
                 "prompts",
 | 
			
		||||
                 "If your email is in our system, you will receive instructions to reset your password shortly."
 | 
			
		||||
               )
 | 
			
		||||
               "If your email is in our system, you will receive instructions to reset your password shortly."
 | 
			
		||||
 | 
			
		||||
      assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context == "reset_password"
 | 
			
		||||
    end
 | 
			
		||||
@@ -43,16 +37,13 @@ defmodule CanneryWeb.UserResetPasswordControllerTest do
 | 
			
		||||
    test "does not send reset password token if email is invalid", %{conn: conn} do
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_reset_password_path(conn, :create), %{
 | 
			
		||||
          "user" => %{"email" => "unknown@example.com"}
 | 
			
		||||
          user: %{email: "unknown@example.com"}
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :info) =~
 | 
			
		||||
               dgettext(
 | 
			
		||||
                 "prompts",
 | 
			
		||||
                 "If your email is in our system, you will receive instructions to reset your password shortly."
 | 
			
		||||
               )
 | 
			
		||||
               "If your email is in our system, you will receive instructions to reset your password shortly."
 | 
			
		||||
 | 
			
		||||
      assert Repo.all(Accounts.UserToken) == []
 | 
			
		||||
    end
 | 
			
		||||
@@ -70,15 +61,13 @@ defmodule CanneryWeb.UserResetPasswordControllerTest do
 | 
			
		||||
 | 
			
		||||
    test "renders reset password", %{conn: conn, token: token} do
 | 
			
		||||
      conn = get(conn, Routes.user_reset_password_path(conn, :edit, token))
 | 
			
		||||
      assert html_response(conn, 200) =~ dgettext("actions", "Reset password")
 | 
			
		||||
      assert html_response(conn, 200) =~ "Reset password"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "does not render reset password with invalid token", %{conn: conn} do
 | 
			
		||||
      conn = get(conn, Routes.user_reset_password_path(conn, :edit, "oops"))
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :error) =~
 | 
			
		||||
               dgettext("errors", "Reset password link is invalid or it has expired")
 | 
			
		||||
      assert get_flash(conn, :error) =~ "Reset password link is invalid or it has expired"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@@ -95,39 +84,37 @@ defmodule CanneryWeb.UserResetPasswordControllerTest do
 | 
			
		||||
    test "resets password once", %{conn: conn, user: user, token: token} do
 | 
			
		||||
      conn =
 | 
			
		||||
        put(conn, Routes.user_reset_password_path(conn, :update, token), %{
 | 
			
		||||
          "user" => %{
 | 
			
		||||
            "password" => "new valid password",
 | 
			
		||||
            "password_confirmation" => "new valid password"
 | 
			
		||||
          user: %{
 | 
			
		||||
            password: "new valid password",
 | 
			
		||||
            password_confirmation: "new valid password"
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == Routes.user_session_path(conn, :new)
 | 
			
		||||
      refute get_session(conn, :user_token)
 | 
			
		||||
      assert get_flash(conn, :info) =~ dgettext("prompts", "Password reset successfully")
 | 
			
		||||
      assert get_flash(conn, :info) =~ "Password reset successfully"
 | 
			
		||||
      assert Accounts.get_user_by_email_and_password(user.email, "new valid password")
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "does not reset password on invalid data", %{conn: conn, token: token} do
 | 
			
		||||
      conn =
 | 
			
		||||
        put(conn, Routes.user_reset_password_path(conn, :update, token), %{
 | 
			
		||||
          "user" => %{
 | 
			
		||||
            "password" => "too short",
 | 
			
		||||
            "password_confirmation" => "does not match"
 | 
			
		||||
          user: %{
 | 
			
		||||
            password: "too short",
 | 
			
		||||
            password_confirmation: "does not match"
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ gettext("Reset password")
 | 
			
		||||
      assert response =~ dgettext("errors", "should be at least 12 character(s)")
 | 
			
		||||
      assert response =~ dgettext("errors", "does not match password")
 | 
			
		||||
      assert response =~ "Reset password"
 | 
			
		||||
      assert response =~ "should be at least 12 character(s)"
 | 
			
		||||
      assert response =~ "does not match password"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "does not reset password with invalid token", %{conn: conn} do
 | 
			
		||||
      conn = put(conn, Routes.user_reset_password_path(conn, :update, "oops"))
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :error) =~
 | 
			
		||||
               dgettext("errors", "Reset password link is invalid or it has expired")
 | 
			
		||||
      assert get_flash(conn, :error) =~ "Reset password link is invalid or it has expired"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,6 @@ defmodule CanneryWeb.UserSessionControllerTest do
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use CanneryWeb.ConnCase, async: true
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
 | 
			
		||||
  @moduletag :user_session_controller_test
 | 
			
		||||
 | 
			
		||||
@@ -16,7 +15,7 @@ defmodule CanneryWeb.UserSessionControllerTest do
 | 
			
		||||
    test "renders log in page", %{conn: conn} do
 | 
			
		||||
      conn = get(conn, Routes.user_session_path(conn, :new))
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ dgettext("actions", "Log in")
 | 
			
		||||
      assert response =~ "Log in"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "redirects if already logged in", %{conn: conn, current_user: current_user} do
 | 
			
		||||
@@ -29,7 +28,7 @@ defmodule CanneryWeb.UserSessionControllerTest do
 | 
			
		||||
    test "logs the user in", %{conn: conn, current_user: current_user} do
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_session_path(conn, :create), %{
 | 
			
		||||
          "user" => %{"email" => current_user.email, "password" => valid_user_password()}
 | 
			
		||||
          user: %{email: current_user.email, password: valid_user_password()}
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      assert get_session(conn, :user_token)
 | 
			
		||||
@@ -39,16 +38,16 @@ defmodule CanneryWeb.UserSessionControllerTest do
 | 
			
		||||
      conn = get(conn, "/")
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ current_user.email
 | 
			
		||||
      assert response =~ dgettext("prompts", "Are you sure you want to log out?")
 | 
			
		||||
      assert response =~ "Are you sure you want to log out?"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "logs the user in with remember me", %{conn: conn, current_user: current_user} do
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_session_path(conn, :create), %{
 | 
			
		||||
          "user" => %{
 | 
			
		||||
            "email" => current_user.email,
 | 
			
		||||
            "password" => valid_user_password(),
 | 
			
		||||
            "remember_me" => "true"
 | 
			
		||||
          user: %{
 | 
			
		||||
            email: current_user.email,
 | 
			
		||||
            password: valid_user_password(),
 | 
			
		||||
            remember_me: "true"
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
@@ -61,9 +60,9 @@ defmodule CanneryWeb.UserSessionControllerTest do
 | 
			
		||||
        conn
 | 
			
		||||
        |> init_test_session(user_return_to: "/foo/bar")
 | 
			
		||||
        |> post(Routes.user_session_path(conn, :create), %{
 | 
			
		||||
          "user" => %{
 | 
			
		||||
            "email" => current_user.email,
 | 
			
		||||
            "password" => valid_user_password()
 | 
			
		||||
          user: %{
 | 
			
		||||
            email: current_user.email,
 | 
			
		||||
            password: valid_user_password()
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
@@ -74,12 +73,12 @@ defmodule CanneryWeb.UserSessionControllerTest do
 | 
			
		||||
         %{conn: conn, current_user: current_user} do
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_session_path(conn, :create), %{
 | 
			
		||||
          "user" => %{"email" => current_user.email, "password" => "bad"}
 | 
			
		||||
          user: %{email: current_user.email, password: "bad"}
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ dgettext("actions", "Log in")
 | 
			
		||||
      assert response =~ dgettext("errors", "Invalid email or password")
 | 
			
		||||
      assert response =~ "Log in"
 | 
			
		||||
      assert response =~ "Invalid email or password"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@@ -88,14 +87,14 @@ defmodule CanneryWeb.UserSessionControllerTest do
 | 
			
		||||
      conn = conn |> log_in_user(current_user) |> delete(Routes.user_session_path(conn, :delete))
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
      refute get_session(conn, :user_token)
 | 
			
		||||
      assert get_flash(conn, :info) =~ gettext("Logged out successfully")
 | 
			
		||||
      assert get_flash(conn, :info) =~ "Logged out successfully"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "succeeds even if the user is not logged in", %{conn: conn} do
 | 
			
		||||
      conn = delete(conn, Routes.user_session_path(conn, :delete))
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
      refute get_session(conn, :user_token)
 | 
			
		||||
      assert get_flash(conn, :info) =~ gettext("Logged out successfully")
 | 
			
		||||
      assert get_flash(conn, :info) =~ "Logged out successfully"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,6 @@ defmodule CanneryWeb.UserSettingsControllerTest do
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use CanneryWeb.ConnCase, async: true
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  alias Cannery.Accounts
 | 
			
		||||
 | 
			
		||||
  @moduletag :user_settings_controller_test
 | 
			
		||||
@@ -15,7 +14,7 @@ defmodule CanneryWeb.UserSettingsControllerTest do
 | 
			
		||||
    test "renders settings page", %{conn: conn} do
 | 
			
		||||
      conn = get(conn, Routes.user_settings_path(conn, :edit))
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ gettext("Settings")
 | 
			
		||||
      assert response =~ "Settings"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "redirects if user is not logged in" do
 | 
			
		||||
@@ -30,40 +29,36 @@ defmodule CanneryWeb.UserSettingsControllerTest do
 | 
			
		||||
         %{conn: conn, current_user: current_user} do
 | 
			
		||||
      new_password_conn =
 | 
			
		||||
        put(conn, Routes.user_settings_path(conn, :update), %{
 | 
			
		||||
          "action" => "update_password",
 | 
			
		||||
          "current_password" => valid_user_password(),
 | 
			
		||||
          "user" => %{
 | 
			
		||||
            "password" => "new valid password",
 | 
			
		||||
            "password_confirmation" => "new valid password"
 | 
			
		||||
          action: "update_password",
 | 
			
		||||
          current_password: valid_user_password(),
 | 
			
		||||
          user: %{
 | 
			
		||||
            password: "new valid password",
 | 
			
		||||
            password_confirmation: "new valid password"
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(new_password_conn) == Routes.user_settings_path(conn, :edit)
 | 
			
		||||
      assert get_session(new_password_conn, :user_token) != get_session(conn, :user_token)
 | 
			
		||||
 | 
			
		||||
      assert get_flash(new_password_conn, :info) =~
 | 
			
		||||
               dgettext("actions", "Password updated successfully")
 | 
			
		||||
 | 
			
		||||
      assert get_flash(new_password_conn, :info) =~ "Password updated successfully"
 | 
			
		||||
      assert Accounts.get_user_by_email_and_password(current_user.email, "new valid password")
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "does not update password on invalid data", %{conn: conn} do
 | 
			
		||||
      old_password_conn =
 | 
			
		||||
        put(conn, Routes.user_settings_path(conn, :update), %{
 | 
			
		||||
          "action" => "update_password",
 | 
			
		||||
          "current_password" => "invalid",
 | 
			
		||||
          "user" => %{
 | 
			
		||||
            "password" => "too short",
 | 
			
		||||
            "password_confirmation" => "does not match"
 | 
			
		||||
          action: "update_password",
 | 
			
		||||
          current_password: "invalid",
 | 
			
		||||
          user: %{
 | 
			
		||||
            password: "too short",
 | 
			
		||||
            password_confirmation: "does not match"
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      response = html_response(old_password_conn, 200)
 | 
			
		||||
      assert response =~ gettext("Settings")
 | 
			
		||||
      assert response =~ dgettext("errors", "should be at least 12 character(s)")
 | 
			
		||||
      assert response =~ dgettext("errors", "does not match password")
 | 
			
		||||
      assert response =~ dgettext("errors", "is not valid")
 | 
			
		||||
 | 
			
		||||
      assert response =~ "Settings"
 | 
			
		||||
      assert response =~ "should be at least 12 character(s)"
 | 
			
		||||
      assert response =~ "does not match password"
 | 
			
		||||
      assert response =~ "is not valid"
 | 
			
		||||
      assert get_session(old_password_conn, :user_token) == get_session(conn, :user_token)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
@@ -73,18 +68,15 @@ defmodule CanneryWeb.UserSettingsControllerTest do
 | 
			
		||||
    test "updates the user email", %{conn: conn, current_user: current_user} do
 | 
			
		||||
      conn =
 | 
			
		||||
        put(conn, Routes.user_settings_path(conn, :update), %{
 | 
			
		||||
          "action" => "update_email",
 | 
			
		||||
          "current_password" => valid_user_password(),
 | 
			
		||||
          "user" => %{"email" => unique_user_email()}
 | 
			
		||||
          action: "update_email",
 | 
			
		||||
          current_password: valid_user_password(),
 | 
			
		||||
          user: %{email: unique_user_email()}
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == Routes.user_settings_path(conn, :edit)
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :info) =~
 | 
			
		||||
               dgettext(
 | 
			
		||||
                 "prompts",
 | 
			
		||||
                 "A link to confirm your email change has been sent to the new address."
 | 
			
		||||
               )
 | 
			
		||||
               "A link to confirm your email change has been sent to the new address."
 | 
			
		||||
 | 
			
		||||
      assert Accounts.get_user_by_email(current_user.email)
 | 
			
		||||
    end
 | 
			
		||||
@@ -92,15 +84,15 @@ defmodule CanneryWeb.UserSettingsControllerTest do
 | 
			
		||||
    test "does not update email on invalid data", %{conn: conn} do
 | 
			
		||||
      conn =
 | 
			
		||||
        put(conn, Routes.user_settings_path(conn, :update), %{
 | 
			
		||||
          "action" => "update_email",
 | 
			
		||||
          "current_password" => "invalid",
 | 
			
		||||
          "user" => %{"email" => "with spaces"}
 | 
			
		||||
          action: "update_email",
 | 
			
		||||
          current_password: "invalid",
 | 
			
		||||
          user: %{email: "with spaces"}
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ gettext("Settings")
 | 
			
		||||
      assert response =~ dgettext("errors", "must have the @ sign and no spaces")
 | 
			
		||||
      assert response =~ dgettext("errors", "is not valid")
 | 
			
		||||
      assert response =~ "Settings"
 | 
			
		||||
      assert response =~ "must have the @ sign and no spaces"
 | 
			
		||||
      assert response =~ "is not valid"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@@ -124,24 +116,19 @@ defmodule CanneryWeb.UserSettingsControllerTest do
 | 
			
		||||
         %{conn: conn, current_user: current_user, token: token, email: email} do
 | 
			
		||||
      conn = get(conn, Routes.user_settings_path(conn, :confirm_email, token))
 | 
			
		||||
      assert redirected_to(conn) == Routes.user_settings_path(conn, :edit)
 | 
			
		||||
      assert get_flash(conn, :info) =~ dgettext("prompts", "Email changed successfully")
 | 
			
		||||
      assert get_flash(conn, :info) =~ "Email changed successfully"
 | 
			
		||||
      refute Accounts.get_user_by_email(current_user.email)
 | 
			
		||||
      assert Accounts.get_user_by_email(email)
 | 
			
		||||
 | 
			
		||||
      conn = get(conn, Routes.user_settings_path(conn, :confirm_email, token))
 | 
			
		||||
      assert redirected_to(conn) == Routes.user_settings_path(conn, :edit)
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :error) =~
 | 
			
		||||
               dgettext("errors", "Email change link is invalid or it has expired")
 | 
			
		||||
      assert get_flash(conn, :error) =~ "Email change link is invalid or it has expired"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "does not update email with invalid token", %{conn: conn, current_user: current_user} do
 | 
			
		||||
      conn = get(conn, Routes.user_settings_path(conn, :confirm_email, "oops"))
 | 
			
		||||
      assert redirected_to(conn) == Routes.user_settings_path(conn, :edit)
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :error) =~
 | 
			
		||||
               dgettext("errors", "Email change link is invalid or it has expired")
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :error) =~ "Email change link is invalid or it has expired"
 | 
			
		||||
      assert Accounts.get_user_by_email(current_user.email)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user