defmodule GitGud.Audit.Workers.PruneTest do
# Changes the application env, so not async.
use GitGud.DataCase, async: false
import GitGud.ForgeFixtures
alias GitGud.Accounts.Scope
alias GitGud.Audit
alias GitGud.Audit.Entry
alias GitGud.Audit.Workers.Prune
setup do
previous = Application.get_env(:git_gud, Audit)
on_exit(fn -> Application.put_env(:git_gud, Audit, previous) end)
{user, repo} = repository_fixture()
{:ok, scope: Scope.for_user(user), repo: repo}
end
defp entry_aged(ctx, days) do
ctx.scope
|> Audit.record("repo_secret.set", %{repository: ctx.repo})
|> Ecto.Changeset.change(
inserted_at: DateTime.add(DateTime.utc_now(), -days * 86_400, :second)
)
|> Repo.update!()
end
defp actions, do: Repo.all(from e in Entry, order_by: e.id, select: e.action)
test "keeps everything by default", ctx do
entry_aged(ctx, 5000)
Application.put_env(:git_gud, Audit, retention_days: nil)
assert :ok = Prune.perform(%Oban.Job{})
assert actions() == ["repo_secret.set"]
end
test "prunes what's older than the window, and says so", ctx do
entry_aged(ctx, 40)
entry_aged(ctx, 10)
Application.put_env(:git_gud, Audit, retention_days: 30)
assert :ok = Prune.perform(%Oban.Job{})
assert actions() == ["repo_secret.set", "audit.pruned"]
pruned = Repo.get_by!(Entry, action: "audit.pruned")
assert pruned.metadata["count"] == 1
assert pruned.actor_type == "system"
assert pruned.via == "cron"
end
test "a prune that finds nothing leaves no entry", ctx do
entry_aged(ctx, 1)
Application.put_env(:git_gud, Audit, retention_days: 30)
assert :ok = Prune.perform(%Oban.Job{})
assert actions() == ["repo_secret.set"]
end
test "retention_days! accepts nil and positive integers only" do
Application.put_env(:git_gud, Audit, retention_days: nil)
assert Audit.retention_days!() == nil
Application.put_env(:git_gud, Audit, retention_days: 30)
assert Audit.retention_days!() == 30
for bad <- [0, -1, "30", 1.5] do
Application.put_env(:git_gud, Audit, retention_days: bad)
assert_raise ArgumentError, fn -> Audit.retention_days!() end
end
end
end
neiam
/gitgud
Git Gud
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
1
Log in to mark this repository.
2.2 KiB · text
History
b868907