File: config_test.go

package info (click to toggle)
golang-github-zitadel-oidc 3.44.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,520 kB
  • sloc: makefile: 5
file content (77 lines) | stat: -rw-r--r-- 1,676 bytes parent folder | download | duplicates (4)
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
package config

import (
	"fmt"
	"os"
	"testing"
)

func TestFromEnvVars(t *testing.T) {

	for _, tc := range []struct {
		name     string
		env      map[string]string
		defaults *Config
		want     *Config
	}{
		{
			name: "no vars, no default values",
			env:  map[string]string{},
			want: &Config{},
		},
		{
			name: "no vars, only defaults",
			env:  map[string]string{},
			defaults: &Config{
				Port:        "6666",
				UsersFile:   "/default/user/path",
				RedirectURI: []string{"re", "direct", "uris"},
			},
			want: &Config{
				Port:        "6666",
				UsersFile:   "/default/user/path",
				RedirectURI: []string{"re", "direct", "uris"},
			},
		},
		{
			name: "overriding default values",
			env: map[string]string{
				"PORT":         "1234",
				"USERS_FILE":   "/path/to/users",
				"REDIRECT_URI": "http://redirect/redirect",
			},
			defaults: &Config{
				Port:        "6666",
				UsersFile:   "/default/user/path",
				RedirectURI: []string{"re", "direct", "uris"},
			},
			want: &Config{
				Port:        "1234",
				UsersFile:   "/path/to/users",
				RedirectURI: []string{"http://redirect/redirect"},
			},
		},
		{
			name: "multiple redirect uris",
			env: map[string]string{
				"REDIRECT_URI": "http://host_1,http://host_2,http://host_3",
			},
			want: &Config{
				RedirectURI: []string{
					"http://host_1", "http://host_2", "http://host_3",
				},
			},
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			os.Clearenv()
			for k, v := range tc.env {
				os.Setenv(k, v)
			}
			cfg := FromEnvVars(tc.defaults)
			if fmt.Sprint(cfg) != fmt.Sprint(tc.want) {
				t.Errorf("Expected FromEnvVars()=%q, but got %q", tc.want, cfg)
			}
		})
	}
}