| 
									
										
										
										
											2023-03-12 16:00:57 +01:00
										 |  |  | // 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/>. | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | package processing_test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2024-04-11 10:45:35 +01:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-26 15:34:10 +02:00
										 |  |  | 	apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/gtsmodel" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/testrig" | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 	"github.com/stretchr/testify/suite" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | // TODO: move this to the "internal/processing/account" pkg | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | type FollowRequestTestSuite struct { | 
					
						
							|  |  |  | 	ProcessingStandardTestSuite | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *FollowRequestTestSuite) TestFollowRequestAccept() { | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 	// The authed local account we are going to use for HTTP requests | 
					
						
							|  |  |  | 	requestingAccount := suite.testAccounts["local_account_1"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// The remote account whose follow request we are accepting | 
					
						
							|  |  |  | 	targetAccount := suite.testAccounts["remote_account_2"] | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// put a follow request in the database | 
					
						
							|  |  |  | 	fr := >smodel.FollowRequest{ | 
					
						
							|  |  |  | 		ID:              "01FJ1S8DX3STJJ6CEYPMZ1M0R3", | 
					
						
							|  |  |  | 		CreatedAt:       time.Now(), | 
					
						
							|  |  |  | 		UpdatedAt:       time.Now(), | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 		URI:             fmt.Sprintf("%s/follow/01FJ1S8DX3STJJ6CEYPMZ1M0R3", targetAccount.URI), | 
					
						
							|  |  |  | 		AccountID:       targetAccount.ID, | 
					
						
							|  |  |  | 		TargetAccountID: requestingAccount.ID, | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-22 12:26:11 +02:00
										 |  |  | 	err := suite.db.Put(suite.T().Context(), fr) | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 	suite.NoError(err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 	relationship, errWithCode := suite.processor.Account().FollowRequestAccept( | 
					
						
							| 
									
										
										
										
											2025-05-22 12:26:11 +02:00
										 |  |  | 		suite.T().Context(), | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 		requestingAccount, | 
					
						
							|  |  |  | 		targetAccount.ID, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 	suite.NoError(errWithCode) | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 	suite.EqualValues(&apimodel.Relationship{ | 
					
						
							|  |  |  | 		ID:                  "01FHMQX3GAABWSM0S2VZEC2SWC", | 
					
						
							|  |  |  | 		Following:           false, | 
					
						
							|  |  |  | 		ShowingReblogs:      false, | 
					
						
							|  |  |  | 		Notifying:           false, | 
					
						
							|  |  |  | 		FollowedBy:          true, | 
					
						
							|  |  |  | 		Blocking:            false, | 
					
						
							|  |  |  | 		BlockedBy:           false, | 
					
						
							|  |  |  | 		Muting:              false, | 
					
						
							|  |  |  | 		MutingNotifications: false, | 
					
						
							|  |  |  | 		Requested:           false, | 
					
						
							|  |  |  | 		DomainBlocking:      false, | 
					
						
							|  |  |  | 		Endorsed:            false, | 
					
						
							|  |  |  | 		Note:                "", | 
					
						
							|  |  |  | 	}, relationship) | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	accept := &struct { | 
					
						
							|  |  |  | 		Actor  string `json:"actor"` | 
					
						
							|  |  |  | 		ID     string `json:"id"` | 
					
						
							|  |  |  | 		Object struct { | 
					
						
							|  |  |  | 			Actor  string `json:"actor"` | 
					
						
							|  |  |  | 			ID     string `json:"id"` | 
					
						
							|  |  |  | 			Object string `json:"object"` | 
					
						
							|  |  |  | 			To     string `json:"to"` | 
					
						
							|  |  |  | 			Type   string `json:"type"` | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		To   string `json:"to"` | 
					
						
							|  |  |  | 		Type string `json:"type"` | 
					
						
							|  |  |  | 	}{} | 
					
						
							| 
									
										
										
										
											2024-04-11 10:45:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// accept should be sent to Some_User | 
					
						
							|  |  |  | 	var sent []byte | 
					
						
							|  |  |  | 	if !testrig.WaitFor(func() bool { | 
					
						
							|  |  |  | 		delivery, ok := suite.state.Workers.Delivery.Queue.Pop() | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !testrig.EqualRequestURIs(delivery.Request.URL, targetAccount.InboxURI) { | 
					
						
							|  |  |  | 			panic("differing request uris") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		sent, err = io.ReadAll(delivery.Request.Body) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			panic("error reading body: " + err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		err = json.Unmarshal(sent, accept) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			panic("error unmarshaling json: " + err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return true | 
					
						
							|  |  |  | 	}) { | 
					
						
							|  |  |  | 		suite.FailNow("timed out waiting for message") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 	suite.Equal(requestingAccount.URI, accept.Actor) | 
					
						
							|  |  |  | 	suite.Equal(targetAccount.URI, accept.Object.Actor) | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 	suite.Equal(fr.URI, accept.Object.ID) | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 	suite.Equal(requestingAccount.URI, accept.Object.Object) | 
					
						
							|  |  |  | 	suite.Equal(requestingAccount.URI, accept.Object.To) | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 	suite.Equal("Follow", accept.Object.Type) | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 	suite.Equal(targetAccount.URI, accept.To) | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 	suite.Equal("Accept", accept.Type) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *FollowRequestTestSuite) TestFollowRequestReject() { | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 	requestingAccount := suite.testAccounts["local_account_1"] | 
					
						
							|  |  |  | 	targetAccount := suite.testAccounts["remote_account_2"] | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// put a follow request in the database | 
					
						
							|  |  |  | 	fr := >smodel.FollowRequest{ | 
					
						
							|  |  |  | 		ID:              "01FJ1S8DX3STJJ6CEYPMZ1M0R3", | 
					
						
							|  |  |  | 		CreatedAt:       time.Now(), | 
					
						
							|  |  |  | 		UpdatedAt:       time.Now(), | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 		URI:             fmt.Sprintf("%s/follow/01FJ1S8DX3STJJ6CEYPMZ1M0R3", targetAccount.URI), | 
					
						
							|  |  |  | 		AccountID:       targetAccount.ID, | 
					
						
							|  |  |  | 		TargetAccountID: requestingAccount.ID, | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-22 12:26:11 +02:00
										 |  |  | 	err := suite.db.Put(suite.T().Context(), fr) | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 	suite.NoError(err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 	relationship, errWithCode := suite.processor.Account().FollowRequestReject( | 
					
						
							| 
									
										
										
										
											2025-05-22 12:26:11 +02:00
										 |  |  | 		suite.T().Context(), | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 		requestingAccount, | 
					
						
							|  |  |  | 		targetAccount.ID, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 	suite.NoError(errWithCode) | 
					
						
							|  |  |  | 	suite.EqualValues(&apimodel.Relationship{ID: "01FHMQX3GAABWSM0S2VZEC2SWC", Following: false, ShowingReblogs: false, Notifying: false, FollowedBy: false, Blocking: false, BlockedBy: false, Muting: false, MutingNotifications: false, Requested: false, DomainBlocking: false, Endorsed: false, Note: ""}, relationship) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	reject := &struct { | 
					
						
							|  |  |  | 		Actor  string `json:"actor"` | 
					
						
							|  |  |  | 		ID     string `json:"id"` | 
					
						
							|  |  |  | 		Object struct { | 
					
						
							|  |  |  | 			Actor  string `json:"actor"` | 
					
						
							|  |  |  | 			ID     string `json:"id"` | 
					
						
							|  |  |  | 			Object string `json:"object"` | 
					
						
							|  |  |  | 			To     string `json:"to"` | 
					
						
							|  |  |  | 			Type   string `json:"type"` | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		To   string `json:"to"` | 
					
						
							|  |  |  | 		Type string `json:"type"` | 
					
						
							|  |  |  | 	}{} | 
					
						
							| 
									
										
										
										
											2024-04-11 10:45:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// reject should be sent to Some_User | 
					
						
							|  |  |  | 	var sent []byte | 
					
						
							|  |  |  | 	if !testrig.WaitFor(func() bool { | 
					
						
							|  |  |  | 		delivery, ok := suite.state.Workers.Delivery.Queue.Pop() | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !testrig.EqualRequestURIs(delivery.Request.URL, targetAccount.InboxURI) { | 
					
						
							|  |  |  | 			panic("differing request uris") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		sent, err = io.ReadAll(delivery.Request.Body) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			panic("error reading body: " + err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		err = json.Unmarshal(sent, reject) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			panic("error unmarshaling json: " + err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return true | 
					
						
							|  |  |  | 	}) { | 
					
						
							|  |  |  | 		suite.FailNow("timed out waiting for message") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 	suite.Equal(requestingAccount.URI, reject.Actor) | 
					
						
							|  |  |  | 	suite.Equal(targetAccount.URI, reject.Object.Actor) | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 	suite.Equal(fr.URI, reject.Object.ID) | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 	suite.Equal(requestingAccount.URI, reject.Object.Object) | 
					
						
							|  |  |  | 	suite.Equal(requestingAccount.URI, reject.Object.To) | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 	suite.Equal("Follow", reject.Object.Type) | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 	suite.Equal(targetAccount.URI, reject.To) | 
					
						
							| 
									
										
										
										
											2021-10-16 13:27:43 +02:00
										 |  |  | 	suite.Equal("Reject", reject.Type) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestFollowRequestTestSuite(t *testing.T) { | 
					
						
							|  |  |  | 	suite.Run(t, &FollowRequestTestSuite{}) | 
					
						
							|  |  |  | } |