44 lines
1.3 KiB
Elixir
44 lines
1.3 KiB
Elixir
defmodule Towerops.Accounts.GrantSudoModeTest do
|
|
use Towerops.DataCase, async: true
|
|
|
|
import Towerops.AccountsFixtures
|
|
|
|
alias Towerops.Accounts
|
|
alias Towerops.Accounts.User
|
|
|
|
describe "grant_sudo_mode/1" do
|
|
test "updates user's last_sudo_at timestamp" do
|
|
user = user_fixture()
|
|
assert is_nil(user.last_sudo_at)
|
|
|
|
{:ok, updated_user} = Accounts.grant_sudo_mode(user)
|
|
|
|
assert %DateTime{} = updated_user.last_sudo_at
|
|
assert DateTime.diff(DateTime.utc_now(), updated_user.last_sudo_at, :second) < 2
|
|
end
|
|
|
|
test "updates an already set last_sudo_at timestamp" do
|
|
user = user_fixture()
|
|
|
|
# First grant
|
|
{:ok, user_with_sudo} = Accounts.grant_sudo_mode(user)
|
|
first_timestamp = user_with_sudo.last_sudo_at
|
|
|
|
# Wait to ensure different timestamp (at least 1 second)
|
|
Process.sleep(1100)
|
|
|
|
# Second grant should update the timestamp
|
|
{:ok, updated_user} = Accounts.grant_sudo_mode(user_with_sudo)
|
|
assert DateTime.after?(updated_user.last_sudo_at, first_timestamp)
|
|
end
|
|
|
|
test "validates changeset" do
|
|
user = user_fixture()
|
|
|
|
# Test the changeset validation directly
|
|
changeset = User.sudo_changeset(user, %{})
|
|
refute changeset.valid?
|
|
assert "can't be blank" in errors_on(changeset).last_sudo_at
|
|
end
|
|
end
|
|
end
|