mirror of
https://github.com/outbackdingo/firezone.git
synced 2026-01-27 18:18:55 +00:00
Add admin_count and active_device_count (#956)
* install, compile deps for test env * Add admin_count and active_device_counts to telemetry * Update apps/fz_http/test/fz_http/devices_test.exs Signed-off-by: Jamil <jamilbk@users.noreply.github.com> Signed-off-by: Jamil <jamilbk@users.noreply.github.com>
This commit is contained in:
@@ -47,6 +47,7 @@ COPY apps/fz_wall/mix.exs /var/app/apps/fz_wall/mix.exs
|
||||
COPY mix.exs /var/app/mix.exs
|
||||
COPY mix.lock /var/app/mix.lock
|
||||
RUN mix do deps.get, deps.compile, compile
|
||||
RUN mix do deps.get --only test, deps.compile --only test, compile --only test
|
||||
|
||||
COPY apps /var/app/apps
|
||||
COPY config /var/app/config
|
||||
|
||||
@@ -11,6 +11,16 @@ defmodule FzHttp.Devices do
|
||||
|
||||
require Logger
|
||||
|
||||
def count_active_within(duration_in_secs) when is_integer(duration_in_secs) do
|
||||
cutoff = DateTime.add(DateTime.utc_now(), -1 * duration_in_secs)
|
||||
|
||||
Repo.one(
|
||||
from d in Device,
|
||||
select: count(d.id),
|
||||
where: d.latest_handshake > ^cutoff
|
||||
)
|
||||
end
|
||||
|
||||
def count do
|
||||
Repo.one(from d in Device, select: count(d.id))
|
||||
end
|
||||
|
||||
@@ -74,9 +74,13 @@ defmodule FzHttp.Telemetry do
|
||||
telemetry_module().capture("ping", ping_data())
|
||||
end
|
||||
|
||||
# How far back to count handshakes as an active device
|
||||
@active_device_window 86_400
|
||||
def ping_data do
|
||||
common_fields() ++
|
||||
[
|
||||
devices_active_within_24h: Devices.count_active_within(@active_device_window),
|
||||
admin_count: Users.count(role: :admin),
|
||||
user_count: Users.count(),
|
||||
device_count: Devices.count(),
|
||||
max_devices_for_users: Devices.max_count_by_user_id(),
|
||||
|
||||
@@ -17,6 +17,10 @@ defmodule FzHttp.Users do
|
||||
Repo.one(from u in User, select: count(u.id))
|
||||
end
|
||||
|
||||
def count(role: role) do
|
||||
Repo.one(from u in User, select: count(u.id), where: u.role == ^role)
|
||||
end
|
||||
|
||||
def consume_sign_in_token(token) when is_binary(token) do
|
||||
case find_and_clear_token(token) do
|
||||
{:ok, {:ok, user}} -> {:ok, user}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
defmodule FzHttp.DevicesTest do
|
||||
# XXX: Update the device IP query to be an insert
|
||||
use FzHttp.DataCase, async: false
|
||||
alias FzHttp.{Devices, Users}
|
||||
alias FzHttp.Devices
|
||||
alias FzHttp.DevicesFixtures
|
||||
alias FzHttp.Users
|
||||
|
||||
describe "trimmed fields" do
|
||||
test "trims expected fields" do
|
||||
@@ -34,6 +36,23 @@ defmodule FzHttp.DevicesTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "count_active_within/1" do
|
||||
@active_within 30
|
||||
|
||||
test "returns device count active within the last 30 seconds" do
|
||||
DevicesFixtures.device(%{latest_handshake: DateTime.utc_now()})
|
||||
|
||||
assert Devices.count_active_within(@active_within) == 1
|
||||
end
|
||||
|
||||
test "omits device active exceeding 30 seconds" do
|
||||
latest_handshake = DateTime.add(DateTime.utc_now(), -31)
|
||||
DevicesFixtures.device(%{latest_handshake: latest_handshake})
|
||||
|
||||
assert Devices.count_active_within(@active_within) == 0
|
||||
end
|
||||
end
|
||||
|
||||
describe "list_devices/0" do
|
||||
setup [:create_device]
|
||||
|
||||
|
||||
@@ -3,6 +3,28 @@ defmodule FzHttp.UsersTest do
|
||||
|
||||
alias FzHttp.{Repo, Users}
|
||||
|
||||
describe "count/0" do
|
||||
setup :create_user
|
||||
|
||||
test "returns correct count of all users" do
|
||||
assert Users.count() == 1
|
||||
end
|
||||
end
|
||||
|
||||
describe "count/1" do
|
||||
setup :create_users
|
||||
|
||||
@tag count: 3
|
||||
test "returns the correct count of admin users" do
|
||||
assert Users.count(role: :admin) == 3
|
||||
end
|
||||
|
||||
@tag count: 7, role: :unprivileged
|
||||
test "returns the correct count of unprivileged users" do
|
||||
assert Users.count(role: :unprivileged) == 7
|
||||
end
|
||||
end
|
||||
|
||||
describe "trimmed fields" do
|
||||
test "trims expected fields" do
|
||||
changeset =
|
||||
@@ -325,7 +347,7 @@ defmodule FzHttp.UsersTest do
|
||||
{:ok, user: user}
|
||||
end
|
||||
|
||||
@tag :unprivileged
|
||||
@tag role: :unprivileged
|
||||
test "enable via OIDC", %{user: user} do
|
||||
Users.enable_vpn_connection(user, %{provider: :oidc})
|
||||
|
||||
@@ -334,7 +356,7 @@ defmodule FzHttp.UsersTest do
|
||||
assert %{disabled_at: nil} = user
|
||||
end
|
||||
|
||||
@tag :unprivileged
|
||||
@tag role: :unprivileged
|
||||
test "no change via password", %{user: user} do
|
||||
Users.enable_vpn_connection(user, %{provider: :identity})
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ defmodule FzHttpWeb.UserLive.VPNConnectionComponentTest do
|
||||
describe "unprivileged" do
|
||||
setup :create_user
|
||||
|
||||
@tag :unprivileged
|
||||
@tag role: :unprivileged
|
||||
test "checkbox is not disabled", %{user: user} do
|
||||
refute render_component(VPNConnectionComponent, id: "1", user: user) =~ ~r"\bdisabled\b"
|
||||
end
|
||||
|
||||
@@ -91,12 +91,8 @@ defmodule FzHttp.TestHelpers do
|
||||
end
|
||||
|
||||
def create_user(tags) do
|
||||
user =
|
||||
if tags[:unprivileged] do
|
||||
UsersFixtures.user(%{role: :unprivileged})
|
||||
else
|
||||
UsersFixtures.user()
|
||||
end
|
||||
role = tags[:role] || :admin
|
||||
user = UsersFixtures.user(%{role: role})
|
||||
|
||||
{:ok, user: user}
|
||||
end
|
||||
@@ -208,12 +204,13 @@ defmodule FzHttp.TestHelpers do
|
||||
})}
|
||||
end
|
||||
|
||||
def create_users(opts) do
|
||||
count = opts[:count] || 5
|
||||
def create_users(tags) do
|
||||
count = tags[:count] || 5
|
||||
role = tags[:role] || :admin
|
||||
|
||||
users =
|
||||
Enum.map(1..count, fn i ->
|
||||
UsersFixtures.user(%{email: "userlist#{i}@test"})
|
||||
UsersFixtures.user(%{role: role, email: "userlist#{i}@test"})
|
||||
end)
|
||||
|
||||
{:ok, users: users}
|
||||
|
||||
Reference in New Issue
Block a user