forgejo/tests/integration/pull_editable_test.go
Antonin Delpeuch 64cfb3580d feat: display the PR editable status in the right-hand side menu (#9392)
Fixes #9332.

I decided to leave the "Editable" badge next to the PR branch name, because I think it doesn't hurt to have it there, especially if some users have already learned to look for it there.

For those more familiar with the Github UI and not knowing to look for the "Editable" badge, this patch will also put the information in a more familiar place.

Unfortunately the changes to the existing test had to be more involved because the existing test used a PR which was made with the same head repo as the base repo (whereas the editable setting is only offered for PRs from forks).

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9392
Reviewed-by: Lucas <sclu1034@noreply.codeberg.org>
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Antonin Delpeuch <antonin@delpeuch.eu>
Co-committed-by: Antonin Delpeuch <antonin@delpeuch.eu>
2025-09-27 13:08:38 +02:00

70 lines
2.3 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package integration
import (
"net/http"
"strings"
"testing"
auth_model "forgejo.org/models/auth"
"forgejo.org/models/unittest"
api "forgejo.org/modules/structs"
"forgejo.org/modules/translation"
"forgejo.org/tests"
"github.com/stretchr/testify/assert"
)
func TestPullEditable_ShowEditableLabel(t *testing.T) {
// This fixture loads a PR which is made from a different repository,
// and opened by the user who owns the fork (which is necessary for
// them to be allowed to set the PR as editable).
defer unittest.OverrideFixtures("tests/integration/fixtures/TestPullEditable")()
defer tests.PrepareTestEnv(t)()
t.Run("Show editable label if PR is editable", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
editable := true
setPREditable(t, editable)
testEditableLabelShown(t, editable)
})
t.Run("Don't show editable label if PR is not editable", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
editable := false
setPREditable(t, editable)
testEditableLabelShown(t, editable)
})
}
func setPREditable(t *testing.T, editable bool) {
t.Helper()
session := loginUser(t, "user13")
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
req := NewRequestWithJSON(t, "PATCH", "/api/v1/repos/user12/repo10/pulls/2", &api.EditPullRequestOption{
AllowMaintainerEdit: &editable,
}).AddTokenAuth(token)
session.MakeRequest(t, req, http.StatusCreated)
}
func testEditableLabelShown(t *testing.T, expectLabel bool) {
t.Helper()
session := loginUser(t, "user12")
req := NewRequest(t, "GET", "/user12/repo10/pulls/2")
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
htmlDoc.AssertElement(t, "#editable-label", expectLabel)
locale := translation.NewLocale("en-US")
if expectLabel {
sidebarText := htmlDoc.Find(".issue-content-right span.maintainers-can-edit-status").First().Text()
assert.Equal(t, locale.TrString("repo.pulls.maintainers_can_edit"), strings.TrimSpace(sidebarText))
} else {
sidebarText := htmlDoc.Find(".issue-content-right span.maintainers-can-edit-status").First().Text()
assert.Equal(t, locale.TrString("repo.pulls.maintainers_cannot_edit"), strings.TrimSpace(sidebarText))
}
}