summaryrefslogtreecommitdiff
path: root/internal/configuration/template_test.go
blob: 909b85b9d3f18d1a393d571bbb288888ad72324a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package configuration

import (
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/authelia/authelia/v4/internal/utils"
)

func TestShouldGenerateConfiguration(t *testing.T) {
	dir := t.TempDir()

	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)
}

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)
}

func TestShouldNotGenerateConfigurationOnFSAccessDenied(t *testing.T) {
	if runtime.GOOS == constWindows {
		t.Skip("skipping test due to being on windows")
	}

	dir := t.TempDir()

	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) {
	dir := t.TempDir()

	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)
}