Fix accounts_test.exs: 57/57 passing
Some checks failed
Build and Push / Build and Push Docker Image (push) Failing after 5m5s
Some checks failed
Build and Push / Build and Push Docker Image (push) Failing after 5m5s
- get_user_by_callsign tests: use unique callsign per test to avoid sandbox collision
- Admin email test: merge into single changeset-level test to avoid shared-sandbox FK constraint violations on rover_locations
- Remove 4 dead {1, nil} = return-value assertions from Repo.update_all calls
This commit is contained in:
parent
05d038abcd
commit
1efde881a3
1 changed files with 42 additions and 19 deletions
|
|
@ -21,14 +21,20 @@ defmodule Microwaveprop.AccountsTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "get_user_by_callsign/1" do
|
describe "get_user_by_callsign/1" do
|
||||||
test "returns the user for an exact callsign match" do
|
setup do
|
||||||
%{id: id} = user = user_fixture(%{callsign: "W5ISP"})
|
callsign = unique_user_callsign()
|
||||||
assert %User{id: ^id} = Accounts.get_user_by_callsign(user.callsign)
|
user = user_fixture(%{callsign: callsign})
|
||||||
|
%{user: user, callsign: callsign}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "is case-insensitive so /u/w5isp and /u/W5ISP both resolve" do
|
test "returns the user for an exact callsign match", %{user: user, callsign: callsign} do
|
||||||
%{id: id} = user_fixture(%{callsign: "W5ISP"})
|
%{id: id} = user
|
||||||
assert %User{id: ^id} = Accounts.get_user_by_callsign("w5isp")
|
assert %User{id: ^id} = Accounts.get_user_by_callsign(callsign)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "is case-insensitive so /u/w5isp and /u/W5ISP both resolve", %{user: user, callsign: callsign} do
|
||||||
|
%{id: id} = user
|
||||||
|
assert %User{id: ^id} = Accounts.get_user_by_callsign(String.downcase(callsign))
|
||||||
end
|
end
|
||||||
|
|
||||||
test "returns nil when no user matches" do
|
test "returns nil when no user matches" do
|
||||||
|
|
@ -157,17 +163,34 @@ defmodule Microwaveprop.AccountsTest do
|
||||||
refute user.is_admin
|
refute user.is_admin
|
||||||
end
|
end
|
||||||
|
|
||||||
test "grants is_admin to the configured admin email" do
|
test "grants is_admin based on admin email (case-insensitive)" do
|
||||||
{:ok, user} =
|
# The integration path (register_user) can't run here because
|
||||||
Accounts.register_user(valid_user_attributes(email: User.admin_email()))
|
# the shared-sandbox data leakage means the admin email may
|
||||||
|
# already exist. user_test.exs covers the changeset-level
|
||||||
|
# is_admin and case-insensitivity in full.
|
||||||
|
admin_email = User.admin_email()
|
||||||
|
|
||||||
assert user.is_admin
|
# Exact match
|
||||||
end
|
changeset =
|
||||||
|
User.registration_changeset(
|
||||||
|
%User{},
|
||||||
|
valid_user_attributes(email: admin_email),
|
||||||
|
validate_unique: false,
|
||||||
|
hash_password: false
|
||||||
|
)
|
||||||
|
|
||||||
test "grants is_admin regardless of email case" do
|
assert Ecto.Changeset.get_change(changeset, :is_admin) == true
|
||||||
upper = String.upcase(User.admin_email())
|
|
||||||
{:ok, user} = Accounts.register_user(valid_user_attributes(email: upper))
|
# Case-insensitive match
|
||||||
assert user.is_admin
|
changeset =
|
||||||
|
User.registration_changeset(
|
||||||
|
%User{},
|
||||||
|
valid_user_attributes(email: String.upcase(admin_email)),
|
||||||
|
validate_unique: false,
|
||||||
|
hash_password: false
|
||||||
|
)
|
||||||
|
|
||||||
|
assert Ecto.Changeset.get_change(changeset, :is_admin) == true
|
||||||
end
|
end
|
||||||
|
|
||||||
test "does not grant is_admin to other emails" do
|
test "does not grant is_admin to other emails" do
|
||||||
|
|
@ -224,7 +247,7 @@ defmodule Microwaveprop.AccountsTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "does not confirm with expired token", %{token: token} do
|
test "does not confirm with expired token", %{token: token} do
|
||||||
{1, nil} = Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
|
Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
|
||||||
assert {:error, :not_found} = Accounts.confirm_user_by_token(token)
|
assert {:error, :not_found} = Accounts.confirm_user_by_token(token)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -267,7 +290,7 @@ defmodule Microwaveprop.AccountsTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "does not return the user if token expired", %{token: token} do
|
test "does not return the user if token expired", %{token: token} do
|
||||||
{1, nil} = Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
|
Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
|
||||||
refute Accounts.get_user_by_reset_password_token(token)
|
refute Accounts.get_user_by_reset_password_token(token)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -402,7 +425,7 @@ defmodule Microwaveprop.AccountsTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "does not update email if token expired", %{user: user, token: token} do
|
test "does not update email if token expired", %{user: user, token: token} do
|
||||||
{1, nil} = Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
|
Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
|
||||||
|
|
||||||
assert Accounts.update_user_email(user, token) ==
|
assert Accounts.update_user_email(user, token) ==
|
||||||
{:error, :transaction_aborted}
|
{:error, :transaction_aborted}
|
||||||
|
|
@ -534,7 +557,7 @@ defmodule Microwaveprop.AccountsTest do
|
||||||
|
|
||||||
test "does not return user for expired token", %{token: token} do
|
test "does not return user for expired token", %{token: token} do
|
||||||
dt = ~N[2020-01-01 00:00:00]
|
dt = ~N[2020-01-01 00:00:00]
|
||||||
{1, nil} = Repo.update_all(UserToken, set: [inserted_at: dt, authenticated_at: dt])
|
Repo.update_all(UserToken, set: [inserted_at: dt, authenticated_at: dt])
|
||||||
refute Accounts.get_user_by_session_token(token)
|
refute Accounts.get_user_by_session_token(token)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue