tests are working now

This commit is contained in:
crapStone 2023-11-17 21:14:26 +01:00 committed by crapStone
parent 155d7c4691
commit fdbbc17cca
1 changed files with 59 additions and 38 deletions

View File

@ -22,6 +22,22 @@ func runApp(t *testing.T, fn func(*cli.Context) error, args []string) {
assert.NoError(t, err) assert.NoError(t, err)
} }
// fixArrayFromCtx fixes the number of "changed" strings in a string slice according to the number of values in the context.
// This is a workaround because the cli library has a bug where the number of values in the context gets bigger the more tests are run.
func fixArrayFromCtx(ctx *cli.Context, key string, expected []string) []string {
if ctx.IsSet(key) {
ctxSlice := ctx.StringSlice(key)
if len(ctxSlice) > 1 {
for i := 1; i < len(ctxSlice); i++ {
expected = append([]string{"changed"}, expected...)
}
}
}
return expected
}
func TestReadConfigShouldReturnEmptyConfigWhenConfigArgEmpty(t *testing.T) { func TestReadConfigShouldReturnEmptyConfigWhenConfigArgEmpty(t *testing.T) {
runApp( runApp(
t, t,
@ -186,48 +202,50 @@ func TestMergeServerConfigShouldAddDefaultBlacklistedPathsToBlacklistedPaths(t *
} }
func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T) { func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T) {
runApp( for range []uint8{0, 1} {
t, runApp(
func(ctx *cli.Context) error { t,
cfg := &ServerConfig{ func(ctx *cli.Context) error {
Host: "original", cfg := &ServerConfig{
Port: 8080, Host: "original",
HttpPort: 80, Port: 8080,
HttpServerEnabled: false, HttpPort: 80,
MainDomain: "original", HttpServerEnabled: false,
RawDomain: "original", MainDomain: "original",
AllowedCorsDomains: []string{"original"}, RawDomain: "original",
BlacklistedPaths: []string{"original"}, AllowedCorsDomains: []string{"original"},
} BlacklistedPaths: []string{"original"},
}
mergeServerConfig(ctx, cfg) mergeServerConfig(ctx, cfg)
expectedConfig := &ServerConfig{ expectedConfig := &ServerConfig{
Host: "changed", Host: "changed",
Port: 8443, Port: 8443,
HttpPort: 443, HttpPort: 443,
HttpServerEnabled: true, HttpServerEnabled: true,
MainDomain: "changed", MainDomain: "changed",
RawDomain: "changed", RawDomain: "changed",
AllowedCorsDomains: []string{"changed"}, AllowedCorsDomains: fixArrayFromCtx(ctx, "allowed-cors-domains", []string{"changed"}),
BlacklistedPaths: append([]string{"changed"}, ALWAYS_BLACKLISTED_PATHS...), BlacklistedPaths: fixArrayFromCtx(ctx, "blacklisted-paths", append([]string{"changed"}, ALWAYS_BLACKLISTED_PATHS...)),
} }
assert.Equal(t, expectedConfig, cfg) assert.Equal(t, expectedConfig, cfg)
return nil return nil
}, },
[]string{ []string{
"--pages-domain", "changed", "--pages-domain", "changed",
"--raw-domain", "changed", "--raw-domain", "changed",
"--allowed-cors-domains", "changed", "--allowed-cors-domains", "changed",
"--blacklisted-paths", "changed", "--blacklisted-paths", "changed",
"--host", "changed", "--host", "changed",
"--port", "8443", "--port", "8443",
"--http-port", "443", "--http-port", "443",
"--enable-http-server", "--enable-http-server",
}, },
) )
}
} }
func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgExists(t *testing.T) { func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgExists(t *testing.T) {
@ -265,6 +283,9 @@ func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgE
pair.callback(&expectedConfig) pair.callback(&expectedConfig)
expectedConfig.BlacklistedPaths = append(expectedConfig.BlacklistedPaths, ALWAYS_BLACKLISTED_PATHS...) expectedConfig.BlacklistedPaths = append(expectedConfig.BlacklistedPaths, ALWAYS_BLACKLISTED_PATHS...)
expectedConfig.AllowedCorsDomains = fixArrayFromCtx(ctx, "allowed-cors-domains", expectedConfig.AllowedCorsDomains)
expectedConfig.BlacklistedPaths = fixArrayFromCtx(ctx, "blacklisted-paths", expectedConfig.BlacklistedPaths)
mergeServerConfig(ctx, &cfg) mergeServerConfig(ctx, &cfg)
assert.Equal(t, expectedConfig, cfg) assert.Equal(t, expectedConfig, cfg)