blob: d9a873b046c2834b263ac706c97e67ddc8ea2e00 [file] [log] [blame]
gio36b23b32024-08-25 12:20:54 +04001package installer
2
3import (
giof6ad2982024-08-23 17:42:49 +04004 "net"
gio36b23b32024-08-25 12:20:54 +04005 "testing"
6)
7
8type testKeyGen struct{}
9
gio864b4332024-09-05 13:56:47 +040010func (g testKeyGen) GenerateAuthKey(username string) (string, error) {
gio36b23b32024-08-25 12:20:54 +040011 return username, nil
12}
13
gio864b4332024-09-05 13:56:47 +040014func (g testKeyGen) ExpireKey(username, key string) error {
15 return nil
16}
17
18func (g testKeyGen) ExpireNode(username, node string) error {
19 return nil
20}
21
22func (g testKeyGen) RemoveNode(username, node string) error {
23 return nil
24}
25
giof6ad2982024-08-23 17:42:49 +040026func (g testKeyGen) GetNodeIP(username, node string) (net.IP, error) {
27 return nil, nil
28}
29
gio36b23b32024-08-25 12:20:54 +040030func TestDeriveVPNAuthKey(t *testing.T) {
31 schema := structSchema{
32 "input",
33 []Field{
34 Field{"username", basicSchema{"username", KindString, false, nil}},
35 Field{"authKey", basicSchema{"authKey", KindVPNAuthKey, false, map[string]string{
36 "usernameField": "username",
37 }}},
38 },
39 false,
40 }
41 input := map[string]any{
42 "username": "foo",
43 }
giof6ad2982024-08-23 17:42:49 +040044 v, err := deriveValues(input, input, schema, nil, nil, testKeyGen{})
gio36b23b32024-08-25 12:20:54 +040045 if err != nil {
46 t.Fatal(err)
47 }
48 if key, ok := v["authKey"].(string); !ok || key != "foo" {
49 t.Fatal(v)
50 }
51}
gio29f6b872024-09-08 16:14:58 +040052
53func TestDeriveVPNAuthKeyDisabled(t *testing.T) {
54 schema := structSchema{
55 "input",
56 []Field{
57 Field{"username", basicSchema{"username", KindString, false, nil}},
58 Field{"enabled", basicSchema{"enabled", KindBoolean, false, nil}},
59 Field{"authKey", basicSchema{"authKey", KindVPNAuthKey, false, map[string]string{
60 "usernameField": "username",
61 "enabledField": "enabled",
62 }}},
63 },
64 false,
65 }
66 input := map[string]any{
67 "username": "foo",
68 "enabled": false,
69 }
giof6ad2982024-08-23 17:42:49 +040070 v, err := deriveValues(input, input, schema, nil, nil, testKeyGen{})
gio29f6b872024-09-08 16:14:58 +040071 if err != nil {
72 t.Fatal(err)
73 }
74 if _, ok := v["authKey"].(string); ok {
75 t.Fatal(v)
76 }
77}
78
79func TestDeriveVPNAuthKeyEnabledExplicitly(t *testing.T) {
80 schema := structSchema{
81 "input",
82 []Field{
83 Field{"username", basicSchema{"username", KindString, false, nil}},
84 Field{"enabled", basicSchema{"enabled", KindBoolean, false, nil}},
85 Field{"authKey", basicSchema{"authKey", KindVPNAuthKey, false, map[string]string{
86 "usernameField": "username",
87 "enabledField": "enabled",
88 }}},
89 },
90 false,
91 }
92 input := map[string]any{
93 "username": "foo",
94 "enabled": true,
95 }
giof6ad2982024-08-23 17:42:49 +040096 v, err := deriveValues(input, input, schema, nil, nil, testKeyGen{})
gio29f6b872024-09-08 16:14:58 +040097 if err != nil {
98 t.Fatal(err)
99 }
100 if key, ok := v["authKey"].(string); !ok || key != "foo" {
101 t.Fatal(v)
102 }
103}