From ae427e865881b223a052ac07f2e12fd64f3f5a0a Mon Sep 17 00:00:00 2001 From: Leigh MacDonald Date: Fri, 21 Jun 2024 19:18:00 -0600 Subject: [PATCH] Add config tests --- internal/config/config_service.go | 3 +- internal/config/config_usecase.go | 2 - internal/domain/config.go | 6 -- internal/test/config_test.go | 136 ++++++++++++++++++++++++++++++ internal/test/main_test.go | 6 ++ 5 files changed, 144 insertions(+), 9 deletions(-) create mode 100644 internal/test/config_test.go diff --git a/internal/config/config_service.go b/internal/config/config_service.go index 140c5fcc..1b595238 100644 --- a/internal/config/config_service.go +++ b/internal/config/config_service.go @@ -42,13 +42,14 @@ func (c configHandler) onAPIPutConfig() gin.HandlerFunc { } if errSave := c.config.Write(ctx, req); errSave != nil { + httphelper.HandleErrs(ctx, errSave) slog.Error("Failed to save new config", log.ErrAttr(errSave)) - httphelper.HandleErrBadRequest(ctx) return } ctx.JSON(http.StatusOK, req) + slog.Info("Wrote new config") } } diff --git a/internal/config/config_usecase.go b/internal/config/config_usecase.go index 0eddc131..6f6d9072 100644 --- a/internal/config/config_usecase.go +++ b/internal/config/config_usecase.go @@ -51,8 +51,6 @@ func (c *configUsecase) Write(ctx context.Context, config domain.Config) error { return errReload } - slog.Info("Wrote new config") - return nil } diff --git a/internal/domain/config.go b/internal/domain/config.go index 61acdbd2..4d2325ef 100644 --- a/internal/domain/config.go +++ b/internal/domain/config.go @@ -113,12 +113,6 @@ type ConfigLocalStore struct { PathRoot string `json:"path_root"` } -type ConfigDB struct { - DSN string `mapstructure:"dsn"` - AutoMigrate bool `mapstructure:"auto_migrate"` - LogQueries bool `mapstructure:"log_queries"` -} - type ConfigClientprefs struct { CenterProjectiles bool `mapstructure:"center_projectiles"` } diff --git a/internal/test/config_test.go b/internal/test/config_test.go new file mode 100644 index 00000000..c0a9e8d5 --- /dev/null +++ b/internal/test/config_test.go @@ -0,0 +1,136 @@ +package test_test + +import ( + "net/http" + "testing" + + "github.com/leighmacdonald/gbans/internal/domain" + "github.com/leighmacdonald/gbans/pkg/log" + "github.com/stretchr/testify/require" +) + +func TestConfig(t *testing.T) { + router := testRouter() + owner := loginUser(getOwner()) + + var config domain.Config + testEndpointWithReceiver(t, router, http.MethodGet, "/api/config", nil, http.StatusOK, owner, &config) + config.StaticConfig = configUC.Config().StaticConfig + require.EqualValues(t, configUC.Config(), config) + + config.General.SiteName += "x" + config.General.Mode = domain.DebugMode + config.General.FileServeMode = "local" + config.General.SrcdsLogAddr += "x" + config.General.AssetURL += "x" + config.General.DefaultRoute = "/" + config.General.NewsEnabled = !config.General.NewsEnabled + config.General.ForumsEnabled = !config.General.ForumsEnabled + config.General.ContestsEnabled = !config.General.ContestsEnabled + config.General.WikiEnabled = !config.General.WikiEnabled + config.General.StatsEnabled = !config.General.StatsEnabled + config.General.ServersEnabled = !config.General.ServersEnabled + config.General.ReportsEnabled = !config.General.ReportsEnabled + config.General.ChatlogsEnabled = !config.General.ChatlogsEnabled + config.General.DemosEnabled = !config.General.DemosEnabled + + config.Debug.SkipOpenIDValidation = !config.Debug.SkipOpenIDValidation + config.Debug.AddRCONLogAddress = "1.2.3.4:27115" + + config.Log.Level = log.Warn + config.Log.File += "x" + config.Log.HTTPLevel = log.Warn + config.Log.HTTPEnabled = !config.Log.HTTPEnabled + config.Log.HTTPOtelEnabled = !config.Log.HTTPOtelEnabled + + config.Patreon.ClientID += "x" + config.Patreon.ClientSecret += "x" + config.Patreon.IntegrationsEnabled = !config.Patreon.IntegrationsEnabled + config.Patreon.CreatorAccessToken += "x" + config.Patreon.CreatorRefreshToken += "x" + config.Patreon.CreatorAccessToken += "x" + + config.Discord.Enabled = !config.Discord.Enabled + config.Discord.IntegrationsEnabled = !config.Discord.IntegrationsEnabled + config.Discord.BotEnabled = !config.Discord.BotEnabled + config.Discord.AppID += "x" + config.Discord.AppSecret += "x" + config.Discord.LinkID += "x" + config.Discord.Token += "x" + config.Discord.GuildID += "x" + config.Discord.LogChannelID += "x" + config.Discord.PublicLogChannelEnable = !config.Discord.PublicLogChannelEnable + config.Discord.PublicLogChannelID += "x" + config.Discord.PublicMatchLogChannelID += "x" + config.Discord.VoteLogChannelID += "x" + config.Discord.AppealLogChannelID += "x" + config.Discord.BanLogChannelID += "x" + config.Discord.ForumLogChannelID += "x" + config.Discord.WordFilterLogChannelID += "x" + config.Discord.KickLogChannelID += "x" + config.Discord.ModPingRoleID += "x" + config.Discord.UnregisterOnStart = !config.Discord.UnregisterOnStart + + config.Sentry.SentryDSN += "x" + config.Sentry.SentryDSN += "x" + config.Sentry.SentryTrace = !config.Sentry.SentryTrace + config.Sentry.SentrySampleRate *= 2 + + config.Filters.Enabled = !config.Filters.Enabled + config.Filters.WarningTimeout *= 2 + config.Filters.WarningLimit *= 2 + config.Filters.Dry = !config.Filters.Dry + config.Filters.PingDiscord = !config.Filters.PingDiscord + config.Filters.MaxWeight *= 2 + config.Filters.CheckTimeout *= 2 + config.Filters.MatchTimeout *= 2 + + config.GeoLocation.Enabled = !config.GeoLocation.Enabled + config.GeoLocation.CachePath += "x" + config.GeoLocation.Token += "x" + + config.Demo.DemoCleanupEnabled = !config.Demo.DemoCleanupEnabled + config.Demo.DemoCleanupStrategy = "count" + config.Demo.DemoCleanupMinPct *= 2 + config.Demo.DemoCleanupMount += "/x" + config.Demo.DemoCountLimit *= 2 + + config.Clientprefs.CenterProjectiles = !config.Clientprefs.CenterProjectiles + + config.LocalStore.PathRoot += "/x" + + config.Exports.AuthorizedKeys = append(config.Exports.AuthorizedKeys, "test-key") + config.Exports.BDEnabled = !config.Exports.BDEnabled + config.Exports.ValveEnabled = !config.Exports.ValveEnabled + + config.SSH.Enabled = !config.SSH.Enabled + config.SSH.Username += "x" + config.SSH.Port += 2 + config.SSH.PrivateKeyPath += "/x" + config.SSH.Password += "x" + config.SSH.UpdateInterval *= 2 + config.SSH.Timeout *= 2 + config.SSH.DemoPathFmt += "x" + + var updated domain.Config + testEndpointWithReceiver(t, router, http.MethodPut, "/api/config", config, http.StatusOK, owner, &updated) + updated.StaticConfig = configUC.Config().StaticConfig + require.EqualValues(t, config, updated) +} + +func TestConfigPermissions(t *testing.T) { + testPermissions(t, testRouter(), []permTestValues{ + { + path: "/api/config", + method: http.MethodGet, + code: http.StatusForbidden, + levels: admin, + }, + { + path: "/api/config", + method: http.MethodPut, + code: http.StatusForbidden, + levels: admin, + }, + }) +} diff --git a/internal/test/main_test.go b/internal/test/main_test.go index 8a4a8067..7652dbe3 100644 --- a/internal/test/main_test.go +++ b/internal/test/main_test.go @@ -16,6 +16,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/leighmacdonald/gbans/internal/app" "github.com/leighmacdonald/gbans/internal/asset" "github.com/leighmacdonald/gbans/internal/auth" "github.com/leighmacdonald/gbans/internal/ban" @@ -109,6 +110,10 @@ func TestMain(m *testing.M) { panic(err) } + if err := configUC.Write(testCtx, configUC.Config()); err != nil { + panic(err) + } + authRepo = auth.NewAuthRepository(databaseConn) discordUC = discord.NewDiscordUsecase(discord.NewNullDiscordRepository(), configUC) @@ -186,6 +191,7 @@ func testRouter() *gin.Engine { news.NewNewsHandler(router, newsUC, discordUC, authUC) wiki.NewWIkiHandler(router, wikiUC, authUC) votes.NewVoteHandler(router, votesUC, authUC) + config.NewConfigHandler(router, configUC, authUC, app.Version()) return router }