fixes + db changes (#204)

* fixes + db changes

* make duration more lenient
This commit is contained in:
tobi 2021-09-10 14:36:10 +02:00 committed by GitHub
commit e681aac589
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 401 additions and 78 deletions

View file

@ -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)
}

View file

@ -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
}
}

View file

@ -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))
}