1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-09-04 05:56:23 -07:00
gochan/pkg/config/config_test.go

73 lines
1.6 KiB
Go
Raw Normal View History

package config
import (
"encoding/json"
"strings"
"testing"
2024-05-17 13:45:32 -07:00
_ "github.com/go-sql-driver/mysql"
"github.com/gochan-org/gochan/pkg/gcutil/testutil"
2024-05-17 13:45:32 -07:00
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
)
func TestBadTypes(t *testing.T) {
var c GochanConfig
err := json.NewDecoder(strings.NewReader(badTypeJSON)).Decode(&c)
assert.Error(t, err)
}
func TestValidJSON(t *testing.T) {
var c GochanConfig
err := json.NewDecoder(strings.NewReader(validCfgJSON)).Decode(&c)
2024-05-17 12:45:15 -07:00
assert.NoError(t, err)
}
2024-05-17 13:45:32 -07:00
func TestValidateValues(t *testing.T) {
testutil.GoToGochanRoot(t)
InitConfig()
2024-05-17 13:45:32 -07:00
SetRandomSeed("test")
assert.NoError(t, cfg.ValidateValues())
cfg.CookieMaxAge = "not a duration"
assert.Error(t, cfg.ValidateValues())
cfg.CookieMaxAge = "1y"
assert.NoError(t, cfg.ValidateValues())
SetTestDBConfig("not a valid driver", "127.0.0.1", "gochan", "gochan", "", "")
assert.Error(t, cfg.ValidateValues())
SetTestDBConfig("postgresql", "127.0.0.1", "gochan", "gochan", "", "")
assert.NoError(t, cfg.ValidateValues())
}
type webRootTest struct {
webRoot string
pathArgs []string
expectPath string
}
func TestWebPath(t *testing.T) {
testutil.GoToGochanRoot(t)
InitConfig()
2024-05-17 13:45:32 -07:00
testCases := []webRootTest{
{
webRoot: "/",
pathArgs: []string{"b", "res", "1234.html"},
expectPath: "/b/res/1234.html",
},
{
webRoot: "/chan",
pathArgs: []string{"b", "res", "1234.html"},
expectPath: "/chan/b/res/1234.html",
},
}
for _, tC := range testCases {
t.Run(tC.expectPath, func(t *testing.T) {
cfg.WebRoot = tC.webRoot
wp := WebPath(tC.pathArgs...)
assert.Equal(t, tC.expectPath, wp)
})
}
}