2021-08-03 09:55:21 +00:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2021-08-03 09:55:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShouldGenerateConfiguration(t *testing.T) {
|
2022-12-04 09:57:28 +00:00
|
|
|
dir := t.TempDir()
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
cfg := filepath.Join(dir, "config.yml")
|
|
|
|
|
|
|
|
created, err := EnsureConfigurationExists(cfg)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, created)
|
|
|
|
|
|
|
|
_, err = os.Stat(cfg)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:15:28 +00:00
|
|
|
func TestNotShouldGenerateConfigurationifExists(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
cfg := filepath.Join(dir, "config.yml")
|
|
|
|
|
|
|
|
created, err := EnsureConfigurationExists(cfg)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, created)
|
|
|
|
|
|
|
|
created, err = EnsureConfigurationExists(cfg)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, created)
|
|
|
|
|
|
|
|
_, err = os.Stat(cfg)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
func TestShouldNotGenerateConfigurationOnFSAccessDenied(t *testing.T) {
|
|
|
|
if runtime.GOOS == constWindows {
|
|
|
|
t.Skip("skipping test due to being on windows")
|
|
|
|
}
|
|
|
|
|
2022-12-04 09:57:28 +00:00
|
|
|
dir := t.TempDir()
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
assert.NoError(t, os.Mkdir(filepath.Join(dir, "zero"), 0000))
|
|
|
|
|
|
|
|
cfg := filepath.Join(dir, "zero", "config.yml")
|
|
|
|
|
|
|
|
created, err := EnsureConfigurationExists(cfg)
|
|
|
|
assert.EqualError(t, err, fmt.Sprintf("error occurred generating configuration: stat %s: permission denied", cfg))
|
|
|
|
assert.False(t, created)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotGenerateConfiguration(t *testing.T) {
|
2022-12-04 09:57:28 +00:00
|
|
|
dir := t.TempDir()
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
cfg := filepath.Join(dir, "..", "not-a-dir", "config.yml")
|
|
|
|
|
|
|
|
created, err := EnsureConfigurationExists(cfg)
|
|
|
|
|
|
|
|
expectedErr := fmt.Sprintf(utils.GetExpectedErrTxt("pathnotfound"), cfg)
|
|
|
|
|
|
|
|
assert.EqualError(t, err, fmt.Sprintf(errFmtGenerateConfiguration, expectedErr))
|
|
|
|
assert.False(t, created)
|
|
|
|
}
|