config_reloader_user_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package config
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/knadh/koanf/parsers/yaml"
  6. "github.com/knadh/koanf/providers/file"
  7. "github.com/knadh/koanf/v2"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestUserLoadingFromConfig(t *testing.T) {
  12. // Create a temporary test config file
  13. testConfig := `
  14. authLocalUsers:
  15. enabled: true
  16. users:
  17. - username: testuser1
  18. usergroup: admin
  19. password: password1
  20. - username: testuser2
  21. usergroup: guest
  22. password: password2
  23. actions:
  24. - title: Test Action
  25. shell: echo "test"
  26. `
  27. // Create temporary file
  28. tmpFile, err := os.CreateTemp("", "test_config_*.yaml")
  29. require.NoError(t, err, "Should create temporary file")
  30. defer func() { _ = os.Remove(tmpFile.Name()) }()
  31. // Write test config to file
  32. _, err = tmpFile.WriteString(testConfig)
  33. require.NoError(t, err, "Should write test config to file")
  34. require.NoError(t, tmpFile.Close())
  35. // Load config using koanf
  36. k := koanf.New(".")
  37. err = k.Load(file.Provider(tmpFile.Name()), yaml.Parser())
  38. require.NoError(t, err, "Should load config file")
  39. // Create config struct and load it
  40. cfg := &Config{}
  41. AppendSource(cfg, k, tmpFile.Name())
  42. // Test that authLocalUsers was loaded correctly
  43. assert.True(t, cfg.AuthLocalUsers.Enabled, "AuthLocalUsers should be enabled")
  44. assert.Len(t, cfg.AuthLocalUsers.Users, 2, "Should load 2 users")
  45. // Test individual users
  46. user1 := cfg.FindUserByUsername("testuser1")
  47. assert.NotNil(t, user1, "Should find testuser1")
  48. assert.Equal(t, "testuser1", user1.Username, "User1 should have correct username")
  49. assert.Equal(t, "admin", user1.Usergroup, "User1 should have correct usergroup")
  50. assert.Equal(t, "password1", user1.Password, "User1 should have correct password")
  51. user2 := cfg.FindUserByUsername("testuser2")
  52. assert.NotNil(t, user2, "Should find testuser2")
  53. assert.Equal(t, "testuser2", user2.Username, "User2 should have correct username")
  54. assert.Equal(t, "guest", user2.Usergroup, "User2 should have correct usergroup")
  55. assert.Equal(t, "password2", user2.Password, "User2 should have correct password")
  56. // Test non-existent user
  57. assert.Nil(t, cfg.FindUserByUsername("nonexistent"), "Should return nil for non-existent user")
  58. }
  59. func TestUserLoadingWithEmptyUsers(t *testing.T) {
  60. // Test config with enabled but no users
  61. testConfig := `
  62. authLocalUsers:
  63. enabled: true
  64. users: []
  65. actions:
  66. - title: Test Action
  67. shell: echo "test"
  68. `
  69. tmpFile, err := os.CreateTemp("", "test_config_empty_*.yaml")
  70. require.NoError(t, err, "Should create temporary file")
  71. defer func() { _ = os.Remove(tmpFile.Name()) }()
  72. _, err = tmpFile.WriteString(testConfig)
  73. require.NoError(t, err, "Should write test config to file")
  74. require.NoError(t, tmpFile.Close())
  75. k := koanf.New(".")
  76. err = k.Load(file.Provider(tmpFile.Name()), yaml.Parser())
  77. require.NoError(t, err, "Should load config file")
  78. cfg := &Config{}
  79. AppendSource(cfg, k, tmpFile.Name())
  80. assert.True(t, cfg.AuthLocalUsers.Enabled, "AuthLocalUsers should be enabled")
  81. assert.Empty(t, cfg.AuthLocalUsers.Users, "Should have 0 users")
  82. assert.Nil(t, cfg.FindUserByUsername("anyuser"), "Should return nil for any user")
  83. }
  84. func TestUserLoadingWithDisabledAuth(t *testing.T) {
  85. // Test config with disabled auth
  86. testConfig := `
  87. authLocalUsers:
  88. enabled: false
  89. users:
  90. - username: testuser
  91. usergroup: admin
  92. password: password
  93. actions:
  94. - title: Test Action
  95. shell: echo "test"
  96. `
  97. tmpFile, err := os.CreateTemp("", "test_config_disabled_*.yaml")
  98. require.NoError(t, err, "Should create temporary file")
  99. defer func() { _ = os.Remove(tmpFile.Name()) }()
  100. _, err = tmpFile.WriteString(testConfig)
  101. require.NoError(t, err, "Should write test config to file")
  102. require.NoError(t, tmpFile.Close())
  103. k := koanf.New(".")
  104. err = k.Load(file.Provider(tmpFile.Name()), yaml.Parser())
  105. require.NoError(t, err, "Should load config file")
  106. cfg := &Config{}
  107. AppendSource(cfg, k, tmpFile.Name())
  108. assert.False(t, cfg.AuthLocalUsers.Enabled, "AuthLocalUsers should be disabled")
  109. assert.Len(t, cfg.AuthLocalUsers.Users, 1, "Should still load users even when disabled")
  110. // User should still be findable even when auth is disabled
  111. user := cfg.FindUserByUsername("testuser")
  112. assert.NotNil(t, user, "Should find user even when auth is disabled")
  113. }
  114. func TestUserLoadingWithoutAuthSection(t *testing.T) {
  115. // Test config without authLocalUsers section
  116. testConfig := `
  117. actions:
  118. - title: Test Action
  119. shell: echo "test"
  120. `
  121. tmpFile, err := os.CreateTemp("", "test_config_no_auth_*.yaml")
  122. require.NoError(t, err, "Should create temporary file")
  123. defer func() { _ = os.Remove(tmpFile.Name()) }()
  124. _, err = tmpFile.WriteString(testConfig)
  125. require.NoError(t, err, "Should write test config to file")
  126. require.NoError(t, tmpFile.Close())
  127. k := koanf.New(".")
  128. err = k.Load(file.Provider(tmpFile.Name()), yaml.Parser())
  129. require.NoError(t, err, "Should load config file")
  130. cfg := &Config{}
  131. AppendSource(cfg, k, tmpFile.Name())
  132. // Should have default values
  133. assert.False(t, cfg.AuthLocalUsers.Enabled, "AuthLocalUsers should be disabled by default")
  134. assert.Empty(t, cfg.AuthLocalUsers.Users, "Should have 0 users by default")
  135. assert.Nil(t, cfg.FindUserByUsername("anyuser"), "Should return nil for any user")
  136. }