Skip to content

Commit

Permalink
Add config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Jun 22, 2024
1 parent 7e621eb commit ae427e8
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 9 deletions.
3 changes: 2 additions & 1 deletion internal/config/config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
2 changes: 0 additions & 2 deletions internal/config/config_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ func (c *configUsecase) Write(ctx context.Context, config domain.Config) error {
return errReload
}

slog.Info("Wrote new config")

return nil
}

Expand Down
6 changes: 0 additions & 6 deletions internal/domain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
136 changes: 136 additions & 0 deletions internal/test/config_test.go
Original file line number Diff line number Diff line change
@@ -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,
},
})
}
6 changes: 6 additions & 0 deletions internal/test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit ae427e8

Please sign in to comment.