Skip to content

Commit 419949e

Browse files
author
Taras Tyshko
committed
Add unitest for Audit Logs pagination at Device page
1 parent f03b790 commit 419949e

File tree

5 files changed

+184
-9
lines changed

5 files changed

+184
-9
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ test/fixtures/ssl/device-root-ca.srl
2121
.elixir-tools
2222

2323
.DS_Store
24-
/cover/
24+
.idea/
25+
/cover/
26+
nerves_hub.iml

lib/nerves_hub_web/components/device_page/activity_tab.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ defmodule NervesHubWeb.Components.DevicePage.ActivityTab do
9999
end
100100

101101
def hooked_event("set-paginate-opts", %{"page-size" => page_size}, socket) do
102-
params = %{"page_size" => page_size, "page_number" => 1}
102+
params = %{"page_size" => page_size, "page_number" => "1"}
103103

104104
%{org: org, product: product, device: device} = socket.assigns
105105

lib/nerves_hub_web/live/devices/show.ex

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,11 @@ defmodule NervesHubWeb.Live.Devices.Show do
491491
end
492492

493493
def handle_event("set-paginate-opts", %{"page-size" => page_size}, socket) do
494-
params = %{"page_size" => page_size, "page_number" => 1}
495-
494+
params = %{"page_size" => page_size, "page_number" => "1"}
496495
%{org: org, product: product, device: device} = socket.assigns
496+
url = ~p"/org/#{org}/#{product}/devices/#{device}?#{params}"
497497

498-
url = ~p"/org/#{org}/#{product}/devices/#{device}/activity?#{params}"
499-
500-
socket
501-
|> push_patch(to: url)
502-
|> noreply()
498+
socket |> push_patch(to: url) |> noreply()
503499
end
504500

505501
def handle_event("select-firmware-version", _, socket) do

test/nerves_hub_web/live/devices/show_test.exs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,78 @@ defmodule NervesHubWeb.Live.Devices.ShowTest do
686686
end
687687
end
688688

689+
describe "audit logs pagination" do
690+
test "pagination works with URL parameters", %{
691+
conn: conn,
692+
org: org,
693+
product: product,
694+
device: device,
695+
user: user
696+
} do
697+
# Create multiple audit log entries for pagination testing
698+
Enum.each(1..12, fn i ->
699+
NervesHub.AuditLogs.audit!(
700+
user,
701+
device,
702+
"Test audit log entry #{i}"
703+
)
704+
end)
705+
706+
# Test page 1 with default page_size=5
707+
conn
708+
|> visit("/org/#{org.name}/#{product.name}/devices/#{device.identifier}")
709+
|> assert_has("div.audit-log-item", count: 5)
710+
|> assert_has("button[phx-value-page=\"2\"]")
711+
712+
# Test page 2 with page_size=5
713+
conn
714+
|> visit(
715+
"/org/#{org.name}/#{product.name}/devices/#{device.identifier}?page_number=2&page_size=5"
716+
)
717+
# Still showing 5 per page
718+
|> assert_has("div.audit-log-item", count: 5)
719+
|> assert_has("button[phx-value-page=\"1\"]")
720+
721+
# Test custom page_size=10
722+
conn
723+
|> visit(
724+
"/org/#{org.name}/#{product.name}/devices/#{device.identifier}?page_number=1&page_size=10"
725+
)
726+
|> assert_has("div.audit-log-item", count: 10)
727+
end
728+
729+
test "pagination events work correctly", %{
730+
conn: conn,
731+
org: org,
732+
product: product,
733+
device: device,
734+
user: user
735+
} do
736+
# Create enough audit logs for pagination
737+
Enum.each(1..8, fn i ->
738+
NervesHub.AuditLogs.audit!(
739+
user,
740+
device,
741+
"Pagination test entry #{i}"
742+
)
743+
end)
744+
745+
{:ok, view, _html} =
746+
live(conn, "/org/#{org.name}/#{product.name}/devices/#{device.identifier}")
747+
748+
# Test paginate event
749+
view
750+
|> element("button[phx-click=\"paginate\"][phx-value-page=\"2\"]", "2")
751+
|> render_click()
752+
753+
# Should redirect to page 2 on same device page
754+
assert_patch(
755+
view,
756+
"/org/#{org.name}/#{product.name}/devices/#{device.identifier}?page_number=2&page_size=5"
757+
)
758+
end
759+
end
760+
689761
def device_show_path(%{device: device, org: org, product: product}) do
690762
~p"/org/#{org}/#{product}/devices/#{device}"
691763
end

test/nerves_hub_web/live/new_ui/devices/activity_tab_test.exs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,109 @@ defmodule NervesHubWeb.Live.NewUI.Devices.ActivityTabTest do
3232
|> visit("/org/#{org.name}/#{product.name}/devices/#{device.identifier}/activity")
3333
|> assert_has("div", text: "User #{user.name} rebooted device #{device.identifier}")
3434
end
35+
36+
describe "pagination" do
37+
test "pagination works with URL parameters", %{
38+
conn: conn,
39+
org: org,
40+
product: product,
41+
device: device,
42+
user: user
43+
} do
44+
# Create multiple audit log entries for pagination testing (30 entries)
45+
Enum.each(1..30, fn i ->
46+
NervesHub.AuditLogs.audit!(
47+
user,
48+
device,
49+
"New UI test audit log entry #{i}"
50+
)
51+
end)
52+
53+
# Test page 1 with default page_size=25
54+
conn
55+
|> visit("/org/#{org.name}/#{product.name}/devices/#{device.identifier}/activity")
56+
|> assert_has("div.flex.items-center.gap-6", count: 25)
57+
|> assert_has("button[phx-value-page=\"2\"]")
58+
59+
# Test page 2 with page_size=25
60+
conn
61+
|> visit(
62+
"/org/#{org.name}/#{product.name}/devices/#{device.identifier}/activity?page_number=2&page_size=25"
63+
)
64+
# Remaining 5 entries
65+
|> assert_has("div.flex.items-center.gap-6", count: 5)
66+
|> assert_has("button[phx-value-page=\"1\"]")
67+
68+
# Test custom page_size=10
69+
conn
70+
|> visit(
71+
"/org/#{org.name}/#{product.name}/devices/#{device.identifier}/activity?page_number=1&page_size=10"
72+
)
73+
|> assert_has("div.flex.items-center.gap-6", count: 10)
74+
|> assert_has("button[phx-value-page=\"2\"]")
75+
|> assert_has("button[phx-value-page=\"3\"]")
76+
end
77+
78+
test "pagination events work correctly", %{
79+
conn: conn,
80+
org: org,
81+
product: product,
82+
device: device,
83+
user: user
84+
} do
85+
# Create enough audit logs for pagination
86+
Enum.each(1..30, fn i ->
87+
NervesHub.AuditLogs.audit!(
88+
user,
89+
device,
90+
"New UI pagination test entry #{i}"
91+
)
92+
end)
93+
94+
{:ok, view, _html} =
95+
live(conn, "/org/#{org.name}/#{product.name}/devices/#{device.identifier}/activity")
96+
97+
# Test paginate event
98+
view
99+
|> element("button[phx-click=\"paginate\"][phx-value-page=\"2\"]", "2")
100+
|> render_click()
101+
102+
# Should redirect to page 2 on activity page
103+
assert_patch(
104+
view,
105+
"/org/#{org.name}/#{product.name}/devices/#{device.identifier}/activity?page_number=2&page_size=25"
106+
)
107+
end
108+
109+
test "page size selection works correctly", %{
110+
conn: conn,
111+
org: org,
112+
product: product,
113+
device: device,
114+
user: user
115+
} do
116+
# Create 60 audit logs for testing page size changes
117+
Enum.each(1..60, fn i ->
118+
NervesHub.AuditLogs.audit!(
119+
user,
120+
device,
121+
"Page size test entry #{i}"
122+
)
123+
end)
124+
125+
{:ok, view, _html} =
126+
live(conn, "/org/#{org.name}/#{product.name}/devices/#{device.identifier}/activity")
127+
128+
# Test changing page size to 50
129+
view
130+
|> element("button[phx-click=\"set-paginate-opts\"][phx-value-page-size=\"50\"]")
131+
|> render_click()
132+
133+
# Should redirect to page 1 with page_size=50
134+
assert_patch(
135+
view,
136+
"/org/#{org.name}/#{product.name}/devices/#{device.identifier}/activity?page_number=1&page_size=50"
137+
)
138+
end
139+
end
35140
end

0 commit comments

Comments
 (0)