summaryrefslogtreecommitdiff
path: root/internal/processing
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2021-09-04 13:29:56 +0200
committerLibravatar GitHub <noreply@github.com>2021-09-04 13:29:56 +0200
commitff05046df7c0ce21f70b0dd8dce59dd5e01771de (patch)
tree227720dc9ca30da106e508108eba08426e8bd2ee /internal/processing
parentMerge pull request #186 from superseriousbusiness/struct_validation (diff)
downloadgotosocial-ff05046df7c0ce21f70b0dd8dce59dd5e01771de.tar.xz
tests + announce notification fix (#193)
Diffstat (limited to 'internal/processing')
-rw-r--r--internal/processing/fromclientapi.go2
-rw-r--r--internal/processing/fromcommon.go14
-rw-r--r--internal/processing/fromfederator.go6
-rw-r--r--internal/processing/fromfederator_test.go86
-rw-r--r--internal/processing/notification_test.go47
-rw-r--r--internal/processing/processor.go8
-rw-r--r--internal/processing/processor_test.go124
7 files changed, 272 insertions, 15 deletions
diff --git a/internal/processing/fromclientapi.go b/internal/processing/fromclientapi.go
index 97c6cc8b2..5f1ed325b 100644
--- a/internal/processing/fromclientapi.go
+++ b/internal/processing/fromclientapi.go
@@ -31,7 +31,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/messages"
)
-func (p *processor) processFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
+func (p *processor) ProcessFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
switch clientMsg.APActivityType {
case ap.ActivityCreate:
// CREATE
diff --git a/internal/processing/fromcommon.go b/internal/processing/fromcommon.go
index b7a6defc3..613ad5fca 100644
--- a/internal/processing/fromcommon.go
+++ b/internal/processing/fromcommon.go
@@ -61,17 +61,15 @@ func (p *processor) notifyStatus(ctx context.Context, status *gtsmodel.Status) e
}
// make sure a notif doesn't already exist for this mention
- err := p.db.GetWhere(ctx, []db.Where{
+ if err := p.db.GetWhere(ctx, []db.Where{
{Key: "notification_type", Value: gtsmodel.NotificationMention},
{Key: "target_account_id", Value: m.TargetAccountID},
- {Key: "origin_account_id", Value: status.AccountID},
- {Key: "status_id", Value: status.ID},
- }, &gtsmodel.Notification{})
- if err == nil {
+ {Key: "origin_account_id", Value: m.OriginAccountID},
+ {Key: "status_id", Value: m.StatusID},
+ }, &gtsmodel.Notification{}); err == nil {
// notification exists already so just continue
continue
- }
- if err != db.ErrNoEntries {
+ } else if err != db.ErrNoEntries {
// there's a real error in the db
return fmt.Errorf("notifyStatus: error checking existence of notification for mention with id %s : %s", m.ID, err)
}
@@ -254,7 +252,7 @@ func (p *processor) notifyAnnounce(ctx context.Context, status *gtsmodel.Status)
status.BoostOfAccount = boostedAcct
}
- if status.BoostOfAccount.Domain == "" {
+ if status.BoostOfAccount.Domain != "" {
// remote account, nothing to do
return nil
}
diff --git a/internal/processing/fromfederator.go b/internal/processing/fromfederator.go
index d2e949cef..1bf841bba 100644
--- a/internal/processing/fromfederator.go
+++ b/internal/processing/fromfederator.go
@@ -32,7 +32,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/messages"
)
-func (p *processor) processFromFederator(ctx context.Context, federatorMsg messages.FromFederator) error {
+func (p *processor) ProcessFromFederator(ctx context.Context, federatorMsg messages.FromFederator) error {
l := p.log.WithFields(logrus.Fields{
"func": "processFromFederator",
"federatorMsg": fmt.Sprintf("%+v", federatorMsg),
@@ -104,9 +104,7 @@ func (p *processor) processFromFederator(ctx context.Context, federatorMsg messa
incomingAnnounce.ID = incomingAnnounceID
if err := p.db.PutStatus(ctx, incomingAnnounce); err != nil {
- if err != db.ErrNoEntries {
- return fmt.Errorf("error adding dereferenced announce to the db: %s", err)
- }
+ return fmt.Errorf("error adding dereferenced announce to the db: %s", err)
}
if err := p.timelineStatus(ctx, incomingAnnounce); err != nil {
diff --git a/internal/processing/fromfederator_test.go b/internal/processing/fromfederator_test.go
new file mode 100644
index 000000000..598af5337
--- /dev/null
+++ b/internal/processing/fromfederator_test.go
@@ -0,0 +1,86 @@
+/*
+ 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 <http://www.gnu.org/licenses/>.
+*/
+
+package processing_test
+
+import (
+ "context"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/suite"
+ "github.com/superseriousbusiness/gotosocial/internal/ap"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/messages"
+)
+
+type FromFederatorTestSuite struct {
+ ProcessingStandardTestSuite
+}
+
+// remote_account_1 boosts the first status of local_account_1
+func (suite *FromFederatorTestSuite) TestProcessFederationAnnounce() {
+ boostedStatus := suite.testStatuses["local_account_1_status_1"]
+ boostingAccount := suite.testAccounts["remote_account_1"]
+ announceStatus := &gtsmodel.Status{}
+ announceStatus.URI = "https://example.org/some-announce-uri"
+ announceStatus.BoostOf = &gtsmodel.Status{
+ URI: boostedStatus.URI,
+ }
+ announceStatus.CreatedAt = time.Now()
+ announceStatus.UpdatedAt = time.Now()
+ announceStatus.AccountID = boostingAccount.ID
+ announceStatus.AccountURI = boostingAccount.URI
+ announceStatus.Account = boostingAccount
+ announceStatus.Visibility = boostedStatus.Visibility
+
+ err := suite.processor.ProcessFromFederator(context.Background(), messages.FromFederator{
+ APObjectType: ap.ActivityAnnounce,
+ APActivityType: ap.ActivityCreate,
+ GTSModel: announceStatus,
+ ReceivingAccount: suite.testAccounts["local_account_1"],
+ })
+ suite.NoError(err)
+
+ // side effects should be triggered
+ // 1. status should have an ID, and be in the database
+ suite.NotEmpty(announceStatus.ID)
+ _, err = suite.db.GetStatusByID(context.Background(), announceStatus.ID)
+ suite.NoError(err)
+
+ // 2. a notification should exist for the announce
+ where := []db.Where{
+ {
+ Key: "status_id",
+ Value: announceStatus.ID,
+ },
+ }
+ notif := &gtsmodel.Notification{}
+ err = suite.db.GetWhere(context.Background(), where, notif)
+ suite.NoError(err)
+ suite.Equal(gtsmodel.NotificationReblog, notif.NotificationType)
+ suite.Equal(boostedStatus.AccountID, notif.TargetAccountID)
+ suite.Equal(announceStatus.AccountID, notif.OriginAccountID)
+ suite.Equal(announceStatus.ID, notif.StatusID)
+ suite.False(notif.Read)
+}
+
+func TestFromFederatorTestSuite(t *testing.T) {
+ suite.Run(t, &FromFederatorTestSuite{})
+}
diff --git a/internal/processing/notification_test.go b/internal/processing/notification_test.go
new file mode 100644
index 000000000..b50920482
--- /dev/null
+++ b/internal/processing/notification_test.go
@@ -0,0 +1,47 @@
+/*
+ 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 <http://www.gnu.org/licenses/>.
+*/
+
+package processing_test
+
+import (
+ "context"
+ "testing"
+
+ "github.com/stretchr/testify/suite"
+)
+
+type NotificationTestSuite struct {
+ ProcessingStandardTestSuite
+}
+
+// get a notification where someone has liked our status
+func (suite *NotificationTestSuite) TestGetNotifications() {
+ receivingAccount := suite.testAccounts["local_account_1"]
+ notifs, err := suite.processor.NotificationsGet(context.Background(), suite.testAutheds["local_account_1"], 10, "", "")
+ suite.NoError(err)
+ suite.Len(notifs, 1)
+ notif := notifs[0]
+ suite.NotNil(notif.Status)
+ suite.NotNil(notif.Status)
+ suite.NotNil(notif.Status.Account)
+ suite.Equal(receivingAccount.ID, notif.Status.Account.ID)
+}
+
+func TestNotificationTestSuite(t *testing.T) {
+ suite.Run(t, &NotificationTestSuite{})
+}
diff --git a/internal/processing/processor.go b/internal/processing/processor.go
index 38076123f..5dcdca152 100644
--- a/internal/processing/processor.go
+++ b/internal/processing/processor.go
@@ -56,6 +56,10 @@ type Processor interface {
Start(ctx context.Context) error
// Stop stops the processor cleanly, finishing handling any remaining messages before closing down.
Stop() error
+ // ProcessFromClientAPI processes one message coming from the clientAPI channel, and triggers appropriate side effects.
+ ProcessFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error
+ // ProcessFromFederator processes one message coming from the federator channel, and triggers appropriate side effects.
+ ProcessFromFederator(ctx context.Context, federatorMsg messages.FromFederator) error
/*
CLIENT API-FACING PROCESSING FUNCTIONS
@@ -290,14 +294,14 @@ func (p *processor) Start(ctx context.Context) error {
case clientMsg := <-p.fromClientAPI:
p.log.Tracef("received message FROM client API: %+v", clientMsg)
go func() {
- if err := p.processFromClientAPI(ctx, clientMsg); err != nil {
+ if err := p.ProcessFromClientAPI(ctx, clientMsg); err != nil {
p.log.Error(err)
}
}()
case federatorMsg := <-p.fromFederator:
p.log.Tracef("received message FROM federator: %+v", federatorMsg)
go func() {
- if err := p.processFromFederator(ctx, federatorMsg); err != nil {
+ if err := p.ProcessFromFederator(ctx, federatorMsg); err != nil {
p.log.Error(err)
}
}()
diff --git a/internal/processing/processor_test.go b/internal/processing/processor_test.go
new file mode 100644
index 000000000..7335b686d
--- /dev/null
+++ b/internal/processing/processor_test.go
@@ -0,0 +1,124 @@
+/*
+ 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 <http://www.gnu.org/licenses/>.
+*/
+
+package processing_test
+
+import (
+ "context"
+
+ "github.com/sirupsen/logrus"
+ "github.com/stretchr/testify/suite"
+ "github.com/superseriousbusiness/gotosocial/internal/blob"
+ "github.com/superseriousbusiness/gotosocial/internal/config"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
+ "github.com/superseriousbusiness/gotosocial/internal/federation"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/media"
+ "github.com/superseriousbusiness/gotosocial/internal/oauth"
+ "github.com/superseriousbusiness/gotosocial/internal/processing"
+ "github.com/superseriousbusiness/gotosocial/internal/timeline"
+ "github.com/superseriousbusiness/gotosocial/internal/transport"
+ "github.com/superseriousbusiness/gotosocial/internal/typeutils"
+ "github.com/superseriousbusiness/gotosocial/testrig"
+)
+
+type ProcessingStandardTestSuite struct {
+ // standard suite interfaces
+ suite.Suite
+ config *config.Config
+ db db.DB
+ log *logrus.Logger
+ storage blob.Storage
+ typeconverter typeutils.TypeConverter
+ transportController transport.Controller
+ federator federation.Federator
+ oauthServer oauth.Server
+ mediaHandler media.Handler
+ timelineManager timeline.Manager
+
+ // standard suite models
+ testTokens map[string]*gtsmodel.Token
+ testClients map[string]*gtsmodel.Client
+ testApplications map[string]*gtsmodel.Application
+ testUsers map[string]*gtsmodel.User
+ testAccounts map[string]*gtsmodel.Account
+ testAttachments map[string]*gtsmodel.MediaAttachment
+ testStatuses map[string]*gtsmodel.Status
+ testTags map[string]*gtsmodel.Tag
+ testMentions map[string]*gtsmodel.Mention
+ testAutheds map[string]*oauth.Auth
+
+ processor processing.Processor
+}
+
+func (suite *ProcessingStandardTestSuite) SetupSuite() {
+ suite.testTokens = testrig.NewTestTokens()
+ suite.testClients = testrig.NewTestClients()
+ suite.testApplications = testrig.NewTestApplications()
+ suite.testUsers = testrig.NewTestUsers()
+ suite.testAccounts = testrig.NewTestAccounts()
+ suite.testAttachments = testrig.NewTestAttachments()
+ suite.testStatuses = testrig.NewTestStatuses()
+ suite.testTags = testrig.NewTestTags()
+ suite.testMentions = testrig.NewTestMentions()
+ suite.testAutheds = map[string]*oauth.Auth{
+ "local_account_1": {
+ Application: suite.testApplications["local_account_1"],
+ User: suite.testUsers["local_account_1"],
+ Account: suite.testAccounts["local_account_1"],
+ },
+ }
+}
+
+func (suite *ProcessingStandardTestSuite) SetupTest() {
+ suite.config = testrig.NewTestConfig()
+ suite.db = testrig.NewTestDB()
+ suite.log = testrig.NewTestLog()
+ suite.storage = testrig.NewTestStorage()
+ suite.typeconverter = testrig.NewTestTypeConverter(suite.db)
+ suite.transportController = testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil), suite.db)
+ suite.federator = testrig.NewTestFederator(suite.db, suite.transportController, suite.storage)
+ suite.oauthServer = testrig.NewTestOauthServer(suite.db)
+ suite.mediaHandler = testrig.NewTestMediaHandler(suite.db, suite.storage)
+ suite.timelineManager = testrig.NewTestTimelineManager(suite.db)
+
+ suite.processor = processing.NewProcessor(
+ suite.config,
+ suite.typeconverter,
+ suite.federator,
+ suite.oauthServer,
+ suite.mediaHandler,
+ suite.storage,
+ suite.timelineManager,
+ suite.db,
+ suite.log)
+
+ testrig.StandardDBSetup(suite.db, suite.testAccounts)
+ testrig.StandardStorageSetup(suite.storage, "../../testrig/media")
+ if err := suite.processor.Start(context.Background()); err != nil {
+ panic(err)
+ }
+}
+
+func (suite *ProcessingStandardTestSuite) TearDownTest() {
+ testrig.StandardDBTeardown(suite.db)
+ testrig.StandardStorageTeardown(suite.storage)
+ if err := suite.processor.Stop(); err != nil {
+ panic(err)
+ }
+}