diff options
Diffstat (limited to 'testrig')
-rw-r--r-- | testrig/config.go | 102 | ||||
-rw-r--r-- | testrig/db.go | 20 | ||||
-rw-r--r-- | testrig/email.go | 10 | ||||
-rw-r--r-- | testrig/federatingdb.go | 2 | ||||
-rw-r--r-- | testrig/federator.go | 2 | ||||
-rw-r--r-- | testrig/mediahandler.go | 2 | ||||
-rw-r--r-- | testrig/processor.go | 2 | ||||
-rw-r--r-- | testrig/router.go | 2 | ||||
-rw-r--r-- | testrig/timelinemanager.go | 2 | ||||
-rw-r--r-- | testrig/transportcontroller.go | 2 | ||||
-rw-r--r-- | testrig/typeconverter.go | 2 |
11 files changed, 123 insertions, 25 deletions
diff --git a/testrig/config.go b/testrig/config.go index f7028b1b5..be5efab61 100644 --- a/testrig/config.go +++ b/testrig/config.go @@ -18,9 +18,103 @@ package testrig -import "github.com/superseriousbusiness/gotosocial/internal/config" +import ( + "reflect" -// NewTestConfig returns a config initialized with test defaults -func NewTestConfig() *config.Config { - return config.TestDefault() + "github.com/coreos/go-oidc/v3/oidc" + "github.com/spf13/viper" + "github.com/superseriousbusiness/gotosocial/internal/config" +) + +// InitTestConfig resets + initializes the viper configuration with test defaults. +func InitTestConfig() { + // reset viper to an empty state + viper.Reset() + + // get the field names of config.Keys + keyFields := reflect.VisibleFields(reflect.TypeOf(config.Keys)) + + // get the field values of config.Keys + keyValues := reflect.ValueOf(config.Keys) + + // get the field values of TestDefaults + testDefaults := reflect.ValueOf(TestDefaults) + + // for each config field... + for _, field := range keyFields { + // the viper config key should be the value of the key + key, ok := keyValues.FieldByName(field.Name).Interface().(string) + if !ok { + panic("could not convert config.Keys value to string") + } + + // the value should be the test default corresponding to the given fieldName + value := testDefaults.FieldByName(field.Name).Interface() + + // actually set the value in viper -- this will override everything + viper.Set(key, value) + } +} + +// TestDefaults returns a Values struct with values set that are suitable for local testing. +var TestDefaults = config.Values{ + LogLevel: "trace", + ApplicationName: "gotosocial", + ConfigPath: "", + Host: "localhost:8080", + AccountDomain: "localhost:8080", + Protocol: "http", + BindAddress: "127.0.0.1", + Port: 8080, + TrustedProxies: []string{"127.0.0.1/32"}, + + DbType: "sqlite", + DbAddress: ":memory:", + DbPort: 5432, + DbUser: "postgres", + DbPassword: "postgres", + DbDatabase: "postgres", + + WebTemplateBaseDir: "./web/template/", + WebAssetBaseDir: "./web/assets/", + + AccountsRegistrationOpen: true, + AccountsApprovalRequired: true, + AccountsReasonRequired: true, + + MediaImageMaxSize: 1048576, // 1mb + MediaVideoMaxSize: 5242880, // 5mb + MediaDescriptionMinChars: 0, + MediaDescriptionMaxChars: 500, + + StorageBackend: "local", + StorageBasePath: "/gotosocial/storage", + StorageServeProtocol: "http", + StorageServeHost: "localhost:8080", + StorageServeBasePath: "/fileserver", + + StatusesMaxChars: 5000, + StatusesCWMaxChars: 100, + StatusesPollMaxOptions: 6, + StatusesPollOptionMaxChars: 50, + StatusesMediaMaxFiles: 6, + + LetsEncryptEnabled: false, + LetsEncryptPort: 0, + LetsEncryptCertDir: "", + LetsEncryptEmailAddress: "", + + OIDCEnabled: false, + OIDCIdpName: "", + OIDCSkipVerification: false, + OIDCIssuer: "", + OIDCClientID: "", + OIDCClientSecret: "", + OIDCScopes: []string{oidc.ScopeOpenID, "profile", "email", "groups"}, + + SMTPHost: "", + SMTPPort: 0, + SMTPUsername: "", + SMTPPassword: "", + SMTPFrom: "GoToSocial", } diff --git a/testrig/db.go b/testrig/db.go index 488d30734..d2146ab34 100644 --- a/testrig/db.go +++ b/testrig/db.go @@ -24,6 +24,8 @@ import ( "strconv" "github.com/sirupsen/logrus" + "github.com/spf13/viper" + "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/db/bundb" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" @@ -66,27 +68,23 @@ var testModels = []interface{}{ // If the environment variable GTS_DB_PORT is set, it will take that // value as the port instead. func NewTestDB() db.DB { - config := NewTestConfig() - alternateAddress := os.Getenv("GTS_DB_ADDRESS") - if alternateAddress != "" { - config.DBConfig.Address = alternateAddress + if alternateAddress := os.Getenv("GTS_DB_ADDRESS"); alternateAddress != "" { + viper.Set(config.Keys.DbAddress, alternateAddress) } - alternateDBType := os.Getenv("GTS_DB_TYPE") - if alternateDBType != "" { - config.DBConfig.Type = alternateDBType + if alternateDBType := os.Getenv("GTS_DB_TYPE"); alternateDBType != "" { + viper.Set(config.Keys.DbType, alternateDBType) } - alternateDBPort := os.Getenv("GTS_DB_PORT") - if alternateDBPort != "" { + if alternateDBPort := os.Getenv("GTS_DB_PORT"); alternateDBPort != "" { port, err := strconv.ParseInt(alternateDBPort, 10, 64) if err != nil { panic(err) } - config.DBConfig.Port = int(port) + viper.Set(config.Keys.DbPort, port) } - testDB, err := bundb.NewBunDBService(context.Background(), config) + testDB, err := bundb.NewBunDBService(context.Background()) if err != nil { logrus.Panic(err) } diff --git a/testrig/email.go b/testrig/email.go index 60ee96ac2..714025d85 100644 --- a/testrig/email.go +++ b/testrig/email.go @@ -18,7 +18,11 @@ package testrig -import "github.com/superseriousbusiness/gotosocial/internal/email" +import ( + "github.com/spf13/viper" + "github.com/superseriousbusiness/gotosocial/internal/config" + "github.com/superseriousbusiness/gotosocial/internal/email" +) // NewEmailSender returns a noop email sender that won't make any remote calls. // @@ -26,6 +30,8 @@ import "github.com/superseriousbusiness/gotosocial/internal/email" // the map, with email address of the recipient as the key, and the value as the // parsed email message as it would have been sent. func NewEmailSender(templateBaseDir string, sentEmails map[string]string) email.Sender { + viper.Set(config.Keys.WebTemplateBaseDir, templateBaseDir) + var sendCallback func(toAddress string, message string) if sentEmails != nil { @@ -34,7 +40,7 @@ func NewEmailSender(templateBaseDir string, sentEmails map[string]string) email. } } - s, err := email.NewNoopSender(templateBaseDir, sendCallback) + s, err := email.NewNoopSender(sendCallback) if err != nil { panic(err) } diff --git a/testrig/federatingdb.go b/testrig/federatingdb.go index b0ef511bc..f01a4e9fe 100644 --- a/testrig/federatingdb.go +++ b/testrig/federatingdb.go @@ -7,5 +7,5 @@ import ( // NewTestFederatingDB returns a federating DB with the underlying db func NewTestFederatingDB(db db.DB) federatingdb.DB { - return federatingdb.New(db, NewTestConfig()) + return federatingdb.New(db) } diff --git a/testrig/federator.go b/testrig/federator.go index 42474566b..e1050ad9f 100644 --- a/testrig/federator.go +++ b/testrig/federator.go @@ -27,5 +27,5 @@ import ( // NewTestFederator returns a federator with the given database and (mock!!) transport controller. func NewTestFederator(db db.DB, tc transport.Controller, storage *kv.KVStore) federation.Federator { - return federation.NewFederator(db, NewTestFederatingDB(db), tc, NewTestConfig(), NewTestTypeConverter(db), NewTestMediaHandler(db, storage)) + return federation.NewFederator(db, NewTestFederatingDB(db), tc, NewTestTypeConverter(db), NewTestMediaHandler(db, storage)) } diff --git a/testrig/mediahandler.go b/testrig/mediahandler.go index a6b5cfec7..abc714551 100644 --- a/testrig/mediahandler.go +++ b/testrig/mediahandler.go @@ -26,5 +26,5 @@ import ( // NewTestMediaHandler returns a media handler with the default test config, and the given db and storage. func NewTestMediaHandler(db db.DB, storage *kv.KVStore) media.Handler { - return media.New(NewTestConfig(), db, storage) + return media.New(db, storage) } diff --git a/testrig/processor.go b/testrig/processor.go index 900d60b18..c09748e3c 100644 --- a/testrig/processor.go +++ b/testrig/processor.go @@ -28,5 +28,5 @@ import ( // NewTestProcessor returns a Processor suitable for testing purposes func NewTestProcessor(db db.DB, storage *kv.KVStore, federator federation.Federator, emailSender email.Sender) processing.Processor { - return processing.NewProcessor(NewTestConfig(), NewTestTypeConverter(db), federator, NewTestOauthServer(db), NewTestMediaHandler(db, storage), storage, NewTestTimelineManager(db), db, emailSender) + return processing.NewProcessor(NewTestTypeConverter(db), federator, NewTestOauthServer(db), NewTestMediaHandler(db, storage), storage, NewTestTimelineManager(db), db, emailSender) } diff --git a/testrig/router.go b/testrig/router.go index 8efe1e4de..4bfa57603 100644 --- a/testrig/router.go +++ b/testrig/router.go @@ -27,7 +27,7 @@ import ( // NewTestRouter returns a Router suitable for testing func NewTestRouter(db db.DB) router.Router { - r, err := router.New(context.Background(), NewTestConfig(), db) + r, err := router.New(context.Background(), db) if err != nil { panic(err) } diff --git a/testrig/timelinemanager.go b/testrig/timelinemanager.go index dd39f6548..4b316a52e 100644 --- a/testrig/timelinemanager.go +++ b/testrig/timelinemanager.go @@ -7,5 +7,5 @@ import ( // NewTestTimelineManager retuts a new timeline.Manager, suitable for testing, using the given db. func NewTestTimelineManager(db db.DB) timeline.Manager { - return timeline.NewManager(db, NewTestTypeConverter(db), NewTestConfig()) + return timeline.NewManager(db, NewTestTypeConverter(db)) } diff --git a/testrig/transportcontroller.go b/testrig/transportcontroller.go index c82b55f69..d383e096c 100644 --- a/testrig/transportcontroller.go +++ b/testrig/transportcontroller.go @@ -39,7 +39,7 @@ import ( // PER TEST rather than per suite, so that the do function can be set on a test by test (or even more granular) // basis. func NewTestTransportController(client pub.HttpClient, db db.DB) transport.Controller { - return transport.NewController(NewTestConfig(), db, &federation.Clock{}, client) + return transport.NewController(db, &federation.Clock{}, client) } // NewMockHTTPClient returns a client that conforms to the pub.HttpClient interface, diff --git a/testrig/typeconverter.go b/testrig/typeconverter.go index 9d49e6c99..bf064c245 100644 --- a/testrig/typeconverter.go +++ b/testrig/typeconverter.go @@ -25,5 +25,5 @@ import ( // NewTestTypeConverter returned a type converter with the given db and the default test config func NewTestTypeConverter(db db.DB) typeutils.TypeConverter { - return typeutils.NewConverter(NewTestConfig(), db) + return typeutils.NewConverter(db) } |