forgejo/tests/integration/admin_moderation_test.go
floss4good d87e2e7e40 feat: Admin interface for abuse reports (#7905)
- Implementation of milestone 5. from **Task F. Moderation features: Reporting** (part of [amendment of the workplan](https://codeberg.org/forgejo/sustainability/src/branch/main/2022-12-01-nlnet/2025-02-07-extended-workplan.md#task-f-moderation-features-reporting) for NLnet 2022-12-035):
  `5. Forgejo admins can see a list of reports`
  There is a lot of room for improvements, but it was decided to start with a basic version so that feedback can be collected from real-life usages (based on which the UI might change a lot).
- Also covers milestone 2. from same **Task F. Moderation features: Reporting**:
  `2. Reports from multiple users are combined in the database and don't create additional reports.`
  But instead of combining the reports when stored, they are grouped when retrieved (it was concluded _that it might be preferable to take care of the deduplication while implementing the admin interface_; see https://codeberg.org/forgejo/forgejo/pulls/7939#issuecomment-4841754 for more details).

---

Follow-up of !6977

### See also:
- forgejo/design#30

---

This adds a new _Moderation reports_ section (/admin/moderation/reports) within the _Site administration_ page, where administrators can see an overview with the submitted abuse reports that are still open (not yet handled in any way). When multiple reports exist for the same content (submitted by distinct users) only the first one will be shown in the list and a counter can be seen on the right side (indicating the number of open reports for the same content type and ID). Clicking on the counter or the icon from the right side will open the details page where a list with all the reports (when multiple) linked to the reported content is available, as well as any shadow copy saved for the current report(s).
The new section is available only when moderation in enabled ([moderation] ENABLED config is set as true within app.ini).

Discussions regarding the UI/UX started with https://codeberg.org/forgejo/design/issues/30#issuecomment-2908849

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7905
Reviewed-by: Otto <otto@codeberg.org>
Reviewed-by: jerger <jerger@noreply.codeberg.org>
Co-authored-by: floss4good <floss4good@disroot.org>
Co-committed-by: floss4good <floss4good@disroot.org>
2025-07-23 00:20:15 +02:00

158 lines
6.2 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"
"forgejo.org/models/unittest"
"forgejo.org/modules/setting"
"forgejo.org/modules/test"
"forgejo.org/routers"
"forgejo.org/tests"
"github.com/stretchr/testify/assert"
)
func testReportDetails(t *testing.T, htmlDoc *HTMLDoc, reportID, contentIcon, contentRef, contentURL, category, reportsNo string) {
// Check icon octicon
icon := htmlDoc.Find("#report-" + reportID + " svg." + contentIcon)
assert.Equal(t, 1, icon.Length())
// Check content reference and URL
title := htmlDoc.Find("#report-" + reportID + " .flex-item-main .flex-item-title a")
if len(contentURL) == 0 {
// No URL means that the content was already deleted, so we should not find the anchor element.
assert.Zero(t, title.Length())
// Instead we should find an emphasis element.
title = htmlDoc.Find("#report-" + reportID + " .flex-item-main .flex-item-title em")
assert.Equal(t, 1, title.Length())
assert.Equal(t, contentRef, title.Text())
} else {
assert.Equal(t, 1, title.Length())
assert.Equal(t, contentRef, title.Text())
href, exists := title.Attr("href")
assert.True(t, exists)
assert.Equal(t, contentURL, href)
}
// Check category
cat := htmlDoc.Find("#report-" + reportID + " .flex-item-main .flex-items-inline .item:nth-child(3)")
assert.Equal(t, 1, cat.Length())
assert.Equal(t, category, strings.TrimSpace(cat.Text()))
// Check number of reports for the same content
count := htmlDoc.Find("#report-" + reportID + " a span")
assert.Equal(t, 1, count.Length())
assert.Equal(t, reportsNo, count.Text())
}
func TestAdminModerationViewReports(t *testing.T) {
defer unittest.OverrideFixtures("tests/integration/fixtures/TestAdminModerationViewReports")()
defer tests.PrepareTestEnv(t)()
t.Run("Moderation enabled", func(t *testing.T) {
defer test.MockVariableValue(&setting.Moderation.Enabled, true)()
defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())()
t.Run("Anonymous user", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequest(t, "GET", "/admin/moderation/reports")
MakeRequest(t, req, http.StatusSeeOther)
req = NewRequest(t, "GET", "/admin/moderation/reports/type/1/id/1002")
MakeRequest(t, req, http.StatusSeeOther)
})
t.Run("Normal user", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
session := loginUser(t, "user2")
req := NewRequest(t, "GET", "/admin/moderation/reports")
session.MakeRequest(t, req, http.StatusForbidden)
req = NewRequest(t, "GET", "/admin/moderation/reports/type/1/id/1002")
session.MakeRequest(t, req, http.StatusForbidden)
})
t.Run("Admin user", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
session := loginUser(t, "user1")
req := NewRequest(t, "GET", "/admin/moderation/reports")
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
// Check how many reports are being displayed.
// Reports linked to the same content (type and id) should be grouped; therefore we should see only 6 instead of 9.
reports := htmlDoc.Find(".admin-setting-content .flex-list .flex-item.report")
assert.Equal(t, 7, reports.Length())
// Check details for shown reports.
testReportDetails(t, htmlDoc, "1", "octicon-person", "@SPAM-services", "/SPAM-services", "Illegal content", "1")
testReportDetails(t, htmlDoc, "2", "octicon-repo", "SPAM-services/spammer-Tools", "/SPAM-services/spammer-Tools", "Illegal content", "1")
testReportDetails(t, htmlDoc, "3", "octicon-issue-opened", "SPAM-services/spammer-Tools#1", "/SPAM-services/spammer-Tools/issues/1", "Spam", "1")
// #4 is combined with #7 and #9
testReportDetails(t, htmlDoc, "4", "octicon-person", "@spammer01", "/spammer01", "Spam", "3")
// #5 is combined with #6
testReportDetails(t, htmlDoc, "5", "octicon-comment", "contributor/first/issues/1#issuecomment-1001", "/contributor/first/issues/1#issuecomment-1001", "Malware", "2")
testReportDetails(t, htmlDoc, "8", "octicon-issue-opened", "contributor/first#1", "/contributor/first/issues/1", "Other violations of platform rules", "1")
// #10 is for a Ghost user
testReportDetails(t, htmlDoc, "10", "octicon-person", "Reported content with type 1 and id 9999 no longer exists", "", "Other violations of platform rules", "1")
t.Run("reports details page", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req = NewRequest(t, "GET", "/admin/moderation/reports/type/1/id/1002")
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
// Check the title (content reference) and corresponding URL
title := htmlDoc.Find(".admin-setting-content .flex-item-main .flex-item-title a")
assert.Equal(t, 1, title.Length())
assert.Equal(t, "spammer01", title.Text())
href, exists := title.Attr("href")
assert.True(t, exists)
assert.Equal(t, "/spammer01", href)
// Check how many reports are being displayed for user 1002.
reports = htmlDoc.Find(".admin-setting-content .flex-list .flex-item")
assert.Equal(t, 3, reports.Length())
})
})
})
t.Run("Moderation disabled", func(t *testing.T) {
t.Run("Anonymous user", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequest(t, "GET", "/admin/moderation/reports")
MakeRequest(t, req, http.StatusNotFound)
req = NewRequest(t, "GET", "/admin/moderation/reports/type/1/id/1002")
MakeRequest(t, req, http.StatusNotFound)
})
t.Run("Normal user", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
session := loginUser(t, "user2")
req := NewRequest(t, "GET", "/admin/moderation/reports")
session.MakeRequest(t, req, http.StatusNotFound)
req = NewRequest(t, "GET", "/admin/moderation/reports/type/1/id/1002")
session.MakeRequest(t, req, http.StatusNotFound)
})
t.Run("Admin user", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
session := loginUser(t, "user1")
req := NewRequest(t, "GET", "/admin/moderation/reports")
session.MakeRequest(t, req, http.StatusNotFound)
req = NewRequest(t, "GET", "/admin/moderation/reports/type/1/id/1002")
session.MakeRequest(t, req, http.StatusNotFound)
})
})
}