Files
gitea/tests/integration/privateactivity_test.go
T
1f614953a2 enhance: allow admins to force user activity visibility (#40002)
Adds `[service] FORCE_USER_ACTIVITY_VISIBILITY` so admins can force the
activity of all users to be `public` or `private`, e.g. for company
instances that require public activity.

- When the value changes, the next start overwrites the "Hide Activity"
setting of all users. New users get the forced value too.
- The "Hide Activity" checkbox is disabled with an explanatory tooltip
while forced, and `PATCH /api/v1/user/settings` rejects a conflicting
`hide_activity` with 422.

| Before | After |
| --- | --- |
| <img width="122" height="17" alt="before"
src="https://github.com/user-attachments/assets/d138acec-5d40-4a0a-881c-0097e5197a8f"
/> | <img width="258" height="46" alt="after"
src="https://github.com/user-attachments/assets/7e8f7d1b-3303-4473-8d3b-aa35ef8fb20d"
/> |

Fixes: https://github.com/go-gitea/gitea/issues/37961

Assisted-by: Claude Code:claude-opus-5-5

---------

Co-authored-by: bircni <bircni@icloud.com>
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2026-11-13 17:32:29 +00:00

296 lines
11 KiB
Go

// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"fmt"
"net/http"
"testing"
activities_model "gitea.dev/models/activities"
auth_model "gitea.dev/models/auth"
repo_model "gitea.dev/models/repo"
"gitea.dev/models/unittest"
user_model "gitea.dev/models/user"
"gitea.dev/modules/setting"
api "gitea.dev/modules/structs"
"gitea.dev/modules/test"
user_service "gitea.dev/services/user"
"gitea.dev/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
privateActivityTestAdmin = "user1"
privateActivityTestUser = "user2"
)
// org3 is an organization so it is not usable here
const privateActivityTestOtherUser = "user4"
// activity helpers
func testPrivateActivityDoSomethingForActionEntries(t *testing.T) {
repoBefore := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repoBefore.OwnerID})
session := loginUser(t, privateActivityTestUser)
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues?state=all", owner.Name, repoBefore.Name)
req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueOption{
Body: "test",
Title: "test",
}).AddTokenAuth(token)
session.MakeRequest(t, req, http.StatusCreated)
}
// private activity helpers
func testPrivateActivityHelperEnablePrivateActivity(t *testing.T) {
session := loginUser(t, privateActivityTestUser)
req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
"name": privateActivityTestUser,
"email": privateActivityTestUser + "@example.com",
"language": "en-US",
"keep_activity_private": "1",
})
session.MakeRequest(t, req, http.StatusOK)
}
func testPrivateActivityHelperHasVisibleActivitiesInHTMLDoc(htmlDoc *HTMLDoc) bool {
return htmlDoc.doc.Find("#activity-feed").Find(".item").Length() > 0
}
func testPrivateActivityHelperHasVisibleActivitiesFromSession(t *testing.T, session *TestSession) bool {
req := NewRequestf(t, "GET", "/%s?tab=activity", privateActivityTestUser)
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
return testPrivateActivityHelperHasVisibleActivitiesInHTMLDoc(htmlDoc)
}
func testPrivateActivityHelperHasVisibleActivitiesFromPublic(t *testing.T) bool {
req := NewRequestf(t, "GET", "/%s?tab=activity", privateActivityTestUser)
resp := MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
return testPrivateActivityHelperHasVisibleActivitiesInHTMLDoc(htmlDoc)
}
// heatmap UI helpers
func testPrivateActivityHelperHasVisibleHeatmapInHTMLDoc(htmlDoc *HTMLDoc) bool {
return htmlDoc.doc.Find("#user-heatmap").Length() > 0
}
func testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t *testing.T, session *TestSession) bool {
req := NewRequestf(t, "GET", "/%s?tab=activity", privateActivityTestUser)
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
return testPrivateActivityHelperHasVisibleHeatmapInHTMLDoc(htmlDoc)
}
func testPrivateActivityHelperHasVisibleDashboardHeatmapFromSession(t *testing.T, session *TestSession) bool {
req := NewRequest(t, "GET", "/")
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
return testPrivateActivityHelperHasVisibleHeatmapInHTMLDoc(htmlDoc)
}
func testPrivateActivityHelperHasVisibleHeatmapFromPublic(t *testing.T) bool {
req := NewRequestf(t, "GET", "/%s?tab=activity", privateActivityTestUser)
resp := MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
return testPrivateActivityHelperHasVisibleHeatmapInHTMLDoc(htmlDoc)
}
// heatmap API helpers
func testPrivateActivityHelperHasHeatmapContentFromPublic(t *testing.T) bool {
req := NewRequestf(t, "GET", "/api/v1/users/%s/heatmap", privateActivityTestUser)
resp := MakeRequest(t, req, http.StatusOK)
items := DecodeJSON(t, resp, []*activities_model.UserHeatmapData{})
return len(items) != 0
}
func testPrivateActivityHelperHasHeatmapContentFromSession(t *testing.T, session *TestSession) bool {
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeAll)
req := NewRequestf(t, "GET", "/api/v1/users/%s/heatmap", privateActivityTestUser).
AddTokenAuth(token)
resp := session.MakeRequest(t, req, http.StatusOK)
items := DecodeJSON(t, resp, []*activities_model.UserHeatmapData{})
return len(items) != 0
}
func TestPrivateActivityVisibility(t *testing.T) {
for _, test := range []struct {
name string
private bool
user string
visible bool
}{
// check activity visibility if the visibility is enabled
{"NoVisibleForPublic", false, "", true},
{"NoVisibleForUserItself", false, privateActivityTestUser, true},
{"NoVisibleForOtherUser", false, privateActivityTestOtherUser, true},
{"NoVisibleForAdmin", false, privateActivityTestAdmin, true},
// check activity visibility if the visibility is disabled
{"YesInvisibleForPublic", true, "", false},
{"YesVisibleForUserItself", true, privateActivityTestUser, true},
{"YesInvisibleForOtherUser", true, privateActivityTestOtherUser, false},
{"YesVisibleForAdmin", true, privateActivityTestAdmin, true},
} {
t.Run(test.name, func(t *testing.T) {
defer tests.PrepareTestEnv(t)()
testPrivateActivityDoSomethingForActionEntries(t)
if test.private {
testPrivateActivityHelperEnablePrivateActivity(t)
}
var visible bool
if test.user == "" {
visible = testPrivateActivityHelperHasVisibleActivitiesFromPublic(t)
} else {
visible = testPrivateActivityHelperHasVisibleActivitiesFromSession(t, loginUser(t, test.user))
}
if test.visible {
assert.True(t, visible, "user should have visible activities")
} else {
assert.False(t, visible, "user should have no visible activities")
}
})
}
}
func TestPrivateActivityHeatmapVisibility(t *testing.T) {
for _, test := range []struct {
name string
private bool
user string
dashboard bool
visible bool
}{
// check heatmap visibility if the visibility is enabled
{"NoHeatmapVisibleForPublic", false, "", false, true},
{"NoHeatmapVisibleForUserItselfAtProfile", false, privateActivityTestUser, false, true},
{"NoHeatmapVisibleForUserItselfAtDashboard", false, privateActivityTestUser, true, true},
{"NoHeatmapVisibleForOtherUser", false, privateActivityTestOtherUser, false, true},
{"NoHeatmapVisibleForAdmin", false, privateActivityTestAdmin, false, true},
// check heatmap visibility if the visibility is disabled
{"YesHeatmapInvisibleForPublic", true, "", false, false},
{"YesHeatmapVisibleForUserItselfAtProfile", true, privateActivityTestUser, false, true},
{"YesHeatmapVisibleForUserItselfAtDashboard", true, privateActivityTestUser, true, true},
{"YesHeatmapInvisibleForOtherUser", true, privateActivityTestOtherUser, false, false},
{"YesHeatmapVisibleForAdmin", true, privateActivityTestAdmin, false, true},
} {
t.Run(test.name, func(t *testing.T) {
defer tests.PrepareTestEnv(t)()
testPrivateActivityDoSomethingForActionEntries(t)
if test.private {
testPrivateActivityHelperEnablePrivateActivity(t)
}
var visible bool
if test.user == "" {
visible = testPrivateActivityHelperHasVisibleHeatmapFromPublic(t)
} else if test.dashboard {
visible = testPrivateActivityHelperHasVisibleDashboardHeatmapFromSession(t, loginUser(t, test.user))
} else {
visible = testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t, loginUser(t, test.user))
}
if test.visible {
assert.True(t, visible, "user should have visible heatmap")
} else {
assert.False(t, visible, "user should have no visible heatmap")
}
})
}
}
func TestPrivateActivityHeatmapContent(t *testing.T) {
for _, test := range []struct {
name string
private bool
user string
hasContent bool
message string
}{
// check heatmap api provides content if the visibility is enabled
{"NoHeatmapHasContentForPublic", false, "", true, "user should have heatmap content"},
{"NoHeatmapHasContentForUserItself", false, privateActivityTestUser, true, "user should have heatmap content"},
{"NoHeatmapHasContentForOtherUser", false, privateActivityTestOtherUser, true, "user should have heatmap content"},
{"NoHeatmapHasContentForAdmin", false, privateActivityTestAdmin, true, "user should have heatmap content"},
// check heatmap api provides no content if the visibility is disabled
// this should be equal to the hidden heatmap at the UI
{"YesHeatmapHasNoContentForPublic", true, "", false, "user should have no heatmap content"},
{"YesHeatmapHasNoContentForUserItself", true, privateActivityTestUser, true, "user should see their own heatmap content"},
{"YesHeatmapHasNoContentForOtherUser", true, privateActivityTestOtherUser, false, "other user should not see heatmap content"},
{"YesHeatmapHasNoContentForAdmin", true, privateActivityTestAdmin, true, "heatmap should show content for admin"},
} {
t.Run(test.name, func(t *testing.T) {
defer tests.PrepareTestEnv(t)()
testPrivateActivityDoSomethingForActionEntries(t)
if test.private {
testPrivateActivityHelperEnablePrivateActivity(t)
}
var hasContent bool
if test.user == "" {
hasContent = testPrivateActivityHelperHasHeatmapContentFromPublic(t)
} else {
hasContent = testPrivateActivityHelperHasHeatmapContentFromSession(t, loginUser(t, test.user))
}
if test.hasContent {
assert.True(t, hasContent, test.message)
} else {
assert.False(t, hasContent, test.message)
}
})
}
}
func TestPrivateActivityForcedVisibility(t *testing.T) {
defer tests.PrepareTestEnv(t)()
testPrivateActivityHelperEnablePrivateActivity(t)
keepActivityPrivate := func() bool {
return unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: privateActivityTestUser}).KeepActivityPrivate
}
defer test.MockVariableValue(&setting.Service.ForceUserActivityVisibility, "public")()
require.NoError(t, user_service.SyncForcedActivityVisibility(t.Context()))
assert.False(t, keepActivityPrivate())
session := loginUser(t, privateActivityTestUser)
resp := session.MakeRequest(t, NewRequest(t, "GET", "/user/settings"), http.StatusOK)
AssertHTMLElement(t, NewHTMLParser(t, resp.Body), `#keep-activity-private input[disabled]`, true)
testPrivateActivityHelperEnablePrivateActivity(t)
assert.False(t, keepActivityPrivate())
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteUser)
req := NewRequestWithJSON(t, "PATCH", "/api/v1/user/settings", &api.UserSettingsOptions{HideActivity: new(true)}).AddTokenAuth(token)
MakeRequest(t, req, http.StatusUnprocessableEntity)
req = NewRequestWithJSON(t, "PATCH", "/api/v1/user/settings", &api.UserSettingsOptions{HideActivity: new(false)}).AddTokenAuth(token)
MakeRequest(t, req, http.StatusOK)
setting.Service.ForceUserActivityVisibility = "private"
require.NoError(t, user_service.SyncForcedActivityVisibility(t.Context()))
assert.True(t, keepActivityPrivate())
assert.False(t, unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "org3"}).KeepActivityPrivate)
}