config_reloader_user_test.go 5.0 KB

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