diff options
Diffstat (limited to 'internal/federation')
-rw-r--r-- | internal/federation/federatingdb/following.go | 18 | ||||
-rw-r--r-- | internal/federation/federatingdb/inbox_test.go | 56 | ||||
-rw-r--r-- | internal/federation/federatingprotocol_test.go | 122 | ||||
-rw-r--r-- | internal/federation/federator_test.go | 104 |
4 files changed, 201 insertions, 99 deletions
diff --git a/internal/federation/federatingdb/following.go b/internal/federation/federatingdb/following.go index 54d703742..62b148d5b 100644 --- a/internal/federation/federatingdb/following.go +++ b/internal/federation/federatingdb/following.go @@ -1,3 +1,21 @@ +/* + GoToSocial + Copyright (C) 2021-2022 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 <http://www.gnu.org/licenses/>. +*/ + package federatingdb import ( diff --git a/internal/federation/federatingdb/inbox_test.go b/internal/federation/federatingdb/inbox_test.go new file mode 100644 index 000000000..d5cc46b8b --- /dev/null +++ b/internal/federation/federatingdb/inbox_test.go @@ -0,0 +1,56 @@ +/* + GoToSocial + Copyright (C) 2021-2022 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 <http://www.gnu.org/licenses/>. +*/ + +package federatingdb_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type InboxTestSuite struct { + FederatingDBTestSuite +} + +func (suite *InboxTestSuite) TestInboxesForFollowersIRI() { + ctx := context.Background() + testAccount := suite.testAccounts["local_account_1"] + + inboxIRIs, err := suite.federatingDB.InboxesForIRI(ctx, testrig.URLMustParse(testAccount.FollowersURI)) + suite.NoError(err) + suite.Len(inboxIRIs, 2) + suite.Equal(inboxIRIs[0].String(), suite.testAccounts["local_account_2"].InboxURI) + suite.Equal(inboxIRIs[1].String(), suite.testAccounts["admin_account"].InboxURI) +} + +func (suite *InboxTestSuite) TestInboxesForAccountIRI() { + ctx := context.Background() + testAccount := suite.testAccounts["local_account_1"] + + inboxIRIs, err := suite.federatingDB.InboxesForIRI(ctx, testrig.URLMustParse(testAccount.URI)) + suite.NoError(err) + suite.Len(inboxIRIs, 1) + suite.Equal(inboxIRIs[0].String(), testAccount.InboxURI) +} + +func TestInboxTestSuite(t *testing.T) { + suite.Run(t, &InboxTestSuite{}) +} diff --git a/internal/federation/federatingprotocol_test.go b/internal/federation/federatingprotocol_test.go new file mode 100644 index 000000000..953ad348b --- /dev/null +++ b/internal/federation/federatingprotocol_test.go @@ -0,0 +1,122 @@ +/* + GoToSocial + Copyright (C) 2021-2022 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 <http://www.gnu.org/licenses/>. +*/ + +package federation_test + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-fed/httpsig" + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/activity/pub" + "github.com/superseriousbusiness/gotosocial/internal/ap" + "github.com/superseriousbusiness/gotosocial/internal/federation" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/messages" + "github.com/superseriousbusiness/gotosocial/internal/worker" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type FederatingProtocolTestSuite struct { + FederatorStandardTestSuite +} + +// make sure PostInboxRequestBodyHook properly sets the inbox username and activity on the context +func (suite *FederatingProtocolTestSuite) TestPostInboxRequestBodyHook() { + // the activity we're gonna use + activity := suite.activities["dm_for_zork"] + + fedWorker := worker.New[messages.FromFederator](-1, -1) + + // setup transport controller with a no-op client so we don't make external calls + tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(func(req *http.Request) (*http.Response, error) { + return nil, nil + }), suite.db, fedWorker) + // setup module being tested + federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db, fedWorker), tc, suite.typeConverter, testrig.NewTestMediaManager(suite.db, suite.storage)) + + // setup request + ctx := context.Background() + request := httptest.NewRequest(http.MethodPost, "http://localhost:8080/users/the_mighty_zork/inbox", nil) // the endpoint we're hitting + request.Header.Set("Signature", activity.SignatureHeader) + + // trigger the function being tested, and return the new context it creates + newContext, err := federator.PostInboxRequestBodyHook(ctx, request, activity.Activity) + suite.NoError(err) + suite.NotNil(newContext) + + // activity should be set on context now + activityI := newContext.Value(ap.ContextActivity) + suite.NotNil(activityI) + returnedActivity, ok := activityI.(pub.Activity) + suite.True(ok) + suite.NotNil(returnedActivity) + suite.EqualValues(activity.Activity, returnedActivity) +} + +func (suite *FederatingProtocolTestSuite) TestAuthenticatePostInbox() { + // the activity we're gonna use + activity := suite.activities["dm_for_zork"] + sendingAccount := suite.accounts["remote_account_1"] + inboxAccount := suite.accounts["local_account_1"] + + fedWorker := worker.New[messages.FromFederator](-1, -1) + + tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil), suite.db, fedWorker) + // now setup module being tested, with the mock transport controller + federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db, fedWorker), tc, suite.typeConverter, testrig.NewTestMediaManager(suite.db, suite.storage)) + + request := httptest.NewRequest(http.MethodPost, "http://localhost:8080/users/the_mighty_zork/inbox", nil) + // we need these headers for the request to be validated + request.Header.Set("Signature", activity.SignatureHeader) + request.Header.Set("Date", activity.DateHeader) + request.Header.Set("Digest", activity.DigestHeader) + + verifier, err := httpsig.NewVerifier(request) + suite.NoError(err) + + ctx := context.Background() + // by the time AuthenticatePostInbox is called, PostInboxRequestBodyHook should have already been called, + // which should have set the account and username onto the request. We can replicate that behavior here: + ctxWithAccount := context.WithValue(ctx, ap.ContextReceivingAccount, inboxAccount) + ctxWithActivity := context.WithValue(ctxWithAccount, ap.ContextActivity, activity) + ctxWithVerifier := context.WithValue(ctxWithActivity, ap.ContextRequestingPublicKeyVerifier, verifier) + ctxWithSignature := context.WithValue(ctxWithVerifier, ap.ContextRequestingPublicKeySignature, activity.SignatureHeader) + + // we can pass this recorder as a writer and read it back after + recorder := httptest.NewRecorder() + + // trigger the function being tested, and return the new context it creates + newContext, authed, err := federator.AuthenticatePostInbox(ctxWithSignature, recorder, request) + suite.NoError(err) + suite.True(authed) + + // since we know this account already it should be set on the context + requestingAccountI := newContext.Value(ap.ContextRequestingAccount) + suite.NotNil(requestingAccountI) + requestingAccount, ok := requestingAccountI.(*gtsmodel.Account) + suite.True(ok) + suite.Equal(sendingAccount.Username, requestingAccount.Username) +} + +func TestFederatingProtocolTestSuite(t *testing.T) { + suite.Run(t, new(FederatingProtocolTestSuite)) +} diff --git a/internal/federation/federator_test.go b/internal/federation/federator_test.go index 220c3a193..68ad606f8 100644 --- a/internal/federation/federator_test.go +++ b/internal/federation/federator_test.go @@ -19,28 +19,16 @@ package federation_test import ( - "context" - "net/http" - "net/http/httptest" - "testing" - "codeberg.org/gruf/go-store/kv" - "github.com/go-fed/httpsig" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "github.com/superseriousbusiness/activity/pub" - "github.com/superseriousbusiness/gotosocial/internal/ap" "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/federation" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/messages" "github.com/superseriousbusiness/gotosocial/internal/typeutils" - "github.com/superseriousbusiness/gotosocial/internal/worker" "github.com/superseriousbusiness/gotosocial/testrig" ) -type ProtocolTestSuite struct { +type FederatorStandardTestSuite struct { suite.Suite db db.DB storage *kv.KVStore @@ -50,104 +38,22 @@ type ProtocolTestSuite struct { } // SetupSuite sets some variables on the suite that we can use as consts (more or less) throughout -func (suite *ProtocolTestSuite) SetupSuite() { +func (suite *FederatorStandardTestSuite) SetupSuite() { // setup standard items suite.storage = testrig.NewTestStorage() suite.typeConverter = testrig.NewTestTypeConverter(suite.db) suite.accounts = testrig.NewTestAccounts() } -func (suite *ProtocolTestSuite) SetupTest() { - testrig.InitTestLog() +func (suite *FederatorStandardTestSuite) SetupTest() { testrig.InitTestConfig() + testrig.InitTestLog() suite.db = testrig.NewTestDB() suite.activities = testrig.NewTestActivities(suite.accounts) testrig.StandardDBSetup(suite.db, suite.accounts) } // TearDownTest drops tables to make sure there's no data in the db -func (suite *ProtocolTestSuite) TearDownTest() { +func (suite *FederatorStandardTestSuite) TearDownTest() { testrig.StandardDBTeardown(suite.db) } - -// make sure PostInboxRequestBodyHook properly sets the inbox username and activity on the context -func (suite *ProtocolTestSuite) TestPostInboxRequestBodyHook() { - // the activity we're gonna use - activity := suite.activities["dm_for_zork"] - - fedWorker := worker.New[messages.FromFederator](-1, -1) - - // setup transport controller with a no-op client so we don't make external calls - tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(func(req *http.Request) (*http.Response, error) { - return nil, nil - }), suite.db, fedWorker) - // setup module being tested - federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db, fedWorker), tc, suite.typeConverter, testrig.NewTestMediaManager(suite.db, suite.storage)) - - // setup request - ctx := context.Background() - request := httptest.NewRequest(http.MethodPost, "http://localhost:8080/users/the_mighty_zork/inbox", nil) // the endpoint we're hitting - request.Header.Set("Signature", activity.SignatureHeader) - - // trigger the function being tested, and return the new context it creates - newContext, err := federator.PostInboxRequestBodyHook(ctx, request, activity.Activity) - assert.NoError(suite.T(), err) - assert.NotNil(suite.T(), newContext) - - // activity should be set on context now - activityI := newContext.Value(ap.ContextActivity) - assert.NotNil(suite.T(), activityI) - returnedActivity, ok := activityI.(pub.Activity) - assert.True(suite.T(), ok) - assert.NotNil(suite.T(), returnedActivity) - assert.EqualValues(suite.T(), activity.Activity, returnedActivity) -} - -func (suite *ProtocolTestSuite) TestAuthenticatePostInbox() { - // the activity we're gonna use - activity := suite.activities["dm_for_zork"] - sendingAccount := suite.accounts["remote_account_1"] - inboxAccount := suite.accounts["local_account_1"] - - fedWorker := worker.New[messages.FromFederator](-1, -1) - - tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil), suite.db, fedWorker) - // now setup module being tested, with the mock transport controller - federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db, fedWorker), tc, suite.typeConverter, testrig.NewTestMediaManager(suite.db, suite.storage)) - - request := httptest.NewRequest(http.MethodPost, "http://localhost:8080/users/the_mighty_zork/inbox", nil) - // we need these headers for the request to be validated - request.Header.Set("Signature", activity.SignatureHeader) - request.Header.Set("Date", activity.DateHeader) - request.Header.Set("Digest", activity.DigestHeader) - - verifier, err := httpsig.NewVerifier(request) - assert.NoError(suite.T(), err) - - ctx := context.Background() - // by the time AuthenticatePostInbox is called, PostInboxRequestBodyHook should have already been called, - // which should have set the account and username onto the request. We can replicate that behavior here: - ctxWithAccount := context.WithValue(ctx, ap.ContextReceivingAccount, inboxAccount) - ctxWithActivity := context.WithValue(ctxWithAccount, ap.ContextActivity, activity) - ctxWithVerifier := context.WithValue(ctxWithActivity, ap.ContextRequestingPublicKeyVerifier, verifier) - ctxWithSignature := context.WithValue(ctxWithVerifier, ap.ContextRequestingPublicKeySignature, activity.SignatureHeader) - - // we can pass this recorder as a writer and read it back after - recorder := httptest.NewRecorder() - - // trigger the function being tested, and return the new context it creates - newContext, authed, err := federator.AuthenticatePostInbox(ctxWithSignature, recorder, request) - assert.NoError(suite.T(), err) - assert.True(suite.T(), authed) - - // since we know this account already it should be set on the context - requestingAccountI := newContext.Value(ap.ContextRequestingAccount) - assert.NotNil(suite.T(), requestingAccountI) - requestingAccount, ok := requestingAccountI.(*gtsmodel.Account) - assert.True(suite.T(), ok) - assert.Equal(suite.T(), sendingAccount.Username, requestingAccount.Username) -} - -func TestProtocolTestSuite(t *testing.T) { - suite.Run(t, new(ProtocolTestSuite)) -} |