initial work on fixing user tests

This commit is contained in:
Lili (Tlapka) 2021-10-11 16:20:57 +02:00
parent 6598455c39
commit 77f3dc4614
4 changed files with 18 additions and 15 deletions

View File

@ -5,32 +5,32 @@ defmodule PokemonCouture.AccountsTest do
import PokemonCouture.AccountsFixtures import PokemonCouture.AccountsFixtures
alias PokemonCouture.Accounts.{User, UserToken} alias PokemonCouture.Accounts.{User, UserToken}
describe "get_user_by_email/1" do describe "get_user_by_username/1" do
test "does not return the user if the email does not exist" do test "does not return the user if the username does not exist" do
refute Accounts.get_user_by_email("unknown@example.com") refute Accounts.get_user_by_username("unknown@example.com")
end end
test "returns the user if the email exists" do test "returns the user if the username exists" do
%{id: id} = user = user_fixture() %{id: id} = user = user_fixture()
assert %User{id: ^id} = Accounts.get_user_by_email(user.email) assert %User{id: ^id} = Accounts.get_user_by_username(user.username)
end end
end end
describe "get_user_by_email_and_password/2" do describe "get_user_by_username_and_password/2" do
test "does not return the user if the email does not exist" do test "does not return the user if the username does not exist" do
refute Accounts.get_user_by_email_and_password("unknown@example.com", "hello world!") refute Accounts.get_user_by_username_and_password("unknown@example.com", "hello world!")
end end
test "does not return the user if the password is not valid" do test "does not return the user if the password is not valid" do
user = user_fixture() user = user_fixture()
refute Accounts.get_user_by_email_and_password(user.email, "invalid") refute Accounts.get_user_by_username_and_password(user.username, "invalid")
end end
test "returns the user if the email and password are valid" do test "returns the user if the username and password are valid" do
%{id: id} = user = user_fixture() %{id: id} = user = user_fixture()
assert %User{id: ^id} = assert %User{id: ^id} =
Accounts.get_user_by_email_and_password(user.email, valid_user_password()) Accounts.get_user_by_username_and_password(user.username, valid_user_password())
end end
end end
@ -52,6 +52,7 @@ defmodule PokemonCouture.AccountsTest do
{:error, changeset} = Accounts.register_user(%{}) {:error, changeset} = Accounts.register_user(%{})
assert %{ assert %{
username: ["can't be blank"],
password: ["can't be blank"], password: ["can't be blank"],
email: ["can't be blank"] email: ["can't be blank"]
} = errors_on(changeset) } = errors_on(changeset)
@ -294,7 +295,7 @@ defmodule PokemonCouture.AccountsTest do
}) })
assert is_nil(user.password) assert is_nil(user.password)
assert Accounts.get_user_by_email_and_password(user.email, "new valid password") assert Accounts.get_user_by_username_and_password(user.username, "new valid password")
end end
test "deletes all tokens for the given user", %{user: user} do test "deletes all tokens for the given user", %{user: user} do
@ -489,7 +490,7 @@ defmodule PokemonCouture.AccountsTest do
test "updates the password", %{user: user} do test "updates the password", %{user: user} do
{:ok, updated_user} = Accounts.reset_user_password(user, %{password: "new valid password"}) {:ok, updated_user} = Accounts.reset_user_password(user, %{password: "new valid password"})
assert is_nil(updated_user.password) assert is_nil(updated_user.password)
assert Accounts.get_user_by_email_and_password(user.email, "new valid password") assert Accounts.get_user_by_username_and_password(user.username, "new valid password")
end end
test "deletes all tokens for the given user", %{user: user} do test "deletes all tokens for the given user", %{user: user} do

View File

@ -86,7 +86,7 @@ defmodule PokemonCoutureWeb.UserResetPasswordControllerTest do
assert redirected_to(conn) == Routes.user_session_path(conn, :new) assert redirected_to(conn) == Routes.user_session_path(conn, :new)
refute get_session(conn, :user_token) refute get_session(conn, :user_token)
assert get_flash(conn, :info) =~ "Password reset successfully" assert get_flash(conn, :info) =~ "Password reset successfully"
assert Accounts.get_user_by_email_and_password(user.email, "new valid password") assert Accounts.get_user_by_username_and_password(user.username, "new valid password")
end end
test "does not reset password on invalid data", %{conn: conn, token: token} do test "does not reset password on invalid data", %{conn: conn, token: token} do

View File

@ -35,7 +35,7 @@ defmodule PokemonCoutureWeb.UserSettingsControllerTest do
assert redirected_to(new_password_conn) == Routes.user_settings_path(conn, :edit) 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_session(new_password_conn, :user_token) != get_session(conn, :user_token)
assert get_flash(new_password_conn, :info) =~ "Password updated successfully" assert get_flash(new_password_conn, :info) =~ "Password updated successfully"
assert Accounts.get_user_by_email_and_password(user.email, "new valid password") assert Accounts.get_user_by_username_and_password(user.username, "new valid password")
end end
test "does not update password on invalid data", %{conn: conn} do test "does not update password on invalid data", %{conn: conn} do

View File

@ -6,9 +6,11 @@ defmodule PokemonCouture.AccountsFixtures do
def unique_user_email, do: "user#{System.unique_integer()}@example.com" def unique_user_email, do: "user#{System.unique_integer()}@example.com"
def valid_user_password, do: "hello world!" def valid_user_password, do: "hello world!"
def unique_username, do: "user#{System.unique_integer()}"
def valid_user_attributes(attrs \\ %{}) do def valid_user_attributes(attrs \\ %{}) do
Enum.into(attrs, %{ Enum.into(attrs, %{
username: unique_username(),
email: unique_user_email(), email: unique_user_email(),
password: valid_user_password() password: valid_user_password()
}) })