summaryrefslogtreecommitdiff
path: root/cmd/authelia-gen/helpers_test.go
blob: a6aac0ccc1c2ed59a80f2d09e3aaaaa08778471c (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main

import (
	"net/mail"
	"reflect"
	"testing"

	"github.com/spf13/pflag"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/authelia/authelia/v4/internal/configuration/schema"
)

func TestGetPFlagPath(t *testing.T) {
	testCases := []struct {
		name     string
		have     func(t *testing.T) *pflag.FlagSet
		names    []string
		expected string
		err      string
	}{
		{
			"ShouldFailEmptyFlagSet",
			func(t *testing.T) *pflag.FlagSet {
				return pflag.NewFlagSet("example", pflag.ContinueOnError)
			},
			[]string{"abc", "123"},
			"",
			"failed to lookup flag 'abc': flag accessed but not defined: abc",
		},
		{
			"ShouldFailEmptyFlagNames",
			func(t *testing.T) *pflag.FlagSet {
				return pflag.NewFlagSet("example", pflag.ContinueOnError)
			},
			nil,
			"",
			"no flag names",
		},
		{
			"ShouldLookupFlagNames",
			func(t *testing.T) *pflag.FlagSet {
				flagset := pflag.NewFlagSet("example", pflag.ContinueOnError)

				flagset.String("dir.one", "", "")
				flagset.String("dir.two", "", "")
				flagset.String("file.name", "", "")

				require.NoError(t, flagset.Parse([]string{"--dir.one=abc", "--dir.two=123", "--file.name=path.txt"}))

				return flagset
			},
			[]string{"dir.one", "dir.two", "file.name"},
			"abc/123/path.txt",
			"",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			actual, theError := getPFlagPath(tc.have(t), tc.names...)

			if tc.err == "" {
				assert.NoError(t, theError)
				assert.Equal(t, tc.expected, actual)
			} else {
				assert.EqualError(t, theError, tc.err)
				assert.Equal(t, "", actual)
			}
		})
	}
}

func TestBuildCSP(t *testing.T) {
	testCases := []struct {
		name     string
		have     string
		ruleSets [][]CSPValue
		expected string
	}{
		{
			"ShouldParseDefault",
			codeCSPProductionDefaultSrc,
			[][]CSPValue{
				codeCSPValuesCommon,
				codeCSPValuesProduction,
			},
			"default-src 'self'; frame-src 'none'; object-src 'none'; style-src 'self' 'nonce-%s'; frame-ancestors 'none'; base-uri 'self'",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			assert.Equal(t, tc.expected, buildCSP(tc.have, tc.ruleSets...))
		})
	}
}

func TestContainsType(t *testing.T) {
	astring := ""

	testCases := []struct {
		name     string
		have     any
		expected bool
	}{
		{
			"ShouldContainMailAddress",
			mail.Address{},
			true,
		},
		{
			"ShouldContainSchemaAddressPtr",
			&schema.Address{},
			true,
		},
		{
			"ShouldNotContainString",
			astring,
			false,
		},
		{
			"ShouldNotContainStringPtr",
			&astring,
			false,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			assert.Equal(t, tc.expected, containsType(reflect.TypeOf(tc.have), decodedTypes))
		})
	}
}

func TestReadTags(t *testing.T) {
	assert.NotPanics(t, func() {
		iReadTags("", reflect.TypeOf(schema.Configuration{}), false, false, false)
	})

	assert.NotPanics(t, func() {
		iReadTags("", reflect.TypeOf(schema.Configuration{}), true, true, false)
	})
}