blob: 83e07ec15a7a7be3da4237c89d59464e0f15f085 [file] [log] [blame]
gio36b23b32024-08-25 12:20:54 +04001package installer
2
3import (
4 "testing"
5)
6
7type testKeyGen struct{}
8
gio864b4332024-09-05 13:56:47 +04009func (g testKeyGen) GenerateAuthKey(username string) (string, error) {
gio36b23b32024-08-25 12:20:54 +040010 return username, nil
11}
12
gio864b4332024-09-05 13:56:47 +040013func (g testKeyGen) ExpireKey(username, key string) error {
14 return nil
15}
16
17func (g testKeyGen) ExpireNode(username, node string) error {
18 return nil
19}
20
21func (g testKeyGen) RemoveNode(username, node string) error {
22 return nil
23}
24
gio36b23b32024-08-25 12:20:54 +040025func TestDeriveVPNAuthKey(t *testing.T) {
26 schema := structSchema{
27 "input",
28 []Field{
29 Field{"username", basicSchema{"username", KindString, false, nil}},
30 Field{"authKey", basicSchema{"authKey", KindVPNAuthKey, false, map[string]string{
31 "usernameField": "username",
32 }}},
33 },
34 false,
35 }
36 input := map[string]any{
37 "username": "foo",
38 }
39 v, err := deriveValues(input, input, schema, nil, testKeyGen{})
40 if err != nil {
41 t.Fatal(err)
42 }
43 if key, ok := v["authKey"].(string); !ok || key != "foo" {
44 t.Fatal(v)
45 }
46}
gio29f6b872024-09-08 16:14:58 +040047
48func TestDeriveVPNAuthKeyDisabled(t *testing.T) {
49 schema := structSchema{
50 "input",
51 []Field{
52 Field{"username", basicSchema{"username", KindString, false, nil}},
53 Field{"enabled", basicSchema{"enabled", KindBoolean, false, nil}},
54 Field{"authKey", basicSchema{"authKey", KindVPNAuthKey, false, map[string]string{
55 "usernameField": "username",
56 "enabledField": "enabled",
57 }}},
58 },
59 false,
60 }
61 input := map[string]any{
62 "username": "foo",
63 "enabled": false,
64 }
65 v, err := deriveValues(input, input, schema, nil, testKeyGen{})
66 if err != nil {
67 t.Fatal(err)
68 }
69 if _, ok := v["authKey"].(string); ok {
70 t.Fatal(v)
71 }
72}
73
74func TestDeriveVPNAuthKeyEnabledExplicitly(t *testing.T) {
75 schema := structSchema{
76 "input",
77 []Field{
78 Field{"username", basicSchema{"username", KindString, false, nil}},
79 Field{"enabled", basicSchema{"enabled", KindBoolean, false, nil}},
80 Field{"authKey", basicSchema{"authKey", KindVPNAuthKey, false, map[string]string{
81 "usernameField": "username",
82 "enabledField": "enabled",
83 }}},
84 },
85 false,
86 }
87 input := map[string]any{
88 "username": "foo",
89 "enabled": true,
90 }
91 v, err := deriveValues(input, input, schema, nil, testKeyGen{})
92 if err != nil {
93 t.Fatal(err)
94 }
95 if key, ok := v["authKey"].(string); !ok || key != "foo" {
96 t.Fatal(v)
97 }
98}