gotosocial/internal/federation/federatingdb/interactionrequest_test.go
tobi 754b7be9cf [feature] Support new model of interaction flow for forward compat with v0.21.0 (#4394)
~~Still WIP!~~

This PR allows v0.20.0 of GtS to be forward-compatible with the interaction request / authorization flow that will fully replace the current flow in v0.21.0.

Basically, this means we need to recognize LikeRequest, ReplyRequest, and AnnounceRequest, and in response to those requests, deliver either a Reject or an Accept, with the latter pointing towards a LikeAuthorization, ReplyAuthorization, or AnnounceAuthorization, respectively. This can then be used by the remote instance to prove to third parties that the interaction has been accepted by the interactee. These Authorization types need to be dereferencable to third parties, so we need to serve them.

As well as recognizing the above "polite" interaction request types, we also need to still serve appropriate responses to "impolite" interaction request types, where an instance that's unaware of interaction policies tries to interact with a post by sending a reply, like, or boost directly, without wrapping it in a WhateverRequest type.

Doesn't fully close https://codeberg.org/superseriousbusiness/gotosocial/issues/4026 but gets damn near (just gotta update the federating with GtS documentation).

Migrations tested on both Postgres and SQLite.

Co-authored-by: kim <grufwub@gmail.com>
Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4394
Co-authored-by: tobi <tobi.smethurst@protonmail.com>
Co-committed-by: tobi <tobi.smethurst@protonmail.com>
2025-09-14 15:37:35 +02:00

289 lines
9.4 KiB
Go

// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package federatingdb_test
import (
"context"
"encoding/json"
"testing"
"time"
"code.superseriousbusiness.org/activity/streams"
"code.superseriousbusiness.org/activity/streams/vocab"
"code.superseriousbusiness.org/gotosocial/internal/ap"
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
"code.superseriousbusiness.org/gotosocial/testrig"
"github.com/stretchr/testify/suite"
)
type InteractionRequestTestSuite struct {
FederatingDBTestSuite
}
func (suite *InteractionRequestTestSuite) intReq(
receiving *gtsmodel.Account,
requesting *gtsmodel.Account,
jsonStr string,
dbF func(ctx context.Context, req vocab.Type) error,
) error {
ctx := createTestContext(suite.T(), receiving, requesting)
raw := make(map[string]interface{})
if err := json.Unmarshal([]byte(jsonStr), &raw); err != nil {
suite.FailNow(err.Error())
}
t, err := streams.ToType(ctx, raw)
if err != nil {
suite.FailNow(err.Error())
}
return dbF(ctx, t)
}
func (suite *InteractionRequestTestSuite) TestReplyRequest() {
var (
ctx = suite.T().Context()
receiving = suite.testAccounts["admin_account"]
requesting = suite.testAccounts["remote_account_1"]
testStatus = suite.testStatuses["admin_account_status_1"]
intReqURI = "http://fossbros-anonymous.io/requests/87fb1478-ac46-406a-8463-96ce05645219"
intURI = "http://fossbros-anonymous.io/users/foss_satan/statuses/87fb1478-ac46-406a-8463-96ce05645219"
jsonStr = `{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://gotosocial.org/ns"
],
"type": "ReplyRequest",
"id": "` + intReqURI + `",
"actor": "` + requesting.URI + `",
"object": "` + testStatus.URI + `",
"to": "` + receiving.URI + `",
"instrument": {
"attributedTo": "` + requesting.URI + `",
"cc": "` + requesting.FollowersURI + `",
"content": "\u003cp\u003ethis is a reply!\u003c/p\u003e",
"id": "` + intURI + `",
"inReplyTo": "` + testStatus.URI + `",
"tag": {
"href": "` + receiving.URI + `",
"name": "@` + receiving.Username + `@localhost:8080",
"type": "Mention"
},
"to": "https://www.w3.org/ns/activitystreams#Public",
"type": "Note"
}
}`
)
suite.T().Logf("testing reply request:\n\n%s", jsonStr)
// Call the federatingDB function.
err := suite.intReq(
receiving,
requesting,
jsonStr,
func(ctx context.Context, req vocab.Type) error {
replyReq := req.(vocab.GoToSocialReplyRequest)
return suite.federatingDB.ReplyRequest(ctx, replyReq)
},
)
if err != nil {
suite.FailNow(err.Error())
}
// There should be an interaction request in the DB now.
var intReq *gtsmodel.InteractionRequest
if !testrig.WaitFor(func() bool {
intReq, err = suite.state.DB.GetInteractionRequestByInteractionURI(ctx, intURI)
return err == nil && intReq != nil
}) {
suite.FailNow("timed out waiting for int req to appear in the db")
}
suite.Equal(testStatus.ID, intReq.TargetStatusID)
suite.Equal(receiving.ID, intReq.TargetAccountID)
suite.Equal(requesting.ID, intReq.InteractingAccountID)
suite.Equal(intReqURI, intReq.InteractionRequestURI)
suite.Equal(intURI, intReq.InteractionURI)
suite.Equal(gtsmodel.InteractionReply, intReq.InteractionType)
// Should be a message heading to the processor.
msg, _ := suite.getFederatorMsg(5 * time.Second)
suite.Equal(ap.ActivityCreate, msg.APActivityType)
suite.Equal(ap.ActivityReplyRequest, msg.APObjectType)
suite.NotNil(msg.GTSModel)
suite.NotNil(msg.APObject)
suite.NotNil(msg.Receiving)
suite.NotNil(msg.Requesting)
}
func (suite *InteractionRequestTestSuite) TestLikeRequest() {
var (
ctx = suite.T().Context()
receiving = suite.testAccounts["admin_account"]
requesting = suite.testAccounts["remote_account_1"]
testStatus = suite.testStatuses["admin_account_status_1"]
intReqURI = "http://fossbros-anonymous.io/requests/87fb1478-ac46-406a-8463-96ce05645219"
intURI = "http://fossbros-anonymous.io/users/foss_satan/statuses/87fb1478-ac46-406a-8463-96ce05645219"
jsonStr = `{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://gotosocial.org/ns"
],
"type": "LikeRequest",
"id": "` + intReqURI + `",
"actor": "` + requesting.URI + `",
"object": "` + testStatus.URI + `",
"to": "` + receiving.URI + `",
"instrument": {
"id": "` + intURI + `",
"object": "` + testStatus.URI + `",
"actor": "` + requesting.URI + `",
"to": "` + receiving.URI + `",
"type": "Like"
}
}`
)
suite.T().Logf("testing like request:\n\n%s", jsonStr)
// Call the federatingDB function.
err := suite.intReq(
receiving,
requesting,
jsonStr,
func(ctx context.Context, req vocab.Type) error {
likeReq := req.(vocab.GoToSocialLikeRequest)
return suite.federatingDB.LikeRequest(ctx, likeReq)
},
)
if err != nil {
suite.FailNow(err.Error())
}
// There should be an interaction request in the DB now.
var intReq *gtsmodel.InteractionRequest
if !testrig.WaitFor(func() bool {
intReq, err = suite.state.DB.GetInteractionRequestByInteractionURI(ctx, intURI)
return err == nil && intReq != nil
}) {
suite.FailNow("timed out waiting for int req to appear in the db")
}
suite.Equal(testStatus.ID, intReq.TargetStatusID)
suite.Equal(receiving.ID, intReq.TargetAccountID)
suite.Equal(requesting.ID, intReq.InteractingAccountID)
suite.Equal(intReqURI, intReq.InteractionRequestURI)
suite.Equal(intURI, intReq.InteractionURI)
suite.Equal(gtsmodel.InteractionLike, intReq.InteractionType)
// The like should be in the DB now (unapproved).
var statusFave *gtsmodel.StatusFave
if !testrig.WaitFor(func() bool {
statusFave, err = suite.state.DB.GetStatusFaveByURI(ctx, intURI)
return err == nil && intReq != nil
}) {
suite.FailNow("timed out waiting for fave to appear in the db")
}
suite.Equal(requesting.ID, statusFave.AccountID)
suite.Equal(receiving.ID, statusFave.TargetAccountID)
suite.Equal(testStatus.ID, statusFave.StatusID)
suite.Equal(intURI, statusFave.URI)
suite.True(*statusFave.PendingApproval)
suite.Empty(statusFave.ApprovedByURI)
// Should be a message heading to the processor.
msg, _ := suite.getFederatorMsg(5 * time.Second)
suite.Equal(ap.ActivityCreate, msg.APActivityType)
suite.Equal(ap.ActivityLikeRequest, msg.APObjectType)
suite.NotNil(msg.GTSModel)
suite.NotNil(msg.Receiving)
suite.NotNil(msg.Requesting)
}
func (suite *InteractionRequestTestSuite) TestAnnounceRequest() {
var (
ctx = suite.T().Context()
receiving = suite.testAccounts["admin_account"]
requesting = suite.testAccounts["remote_account_1"]
testStatus = suite.testStatuses["admin_account_status_1"]
intReqURI = "http://fossbros-anonymous.io/requests/87fb1478-ac46-406a-8463-96ce05645219"
intURI = "http://fossbros-anonymous.io/users/foss_satan/statuses/87fb1478-ac46-406a-8463-96ce05645219"
jsonStr = `{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://gotosocial.org/ns"
],
"type": "AnnounceRequest",
"id": "` + intReqURI + `",
"actor": "` + requesting.URI + `",
"object": "` + testStatus.URI + `",
"to": "` + receiving.URI + `",
"instrument": {
"id": "` + intURI + `",
"object": "` + testStatus.URI + `",
"actor": "` + requesting.URI + `",
"to": "` + requesting.FollowersURI + `",
"cc": "` + receiving.URI + `",
"type": "Announce"
}
}`
)
suite.T().Logf("testing announce request:\n\n%s", jsonStr)
// Call the federatingDB function.
err := suite.intReq(
receiving,
requesting,
jsonStr,
func(ctx context.Context, req vocab.Type) error {
announceReq := req.(vocab.GoToSocialAnnounceRequest)
return suite.federatingDB.AnnounceRequest(ctx, announceReq)
},
)
if err != nil {
suite.FailNow(err.Error())
}
// There should be an interaction request in the DB now.
var intReq *gtsmodel.InteractionRequest
if !testrig.WaitFor(func() bool {
intReq, err = suite.state.DB.GetInteractionRequestByInteractionURI(ctx, intURI)
return err == nil && intReq != nil
}) {
suite.FailNow("timed out waiting for int req to appear in the db")
}
suite.Equal(testStatus.ID, intReq.TargetStatusID)
suite.Equal(receiving.ID, intReq.TargetAccountID)
suite.Equal(requesting.ID, intReq.InteractingAccountID)
suite.Equal(intReqURI, intReq.InteractionRequestURI)
suite.Equal(intURI, intReq.InteractionURI)
suite.Equal(gtsmodel.InteractionAnnounce, intReq.InteractionType)
// Should be a message heading to the processor.
msg, _ := suite.getFederatorMsg(5 * time.Second)
suite.Equal(ap.ActivityCreate, msg.APActivityType)
suite.Equal(ap.ActivityAnnounceRequest, msg.APObjectType)
suite.NotNil(msg.GTSModel)
suite.NotNil(msg.Receiving)
suite.NotNil(msg.Requesting)
}
func TestInteractionRequestTestSuite(t *testing.T) {
suite.Run(t, &InteractionRequestTestSuite{})
}