From 4f4a0028463759a2b33dad361460c23e127450a1 Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Fri, 15 Oct 2021 14:44:34 +0200 Subject: [PATCH] processor test --- internal/processing/followrequest_test.go | 143 ++++++++++++++++++++++ internal/processing/processor_test.go | 34 ++++- 2 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 internal/processing/followrequest_test.go diff --git a/internal/processing/followrequest_test.go b/internal/processing/followrequest_test.go new file mode 100644 index 000000000..12dcd80aa --- /dev/null +++ b/internal/processing/followrequest_test.go @@ -0,0 +1,143 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + 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 . +*/ + +package processing_test + +import ( + "context" + "encoding/json" + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/suite" + apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" +) + +type FollowRequestTestSuite struct { + ProcessingStandardTestSuite +} + +func (suite *FollowRequestTestSuite) TestFollowRequestAccept() { + requestingAccount := suite.testAccounts["remote_account_2"] + targetAccount := suite.testAccounts["local_account_1"] + + // put a follow request in the database + fr := >smodel.FollowRequest{ + ID: "01FJ1S8DX3STJJ6CEYPMZ1M0R3", + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + URI: fmt.Sprintf("%s/follow/01FJ1S8DX3STJJ6CEYPMZ1M0R3", requestingAccount.URI), + AccountID: requestingAccount.ID, + TargetAccountID: targetAccount.ID, + } + + err := suite.db.Put(context.Background(), fr) + suite.NoError(err) + + relationship, errWithCode := suite.processor.FollowRequestAccept(context.Background(), suite.testAutheds["local_account_1"], requestingAccount.ID) + suite.NoError(errWithCode) + 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) + time.Sleep(10 * time.Millisecond) + + // accept should be sent to some_user + sent, ok := suite.sentHTTPRequests[requestingAccount.InboxURI] + suite.True(ok) + + 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"` + }{} + err = json.Unmarshal(sent, accept) + suite.NoError(err) + + suite.Equal(targetAccount.URI, accept.Actor) + suite.Equal(requestingAccount.URI, accept.Object.Actor) + suite.Equal(fr.URI, accept.Object.ID) + suite.Equal(targetAccount.URI, accept.Object.Object) + suite.Equal(targetAccount.URI, accept.Object.To) + suite.Equal("Follow", accept.Object.Type) + suite.Equal(requestingAccount.URI, accept.To) + suite.Equal("Accept", accept.Type) +} + +func (suite *FollowRequestTestSuite) TestFollowRequestReject() { + requestingAccount := suite.testAccounts["remote_account_2"] + targetAccount := suite.testAccounts["local_account_1"] + + // put a follow request in the database + fr := >smodel.FollowRequest{ + ID: "01FJ1S8DX3STJJ6CEYPMZ1M0R3", + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + URI: fmt.Sprintf("%s/follow/01FJ1S8DX3STJJ6CEYPMZ1M0R3", requestingAccount.URI), + AccountID: requestingAccount.ID, + TargetAccountID: targetAccount.ID, + } + + err := suite.db.Put(context.Background(), fr) + suite.NoError(err) + + relationship, errWithCode := suite.processor.FollowRequestReject(context.Background(), suite.testAutheds["local_account_1"], requestingAccount.ID) + 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) + time.Sleep(10 * time.Millisecond) + + // reject should be sent to some_user + sent, ok := suite.sentHTTPRequests[requestingAccount.InboxURI] + suite.True(ok) + + 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"` + }{} + err = json.Unmarshal(sent, reject) + suite.NoError(err) + + suite.Equal(targetAccount.URI, reject.Actor) + suite.Equal(requestingAccount.URI, reject.Object.Actor) + suite.Equal(fr.URI, reject.Object.ID) + suite.Equal(targetAccount.URI, reject.Object.Object) + suite.Equal(targetAccount.URI, reject.Object.To) + suite.Equal("Follow", reject.Object.Type) + suite.Equal(requestingAccount.URI, reject.To) + suite.Equal("Reject", reject.Type) +} + +func TestFollowRequestTestSuite(t *testing.T) { + suite.Run(t, &FollowRequestTestSuite{}) +} diff --git a/internal/processing/processor_test.go b/internal/processing/processor_test.go index f2a9e455e..beae5dba0 100644 --- a/internal/processing/processor_test.go +++ b/internal/processing/processor_test.go @@ -96,9 +96,9 @@ func (suite *ProcessingStandardTestSuite) SetupSuite() { } func (suite *ProcessingStandardTestSuite) SetupTest() { + testrig.InitTestLog() suite.config = testrig.NewTestConfig() suite.db = testrig.NewTestDB() - testrig.InitTestLog() suite.storage = testrig.NewTestStorage() suite.typeconverter = testrig.NewTestTypeConverter(suite.db) @@ -149,6 +149,38 @@ func (suite *ProcessingStandardTestSuite) SetupTest() { return response, nil } + if req.URL.String() == suite.testAccounts["remote_account_2"].URI { + // the request is for remote account 2 + someAccount := suite.testAccounts["remote_account_2"] + + someAccountAS, err := suite.typeconverter.AccountToAS(context.Background(), someAccount) + if err != nil { + panic(err) + } + + someAccountI, err := streams.Serialize(someAccountAS) + if err != nil { + panic(err) + } + someAccountJson, err := json.Marshal(someAccountI) + if err != nil { + panic(err) + } + responseType := "application/activity+json" + + reader := bytes.NewReader(someAccountJson) + readCloser := io.NopCloser(reader) + response := &http.Response{ + StatusCode: 200, + Body: readCloser, + ContentLength: int64(len(someAccountJson)), + Header: http.Header{ + "content-type": {responseType}, + }, + } + return response, nil + } + if req.URL.String() == "http://example.org/users/some_user/statuses/afaba698-5740-4e32-a702-af61aa543bc1" { // the request is for the forwarded message message := suite.testActivities["forwarded_message"].Activity.GetActivityStreamsObject().At(0).GetActivityStreamsNote()