forked from shibao/cannery
		
	upgrade to phoenix 1.7
This commit is contained in:
		
							
								
								
									
										14
									
								
								test/cannery_web/controllers/error_html_test.exs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								test/cannery_web/controllers/error_html_test.exs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
defmodule CanneryWeb.ErrorHTMLTest do
 | 
			
		||||
  use CanneryWeb.ConnCase, async: true
 | 
			
		||||
  # Bring render_to_string/4 for testing custom views
 | 
			
		||||
  import Phoenix.Template
 | 
			
		||||
  alias CanneryWeb.ErrorHTML
 | 
			
		||||
 | 
			
		||||
  test "renders 404.html" do
 | 
			
		||||
    assert render_to_string(ErrorHTML, "404", "html", []) =~ "Not found"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  test "renders 500.html" do
 | 
			
		||||
    assert render_to_string(ErrorHTML, "500", "html", []) =~ "Internal server error"
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										12
									
								
								test/cannery_web/controllers/error_json_test.exs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								test/cannery_web/controllers/error_json_test.exs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
defmodule CanneryWeb.ErrorJSONTest do
 | 
			
		||||
  use CanneryWeb.ConnCase, async: true
 | 
			
		||||
  alias CanneryWeb.ErrorJSON
 | 
			
		||||
 | 
			
		||||
  test "renders 404" do
 | 
			
		||||
    assert ErrorJSON.render("404.json", %{}) == %{errors: %{detail: "Not found"}}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  test "renders 500" do
 | 
			
		||||
    assert ErrorJSON.render("500.json", %{}) == %{errors: %{detail: "Internal server error"}}
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@@ -40,7 +40,7 @@ defmodule CanneryWeb.ExportControllerTest do
 | 
			
		||||
      shot_record: shot_record,
 | 
			
		||||
      tag: tag
 | 
			
		||||
    } do
 | 
			
		||||
      conn = get(conn, Routes.export_path(conn, :export, :json))
 | 
			
		||||
      conn = get(conn, ~p"/export/json")
 | 
			
		||||
 | 
			
		||||
      ideal_pack = %{
 | 
			
		||||
        "type_id" => pack.type_id,
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ defmodule CanneryWeb.HomeControllerTest do
 | 
			
		||||
  @moduletag :home_controller_test
 | 
			
		||||
 | 
			
		||||
  test "GET /", %{conn: conn} do
 | 
			
		||||
    conn = get(conn, "/")
 | 
			
		||||
    conn = get(conn, ~p"/")
 | 
			
		||||
    assert html_response(conn, 200) =~ "Welcome to Cannery"
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -24,7 +24,7 @@ defmodule CanneryWeb.UserAuthTest do
 | 
			
		||||
      conn = UserAuth.log_in_user(conn, current_user)
 | 
			
		||||
      assert token = get_session(conn, :user_token)
 | 
			
		||||
      assert get_session(conn, :live_socket_id) == "users_sessions:#{Base.url_encode64(token)}"
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
      assert Accounts.get_user_by_session_token(token)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@@ -65,7 +65,7 @@ defmodule CanneryWeb.UserAuthTest do
 | 
			
		||||
      refute get_session(conn, :user_token)
 | 
			
		||||
      refute conn.cookies[@remember_me_cookie]
 | 
			
		||||
      assert %{max_age: 0} = conn.resp_cookies[@remember_me_cookie]
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
      refute Accounts.get_user_by_session_token(user_token)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@@ -87,7 +87,7 @@ defmodule CanneryWeb.UserAuthTest do
 | 
			
		||||
      conn = conn |> fetch_cookies() |> UserAuth.log_out_user()
 | 
			
		||||
      refute get_session(conn, :user_token)
 | 
			
		||||
      assert %{max_age: 0} = conn.resp_cookies[@remember_me_cookie]
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@@ -130,7 +130,7 @@ defmodule CanneryWeb.UserAuthTest do
 | 
			
		||||
        |> UserAuth.redirect_if_user_is_authenticated([])
 | 
			
		||||
 | 
			
		||||
      assert conn.halted
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "does not redirect if user is not authenticated", %{conn: conn} do
 | 
			
		||||
@@ -144,9 +144,9 @@ defmodule CanneryWeb.UserAuthTest do
 | 
			
		||||
    test "redirects if user is not authenticated", %{conn: conn} do
 | 
			
		||||
      conn = conn |> fetch_flash() |> UserAuth.require_authenticated_user([])
 | 
			
		||||
      assert conn.halted
 | 
			
		||||
      assert redirected_to(conn) == Routes.user_session_path(conn, :new)
 | 
			
		||||
      assert redirected_to(conn) == ~p"/users/log_in"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :error) ==
 | 
			
		||||
      assert conn.assigns.flash["error"] ==
 | 
			
		||||
               "You must confirm your account and log in to access this page."
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,7 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
 | 
			
		||||
 | 
			
		||||
  describe "GET /users/confirm" do
 | 
			
		||||
    test "renders the confirmation page", %{conn: conn} do
 | 
			
		||||
      conn = get(conn, Routes.user_confirmation_path(conn, :new))
 | 
			
		||||
      conn = get(conn, ~p"/users/confirm")
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ "Resend confirmation instructions"
 | 
			
		||||
    end
 | 
			
		||||
@@ -23,12 +23,10 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
 | 
			
		||||
  describe "POST /users/confirm" 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}})
 | 
			
		||||
      conn = post(conn, ~p"/users/confirm", %{user: %{email: user.email}})
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :info) =~
 | 
			
		||||
      assert conn.assigns.flash["info"] =~
 | 
			
		||||
               "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"
 | 
			
		||||
@@ -37,24 +35,18 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
 | 
			
		||||
    test "does not send confirmation token if User is confirmed", %{conn: conn, user: user} do
 | 
			
		||||
      Repo.update!(Accounts.User.confirm_changeset(user))
 | 
			
		||||
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_confirmation_path(conn, :create), %{user: %{email: user.email}})
 | 
			
		||||
      conn = post(conn, ~p"/users/confirm", %{user: %{email: user.email}})
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :info) =~
 | 
			
		||||
      assert conn.assigns.flash["info"] =~
 | 
			
		||||
               "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"}
 | 
			
		||||
        })
 | 
			
		||||
      conn = post(conn, ~p"/users/confirm", %{user: %{email: "unknown@example.com"}})
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :info) =~
 | 
			
		||||
      assert conn.assigns.flash["info"] =~
 | 
			
		||||
               "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) == []
 | 
			
		||||
@@ -68,33 +60,33 @@ defmodule CanneryWeb.UserConfirmationControllerTest do
 | 
			
		||||
          Accounts.deliver_user_confirmation_instructions(user, url)
 | 
			
		||||
        end)
 | 
			
		||||
 | 
			
		||||
      conn = get(conn, Routes.user_confirmation_path(conn, :confirm, token))
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
      assert get_flash(conn, :info) =~ "#{user.email} confirmed successfully"
 | 
			
		||||
      conn = get(conn, ~p"/users/confirm/#{token}")
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
      assert conn.assigns.flash["info"] =~ "#{user.email} confirmed successfully"
 | 
			
		||||
      assert Accounts.get_user!(user.id).confirmed_at
 | 
			
		||||
      refute get_session(conn, :user_token)
 | 
			
		||||
      assert Repo.all(Accounts.UserToken) == []
 | 
			
		||||
 | 
			
		||||
      # When not logged in
 | 
			
		||||
      conn = get(conn, Routes.user_confirmation_path(conn, :confirm, token))
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
      conn = get(conn, ~p"/users/confirm/#{token}")
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :error) =~ "User confirmation link is invalid or it has expired"
 | 
			
		||||
      assert conn.assigns.flash["error"] =~ "User confirmation link is invalid or it has expired"
 | 
			
		||||
 | 
			
		||||
      # When logged in
 | 
			
		||||
      conn =
 | 
			
		||||
        build_conn()
 | 
			
		||||
        |> log_in_user(user)
 | 
			
		||||
        |> get(Routes.user_confirmation_path(conn, :confirm, token))
 | 
			
		||||
        |> get(~p"/users/confirm/#{token}")
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
      refute get_flash(conn, :error)
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
      refute conn.assigns.flash["error"]
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    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) =~ "User confirmation link is invalid or it has expired"
 | 
			
		||||
      conn = get(conn, ~p"/users/confirm/oops")
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
      assert conn.assigns.flash["error"] =~ "User confirmation link is invalid or it has expired"
 | 
			
		||||
      refute Accounts.get_user!(user.id).confirmed_at
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 
 | 
			
		||||
@@ -9,15 +9,15 @@ defmodule CanneryWeb.UserRegistrationControllerTest do
 | 
			
		||||
 | 
			
		||||
  describe "GET /users/register" do
 | 
			
		||||
    test "renders registration page", %{conn: conn} do
 | 
			
		||||
      conn = get(conn, Routes.user_registration_path(conn, :new))
 | 
			
		||||
      conn = get(conn, ~p"/users/register")
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ "Register"
 | 
			
		||||
      assert response =~ "Log in"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "redirects if already logged in", %{conn: conn} do
 | 
			
		||||
      conn = conn |> log_in_user(user_fixture()) |> get(Routes.user_registration_path(conn, :new))
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
      conn = conn |> log_in_user(user_fixture()) |> get(~p"/users/register")
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@@ -25,20 +25,16 @@ defmodule CanneryWeb.UserRegistrationControllerTest do
 | 
			
		||||
    @tag :capture_log
 | 
			
		||||
    test "creates account and logs the user in", %{conn: conn} do
 | 
			
		||||
      email = unique_user_email()
 | 
			
		||||
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_registration_path(conn, :create), %{
 | 
			
		||||
          user: valid_user_attributes(email: email)
 | 
			
		||||
        })
 | 
			
		||||
      conn = post(conn, ~p"/users/register", %{user: valid_user_attributes(email: email)})
 | 
			
		||||
 | 
			
		||||
      assert get_session(conn, :phoenix_flash) == %{
 | 
			
		||||
               "info" => "Please check your email to verify your account"
 | 
			
		||||
             }
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) =~ "/"
 | 
			
		||||
      assert redirected_to(conn) =~ ~p"/"
 | 
			
		||||
 | 
			
		||||
      # Now do a logged in request and assert on the menu
 | 
			
		||||
      conn = get(conn, "/")
 | 
			
		||||
      conn = get(conn, ~p"/")
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      # user's email is recorded as admin
 | 
			
		||||
      assert response =~ email
 | 
			
		||||
@@ -46,9 +42,7 @@ 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"}
 | 
			
		||||
        })
 | 
			
		||||
        post(conn, ~p"/users/register", %{user: %{email: "with spaces", password: "too short"}})
 | 
			
		||||
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ "Register"
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,7 @@ defmodule CanneryWeb.UserResetPasswordControllerTest do
 | 
			
		||||
 | 
			
		||||
  describe "GET /users/reset_password" do
 | 
			
		||||
    test "renders the reset password page", %{conn: conn} do
 | 
			
		||||
      conn = get(conn, Routes.user_reset_password_path(conn, :new))
 | 
			
		||||
      conn = get(conn, ~p"/users/reset_password")
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ "Forgot your password?"
 | 
			
		||||
    end
 | 
			
		||||
@@ -23,26 +23,20 @@ defmodule CanneryWeb.UserResetPasswordControllerTest do
 | 
			
		||||
  describe "POST /users/reset_password" 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}})
 | 
			
		||||
      conn = post(conn, ~p"/users/reset_password", %{user: %{email: user.email}})
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :info) =~
 | 
			
		||||
      assert conn.assigns.flash["info"] =~
 | 
			
		||||
               "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
 | 
			
		||||
 | 
			
		||||
    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"}
 | 
			
		||||
        })
 | 
			
		||||
      conn = post(conn, ~p"/users/reset_password", %{user: %{email: "unknown@example.com"}})
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :info) =~
 | 
			
		||||
      assert conn.assigns.flash["info"] =~
 | 
			
		||||
               "If your email is in our system, you will receive instructions to reset your password shortly."
 | 
			
		||||
 | 
			
		||||
      assert Repo.all(Accounts.UserToken) == []
 | 
			
		||||
@@ -60,14 +54,14 @@ defmodule CanneryWeb.UserResetPasswordControllerTest do
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "renders reset password", %{conn: conn, token: token} do
 | 
			
		||||
      conn = get(conn, Routes.user_reset_password_path(conn, :edit, token))
 | 
			
		||||
      conn = get(conn, ~p"/users/reset_password/#{token}")
 | 
			
		||||
      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) =~ "Reset password link is invalid or it has expired"
 | 
			
		||||
      conn = get(conn, ~p"/users/reset_password/oops")
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
      assert conn.assigns.flash["error"] =~ "Reset password link is invalid or it has expired"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@@ -83,22 +77,22 @@ 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), %{
 | 
			
		||||
        put(conn, ~p"/users/reset_password/#{token}", %{
 | 
			
		||||
          user: %{
 | 
			
		||||
            password: "new valid password",
 | 
			
		||||
            password_confirmation: "new valid password"
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == Routes.user_session_path(conn, :new)
 | 
			
		||||
      assert redirected_to(conn) == ~p"/users/log_in"
 | 
			
		||||
      refute get_session(conn, :user_token)
 | 
			
		||||
      assert get_flash(conn, :info) =~ "Password reset successfully"
 | 
			
		||||
      assert conn.assigns.flash["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), %{
 | 
			
		||||
        put(conn, ~p"/users/reset_password/#{token}", %{
 | 
			
		||||
          user: %{
 | 
			
		||||
            password: "too short",
 | 
			
		||||
            password_confirmation: "does not match"
 | 
			
		||||
@@ -112,9 +106,9 @@ defmodule CanneryWeb.UserResetPasswordControllerTest do
 | 
			
		||||
    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) =~ "Reset password link is invalid or it has expired"
 | 
			
		||||
      conn = put(conn, ~p"/users/reset_password/oops")
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
      assert conn.assigns.flash["error"] =~ "Reset password link is invalid or it has expired"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -13,29 +13,29 @@ defmodule CanneryWeb.UserSessionControllerTest do
 | 
			
		||||
 | 
			
		||||
  describe "GET /users/log_in" do
 | 
			
		||||
    test "renders log in page", %{conn: conn} do
 | 
			
		||||
      conn = get(conn, Routes.user_session_path(conn, :new))
 | 
			
		||||
      conn = get(conn, ~p"/users/log_in")
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ "Log in"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "redirects if already logged in", %{conn: conn, current_user: current_user} do
 | 
			
		||||
      conn = conn |> log_in_user(current_user) |> get(Routes.user_session_path(conn, :new))
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
      conn = conn |> log_in_user(current_user) |> get(~p"/users/log_in")
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe "POST /users/log_in" do
 | 
			
		||||
    test "logs the user in", %{conn: conn, current_user: current_user} do
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_session_path(conn, :create), %{
 | 
			
		||||
        post(conn, ~p"/users/log_in", %{
 | 
			
		||||
          user: %{email: current_user.email, password: valid_user_password()}
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      assert get_session(conn, :user_token)
 | 
			
		||||
      assert redirected_to(conn) =~ "/"
 | 
			
		||||
      assert redirected_to(conn) =~ ~p"/"
 | 
			
		||||
 | 
			
		||||
      # Now do a logged in request and assert on the menu
 | 
			
		||||
      conn = get(conn, "/")
 | 
			
		||||
      conn = get(conn, ~p"/")
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ current_user.email
 | 
			
		||||
      assert response =~ "Are you sure you want to log out?"
 | 
			
		||||
@@ -43,7 +43,7 @@ defmodule CanneryWeb.UserSessionControllerTest do
 | 
			
		||||
 | 
			
		||||
    test "logs the user in with remember me", %{conn: conn, current_user: current_user} do
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_session_path(conn, :create), %{
 | 
			
		||||
        post(conn, ~p"/users/log_in", %{
 | 
			
		||||
          user: %{
 | 
			
		||||
            email: current_user.email,
 | 
			
		||||
            password: valid_user_password(),
 | 
			
		||||
@@ -52,14 +52,14 @@ defmodule CanneryWeb.UserSessionControllerTest do
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      assert conn.resp_cookies["_cannery_web_user_remember_me"]
 | 
			
		||||
      assert redirected_to(conn) =~ "/"
 | 
			
		||||
      assert redirected_to(conn) =~ ~p"/"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "logs the user in with return to", %{conn: conn, current_user: current_user} do
 | 
			
		||||
      conn =
 | 
			
		||||
        conn
 | 
			
		||||
        |> init_test_session(user_return_to: "/foo/bar")
 | 
			
		||||
        |> post(Routes.user_session_path(conn, :create), %{
 | 
			
		||||
        |> post(~p"/users/log_in", %{
 | 
			
		||||
          user: %{
 | 
			
		||||
            email: current_user.email,
 | 
			
		||||
            password: valid_user_password()
 | 
			
		||||
@@ -71,11 +71,7 @@ defmodule CanneryWeb.UserSessionControllerTest do
 | 
			
		||||
 | 
			
		||||
    test "emits error message with invalid credentials",
 | 
			
		||||
         %{conn: conn, current_user: current_user} do
 | 
			
		||||
      conn =
 | 
			
		||||
        post(conn, Routes.user_session_path(conn, :create), %{
 | 
			
		||||
          user: %{email: current_user.email, password: "bad"}
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      conn = post(conn, ~p"/users/log_in", %{user: %{email: current_user.email, password: "bad"}})
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ "Log in"
 | 
			
		||||
      assert response =~ "Invalid email or password"
 | 
			
		||||
@@ -84,17 +80,17 @@ defmodule CanneryWeb.UserSessionControllerTest do
 | 
			
		||||
 | 
			
		||||
  describe "DELETE /users/log_out" do
 | 
			
		||||
    test "logs the user out", %{conn: conn, current_user: current_user} do
 | 
			
		||||
      conn = conn |> log_in_user(current_user) |> delete(Routes.user_session_path(conn, :delete))
 | 
			
		||||
      assert redirected_to(conn) == "/"
 | 
			
		||||
      conn = conn |> log_in_user(current_user) |> delete(~p"/users/log_out")
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
      refute get_session(conn, :user_token)
 | 
			
		||||
      assert get_flash(conn, :info) =~ "Logged out successfully"
 | 
			
		||||
      assert conn.assigns.flash["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) == "/"
 | 
			
		||||
      conn = delete(conn, ~p"/users/log_out")
 | 
			
		||||
      assert redirected_to(conn) == ~p"/"
 | 
			
		||||
      refute get_session(conn, :user_token)
 | 
			
		||||
      assert get_flash(conn, :info) =~ "Logged out successfully"
 | 
			
		||||
      assert conn.assigns.flash["info"] =~ "Logged out successfully"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -12,15 +12,15 @@ defmodule CanneryWeb.UserSettingsControllerTest do
 | 
			
		||||
 | 
			
		||||
  describe "GET /users/settings" do
 | 
			
		||||
    test "renders settings page", %{conn: conn} do
 | 
			
		||||
      conn = get(conn, Routes.user_settings_path(conn, :edit))
 | 
			
		||||
      conn = get(conn, ~p"/users/settings")
 | 
			
		||||
      response = html_response(conn, 200)
 | 
			
		||||
      assert response =~ "Settings"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "redirects if user is not logged in" do
 | 
			
		||||
      conn = build_conn()
 | 
			
		||||
      conn = get(conn, Routes.user_settings_path(conn, :edit))
 | 
			
		||||
      assert redirected_to(conn) == Routes.user_session_path(conn, :new)
 | 
			
		||||
      conn = get(conn, ~p"/users/settings")
 | 
			
		||||
      assert redirected_to(conn) == ~p"/users/log_in"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@@ -28,7 +28,7 @@ defmodule CanneryWeb.UserSettingsControllerTest do
 | 
			
		||||
    test "updates the user password and resets tokens",
 | 
			
		||||
         %{conn: conn, current_user: current_user} do
 | 
			
		||||
      new_password_conn =
 | 
			
		||||
        put(conn, Routes.user_settings_path(conn, :update), %{
 | 
			
		||||
        put(conn, ~p"/users/settings", %{
 | 
			
		||||
          action: "update_password",
 | 
			
		||||
          current_password: valid_user_password(),
 | 
			
		||||
          user: %{
 | 
			
		||||
@@ -37,15 +37,15 @@ defmodule CanneryWeb.UserSettingsControllerTest do
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(new_password_conn) == Routes.user_settings_path(conn, :edit)
 | 
			
		||||
      assert redirected_to(new_password_conn) == ~p"/users/settings"
 | 
			
		||||
      assert get_session(new_password_conn, :user_token) != get_session(conn, :user_token)
 | 
			
		||||
      assert get_flash(new_password_conn, :info) =~ "Password updated successfully"
 | 
			
		||||
      assert new_password_conn.assigns.flash["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), %{
 | 
			
		||||
        put(conn, ~p"/users/settings", %{
 | 
			
		||||
          action: "update_password",
 | 
			
		||||
          current_password: "invalid",
 | 
			
		||||
          user: %{
 | 
			
		||||
@@ -67,15 +67,15 @@ defmodule CanneryWeb.UserSettingsControllerTest do
 | 
			
		||||
    @tag :capture_log
 | 
			
		||||
    test "updates the user email", %{conn: conn, current_user: current_user} do
 | 
			
		||||
      conn =
 | 
			
		||||
        put(conn, Routes.user_settings_path(conn, :update), %{
 | 
			
		||||
        put(conn, ~p"/users/settings", %{
 | 
			
		||||
          action: "update_email",
 | 
			
		||||
          current_password: valid_user_password(),
 | 
			
		||||
          user: %{email: unique_user_email()}
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
      assert redirected_to(conn) == Routes.user_settings_path(conn, :edit)
 | 
			
		||||
      assert redirected_to(conn) == ~p"/users/settings"
 | 
			
		||||
 | 
			
		||||
      assert get_flash(conn, :info) =~
 | 
			
		||||
      assert conn.assigns.flash["info"] =~
 | 
			
		||||
               "A link to confirm your email change has been sent to the new address."
 | 
			
		||||
 | 
			
		||||
      assert Accounts.get_user_by_email(current_user.email)
 | 
			
		||||
@@ -83,7 +83,7 @@ defmodule CanneryWeb.UserSettingsControllerTest do
 | 
			
		||||
 | 
			
		||||
    test "does not update email on invalid data", %{conn: conn} do
 | 
			
		||||
      conn =
 | 
			
		||||
        put(conn, Routes.user_settings_path(conn, :update), %{
 | 
			
		||||
        put(conn, ~p"/users/settings", %{
 | 
			
		||||
          action: "update_email",
 | 
			
		||||
          current_password: "invalid",
 | 
			
		||||
          user: %{email: "with spaces"}
 | 
			
		||||
@@ -114,28 +114,28 @@ defmodule CanneryWeb.UserSettingsControllerTest do
 | 
			
		||||
 | 
			
		||||
    test "updates the user email once",
 | 
			
		||||
         %{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) =~ "Email changed successfully"
 | 
			
		||||
      conn = get(conn, ~p"/users/settings/confirm_email/#{token}")
 | 
			
		||||
      assert redirected_to(conn) == ~p"/users/settings"
 | 
			
		||||
      assert conn.assigns.flash["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) =~ "Email change link is invalid or it has expired"
 | 
			
		||||
      conn = get(conn, ~p"/users/settings/confirm_email/#{token}")
 | 
			
		||||
      assert redirected_to(conn) == ~p"/users/settings"
 | 
			
		||||
      assert conn.assigns.flash["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) =~ "Email change link is invalid or it has expired"
 | 
			
		||||
      conn = get(conn, ~p"/users/settings/confirm_email/oops")
 | 
			
		||||
      assert redirected_to(conn) == ~p"/users/settings"
 | 
			
		||||
      assert conn.assigns.flash["error"] =~ "Email change link is invalid or it has expired"
 | 
			
		||||
      assert Accounts.get_user_by_email(current_user.email)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "redirects if user is not logged in", %{token: token} do
 | 
			
		||||
      conn = build_conn()
 | 
			
		||||
      conn = get(conn, Routes.user_settings_path(conn, :confirm_email, token))
 | 
			
		||||
      assert redirected_to(conn) == Routes.user_session_path(conn, :new)
 | 
			
		||||
      conn = get(conn, ~p"/users/settings/confirm_email/#{token}")
 | 
			
		||||
      assert redirected_to(conn) == ~p"/users/log_in"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user