improve tests
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-03-22 22:08:37 -04:00
parent 64320dbdae
commit 5a41d8b3e7
24 changed files with 369 additions and 470 deletions

View File

@ -4,7 +4,7 @@ defmodule MemexWeb.ExportControllerTest do
"""
use MemexWeb.ConnCase
import Memex.{ContextsFixtures, NotesFixtures, PipelinesFixtures, StepsFixtures}
import Memex.Fixtures
@moduletag :export_controller_test

View File

@ -4,7 +4,6 @@ defmodule MemexWeb.UserAuthTest do
"""
use MemexWeb.ConnCase, async: true
import MemexWeb.Gettext
alias Memex.Accounts
alias MemexWeb.UserAuth
@ -45,7 +44,6 @@ defmodule MemexWeb.UserAuthTest do
conn |> fetch_cookies() |> UserAuth.log_in_user(current_user, %{"remember_me" => "true"})
assert get_session(conn, :user_token) == conn.cookies[@remember_me_cookie]
assert %{value: signed_token, max_age: max_age} = conn.resp_cookies[@remember_me_cookie]
assert signed_token != get_session(conn, :user_token)
assert max_age == 5_184_000
@ -148,7 +146,7 @@ defmodule MemexWeb.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

View File

@ -4,7 +4,6 @@ defmodule MemexWeb.UserConfirmationControllerTest do
"""
use MemexWeb.ConnCase, async: true
import MemexWeb.Gettext
alias Memex.{Accounts, Repo}
@moduletag :user_confirmation_controller_test
@ -17,7 +16,7 @@ defmodule MemexWeb.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
@ -26,17 +25,13 @@ defmodule MemexWeb.UserConfirmationControllerTest do
test "sends a new confirmation token", %{conn: conn, user: user} do
conn =
post(conn, Routes.user_confirmation_path(conn, :create), %{
"user" => %{"email" => user.email}
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
@ -46,33 +41,25 @@ defmodule MemexWeb.UserConfirmationControllerTest do
conn =
post(conn, Routes.user_confirmation_path(conn, :create), %{
"user" => %{"email" => user.email}
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
@ -88,8 +75,7 @@ defmodule MemexWeb.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)
@ -98,9 +84,7 @@ defmodule MemexWeb.UserConfirmationControllerTest do
# When not logged in
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,9 +99,7 @@ defmodule MemexWeb.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

View File

@ -4,7 +4,6 @@ defmodule MemexWeb.UserRegistrationControllerTest do
"""
use MemexWeb.ConnCase, async: true
import MemexWeb.Gettext
@moduletag :user_registration_controller_test
@ -12,8 +11,8 @@ defmodule MemexWeb.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 MemexWeb.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) =~ "/"
@ -42,11 +41,11 @@ defmodule MemexWeb.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

View File

@ -4,7 +4,6 @@ defmodule MemexWeb.UserResetPasswordControllerTest do
"""
use MemexWeb.ConnCase, async: true
import MemexWeb.Gettext
alias Memex.{Accounts, Repo}
@moduletag :user_reset_password_controller_test
@ -17,7 +16,7 @@ defmodule MemexWeb.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
@ -26,16 +25,13 @@ defmodule MemexWeb.UserResetPasswordControllerTest do
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}
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 +39,13 @@ defmodule MemexWeb.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 +63,13 @@ defmodule MemexWeb.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 +86,37 @@ defmodule MemexWeb.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

View File

@ -4,7 +4,6 @@ defmodule MemexWeb.UserSessionControllerTest do
"""
use MemexWeb.ConnCase, async: true
import MemexWeb.Gettext
@moduletag :user_session_controller_test
@ -16,7 +15,7 @@ defmodule MemexWeb.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 MemexWeb.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 MemexWeb.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 MemexWeb.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 MemexWeb.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 MemexWeb.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

View File

@ -4,7 +4,6 @@ defmodule MemexWeb.UserSettingsControllerTest do
"""
use MemexWeb.ConnCase, async: true
import MemexWeb.Gettext
alias Memex.Accounts
@moduletag :user_settings_controller_test
@ -15,7 +14,7 @@ defmodule MemexWeb.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,19 +29,17 @@ defmodule MemexWeb.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
@ -50,19 +47,19 @@ defmodule MemexWeb.UserSettingsControllerTest do
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
@ -73,18 +70,15 @@ defmodule MemexWeb.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
@ -98,9 +92,9 @@ defmodule MemexWeb.UserSettingsControllerTest do
})
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 +118,19 @@ defmodule MemexWeb.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