forgejo/tests/integration/activitypub_client_test.go
Michael Jerger 7566ebfba7 Add ActivityPub Person follow from distant (#8720)
This PR is part of #4767. It

1. adds the ability to follow a local person from a distant federation server (see tests/integration/api_activitypub_person_inbox_follow_test.go)
2. streamlines the router code (refactor the person conversion & handling of inbox requests in service direction, unifies service call signature & error handling)
3. introduces queues for decoupling outgoing communication (delivery retry to cope network issues or distant service downtimes) and
4. adds minor fixes to integration tests (test timeout & invalid inbox activities)

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8720
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Michael Jerger <michael.jerger@meissa-gmbh.de>
Co-committed-by: Michael Jerger <michael.jerger@meissa-gmbh.de>
2025-08-03 11:55:01 +02:00

55 lines
1.5 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"net/url"
"testing"
"time"
"forgejo.org/models/unittest"
user_model "forgejo.org/models/user"
"forgejo.org/modules/activitypub"
"forgejo.org/modules/setting"
"forgejo.org/modules/test"
"forgejo.org/routers"
"forgejo.org/services/contexttest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestActivityPubClientBodySize(t *testing.T) {
defer test.MockVariableValue(&setting.Federation.Enabled, true)()
defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())()
onGiteaRun(t, func(t *testing.T, u *url.URL) {
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
url := u.JoinPath("/api/v1/nodeinfo").String()
ctx, _ := contexttest.MockAPIContext(t, url)
clientFactory, err := activitypub.NewClientFactoryWithTimeout(60 * time.Second)
require.NoError(t, err)
apClient, err := clientFactory.WithKeys(ctx, user1, user1.KeyID())
require.NoError(t, err)
// Request with normal MaxSize
t.Run("NormalMaxSize", func(t *testing.T) {
resp, err := apClient.GetBody(url)
require.NoError(t, err)
assert.Contains(t, string(resp), "forgejo")
})
// Set MaxSize to something very low to always fail
// Request with low MaxSize
t.Run("LowMaxSize", func(t *testing.T) {
defer test.MockVariableValue(&setting.Federation.MaxSize, 100)()
_, err = apClient.GetBody(url)
require.Error(t, err)
assert.ErrorContains(t, err, "Request returned")
})
})
}