package settings import ( "os" "path/filepath" "testing" "github.com/spf13/afero" "github.com/stretchr/testify/assert" ) func TestSettingsParsing(t *testing.T) { const file string = `{ "endpoint": "0.0.0.0:8000", "authentication": { "enabled": true, "token": "foobar" }, "rate_limiter": { "enabled": true, "requests_per_minute": 20, "allowed_burst": 5 }, "storage_provider": { "type": 0, "base_path": "assets" } }` t.Run("Settings parsing is successful", func(t *testing.T) { settings := parseSettings([]byte(file)) assert.Equal(t, "0.0.0.0:8000", settings.Endpoint) assert.Equal(t, "foobar", settings.Authentication.Token) assert.Equal(t, "assets", settings.StorageProvider.BasePath) }) } func TestSettingsLoading(t *testing.T) { t.Run("Settings loading creates default settings.json when none is present", func(t *testing.T) { fileSystem := afero.NewMemMapFs() workingDirectory, _ := os.Getwd() path := filepath.Join(workingDirectory, "settings.json") // Settings file does not exist in the beginning doesFileExist, _ := afero.Exists(fileSystem, path) assert.False(t, doesFileExist) _, err := LoadSettings(fileSystem) assert.Nil(t, err) // Settings file should be present after calling LoadSettings doesFileExist, _ = afero.Exists(fileSystem, path) assert.True(t, doesFileExist) }) }