defmodule GitGudWeb.DescriptionEditTest do
@moduledoc """
Editing an issue or PR description in place, the same way titles are
edited, with the change recorded as a collapsed diff in history.
"""
use GitGudWeb.ConnCase, async: false
import Phoenix.LiveViewTest
import GitGud.AccountsFixtures
import GitGud.ForgeFixtures
alias GitGud.Events
alias GitGud.Issues
alias GitGud.PullRequests
alias GitGud.Repositories
defp handle(repo),
do: Repositories.Storage.repo_handle(GitGud.Repo.preload(repo, [:owner, :organization]))
defp issue_path(repo, issue),
do: ~p"/r/#{handle(repo)}/#{repo.name}/issues/#{issue.number}"
defp pr_path(repo, pr),
do: ~p"/r/#{handle(repo)}/#{repo.name}/pulls/#{pr.number}"
describe "issues" do
test "the author can edit the description in place", %{conn: conn} do
{user, repo} = repository_fixture()
issue = issue_fixture(repo, user, %{"body" => "Original description"})
{:ok, lv, _html} = live(log_in_user(conn, user), issue_path(repo, issue))
_ = lv |> element("button[phx-click=edit_body]") |> render_click()
html = lv |> form("form[phx-submit=save_body]", %{body: "Rewritten"}) |> render_submit()
assert html =~ "Rewritten"
assert Issues.get_issue!(repo, issue.number).body == "Rewritten"
end
test "an unrelated user gets no edit affordance", %{conn: conn} do
{_owner, repo} = repository_fixture(%{visibility: "public"})
author = user_fixture()
issue = issue_fixture(repo, author, %{"body" => "Theirs"})
outsider = user_fixture()
{:ok, lv, _html} = live(log_in_user(conn, outsider), issue_path(repo, issue))
refute has_element?(lv, "button[phx-click=edit_body]")
end
test "an unrelated user can't edit by sending the event", %{conn: conn} do
{_owner, repo} = repository_fixture(%{visibility: "public"})
author = user_fixture()
issue = issue_fixture(repo, author, %{"body" => "Untouchable"})
outsider = user_fixture()
{:ok, lv, _html} = live(log_in_user(conn, outsider), issue_path(repo, issue))
render_hook(lv, "save_body", %{"body" => "Hijacked"})
assert Issues.get_issue!(repo, issue.number).body == "Untouchable"
end
test "a repo admin can edit someone else's description", %{conn: conn} do
{owner, repo} = repository_fixture(%{visibility: "public"})
author = user_fixture()
issue = issue_fixture(repo, author, %{"body" => "Needs work"})
{:ok, lv, _html} = live(log_in_user(conn, owner), issue_path(repo, issue))
_ = lv |> element("button[phx-click=edit_body]") |> render_click()
html = lv |> form("form[phx-submit=save_body]", %{body: "Tidied"}) |> render_submit()
assert html =~ "Tidied"
end
test "an unchanged description records nothing", %{conn: conn} do
{user, repo} = repository_fixture()
issue = issue_fixture(repo, user, %{"body" => "Same"})
{:ok, lv, _html} = live(log_in_user(conn, user), issue_path(repo, issue))
_ = lv |> element("button[phx-click=edit_body]") |> render_click()
_ = lv |> form("form[phx-submit=save_body]", %{body: "Same"}) |> render_submit()
assert Events.list_for(issue) == []
end
test "the change shows in history as a collapsed diff", %{conn: conn} do
{user, repo} = repository_fixture()
issue = issue_fixture(repo, user, %{"body" => "line one\nline two"})
{:ok, lv, _html} = live(log_in_user(conn, user), issue_path(repo, issue))
_ = lv |> element("button[phx-click=edit_body]") |> render_click()
_ =
lv
|> form("form[phx-submit=save_body]", %{body: "line one\nline three"})
|> render_submit()
{:ok, _hist, html} = live(log_in_user(conn, user), issue_path(repo, issue) <> "/history")
assert html =~ "edited the description."
assert html =~ "Show what changed"
refute html =~ "<details open"
assert html =~ "line two"
assert html =~ "line three"
end
end
describe "pull requests" do
test "the author can edit the description in place", %{conn: conn} do
{user, repo} = repository_fixture()
pr = pr_with_branches(repo, user, %{"body" => "PR description"})
{:ok, lv, _html} = live(log_in_user(conn, user), pr_path(repo, pr))
_ = lv |> element("button[phx-click=edit_body]") |> render_click()
html =
lv |> form("form[phx-submit=save_body]", %{body: "Better description"}) |> render_submit()
assert html =~ "Better description"
assert PullRequests.get_pull_request!(repo, pr.number).body == "Better description"
end
test "the edit is recorded", %{conn: conn} do
{user, repo} = repository_fixture()
pr = pr_with_branches(repo, user, %{"body" => "before"})
{:ok, lv, _html} = live(log_in_user(conn, user), pr_path(repo, pr))
_ = lv |> element("button[phx-click=edit_body]") |> render_click()
_ = lv |> form("form[phx-submit=save_body]", %{body: "after"}) |> render_submit()
assert [%{kind: "description_changed", data: data}] = Events.list_for(pr)
assert data["from"] == "before"
assert data["to"] == "after"
end
test "an unrelated user can't edit it", %{conn: conn} do
{_owner, repo} = repository_fixture(%{visibility: "public"})
author = user_fixture()
pr = pr_with_branches(repo, author, %{"body" => "Hands off"})
outsider = user_fixture()
{:ok, lv, _html} = live(log_in_user(conn, outsider), pr_path(repo, pr))
refute has_element?(lv, "button[phx-click=edit_body]")
render_hook(lv, "save_body", %{"body" => "Hijacked"})
assert PullRequests.get_pull_request!(repo, pr.number).body == "Hands off"
end
end
end
neiam /gitgud
Git Gud
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Packages
⭐
Log in to mark this repository.
5.6 KiB · text
History
6280797