improve tests
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
shibao 2023-04-15 21:40:57 -04:00
parent 04ebe59afe
commit c1a0b4017f
9 changed files with 44 additions and 42 deletions

View File

@ -1,3 +1,6 @@
# v0.1.12
- ee cummings even more
- Improve tests
# v0.1.11 # v0.1.11
- Update dependencies - Update dependencies

View File

@ -4,7 +4,6 @@ defmodule MemexWeb.CoreComponents do
""" """
use Phoenix.Component use Phoenix.Component
use MemexWeb, :verified_routes use MemexWeb, :verified_routes
import MemexWeb.{Gettext, HTMLHelpers} import MemexWeb.{Gettext, HTMLHelpers}
alias Memex.{Accounts, Accounts.Invite, Accounts.User} alias Memex.{Accounts, Accounts.Invite, Accounts.User}
alias Memex.Contexts.Context alias Memex.Contexts.Context

View File

@ -4,7 +4,7 @@ defmodule Memex.MixProject do
def project do def project do
[ [
app: :memex, app: :memex,
version: "0.1.11", version: "0.1.12",
elixir: "1.14.4", elixir: "1.14.4",
elixirc_paths: elixirc_paths(Mix.env()), elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod, start_permanent: Mix.env() == :prod,

View File

@ -26,8 +26,8 @@ defmodule MemexWeb.UserConfirmationControllerTest do
conn = post(conn, ~p"/users/confirm", %{user: %{email: user.email}}) conn = post(conn, ~p"/users/confirm", %{user: %{email: user.email}})
assert redirected_to(conn) == ~p"/" assert redirected_to(conn) == ~p"/"
conn.assigns.flash["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." "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" assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context == "confirm"
end end
@ -38,16 +38,16 @@ defmodule MemexWeb.UserConfirmationControllerTest do
conn = post(conn, ~p"/users/confirm", %{user: %{email: user.email}}) conn = post(conn, ~p"/users/confirm", %{user: %{email: user.email}})
assert redirected_to(conn) == ~p"/" assert redirected_to(conn) == ~p"/"
conn.assigns.flash["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." "if your email is in our system and it has not been confirmed yet, you will receive an email with instructions shortly."
end end
test "does not send confirmation token if email is invalid", %{conn: conn} do test "does not send confirmation token if email is invalid", %{conn: conn} do
conn = post(conn, ~p"/users/confirm", %{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) == ~p"/"
conn.assigns.flash["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." "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) == [] assert Repo.all(Accounts.UserToken) == []
end end
@ -62,9 +62,7 @@ defmodule MemexWeb.UserConfirmationControllerTest do
conn = get(conn, ~p"/users/confirm/#{token}") conn = get(conn, ~p"/users/confirm/#{token}")
assert redirected_to(conn) == ~p"/" assert redirected_to(conn) == ~p"/"
assert conn.assigns.flash["info"] =~ "#{user.email} confirmed successfully"
conn.assigns.flash["info"] =~ "#{user.email} confirmed successfully"
assert Accounts.get_user!(user.id).confirmed_at assert Accounts.get_user!(user.id).confirmed_at
refute get_session(conn, :user_token) refute get_session(conn, :user_token)
assert Repo.all(Accounts.UserToken) == [] assert Repo.all(Accounts.UserToken) == []
@ -72,7 +70,7 @@ defmodule MemexWeb.UserConfirmationControllerTest do
# When not logged in # When not logged in
conn = get(conn, ~p"/users/confirm/#{token}") conn = get(conn, ~p"/users/confirm/#{token}")
assert redirected_to(conn) == ~p"/" assert redirected_to(conn) == ~p"/"
conn.assigns.flash["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 # When logged in
conn = conn =
@ -85,10 +83,9 @@ defmodule MemexWeb.UserConfirmationControllerTest do
end end
test "does not confirm email with invalid token", %{conn: conn, user: user} do test "does not confirm email with invalid token", %{conn: conn, user: user} do
conn = get(conn, ~p"/users/confirm/#{"oops"}") conn = get(conn, ~p"/users/confirm/oops")
assert redirected_to(conn) == ~p"/" assert redirected_to(conn) == ~p"/"
conn.assigns.flash["error"] =~ "User confirmation link is invalid or it has expired" assert conn.assigns.flash["error"] =~ "user confirmation link is invalid or it has expired"
refute Accounts.get_user!(user.id).confirmed_at refute Accounts.get_user!(user.id).confirmed_at
end end
end end

View File

@ -32,6 +32,12 @@ defmodule MemexWeb.UserRegistrationControllerTest do
} }
assert redirected_to(conn) =~ ~p"/" assert redirected_to(conn) =~ ~p"/"
# Now do a logged in request and assert on the menu
conn = get(conn, ~p"/")
response = html_response(conn, 200)
# user's email is recorded as admin
assert response =~ email
end end
test "render errors for invalid data", %{conn: conn} do test "render errors for invalid data", %{conn: conn} do

View File

@ -26,8 +26,8 @@ defmodule MemexWeb.UserResetPasswordControllerTest do
conn = post(conn, ~p"/users/reset_password", %{user: %{email: user.email}}) conn = post(conn, ~p"/users/reset_password", %{user: %{email: user.email}})
assert redirected_to(conn) == ~p"/" assert redirected_to(conn) == ~p"/"
conn.assigns.flash["info"] =~ assert conn.assigns.flash["info"] =~
"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" assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context == "reset_password"
end end
@ -36,8 +36,8 @@ defmodule MemexWeb.UserResetPasswordControllerTest do
conn = post(conn, ~p"/users/reset_password", %{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) == ~p"/"
conn.assigns.flash["info"] =~ assert conn.assigns.flash["info"] =~
"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) == [] assert Repo.all(Accounts.UserToken) == []
end end
@ -55,13 +55,13 @@ defmodule MemexWeb.UserResetPasswordControllerTest do
test "renders reset password", %{conn: conn, token: token} do test "renders reset password", %{conn: conn, token: token} do
conn = get(conn, ~p"/users/reset_password/#{token}") conn = get(conn, ~p"/users/reset_password/#{token}")
assert html_response(conn, 200) =~ "Reset password" assert html_response(conn, 200) =~ "reset password"
end end
test "does not render reset password with invalid token", %{conn: conn} do test "does not render reset password with invalid token", %{conn: conn} do
conn = get(conn, ~p"/users/reset_password/#{"oops"}") conn = get(conn, ~p"/users/reset_password/oops")
assert redirected_to(conn) == ~p"/" assert redirected_to(conn) == ~p"/"
conn.assigns.flash["error"] =~ "Reset password link is invalid or it has expired" assert conn.assigns.flash["error"] =~ "reset password link is invalid or it has expired"
end end
end end
@ -86,7 +86,7 @@ defmodule MemexWeb.UserResetPasswordControllerTest do
assert redirected_to(conn) == ~p"/users/log_in" assert redirected_to(conn) == ~p"/users/log_in"
refute get_session(conn, :user_token) refute get_session(conn, :user_token)
conn.assigns.flash["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") assert Accounts.get_user_by_email_and_password(user.email, "new valid password")
end end
@ -100,15 +100,15 @@ defmodule MemexWeb.UserResetPasswordControllerTest do
}) })
response = html_response(conn, 200) response = html_response(conn, 200)
assert response =~ "Reset password" assert response =~ "reset password"
assert response =~ "should be at least 12 character(s)" assert response =~ "should be at least 12 character(s)"
assert response =~ "does not match password" assert response =~ "does not match password"
end end
test "does not reset password with invalid token", %{conn: conn} do test "does not reset password with invalid token", %{conn: conn} do
conn = put(conn, ~p"/users/reset_password/#{"oops"}") conn = put(conn, ~p"/users/reset_password/oops")
assert redirected_to(conn) == ~p"/" assert redirected_to(conn) == ~p"/"
conn.assigns.flash["error"] =~ "Reset password link is invalid or it has expired" assert conn.assigns.flash["error"] =~ "reset password link is invalid or it has expired"
end end
end end
end end

View File

@ -83,14 +83,14 @@ defmodule MemexWeb.UserSessionControllerTest do
conn = conn |> log_in_user(current_user) |> delete(~p"/users/log_out") conn = conn |> log_in_user(current_user) |> delete(~p"/users/log_out")
assert redirected_to(conn) == ~p"/" assert redirected_to(conn) == ~p"/"
refute get_session(conn, :user_token) refute get_session(conn, :user_token)
conn.assigns.flash["info"] =~ "logged out successfully" assert conn.assigns.flash["info"] =~ "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, ~p"/users/log_out") conn = delete(conn, ~p"/users/log_out")
assert redirected_to(conn) == ~p"/" assert redirected_to(conn) == ~p"/"
refute get_session(conn, :user_token) refute get_session(conn, :user_token)
conn.assigns.flash["info"] =~ "logged out successfully" assert conn.assigns.flash["info"] =~ "logged out successfully"
end end
end end
end end

View File

@ -39,8 +39,7 @@ defmodule MemexWeb.UserSettingsControllerTest do
assert redirected_to(new_password_conn) == ~p"/users/settings" assert redirected_to(new_password_conn) == ~p"/users/settings"
assert get_session(new_password_conn, :user_token) != get_session(conn, :user_token) assert get_session(new_password_conn, :user_token) != get_session(conn, :user_token)
new_password_conn.assigns.flash["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") assert Accounts.get_user_by_email_and_password(current_user.email, "new valid password")
end end
@ -60,7 +59,6 @@ defmodule MemexWeb.UserSettingsControllerTest do
assert response =~ "should be at least 12 character(s)" assert response =~ "should be at least 12 character(s)"
assert response =~ "does not match password" assert response =~ "does not match password"
assert response =~ "is not valid" assert response =~ "is not valid"
assert get_session(old_password_conn, :user_token) == get_session(conn, :user_token) assert get_session(old_password_conn, :user_token) == get_session(conn, :user_token)
end end
end end
@ -72,13 +70,13 @@ defmodule MemexWeb.UserSettingsControllerTest do
put(conn, ~p"/users/settings", %{ put(conn, ~p"/users/settings", %{
action: "update_email", action: "update_email",
current_password: valid_user_password(), current_password: valid_user_password(),
user: %{"email" => unique_user_email()} user: %{email: unique_user_email()}
}) })
assert redirected_to(conn) == ~p"/users/settings" assert redirected_to(conn) == ~p"/users/settings"
conn.assigns.flash["info"] =~ assert conn.assigns.flash["info"] =~
"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) assert Accounts.get_user_by_email(current_user.email)
end end
@ -86,9 +84,9 @@ defmodule MemexWeb.UserSettingsControllerTest do
test "does not update email on invalid data", %{conn: conn} do test "does not update email on invalid data", %{conn: conn} do
conn = conn =
put(conn, ~p"/users/settings", %{ put(conn, ~p"/users/settings", %{
"action" => "update_email", action: "update_email",
"current_password" => "invalid", current_password: "invalid",
"user" => %{"email" => "with spaces"} user: %{email: "with spaces"}
}) })
response = html_response(conn, 200) response = html_response(conn, 200)
@ -118,19 +116,19 @@ defmodule MemexWeb.UserSettingsControllerTest do
%{conn: conn, current_user: current_user, token: token, email: email} do %{conn: conn, current_user: current_user, token: token, email: email} do
conn = get(conn, ~p"/users/settings/confirm_email/#{token}") conn = get(conn, ~p"/users/settings/confirm_email/#{token}")
assert redirected_to(conn) == ~p"/users/settings" assert redirected_to(conn) == ~p"/users/settings"
conn.assigns.flash["info"] =~ "email changed successfully" assert conn.assigns.flash["info"] =~ "email changed successfully"
refute Accounts.get_user_by_email(current_user.email) refute Accounts.get_user_by_email(current_user.email)
assert Accounts.get_user_by_email(email) assert Accounts.get_user_by_email(email)
conn = get(conn, ~p"/users/settings/confirm_email/#{token}") conn = get(conn, ~p"/users/settings/confirm_email/#{token}")
assert redirected_to(conn) == ~p"/users/settings" assert redirected_to(conn) == ~p"/users/settings"
conn.assigns.flash["error"] =~ "email change link is invalid or it has expired" assert conn.assigns.flash["error"] =~ "email change link is invalid or it has expired"
end end
test "does not update email with invalid token", %{conn: conn, current_user: current_user} do test "does not update email with invalid token", %{conn: conn, current_user: current_user} do
conn = get(conn, ~p"/users/settings/confirm_email/#{"oops"}") conn = get(conn, ~p"/users/settings/confirm_email/oops")
assert redirected_to(conn) == ~p"/users/settings" assert redirected_to(conn) == ~p"/users/settings"
conn.assigns.flash["error"] =~ "email change link is invalid or it has expired" assert conn.assigns.flash["error"] =~ "email change link is invalid or it has expired"
assert Accounts.get_user_by_email(current_user.email) assert Accounts.get_user_by_email(current_user.email)
end end

View File

@ -22,7 +22,6 @@ defmodule MemexWeb.InviteLiveTest do
test "lists all invites", %{conn: conn, invite: invite} do test "lists all invites", %{conn: conn, invite: invite} do
{:ok, _index_live, html} = live(conn, ~p"/invites") {:ok, _index_live, html} = live(conn, ~p"/invites")
assert html =~ "invites" assert html =~ "invites"
assert html =~ invite.name assert html =~ invite.name
end end