gettext user session controller

This commit is contained in:
shibao 2022-02-08 23:58:54 -05:00
parent 7e61c5c59a
commit 9972299fc4
9 changed files with 27 additions and 21 deletions

View File

@ -14,13 +14,13 @@ defmodule CanneryWeb.UserSessionController do
if user = Accounts.get_user_by_email_and_password(email, password) do if user = Accounts.get_user_by_email_and_password(email, password) do
UserAuth.log_in_user(conn, user, user_params) UserAuth.log_in_user(conn, user, user_params)
else else
render(conn, "new.html", error_message: "Invalid email or password") render(conn, "new.html", error_message: dgettext("errors", "Invalid email or password"))
end end
end end
def delete(conn, _params) do def delete(conn, _params) do
conn conn
|> put_flash(:info, "Logged out successfully.") |> put_flash(:info, gettext("Logged out successfully."))
|> UserAuth.log_out_user() |> UserAuth.log_out_user()
end end
end end

View File

@ -109,3 +109,8 @@ msgstr ""
#: lib/cannery_web/templates/user_settings/edit.html.heex:3 #: lib/cannery_web/templates/user_settings/edit.html.heex:3
msgid "Settings" msgid "Settings"
msgstr "" msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/controllers/user_session_controller.ex:23
msgid "Logged out successfully."
msgstr ""

View File

@ -152,3 +152,8 @@ msgstr[1] ""
#: lib/cannery_web/templates/user_settings/edit.html.heex:58 #: lib/cannery_web/templates/user_settings/edit.html.heex:58
msgid "Oops, something went wrong! Please check the errors below." msgid "Oops, something went wrong! Please check the errors below."
msgstr "" msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/controllers/user_session_controller.ex:17
msgid "Invalid email or password"
msgstr ""

View File

@ -1,8 +1,10 @@
defmodule CanneryWeb.UserSessionControllerTest do defmodule CanneryWeb.UserSessionControllerTest do
use CanneryWeb.ConnCase, async: true use CanneryWeb.ConnCase, async: true
import CanneryWeb.Gettext
import Cannery.AccountsFixtures import Cannery.AccountsFixtures
@moduletag :user_session_controller
setup do setup do
%{user: user_fixture()} %{user: user_fixture()}
end end
@ -11,9 +13,8 @@ defmodule CanneryWeb.UserSessionControllerTest do
test "renders log in page", %{conn: conn} do test "renders log in page", %{conn: conn} do
conn = get(conn, Routes.user_session_path(conn, :new)) conn = get(conn, Routes.user_session_path(conn, :new))
response = html_response(conn, 200) response = html_response(conn, 200)
assert response =~ "<h1>Log in</h1>" assert response =~ dgettext("actions", "Log in")
assert response =~ "Log in</a>" assert response =~ dgettext("actions", "Register")
assert response =~ "Register</a>"
end end
test "redirects if already logged in", %{conn: conn, user: user} do test "redirects if already logged in", %{conn: conn, user: user} do
@ -36,8 +37,7 @@ defmodule CanneryWeb.UserSessionControllerTest do
conn = get(conn, "/") conn = get(conn, "/")
response = html_response(conn, 200) response = html_response(conn, 200)
assert response =~ user.email assert response =~ user.email
assert response =~ "Settings</a>" assert response =~ dgettext("prompts", "Are you sure you want to log out?")
assert response =~ "Log out</a>"
end end
test "logs the user in with remember me", %{conn: conn, user: user} do test "logs the user in with remember me", %{conn: conn, user: user} do
@ -75,8 +75,8 @@ defmodule CanneryWeb.UserSessionControllerTest do
}) })
response = html_response(conn, 200) response = html_response(conn, 200)
assert response =~ "<h1>Log in</h1>" assert response =~ dgettext("actions", "Log in")
assert response =~ "Invalid email or password" assert response =~ dgettext("errors", "Invalid email or password")
end end
end end
@ -85,14 +85,14 @@ defmodule CanneryWeb.UserSessionControllerTest do
conn = conn |> log_in_user(user) |> delete(Routes.user_session_path(conn, :delete)) conn = conn |> log_in_user(user) |> delete(Routes.user_session_path(conn, :delete))
assert redirected_to(conn) == "/" assert redirected_to(conn) == "/"
refute get_session(conn, :user_token) refute get_session(conn, :user_token)
assert get_flash(conn, :info) =~ "Logged out successfully" assert get_flash(conn, :info) =~ gettext("Logged out successfully")
end end
test "succeeds even if the user is not logged in", %{conn: conn} do test "succeeds even if the user is not logged in", %{conn: conn} do
conn = delete(conn, Routes.user_session_path(conn, :delete)) conn = delete(conn, Routes.user_session_path(conn, :delete))
assert redirected_to(conn) == "/" assert redirected_to(conn) == "/"
refute get_session(conn, :user_token) refute get_session(conn, :user_token)
assert get_flash(conn, :info) =~ "Logged out successfully" assert get_flash(conn, :info) =~ gettext("Logged out successfully")
end end
end end
end end

View File

@ -1,8 +1,7 @@
defmodule CanneryWeb.AmmoGroupLiveTest do defmodule CanneryWeb.AmmoGroupLiveTest do
use CanneryWeb.ConnCase use CanneryWeb.ConnCase
import Phoenix.LiveViewTest import Phoenix.LiveViewTest
import CanneryWeb.Gettext
alias Cannery.Ammo alias Cannery.Ammo
@create_attrs %{count: 42, notes: "some notes", price_paid: 120.5} @create_attrs %{count: 42, notes: "some notes", price_paid: 120.5}

View File

@ -1,8 +1,7 @@
defmodule CanneryWeb.AmmoTypeLiveTest do defmodule CanneryWeb.AmmoTypeLiveTest do
use CanneryWeb.ConnCase use CanneryWeb.ConnCase
import Phoenix.LiveViewTest import Phoenix.LiveViewTest
import CanneryWeb.Gettext
alias Cannery.Ammo alias Cannery.Ammo
@create_attrs %{ @create_attrs %{

View File

@ -1,7 +1,7 @@
defmodule CanneryWeb.HomeLiveTest do defmodule CanneryWeb.HomeLiveTest do
use CanneryWeb.ConnCase use CanneryWeb.ConnCase
import Phoenix.LiveViewTest import Phoenix.LiveViewTest
import CanneryWeb.Gettext
test "disconnected and connected render", %{conn: conn} do test "disconnected and connected render", %{conn: conn} do
{:ok, home_live, disconnected_html} = live(conn, "/") {:ok, home_live, disconnected_html} = live(conn, "/")

View File

@ -1,8 +1,7 @@
defmodule CanneryWeb.InviteLiveTest do defmodule CanneryWeb.InviteLiveTest do
use CanneryWeb.ConnCase use CanneryWeb.ConnCase
import Phoenix.LiveViewTest import Phoenix.LiveViewTest
import CanneryWeb.Gettext
alias Cannery.Invites alias Cannery.Invites
@create_attrs %{name: "some name", token: "some token"} @create_attrs %{name: "some name", token: "some token"}

View File

@ -1,8 +1,7 @@
defmodule CanneryWeb.TagLiveTest do defmodule CanneryWeb.TagLiveTest do
use CanneryWeb.ConnCase use CanneryWeb.ConnCase
import Phoenix.LiveViewTest import Phoenix.LiveViewTest
import CanneryWeb.Gettext
alias Cannery.Tags alias Cannery.Tags
@create_attrs %{ @create_attrs %{