2021-04-19 19:42:19 +02:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 18:42:19 +01:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-04-19 19:42:19 +02:00
|
|
|
|
|
|
|
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 testrig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-12 21:03:24 +02:00
|
|
|
"os"
|
2021-09-10 18:13:24 +02:00
|
|
|
"strconv"
|
2021-04-19 19:42:19 +02:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-12-07 13:31:39 +01:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2021-04-19 19:42:19 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-08-25 15:34:33 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
|
2021-05-08 14:25:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2021-04-19 19:42:19 +02:00
|
|
|
)
|
|
|
|
|
2021-11-22 08:46:19 +01:00
|
|
|
var testModels = []interface{}{
|
2021-04-19 19:42:19 +02:00
|
|
|
>smodel.Account{},
|
|
|
|
>smodel.Application{},
|
|
|
|
>smodel.Block{},
|
|
|
|
>smodel.DomainBlock{},
|
|
|
|
>smodel.EmailDomainBlock{},
|
|
|
|
>smodel.Follow{},
|
|
|
|
>smodel.FollowRequest{},
|
|
|
|
>smodel.MediaAttachment{},
|
|
|
|
>smodel.Mention{},
|
|
|
|
>smodel.Status{},
|
2021-08-20 12:26:56 +02:00
|
|
|
>smodel.StatusToEmoji{},
|
|
|
|
>smodel.StatusToTag{},
|
2021-04-19 19:42:19 +02:00
|
|
|
>smodel.StatusFave{},
|
|
|
|
>smodel.StatusBookmark{},
|
|
|
|
>smodel.StatusMute{},
|
|
|
|
>smodel.Tag{},
|
|
|
|
>smodel.User{},
|
|
|
|
>smodel.Emoji{},
|
2021-06-21 12:27:23 +02:00
|
|
|
>smodel.Instance{},
|
|
|
|
>smodel.Notification{},
|
2021-07-07 15:46:42 +02:00
|
|
|
>smodel.RouterSession{},
|
2021-09-01 11:45:01 +02:00
|
|
|
>smodel.Token{},
|
|
|
|
>smodel.Client{},
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 21:03:24 +02:00
|
|
|
// NewTestDB returns a new initialized, empty database for testing.
|
|
|
|
//
|
|
|
|
// If the environment variable GTS_DB_ADDRESS is set, it will take that
|
|
|
|
// value as the database address instead.
|
2021-09-10 18:13:24 +02:00
|
|
|
//
|
|
|
|
// If the environment variable GTS_DB_TYPE is set, it will take that
|
|
|
|
// value as the database type instead.
|
|
|
|
//
|
|
|
|
// If the environment variable GTS_DB_PORT is set, it will take that
|
|
|
|
// value as the port instead.
|
2021-04-19 19:42:19 +02:00
|
|
|
func NewTestDB() db.DB {
|
2021-12-07 13:31:39 +01:00
|
|
|
if alternateAddress := os.Getenv("GTS_DB_ADDRESS"); alternateAddress != "" {
|
|
|
|
viper.Set(config.Keys.DbAddress, alternateAddress)
|
2021-08-12 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 13:31:39 +01:00
|
|
|
if alternateDBType := os.Getenv("GTS_DB_TYPE"); alternateDBType != "" {
|
|
|
|
viper.Set(config.Keys.DbType, alternateDBType)
|
2021-09-10 18:13:24 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 13:31:39 +01:00
|
|
|
if alternateDBPort := os.Getenv("GTS_DB_PORT"); alternateDBPort != "" {
|
2021-09-10 18:13:24 +02:00
|
|
|
port, err := strconv.ParseInt(alternateDBPort, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-12-07 13:31:39 +01:00
|
|
|
viper.Set(config.Keys.DbPort, port)
|
2021-09-10 18:13:24 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 13:31:39 +01:00
|
|
|
testDB, err := bundb.NewBunDBService(context.Background())
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
logrus.Panic(err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
return testDB
|
|
|
|
}
|
|
|
|
|
2021-09-09 16:15:25 +02:00
|
|
|
// CreateTestTables creates prerequisite test tables in the database, but doesn't populate them.
|
|
|
|
func CreateTestTables(db db.DB) {
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, m := range testModels {
|
|
|
|
if err := db.CreateTable(ctx, m); err != nil {
|
|
|
|
logrus.Panicf("error creating table for %+v: %s", m, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:42:19 +02:00
|
|
|
// StandardDBSetup populates a given db with all the necessary tables/models for perfoming tests.
|
2021-08-10 13:32:39 +02:00
|
|
|
//
|
|
|
|
// The accounts parameter is provided in case the db should be populated with a certain set of accounts.
|
|
|
|
// If accounts is nil, then the standard test accounts will be used.
|
|
|
|
//
|
|
|
|
// When testing http signatures, you should pass into this function the same accounts map that you generated
|
|
|
|
// signatures with, otherwise this function will randomly generate new keys for accounts and signature
|
|
|
|
// verification will fail.
|
|
|
|
func StandardDBSetup(db db.DB, accounts map[string]*gtsmodel.Account) {
|
2021-08-25 15:34:33 +02:00
|
|
|
if db == nil {
|
|
|
|
logrus.Panic("db setup: db was nil")
|
|
|
|
}
|
|
|
|
|
2021-09-09 16:15:25 +02:00
|
|
|
CreateTestTables(db)
|
2021-08-25 15:34:33 +02:00
|
|
|
|
2021-09-09 16:15:25 +02:00
|
|
|
ctx := context.Background()
|
2021-04-19 19:42:19 +02:00
|
|
|
|
|
|
|
for _, v := range NewTestTokens() {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestClients() {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestApplications() {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 16:15:25 +02:00
|
|
|
for _, v := range NewTestBlocks() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestDomainBlocks() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:42:19 +02:00
|
|
|
for _, v := range NewTestUsers() {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 13:32:39 +02:00
|
|
|
if accounts == nil {
|
|
|
|
for _, v := range NewTestAccounts() {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-08-10 13:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, v := range accounts {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-08-10 13:32:39 +02:00
|
|
|
}
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestAttachments() {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestStatuses() {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.PutStatus(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestEmojis() {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestTags() {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 16:54:54 +02:00
|
|
|
for _, v := range NewTestMentions() {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-08-11 16:54:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:42:19 +02:00
|
|
|
for _, v := range NewTestFaves() {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 12:27:23 +02:00
|
|
|
for _, v := range NewTestFollows() {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-06-21 12:27:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestNotifications() {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-06-21 12:27:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.CreateInstanceAccount(ctx); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2021-06-21 12:27:23 +02:00
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.CreateInstanceInstance(ctx); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-06-21 12:27:23 +02:00
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
|
|
|
|
logrus.Debug("testing db setup complete")
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// StandardDBTeardown drops all the standard testing tables/models from the database to ensure it's clean for the next test.
|
|
|
|
func StandardDBTeardown(db db.DB) {
|
2021-08-25 15:34:33 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
if db == nil {
|
|
|
|
logrus.Panic("db teardown: db was nil")
|
|
|
|
}
|
2021-04-19 19:42:19 +02:00
|
|
|
for _, m := range testModels {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := db.DropTable(ctx, m); err != nil {
|
|
|
|
logrus.Panic(err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|