413 lines
15 KiB
Elixir
413 lines
15 KiB
Elixir
defmodule MvWeb.UserLive.IndexTest do
|
|
use MvWeb.ConnCase, async: true
|
|
import Phoenix.LiveViewTest
|
|
|
|
describe "basic functionality" do
|
|
test "shows translated title in German", %{conn: conn} do
|
|
conn = conn_with_oidc_user(conn)
|
|
conn = Plug.Test.init_test_session(conn, locale: "de")
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
assert html =~ "Benutzer auflisten"
|
|
end
|
|
|
|
test "shows translated title in English", %{conn: conn} do
|
|
conn = conn_with_oidc_user(conn)
|
|
Gettext.put_locale(MvWeb.Gettext, "en")
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
assert html =~ "Listing Users"
|
|
end
|
|
|
|
test "shows New User button", %{conn: conn} do
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
assert html =~ "New User"
|
|
end
|
|
|
|
test "displays users in a table", %{conn: conn} do
|
|
# Create test users
|
|
_user1 = create_test_user(%{email: "alice@example.com", oidc_id: "alice123"})
|
|
_user2 = create_test_user(%{email: "bob@example.com", oidc_id: "bob456"})
|
|
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
|
|
assert html =~ "alice@example.com"
|
|
assert html =~ "bob@example.com"
|
|
assert html =~ "alice123"
|
|
assert html =~ "bob456"
|
|
end
|
|
|
|
test "shows correct action links", %{conn: conn} do
|
|
user = create_test_user(%{email: "test@example.com"})
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
|
|
assert html =~ "Edit"
|
|
assert html =~ "Delete"
|
|
assert html =~ ~r/href="[^"]*\/users\/#{user.id}\/edit"/
|
|
end
|
|
end
|
|
|
|
describe "sorting functionality" do
|
|
setup do
|
|
# Create users with different emails for sorting tests
|
|
user_a = create_test_user(%{email: "alpha@example.com", oidc_id: "alpha"})
|
|
user_z = create_test_user(%{email: "zulu@example.com", oidc_id: "zulu"})
|
|
user_m = create_test_user(%{email: "mike@example.com", oidc_id: "mike"})
|
|
|
|
%{users: [user_a, user_z, user_m]}
|
|
end
|
|
|
|
test "initially sorts by email ascending", %{conn: conn} do
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
|
|
# Should show ascending indicator (up arrow)
|
|
assert html =~ "hero-chevron-up"
|
|
assert html =~ ~s(aria-sort="ascending")
|
|
|
|
# Test actual sort order: alpha should appear before mike, mike before zulu
|
|
alpha_pos = html |> :binary.match("alpha@example.com") |> elem(0)
|
|
mike_pos = html |> :binary.match("mike@example.com") |> elem(0)
|
|
zulu_pos = html |> :binary.match("zulu@example.com") |> elem(0)
|
|
|
|
assert alpha_pos < mike_pos, "alpha@example.com should appear before mike@example.com"
|
|
assert mike_pos < zulu_pos, "mike@example.com should appear before zulu@example.com"
|
|
end
|
|
|
|
test "can sort email descending by clicking sort button", %{conn: conn} do
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, view, _html} = live(conn, "/users")
|
|
|
|
# Click on email sort button and get rendered result
|
|
html = view |> element("button[phx-value-field='email']") |> render_click()
|
|
|
|
# Should now show descending indicator (down arrow)
|
|
assert html =~ "hero-chevron-down"
|
|
assert html =~ ~s(aria-sort="descending")
|
|
|
|
# Test actual sort order reversed: zulu should now appear before mike, mike before alpha
|
|
alpha_pos = html |> :binary.match("alpha@example.com") |> elem(0)
|
|
mike_pos = html |> :binary.match("mike@example.com") |> elem(0)
|
|
zulu_pos = html |> :binary.match("zulu@example.com") |> elem(0)
|
|
|
|
assert zulu_pos < mike_pos,
|
|
"zulu@example.com should appear before mike@example.com when sorted desc"
|
|
|
|
assert mike_pos < alpha_pos,
|
|
"mike@example.com should appear before alpha@example.com when sorted desc"
|
|
end
|
|
|
|
test "toggles back to ascending when clicking sort button twice", %{conn: conn} do
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, view, _html} = live(conn, "/users")
|
|
|
|
# Click twice to toggle: asc -> desc -> asc
|
|
view |> element("button[phx-value-field='email']") |> render_click()
|
|
html = view |> element("button[phx-value-field='email']") |> render_click()
|
|
|
|
# Should be back to ascending
|
|
assert html =~ "hero-chevron-up"
|
|
assert html =~ ~s(aria-sort="ascending")
|
|
|
|
# Should be back to original ascending order
|
|
alpha_pos = html |> :binary.match("alpha@example.com") |> elem(0)
|
|
mike_pos = html |> :binary.match("mike@example.com") |> elem(0)
|
|
zulu_pos = html |> :binary.match("zulu@example.com") |> elem(0)
|
|
|
|
assert alpha_pos < mike_pos, "Should be back to ascending: alpha before mike"
|
|
assert mike_pos < zulu_pos, "Should be back to ascending: mike before zulu"
|
|
end
|
|
|
|
test "shows sort direction icons", %{conn: conn} do
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, view, _html} = live(conn, "/users")
|
|
|
|
# Initially ascending - should show up arrow
|
|
html = render(view)
|
|
assert html =~ "hero-chevron-up"
|
|
|
|
# After clicking, should show down arrow
|
|
view |> element("button[phx-value-field='email']") |> render_click()
|
|
html = render(view)
|
|
assert html =~ "hero-chevron-down"
|
|
end
|
|
end
|
|
|
|
describe "checkbox selection functionality" do
|
|
setup do
|
|
user1 = create_test_user(%{email: "user1@example.com", oidc_id: "user1"})
|
|
user2 = create_test_user(%{email: "user2@example.com", oidc_id: "user2"})
|
|
%{users: [user1, user2]}
|
|
end
|
|
|
|
test "shows select all checkbox", %{conn: conn} do
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
|
|
assert html =~ ~s(name="select_all")
|
|
assert html =~ ~s(phx-click="select_all")
|
|
end
|
|
|
|
test "shows individual user checkboxes", %{conn: conn, users: [user1, user2]} do
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
|
|
assert html =~ ~s(name="#{user1.id}")
|
|
assert html =~ ~s(name="#{user2.id}")
|
|
assert html =~ ~s(phx-click="select_user")
|
|
end
|
|
|
|
test "can select individual users", %{conn: conn, users: [user1, user2]} do
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, view, _html} = live(conn, "/users")
|
|
|
|
# Initially, individual checkboxes should exist but not be checked
|
|
assert view |> element("input[type='checkbox'][name='#{user1.id}']") |> has_element?()
|
|
assert view |> element("input[type='checkbox'][name='#{user2.id}']") |> has_element?()
|
|
|
|
# Initially, select_all should not be checked (since no individual items are selected)
|
|
refute view
|
|
|> element("input[type='checkbox'][name='select_all'][checked]")
|
|
|> has_element?()
|
|
|
|
# Select first user checkbox
|
|
html = view |> element("input[type='checkbox'][name='#{user1.id}']") |> render_click()
|
|
|
|
# The select_all checkbox should still not be checked (not all users selected)
|
|
refute view
|
|
|> element("input[type='checkbox'][name='select_all'][checked]")
|
|
|> has_element?()
|
|
|
|
# Page should still function normally
|
|
assert html =~ "Email"
|
|
assert html =~ to_string(user1.email)
|
|
end
|
|
|
|
test "can deselect individual users", %{conn: conn, users: [user1, _user2]} do
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, view, _html} = live(conn, "/users")
|
|
|
|
# Select user first
|
|
view |> element("input[type='checkbox'][name='#{user1.id}']") |> render_click()
|
|
|
|
# Then deselect user
|
|
html = view |> element("input[type='checkbox'][name='#{user1.id}']") |> render_click()
|
|
|
|
# Select all should not be checked after deselecting individual user
|
|
refute view
|
|
|> element("input[type='checkbox'][name='select_all'][checked]")
|
|
|> has_element?()
|
|
|
|
# Page should still function normally
|
|
assert html =~ "Email"
|
|
assert html =~ to_string(user1.email)
|
|
end
|
|
|
|
test "select all functionality selects all users", %{conn: conn, users: [user1, user2]} do
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, view, _html} = live(conn, "/users")
|
|
|
|
# Initially no checkboxes should be checked
|
|
refute view
|
|
|> element("input[type='checkbox'][name='select_all'][checked]")
|
|
|> has_element?()
|
|
|
|
refute view
|
|
|> element("input[type='checkbox'][name='#{user1.id}'][checked]")
|
|
|> has_element?()
|
|
|
|
refute view
|
|
|> element("input[type='checkbox'][name='#{user2.id}'][checked]")
|
|
|> has_element?()
|
|
|
|
# Click select all
|
|
html = view |> element("input[type='checkbox'][name='select_all']") |> render_click()
|
|
|
|
# After selecting all, the select_all checkbox should be checked
|
|
assert view
|
|
|> element("input[type='checkbox'][name='select_all'][checked]")
|
|
|> has_element?()
|
|
|
|
# Page should still function normally and show all users
|
|
assert html =~ "Email"
|
|
assert html =~ to_string(user1.email)
|
|
assert html =~ to_string(user2.email)
|
|
end
|
|
|
|
test "deselect all functionality deselects all users", %{conn: conn, users: [user1, user2]} do
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, view, _html} = live(conn, "/users")
|
|
|
|
# Select all first
|
|
view |> element("input[type='checkbox'][name='select_all']") |> render_click()
|
|
|
|
# Verify that select_all is checked
|
|
assert view
|
|
|> element("input[type='checkbox'][name='select_all'][checked]")
|
|
|> has_element?()
|
|
|
|
# Then deselect all
|
|
html = view |> element("input[type='checkbox'][name='select_all']") |> render_click()
|
|
|
|
# After deselecting all, no checkboxes should be checked
|
|
refute view
|
|
|> element("input[type='checkbox'][name='select_all'][checked]")
|
|
|> has_element?()
|
|
|
|
refute view
|
|
|> element("input[type='checkbox'][name='#{user1.id}'][checked]")
|
|
|> has_element?()
|
|
|
|
refute view
|
|
|> element("input[type='checkbox'][name='#{user2.id}'][checked]")
|
|
|> has_element?()
|
|
|
|
# Page should still function normally
|
|
assert html =~ "Email"
|
|
assert html =~ to_string(user1.email)
|
|
assert html =~ to_string(user2.email)
|
|
end
|
|
|
|
test "select all automatically checks when all individual users are selected", %{
|
|
conn: conn,
|
|
users: [user1, user2]
|
|
} do
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, view, _html} = live(conn, "/users")
|
|
|
|
# Initially nothing should be checked
|
|
refute view
|
|
|> element("input[type='checkbox'][name='select_all'][checked]")
|
|
|> has_element?()
|
|
|
|
# Select first user
|
|
view |> element("input[type='checkbox'][name='#{user1.id}']") |> render_click()
|
|
# Select all should still not be checked (only 1 of 2+ users selected)
|
|
refute view
|
|
|> element("input[type='checkbox'][name='select_all'][checked]")
|
|
|> has_element?()
|
|
|
|
# Select second user
|
|
html = view |> element("input[type='checkbox'][name='#{user2.id}']") |> render_click()
|
|
|
|
# Now select all should be automatically checked (all individual users are selected)
|
|
# Note: This test might need adjustment based on actual implementation
|
|
# The logic depends on whether authenticated user is included in the count
|
|
assert html =~ "Email"
|
|
assert html =~ to_string(user1.email)
|
|
assert html =~ to_string(user2.email)
|
|
end
|
|
end
|
|
|
|
describe "delete functionality" do
|
|
test "can delete a user", %{conn: conn} do
|
|
_user = create_test_user(%{email: "delete-me@example.com"})
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, view, _html} = live(conn, "/users")
|
|
|
|
# Confirm user is displayed
|
|
assert render(view) =~ "delete-me@example.com"
|
|
|
|
# Click the first delete button to test the functionality
|
|
view |> element("tbody tr:first-child a[data-confirm]") |> render_click()
|
|
|
|
# The page should still render (basic functionality test)
|
|
html = render(view)
|
|
# Table header should still be there
|
|
assert html =~ "Email"
|
|
end
|
|
|
|
test "shows delete confirmation", %{conn: conn} do
|
|
_user = create_test_user(%{email: "confirm-delete@example.com"})
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
|
|
# Check that delete link has confirmation attribute
|
|
assert html =~ ~s(data-confirm="Are you sure?")
|
|
end
|
|
end
|
|
|
|
describe "navigation" do
|
|
test "clicking on user row navigates to user show page", %{conn: conn} do
|
|
user = create_test_user(%{email: "navigate@example.com"})
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, view, _html} = live(conn, "/users")
|
|
|
|
# This test would need to check row click behavior
|
|
# The actual navigation would happen via JavaScript
|
|
html = render(view)
|
|
assert html =~ ~s(/users/#{user.id})
|
|
end
|
|
|
|
test "edit link points to correct edit page", %{conn: conn} do
|
|
user = create_test_user(%{email: "edit-me@example.com"})
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
|
|
assert html =~ ~s(/users/#{user.id}/edit)
|
|
end
|
|
|
|
test "new user button points to correct new page", %{conn: conn} do
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
|
|
assert html =~ ~s(/users/new)
|
|
end
|
|
end
|
|
|
|
describe "translations" do
|
|
test "shows German translations for selection", %{conn: conn} do
|
|
conn = conn_with_oidc_user(conn)
|
|
conn = Plug.Test.init_test_session(conn, locale: "de")
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
|
|
assert html =~ "Alle Benutzer auswählen"
|
|
assert html =~ "Benutzer auswählen"
|
|
end
|
|
|
|
test "shows English translations for selection", %{conn: conn} do
|
|
conn = conn_with_oidc_user(conn)
|
|
Gettext.put_locale(MvWeb.Gettext, "en")
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
|
|
# Note: English translations might be empty strings by default
|
|
# This test would verify the structure is there
|
|
# Checking that aria-label attributes exist
|
|
assert html =~ ~s(aria-label=)
|
|
end
|
|
end
|
|
|
|
describe "edge cases" do
|
|
test "handles empty user list gracefully", %{conn: conn} do
|
|
# Don't create any users besides the authenticated one
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
|
|
# Should still show the table structure
|
|
assert html =~ "Email"
|
|
assert html =~ "OIDC ID"
|
|
# Should show the authenticated user at minimum
|
|
# Matches the generated email pattern oidc.user{unique_id}@example.com
|
|
assert html =~ "oidc.user"
|
|
end
|
|
|
|
test "handles users with missing OIDC ID", %{conn: conn} do
|
|
_user = create_test_user(%{email: "no-oidc@example.com", oidc_id: nil})
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
|
|
assert html =~ "no-oidc@example.com"
|
|
# Should handle nil OIDC ID gracefully
|
|
end
|
|
|
|
test "handles very long email addresses", %{conn: conn} do
|
|
long_email = "very.long.email.address.that.might.break.layouts@example.com"
|
|
_user = create_test_user(%{email: long_email})
|
|
conn = conn_with_oidc_user(conn)
|
|
{:ok, _view, html} = live(conn, "/users")
|
|
|
|
assert html =~ long_email
|
|
end
|
|
end
|
|
end
|