~sbaildon/yemma

38e5b1fa9d88bd60185bf1f2712d50d5929f8432 — Sean Baildon 21 days ago 3311483 main
user_settings_url with conn
M lib/yemma/routes.ex => lib/yemma/routes.ex +2 -2
@@ 6,8 6,8 @@ defmodule Yemma.Routes do

  @callback confirm_session_url(conn :: Plug.Conn.t(), token :: String.t()) :: String.t()

  @callback user_settings_url() :: String.t()
  @callback user_settings_url(token :: String.t()) :: String.t()
  @callback user_settings_url(conn :: Plug.Conn.t()) :: String.t()
  @callback user_settings_url(conn :: Plug.Conn.t(), token :: String.t()) :: String.t()

  @callback already_authenticated_url(conn :: Plug.Conn.t()) :: String.t()


M lib/yemma_web/controllers/user_settings_controller.ex => lib/yemma_web/controllers/user_settings_controller.ex +4 -4
@@ 19,11 19,11 @@ defmodule YemmaWeb.UserSettingsController do
          yemma.name,
          applied_user,
          user.email,
          &yemma.routes.user_settings_url(&1)
          &yemma.routes.user_settings_url(conn, &1)
        )

        conn
        |> redirect(external: yemma.routes.user_settings_url())
        |> redirect(external: yemma.routes.user_settings_url(conn))

      {:error, changeset} ->
        render(conn, "edit.html", email_changeset: changeset, yemma: yemma)


@@ 35,12 35,12 @@ defmodule YemmaWeb.UserSettingsController do
      :ok ->
        conn
        |> put_flash(:info, "Email changed successfully.")
        |> redirect(external: yemma.routes.user_settings_url())
        |> redirect(external: yemma.routes.user_settings_url(conn))

      :error ->
        conn
        |> put_flash(:error, "Email change link is invalid or it has expired.")
        |> redirect(external: yemma.routes.user_settings_url())
        |> redirect(external: yemma.routes.user_settings_url(conn))
    end
  end


M lib/yemma_web/templates/user_settings/edit.html.heex => lib/yemma_web/templates/user_settings/edit.html.heex +1 -1
@@ 2,7 2,7 @@

<h3>Change email</h3>

<.form let={f} for={@email_changeset} action={@yemma.routes.user_settings_url()} id="update_email">
<.form let={f} for={@email_changeset} action={@yemma.routes.user_settings_url(@conn)} id="update_email">
  <%= if @email_changeset.action do %>
    <div class="alert alert-danger">
      <p>Oops, something went wrong! Please check the errors below.</p>

M test/support/routes.ex => test/support/routes.ex +2 -2
@@ 31,12 31,12 @@ defmodule YemmaTest.Routes do
  end

  @impl true
  def user_settings_url() do
  def user_settings_url(_conn) do
    "/settings"
  end

  @impl true
  def user_settings_url(token) do
  def user_settings_url(_conn, token) do
    "/settings/confirm_email/#{token}"
  end


M test/yemma_web/controllers/user_settings_controller_test.exs => test/yemma_web/controllers/user_settings_controller_test.exs +12 -12
@@ 8,14 8,14 @@ defmodule YemmaWeb.UserSettingsControllerTest do

  describe "GET /users/settings" do
    test "renders settings page", %{conn: conn, conf: conf} do
      conn = get(conn, conf.routes.user_settings_url())
      conn = get(conn, conf.routes.user_settings_url(conn))
      response = html_response(conn, 200)
      assert response =~ "<h1>Settings</h1>"
    end

    test "redirects if user is not logged in", %{conf: conf} do
      conn = build_conn()
      conn = get(conn, conf.routes.user_settings_url())
      conn = get(conn, conf.routes.user_settings_url(conn))
      assert queryless_redirected_to(conn) == conf.routes.new_session_url(conn)
    end
  end


@@ 24,18 24,18 @@ defmodule YemmaWeb.UserSettingsControllerTest do
    @tag :capture_log
    test "updates the user email", %{conn: conn, user: user, conf: conf} do
      conn =
        put(conn, conf.routes.user_settings_url(), %{
        put(conn, conf.routes.user_settings_url(conn), %{
          "action" => "update_email",
          "user" => %{"email" => unique_user_email()}
        })

      assert redirected_to(conn) == conf.routes.user_settings_url()
      assert redirected_to(conn) == conf.routes.user_settings_url(conn)
      assert Users.get_user_by_email(conf, user.email)
    end

    test "does not update email on invalid data", %{conn: conn, conf: conf} do
      conn =
        put(conn, conf.routes.user_settings_url(), %{
        put(conn, conf.routes.user_settings_url(conn), %{
          "action" => "update_email",
          "user" => %{"email" => "with spaces"}
        })


@@ 65,27 65,27 @@ defmodule YemmaWeb.UserSettingsControllerTest do
      email: email,
      conf: conf
    } do
      conn = get(conn, conf.routes.user_settings_url(token))
      assert redirected_to(conn) == conf.routes.user_settings_url()
      conn = get(conn, conf.routes.user_settings_url(conn, token))
      assert redirected_to(conn) == conf.routes.user_settings_url(conn)
      assert get_flash(conn, :info) =~ "Email changed successfully"
      refute Users.get_user_by_email(conf, user.email)
      assert Users.get_user_by_email(conf, email)

      conn = get(conn, conf.routes.user_settings_url(token))
      assert redirected_to(conn) == conf.routes.user_settings_url()
      conn = get(conn, conf.routes.user_settings_url(conn, token))
      assert redirected_to(conn) == conf.routes.user_settings_url(conn)
      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, user: user, conf: conf} do
      conn = get(conn, conf.routes.user_settings_url("oops"))
      assert redirected_to(conn) == conf.routes.user_settings_url()
      conn = get(conn, conf.routes.user_settings_url(conn, "oops"))
      assert redirected_to(conn) == conf.routes.user_settings_url(conn)
      assert get_flash(conn, :error) =~ "Email change link is invalid or it has expired"
      assert Users.get_user_by_email(conf, user.email)
    end

    test "redirects if user is not logged in", %{token: token, conf: conf} do
      conn = build_conn()
      conn = get(conn, conf.routes.user_settings_url(token))
      conn = get(conn, conf.routes.user_settings_url(conn, token))
      assert queryless_redirected_to(conn) == conf.routes.new_session_url(conn)
    end
  end