summaryrefslogtreecommitdiff
path: root/internal/processing
diff options
context:
space:
mode:
Diffstat (limited to 'internal/processing')
-rw-r--r--internal/processing/account/account_test.go97
-rw-r--r--internal/processing/account/update.go16
-rw-r--r--internal/processing/account/update_test.go75
-rw-r--r--internal/processing/admin/createdomainblock.go2
-rw-r--r--internal/processing/admin/deletedomainblock.go2
-rw-r--r--internal/processing/instance.go2
-rw-r--r--internal/processing/media/update.go4
7 files changed, 185 insertions, 13 deletions
diff --git a/internal/processing/account/account_test.go b/internal/processing/account/account_test.go
new file mode 100644
index 000000000..1884f6057
--- /dev/null
+++ b/internal/processing/account/account_test.go
@@ -0,0 +1,97 @@
+/*
+ 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 account_test
+
+import (
+ "github.com/go-fed/activity/pub"
+ "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/messages"
+ "github.com/superseriousbusiness/gotosocial/internal/oauth"
+ "github.com/superseriousbusiness/gotosocial/internal/processing/account"
+ "github.com/superseriousbusiness/gotosocial/internal/transport"
+ "github.com/superseriousbusiness/gotosocial/internal/typeutils"
+ "github.com/superseriousbusiness/gotosocial/testrig"
+)
+
+type AccountStandardTestSuite struct {
+ // standard suite interfaces
+ suite.Suite
+ config *config.Config
+ db db.DB
+ log *logrus.Logger
+ tc typeutils.TypeConverter
+ storage blob.Storage
+ mediaHandler media.Handler
+ oauthServer oauth.Server
+ fromClientAPIChan chan messages.FromClientAPI
+ httpClient pub.HttpClient
+ transportController transport.Controller
+ federator federation.Federator
+
+ // 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
+
+ // module being tested
+ accountProcessor account.Processor
+}
+
+func (suite *AccountStandardTestSuite) 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()
+}
+
+func (suite *AccountStandardTestSuite) SetupTest() {
+ suite.config = testrig.NewTestConfig()
+ suite.db = testrig.NewTestDB()
+ suite.log = testrig.NewTestLog()
+ suite.tc = testrig.NewTestTypeConverter(suite.db)
+ suite.storage = testrig.NewTestStorage()
+ suite.mediaHandler = testrig.NewTestMediaHandler(suite.db, suite.storage)
+ suite.oauthServer = testrig.NewTestOauthServer(suite.db)
+ suite.fromClientAPIChan = make(chan messages.FromClientAPI, 100)
+ suite.httpClient = testrig.NewMockHTTPClient(nil)
+ suite.transportController = testrig.NewTestTransportController(suite.httpClient, suite.db)
+ suite.federator = testrig.NewTestFederator(suite.db, suite.transportController, suite.storage)
+ suite.accountProcessor = account.New(suite.db, suite.tc, suite.mediaHandler, suite.oauthServer, suite.fromClientAPIChan, suite.federator, suite.config, suite.log)
+ testrig.StandardDBSetup(suite.db, nil)
+ testrig.StandardStorageSetup(suite.storage, "../../../testrig/media")
+}
+
+func (suite *AccountStandardTestSuite) TearDownTest() {
+ testrig.StandardDBTeardown(suite.db)
+ testrig.StandardStorageTeardown(suite.storage)
+}
diff --git a/internal/processing/account/update.go b/internal/processing/account/update.go
index c0fee8e25..e997a95c7 100644
--- a/internal/processing/account/update.go
+++ b/internal/processing/account/update.go
@@ -39,13 +39,13 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
l := p.log.WithField("func", "AccountUpdate")
if form.Discoverable != nil {
- if err := p.db.UpdateOneByID(ctx, account.ID, "discoverable", *form.Discoverable, &gtsmodel.Account{}); err != nil {
+ if err := p.db.UpdateOneByPrimaryKey(ctx, "discoverable", *form.Discoverable, account); err != nil {
return nil, fmt.Errorf("error updating discoverable: %s", err)
}
}
if form.Bot != nil {
- if err := p.db.UpdateOneByID(ctx, account.ID, "bot", *form.Bot, &gtsmodel.Account{}); err != nil {
+ if err := p.db.UpdateOneByPrimaryKey(ctx, "bot", *form.Bot, account); err != nil {
return nil, fmt.Errorf("error updating bot: %s", err)
}
}
@@ -55,7 +55,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
return nil, err
}
displayName := text.RemoveHTML(*form.DisplayName) // no html allowed in display name
- if err := p.db.UpdateOneByID(ctx, account.ID, "display_name", displayName, &gtsmodel.Account{}); err != nil {
+ if err := p.db.UpdateOneByPrimaryKey(ctx, "display_name", displayName, account); err != nil {
return nil, err
}
}
@@ -65,7 +65,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
return nil, err
}
note := text.SanitizeHTML(*form.Note) // html OK in note but sanitize it
- if err := p.db.UpdateOneByID(ctx, account.ID, "note", note, &gtsmodel.Account{}); err != nil {
+ if err := p.db.UpdateOneByPrimaryKey(ctx, "note", note, account); err != nil {
return nil, err
}
}
@@ -87,7 +87,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
}
if form.Locked != nil {
- if err := p.db.UpdateOneByID(ctx, account.ID, "locked", *form.Locked, &gtsmodel.Account{}); err != nil {
+ if err := p.db.UpdateOneByPrimaryKey(ctx, "locked", *form.Locked, account); err != nil {
return nil, err
}
}
@@ -97,13 +97,13 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
if err := validate.Language(*form.Source.Language); err != nil {
return nil, err
}
- if err := p.db.UpdateOneByID(ctx, account.ID, "language", *form.Source.Language, &gtsmodel.Account{}); err != nil {
+ if err := p.db.UpdateOneByPrimaryKey(ctx, "language", *form.Source.Language, account); err != nil {
return nil, err
}
}
if form.Source.Sensitive != nil {
- if err := p.db.UpdateOneByID(ctx, account.ID, "locked", *form.Locked, &gtsmodel.Account{}); err != nil {
+ if err := p.db.UpdateOneByPrimaryKey(ctx, "locked", *form.Locked, account); err != nil {
return nil, err
}
}
@@ -112,7 +112,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
if err := validate.Privacy(*form.Source.Privacy); err != nil {
return nil, err
}
- if err := p.db.UpdateOneByID(ctx, account.ID, "privacy", *form.Source.Privacy, &gtsmodel.Account{}); err != nil {
+ if err := p.db.UpdateOneByPrimaryKey(ctx, "privacy", *form.Source.Privacy, account); err != nil {
return nil, err
}
}
diff --git a/internal/processing/account/update_test.go b/internal/processing/account/update_test.go
new file mode 100644
index 000000000..b18a5e42e
--- /dev/null
+++ b/internal/processing/account/update_test.go
@@ -0,0 +1,75 @@
+/*
+ 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 account_test
+
+import (
+ "context"
+ "testing"
+
+ "github.com/stretchr/testify/suite"
+ "github.com/superseriousbusiness/gotosocial/internal/ap"
+ apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
+)
+
+type AccountUpdateTestSuite struct {
+ AccountStandardTestSuite
+}
+
+func (suite *AccountUpdateTestSuite) TestAccountUpdateSimple() {
+ testAccount := suite.testAccounts["local_account_1"]
+
+ locked := true
+ displayName := "new display name"
+ note := ""
+
+ form := &apimodel.UpdateCredentialsRequest{
+ DisplayName: &displayName,
+ Locked: &locked,
+ Note: &note,
+ }
+
+ // should get no error from the update function, and an api model account returned
+ apiAccount, err := suite.accountProcessor.Update(context.Background(), testAccount, form)
+ suite.NoError(err)
+ suite.NotNil(apiAccount)
+
+ // fields on the profile should be updated
+ suite.True(apiAccount.Locked)
+ suite.Equal(displayName, apiAccount.DisplayName)
+ suite.Empty(apiAccount.Note)
+
+ // we should have an update in the client api channel
+ msg := <-suite.fromClientAPIChan
+ suite.Equal(ap.ActivityUpdate, msg.APActivityType)
+ suite.Equal(ap.ObjectProfile, msg.APObjectType)
+ suite.NotNil(msg.OriginAccount)
+ suite.Equal(testAccount.ID, msg.OriginAccount.ID)
+ suite.Nil(msg.TargetAccount)
+
+ // fields should be updated in the database as well
+ dbAccount, err := suite.db.GetAccountByID(context.Background(), testAccount.ID)
+ suite.NoError(err)
+ suite.True(dbAccount.Locked)
+ suite.Equal(displayName, dbAccount.DisplayName)
+ suite.Empty(dbAccount.Note)
+}
+
+func TestAccountUpdateTestSuite(t *testing.T) {
+ suite.Run(t, new(AccountUpdateTestSuite))
+}
diff --git a/internal/processing/admin/createdomainblock.go b/internal/processing/admin/createdomainblock.go
index 9c4ff780f..50df056e5 100644
--- a/internal/processing/admin/createdomainblock.go
+++ b/internal/processing/admin/createdomainblock.go
@@ -108,7 +108,7 @@ func (p *processor) initiateDomainBlockSideEffects(ctx context.Context, account
instance.ContactAccountUsername = ""
instance.ContactAccountID = ""
instance.Version = ""
- if err := p.db.UpdateByID(ctx, instance.ID, instance); err != nil {
+ if err := p.db.UpdateByPrimaryKey(ctx, instance); err != nil {
l.Errorf("domainBlockProcessSideEffects: db error updating instance: %s", err)
}
l.Debug("domainBlockProcessSideEffects: instance entry updated")
diff --git a/internal/processing/admin/deletedomainblock.go b/internal/processing/admin/deletedomainblock.go
index 2563b557d..d11374c78 100644
--- a/internal/processing/admin/deletedomainblock.go
+++ b/internal/processing/admin/deletedomainblock.go
@@ -60,7 +60,7 @@ func (p *processor) DomainBlockDelete(ctx context.Context, account *gtsmodel.Acc
}, i); err == nil {
i.SuspendedAt = time.Time{}
i.DomainBlockID = ""
- if err := p.db.UpdateByID(ctx, i.ID, i); err != nil {
+ if err := p.db.UpdateByPrimaryKey(ctx, i); err != nil {
return nil, gtserror.NewErrorInternalError(fmt.Errorf("couldn't update database entry for instance %s: %s", domainBlock.Domain, err))
}
}
diff --git a/internal/processing/instance.go b/internal/processing/instance.go
index e74d3077a..41139c491 100644
--- a/internal/processing/instance.go
+++ b/internal/processing/instance.go
@@ -147,7 +147,7 @@ func (p *processor) InstancePatch(ctx context.Context, form *apimodel.InstanceSe
}
}
- if err := p.db.UpdateByID(ctx, i.ID, i); err != nil {
+ if err := p.db.UpdateByPrimaryKey(ctx, i); err != nil {
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error updating instance %s: %s", p.config.Host, err))
}
diff --git a/internal/processing/media/update.go b/internal/processing/media/update.go
index 6f15f2ace..e6c78563d 100644
--- a/internal/processing/media/update.go
+++ b/internal/processing/media/update.go
@@ -46,7 +46,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, media
if form.Description != nil {
attachment.Description = text.RemoveHTML(*form.Description)
- if err := p.db.UpdateByID(ctx, mediaAttachmentID, attachment); err != nil {
+ if err := p.db.UpdateByPrimaryKey(ctx, attachment); err != nil {
return nil, gtserror.NewErrorInternalError(fmt.Errorf("database error updating description: %s", err))
}
}
@@ -58,7 +58,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, media
}
attachment.FileMeta.Focus.X = focusx
attachment.FileMeta.Focus.Y = focusy
- if err := p.db.UpdateByID(ctx, mediaAttachmentID, attachment); err != nil {
+ if err := p.db.UpdateByPrimaryKey(ctx, attachment); err != nil {
return nil, gtserror.NewErrorInternalError(fmt.Errorf("database error updating focus: %s", err))
}
}