Micro-service for file storage and processing written in Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.3 KiB

  1. package settings
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/spf13/afero"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestSettingsParsing(t *testing.T) {
  10. const file string = `{
  11. "endpoint": "0.0.0.0:8000",
  12. "token": "foobar",
  13. "rate_limiter": {
  14. "enabled": true,
  15. "requests_per_minute": 20,
  16. "allowed_burst": 5
  17. },
  18. "storage_provider": {
  19. "type": 0,
  20. "base_path": "assets"
  21. }
  22. }`
  23. t.Run("Settings parsing is successful", func(t *testing.T) {
  24. settings := parseSettings([]byte(file))
  25. assert.Equal(t, "0.0.0.0:8000", settings.Endpoint)
  26. assert.Equal(t, "foobar", settings.Token)
  27. assert.Equal(t, "assets", settings.StorageProvider.BasePath)
  28. })
  29. }
  30. func TestSettingsLoading(t *testing.T) {
  31. t.Run("Settings loading creates default settings.json when none is present", func(t *testing.T) {
  32. fileSystem := afero.NewMemMapFs()
  33. workingDirectory, _ := os.Getwd()
  34. path := filepath.Join(workingDirectory, "settings.json")
  35. // Settings file does not exist in the beginning
  36. doesFileExist, _ := afero.Exists(fileSystem, path)
  37. assert.False(t, doesFileExist)
  38. _, err := LoadSettings(fileSystem)
  39. assert.Nil(t, err)
  40. // Settings file should be present after calling LoadSettings
  41. doesFileExist, _ = afero.Exists(fileSystem, path)
  42. assert.True(t, doesFileExist)
  43. })
  44. }