2023-03-12 18:00:57 +03:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// 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/>.
|
2021-04-19 20:42:19 +03:00
|
|
|
|
|
|
|
package testrig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-08-25 16:34:33 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
|
2021-05-08 15:25:55 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-07-19 11:47:55 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2022-12-08 20:35:14 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/state"
|
2021-04-19 20:42:19 +03:00
|
|
|
)
|
|
|
|
|
2021-11-22 10:46:19 +03:00
|
|
|
var testModels = []interface{}{
|
2021-04-19 20:42:19 +03:00
|
|
|
>smodel.Account{},
|
2024-07-30 14:58:31 +03:00
|
|
|
>smodel.AccountNote{},
|
|
|
|
>smodel.AccountSettings{},
|
2022-09-26 12:56:01 +03:00
|
|
|
>smodel.AccountToEmoji{},
|
2021-04-19 20:42:19 +03:00
|
|
|
>smodel.Application{},
|
|
|
|
>smodel.Block{},
|
|
|
|
>smodel.DomainBlock{},
|
|
|
|
>smodel.EmailDomainBlock{},
|
2024-03-06 13:15:58 +03:00
|
|
|
>smodel.Filter{},
|
|
|
|
>smodel.FilterKeyword{},
|
|
|
|
>smodel.FilterStatus{},
|
2021-04-19 20:42:19 +03:00
|
|
|
>smodel.Follow{},
|
|
|
|
>smodel.FollowRequest{},
|
2024-08-24 12:49:37 +03:00
|
|
|
>smodel.InteractionRequest{},
|
2023-05-25 11:37:38 +03:00
|
|
|
>smodel.List{},
|
|
|
|
>smodel.ListEntry{},
|
2023-07-29 13:49:14 +03:00
|
|
|
>smodel.Marker{},
|
2021-04-19 20:42:19 +03:00
|
|
|
>smodel.MediaAttachment{},
|
|
|
|
>smodel.Mention{},
|
2023-11-08 17:32:17 +03:00
|
|
|
>smodel.Poll{},
|
|
|
|
>smodel.PollVote{},
|
2021-04-19 20:42:19 +03:00
|
|
|
>smodel.Status{},
|
2021-08-20 13:26:56 +03:00
|
|
|
>smodel.StatusToEmoji{},
|
|
|
|
>smodel.StatusToTag{},
|
2021-04-19 20:42:19 +03:00
|
|
|
>smodel.StatusFave{},
|
|
|
|
>smodel.StatusBookmark{},
|
|
|
|
>smodel.Tag{},
|
2023-10-25 17:04:53 +03:00
|
|
|
>smodel.Thread{},
|
|
|
|
>smodel.ThreadMute{},
|
|
|
|
>smodel.ThreadToStatus{},
|
2021-04-19 20:42:19 +03:00
|
|
|
>smodel.User{},
|
2024-06-06 19:38:02 +03:00
|
|
|
>smodel.UserMute{},
|
2021-04-19 20:42:19 +03:00
|
|
|
>smodel.Emoji{},
|
2021-06-21 13:27:23 +03:00
|
|
|
>smodel.Instance{},
|
|
|
|
>smodel.Notification{},
|
2021-07-07 16:46:42 +03:00
|
|
|
>smodel.RouterSession{},
|
2021-09-01 12:45:01 +03:00
|
|
|
>smodel.Token{},
|
|
|
|
>smodel.Client{},
|
2022-11-15 01:47:27 +03:00
|
|
|
>smodel.EmojiCategory{},
|
2022-11-11 14:18:38 +03:00
|
|
|
>smodel.Tombstone{},
|
2023-01-10 17:19:05 +03:00
|
|
|
>smodel.Report{},
|
2023-08-19 15:33:15 +03:00
|
|
|
>smodel.Rule{},
|
2024-07-30 14:58:31 +03:00
|
|
|
>smodel.WorkerTask{},
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
|
|
|
|
2021-08-12 22:03:24 +03: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 19:13:24 +03: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.
|
2023-03-01 21:26:53 +03:00
|
|
|
func NewTestDB(state *state.State) db.DB {
|
2022-12-08 20:35:14 +03:00
|
|
|
state.Caches.Init()
|
|
|
|
|
2023-03-01 21:26:53 +03:00
|
|
|
testDB, err := bundb.NewBunDBService(context.Background(), state)
|
2021-04-19 20:42:19 +03:00
|
|
|
if err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
2022-12-08 20:35:14 +03:00
|
|
|
|
|
|
|
state.DB = testDB
|
|
|
|
|
2021-04-19 20:42:19 +03:00
|
|
|
return testDB
|
|
|
|
}
|
|
|
|
|
2021-09-09 17:15:25 +03: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 {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panicf(nil, "error creating table for %+v: %s", m, err)
|
2021-09-09 17:15:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 20:42:19 +03:00
|
|
|
// StandardDBSetup populates a given db with all the necessary tables/models for perfoming tests.
|
2021-08-10 14:32:39 +03: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 16:34:33 +03:00
|
|
|
if db == nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, "db setup: db was nil")
|
2021-08-25 16:34:33 +03:00
|
|
|
}
|
|
|
|
|
2021-09-09 17:15:25 +03:00
|
|
|
CreateTestTables(db)
|
2021-08-25 16:34:33 +03:00
|
|
|
|
2021-09-09 17:15:25 +03:00
|
|
|
ctx := context.Background()
|
2021-04-19 20:42:19 +03:00
|
|
|
|
|
|
|
for _, v := range NewTestTokens() {
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestClients() {
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestApplications() {
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 17:15:25 +03:00
|
|
|
for _, v := range NewTestBlocks() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-09-09 17:15:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 17:19:05 +03:00
|
|
|
for _, v := range NewTestReports() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2023-08-19 15:33:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestRules() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
2023-01-10 17:19:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 17:15:25 +03:00
|
|
|
for _, v := range NewTestDomainBlocks() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-09-09 17:15:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 17:54:54 +03:00
|
|
|
for _, v := range NewTestInstances() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2022-06-23 17:54:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 20:42:19 +03:00
|
|
|
for _, v := range NewTestUsers() {
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 14:32:39 +03:00
|
|
|
if accounts == nil {
|
|
|
|
for _, v := range NewTestAccounts() {
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-08-10 14:32:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, v := range accounts {
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-08-10 14:32:39 +03:00
|
|
|
}
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 16:03:46 +03:00
|
|
|
for _, v := range NewTestAccountSettings() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 20:42:19 +03:00
|
|
|
for _, v := range NewTestAttachments() {
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestStatuses() {
|
2022-10-08 14:50:48 +03:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestEmojis() {
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 01:47:27 +03:00
|
|
|
for _, v := range NewTestEmojiCategories() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2022-11-15 01:47:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-08 14:50:48 +03:00
|
|
|
for _, v := range NewTestStatusToEmojis() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2022-10-08 14:50:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 20:42:19 +03:00
|
|
|
for _, v := range NewTestTags() {
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2022-10-08 14:50:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestStatusToTags() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 17:54:54 +03:00
|
|
|
for _, v := range NewTestMentions() {
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-08-11 17:54:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 20:42:19 +03:00
|
|
|
for _, v := range NewTestFaves() {
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 13:27:23 +03:00
|
|
|
for _, v := range NewTestFollows() {
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-06-21 13:27:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 11:37:38 +03:00
|
|
|
for _, v := range NewTestLists() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestListEntries() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 13:27:23 +03:00
|
|
|
for _, v := range NewTestNotifications() {
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-06-21 13:27:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 14:18:38 +03:00
|
|
|
for _, v := range NewTestTombstones() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2022-11-11 14:18:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 13:37:12 +03:00
|
|
|
for _, v := range NewTestBookmarks() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2022-12-09 13:37:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 11:30:39 +03:00
|
|
|
for _, v := range NewTestAccountNotes() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 13:49:14 +03:00
|
|
|
for _, v := range NewTestMarkers() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 17:04:53 +03:00
|
|
|
for _, v := range NewTestThreads() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestThreadToStatus() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 17:32:17 +03:00
|
|
|
for _, v := range NewTestPolls() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestPollVotes() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:15:58 +03:00
|
|
|
for _, v := range NewTestFilters() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestFilterKeywords() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range NewTestFilterStatuses() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 19:38:02 +03:00
|
|
|
for _, v := range NewTestUserMutes() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-24 12:49:37 +03:00
|
|
|
for _, v := range NewTestInteractionRequests() {
|
|
|
|
if err := db.Put(ctx, v); err != nil {
|
|
|
|
log.Panic(nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.CreateInstanceAccount(ctx); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
2021-06-21 13:27:23 +03:00
|
|
|
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.CreateInstanceInstance(ctx); err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Panic(nil, err)
|
2021-06-21 13:27:23 +03:00
|
|
|
}
|
2021-08-25 16:34:33 +03:00
|
|
|
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Debug(nil, "testing db setup complete")
|
2021-04-19 20:42:19 +03: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 16:34:33 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
if db == nil {
|
2023-11-08 17:32:17 +03:00
|
|
|
return
|
2021-08-25 16:34:33 +03:00
|
|
|
}
|
2024-05-27 18:46:15 +03:00
|
|
|
defer db.Close()
|
2021-04-19 20:42:19 +03:00
|
|
|
for _, m := range testModels {
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := db.DropTable(ctx, m); err != nil {
|
2024-05-27 18:46:15 +03:00
|
|
|
log.Error(ctx, err)
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|