defmodule GitGudWeb.BannedContentTest do
@moduledoc """
A banned user's existing content is folded away in the scope they're
banned from — at render time, so lifting the ban restores it without
anything having to be rewritten.
"""
use GitGudWeb.ConnCase, async: false
import Phoenix.LiveViewTest
import GitGud.AccountsFixtures
import GitGud.ForgeFixtures
alias GitGud.Issues
alias GitGud.Moderation
alias GitGud.Organizations
alias GitGud.Repositories
defp issue_path(repo, issue) do
handle = Repositories.Storage.repo_handle(GitGud.Repo.preload(repo, [:owner, :organization]))
~p"/r/#{handle}/#{repo.name}/issues/#{issue.number}"
end
test "a banned author's issue body is hidden", %{conn: conn} do
{owner, repo} = repository_fixture(%{visibility: "public"})
nuisance = user_fixture()
issue = issue_fixture(repo, nuisance, %{"body" => "the offending words"})
{:ok, _} = Moderation.ban(repo, nuisance, banned_by: owner)
{:ok, _lv, html} = live(conn, issue_path(repo, issue))
refute html =~ "the offending words"
assert html =~ "author is banned here"
end
test "their comments are hidden too", %{conn: conn} do
{owner, repo} = repository_fixture(%{visibility: "public"})
nuisance = user_fixture()
issue = issue_fixture(repo, owner)
{:ok, _} = Issues.add_comment(issue, nuisance, %{"body" => "spammy comment"})
{:ok, _} = Moderation.ban(repo, nuisance, banned_by: owner)
{:ok, _lv, html} = live(conn, issue_path(repo, issue))
refute html =~ "spammy comment"
assert html =~ "author is banned here"
end
test "everyone else's content is untouched", %{conn: conn} do
{owner, repo} = repository_fixture(%{visibility: "public"})
nuisance = user_fixture()
issue = issue_fixture(repo, owner, %{"body" => "perfectly fine"})
{:ok, _} = Issues.add_comment(issue, nuisance, %{"body" => "spammy comment"})
{:ok, _} = Moderation.ban(repo, nuisance, banned_by: owner)
{:ok, _lv, html} = live(conn, issue_path(repo, issue))
assert html =~ "perfectly fine"
refute html =~ "spammy comment"
end
test "lifting the ban brings it straight back", %{conn: conn} do
{owner, repo} = repository_fixture(%{visibility: "public"})
nuisance = user_fixture()
issue = issue_fixture(repo, nuisance, %{"body" => "restored words"})
{:ok, ban} = Moderation.ban(repo, nuisance, banned_by: owner)
{:ok, _lv, html} = live(conn, issue_path(repo, issue))
refute html =~ "restored words"
{:ok, _} = Moderation.lift(ban, owner)
{:ok, _lv, html} = live(conn, issue_path(repo, issue))
assert html =~ "restored words"
end
test "the author can still see their own words", %{conn: conn} do
{owner, repo} = repository_fixture(%{visibility: "public"})
nuisance = user_fixture()
issue = issue_fixture(repo, nuisance, %{"body" => "my own words"})
{:ok, _} = Moderation.ban(repo, nuisance, banned_by: owner)
{:ok, _lv, html} = live(log_in_user(conn, nuisance), issue_path(repo, issue))
assert html =~ "Show anyway"
assert html =~ "my own words"
end
test "a repo admin can expand it", %{conn: conn} do
{owner, repo} = repository_fixture(%{visibility: "public"})
nuisance = user_fixture()
issue = issue_fixture(repo, nuisance, %{"body" => "moderator can read this"})
{:ok, _} = Moderation.ban(repo, nuisance, banned_by: owner)
{:ok, _lv, html} = live(log_in_user(conn, owner), issue_path(repo, issue))
assert html =~ "Show anyway"
assert html =~ "moderator can read this"
end
test "an ordinary viewer gets no way to expand it", %{conn: conn} do
{owner, repo} = repository_fixture(%{visibility: "public"})
nuisance = user_fixture()
issue = issue_fixture(repo, nuisance, %{"body" => "not for you"})
{:ok, _} = Moderation.ban(repo, nuisance, banned_by: owner)
{:ok, _lv, html} = live(log_in_user(conn, user_fixture()), issue_path(repo, issue))
refute html =~ "Show anyway"
refute html =~ "not for you"
end
test "hiding is scope-local — elsewhere they read normally", %{conn: conn} do
{owner, repo} = repository_fixture(%{visibility: "public"})
{other_owner, other} = repository_fixture(%{visibility: "public"})
nuisance = user_fixture()
here = issue_fixture(repo, nuisance, %{"body" => "words in the banned repo"})
there = issue_fixture(other, nuisance, %{"body" => "words somewhere else"})
{:ok, _} = Moderation.ban(repo, nuisance, banned_by: owner)
{:ok, _lv, here_html} = live(conn, issue_path(repo, here))
{:ok, _lv, there_html} = live(conn, issue_path(other, there))
refute here_html =~ "words in the banned repo"
assert there_html =~ "words somewhere else"
_ = other_owner
end
test "an org ban hides their content across the org's repos", %{conn: conn} do
admin = user_fixture()
{:ok, org} = Organizations.create_organization(admin, %{"handle" => "hide-org"})
{:ok, repo} =
Repositories.create_repository_for_org(org, admin, %{
"name" => "orgrepo",
"visibility" => "public"
})
nuisance = user_fixture()
issue = issue_fixture(repo, nuisance, %{"body" => "org-wide hidden"})
{:ok, _} = Moderation.ban(org, nuisance, banned_by: admin)
{:ok, _lv, html} = live(conn, issue_path(repo, issue))
refute html =~ "org-wide hidden"
end
test "an expired ban stops hiding", %{conn: conn} do
{_owner, repo} = repository_fixture(%{visibility: "public"})
nuisance = user_fixture()
issue = issue_fixture(repo, nuisance, %{"body" => "visible again"})
GitGud.Repo.insert!(%GitGud.Moderation.Ban{
repository_id: repo.id,
banned_user_id: nuisance.id,
expires_at: DateTime.utc_now(:second) |> DateTime.add(-60, :second)
})
{:ok, _lv, html} = live(conn, issue_path(repo, issue))
assert html =~ "visible again"
end
end
neiam /gitgud
Git Gud
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Packages
⭐
Log in to mark this repository.
5.8 KiB · text
History
6280797