all repos

mugit @ 20a3418b9ff77829a78332c5a71bcff0f3febc3f

🐮 git server that your cow will love

mugit/internal/config/validate_test.go(view raw)

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
package config

import (
	"testing"

	"olexsmir.xyz/x/is"
)

func TestCheckPort(t *testing.T) {
	is.Err(t, checkPort(1), nil)
	is.Err(t, checkPort(80), nil)
	is.Err(t, checkPort(65535), nil)

	is.Err(t, checkPort(0), "must be between")
	is.Err(t, checkPort(-1), "must be between")
	is.Err(t, checkPort(65536), "must be between")
}

func TestConfig_Validate(t *testing.T) {
	hostKey := "testdata/hostkey"
	tests := []struct {
		name     string
		expected any
		c        Config
	}{
		{
			name: "minimal",
			c: Config{
				Meta: MetaConfig{Host: "example.com"},
				Repo: RepoConfig{Dir: t.TempDir()},
			},
		},
		{
			name: "minimal with ssh",
			c: Config{
				Meta: MetaConfig{Host: "example.com"},
				Repo: RepoConfig{Dir: t.TempDir()},
				SSH: SSHConfig{
					Enable:  true,
					HostKey: hostKey,
				},
			},
		},
		{
			name:     "not set meta.host",
			expected: "meta.host is required",
			c: Config{
				Repo: RepoConfig{Dir: t.TempDir()},
			},
		},
		{
			name:     "invalid meta.host",
			expected: "meta.host shouldn't include protocol",
			c: Config{
				Meta: MetaConfig{Host: "https://example.com"},
				Repo: RepoConfig{Dir: t.TempDir()},
			},
		},
		{
			name:     "invalid repo.dir",
			expected: "repo.dir",
			c: Config{
				Meta: MetaConfig{Host: "example.com"},
				Repo: RepoConfig{Dir: "nonexistent"},
			},
		},
		{
			name:     "invalid server port",
			expected: "server.port",
			c: Config{
				Meta:   MetaConfig{Host: "example.com"},
				Repo:   RepoConfig{Dir: t.TempDir()},
				Server: ServerConfig{Port: -1},
			},
		},
		{
			name:     "invalid ssh port",
			expected: "ssh.port",
			c: Config{
				Meta: MetaConfig{Host: "example.com"},
				Repo: RepoConfig{Dir: t.TempDir()},
				SSH: SSHConfig{
					Enable:  true,
					HostKey: hostKey,
					Port:    100000,
				},
			},
		},
		{
			name:     "same ssh and http ports",
			expected: "ssh.port must differ",
			c: Config{
				Meta:   MetaConfig{Host: "example.com"},
				Repo:   RepoConfig{Dir: t.TempDir()},
				SSH:    SSHConfig{Enable: true, Port: 228},
				Server: ServerConfig{Port: 228},
			},
		},
		{
			name:     "invalid ssh.host_key path",
			expected: "ssh.host_key",
			c: Config{
				Meta: MetaConfig{Host: "example.com"},
				Repo: RepoConfig{Dir: t.TempDir()},
				SSH: SSHConfig{
					Enable:  true,
					HostKey: "/somewhere",
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tt.c.ensureDefaults()
			err := tt.c.validate()
			is.Err(t, err, tt.expected)
		})
	}
}