diff --git a/test/accounts/password_email_identification_test.exs b/test/accounts/password_email_identification_test.exs new file mode 100644 index 0000000..db93187 --- /dev/null +++ b/test/accounts/password_email_identification_test.exs @@ -0,0 +1,80 @@ +defmodule Mv.Accounts.PasswordEmailIdentificationTest do + use Mv.DataCase, async: true + + describe "Password Email Identification" do + test "user can sign in with email and password" do + # Get the password strategy + strategy = AshAuthentication.Info.strategy!(Mv.Accounts.User, :password) + + # Create a user with password + {:ok, user} = + AshAuthentication.Strategy.action(strategy, :register, %{ + "email" => "test@example.com", + "password" => "password123" + }) + + # Sign in with email + {:ok, signed_in_user} = + AshAuthentication.Strategy.action(strategy, :sign_in, %{ + "email" => "test@example.com", + "password" => "password123" + }) + + assert signed_in_user.id == user.id + assert to_string(signed_in_user.email) == "test@example.com" + end + + test "sign in fails with wrong password" do + # Get the password strategy + strategy = AshAuthentication.Info.strategy!(Mv.Accounts.User, :password) + + # Create a user with password + {:ok, _user} = + AshAuthentication.Strategy.action(strategy, :register, %{ + "email" => "test2@example.com", + "password" => "password123" + }) + + # Try to sign in with wrong password + {:error, _} = + AshAuthentication.Strategy.action(strategy, :sign_in, %{ + "email" => "test2@example.com", + "password" => "wrongpassword" + }) + end + + test "sign in fails with non-existent email" do + # Get the password strategy + strategy = AshAuthentication.Info.strategy!(Mv.Accounts.User, :password) + + # Try to sign in with non-existent email + {:error, _} = + AshAuthentication.Strategy.action(strategy, :sign_in, %{ + "email" => "nonexistent@example.com", + "password" => "password123" + }) + end + + test "user gets member automatically created during registration" do + # Get the password strategy + strategy = AshAuthentication.Info.strategy!(Mv.Accounts.User, :password) + + # Register a user + {:ok, user} = + AshAuthentication.Strategy.action(strategy, :register, %{ + "email" => "member@example.com", + "password" => "password123" + }) + + # Reload user to get member_id + user = Ash.reload!(user, domain: Mv.Accounts) + + # User should have a member + assert user.member_id + + # Member should have the same email + member = Ash.get!(Mv.Membership.Member, user.member_id, domain: Mv.Membership) + assert member.member_email == "member@example.com" + end + end +end