| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 1 | package installer |
| 2 | |
| 3 | import ( |
| gio | f6ad298 | 2024-08-23 17:42:49 +0400 | [diff] [blame] | 4 | "net" |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | type testKeyGen struct{} |
| 9 | |
| gio | 864b433 | 2024-09-05 13:56:47 +0400 | [diff] [blame] | 10 | func (g testKeyGen) GenerateAuthKey(username string) (string, error) { |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 11 | return username, nil |
| 12 | } |
| 13 | |
| gio | 864b433 | 2024-09-05 13:56:47 +0400 | [diff] [blame] | 14 | func (g testKeyGen) ExpireKey(username, key string) error { |
| 15 | return nil |
| 16 | } |
| 17 | |
| 18 | func (g testKeyGen) ExpireNode(username, node string) error { |
| 19 | return nil |
| 20 | } |
| 21 | |
| 22 | func (g testKeyGen) RemoveNode(username, node string) error { |
| 23 | return nil |
| 24 | } |
| 25 | |
| gio | f6ad298 | 2024-08-23 17:42:49 +0400 | [diff] [blame] | 26 | func (g testKeyGen) GetNodeIP(username, node string) (net.IP, error) { |
| 27 | return nil, nil |
| 28 | } |
| 29 | |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 30 | func 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 | } |
| gio | f6ad298 | 2024-08-23 17:42:49 +0400 | [diff] [blame] | 44 | v, err := deriveValues(input, input, schema, nil, nil, testKeyGen{}) |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | if key, ok := v["authKey"].(string); !ok || key != "foo" { |
| 49 | t.Fatal(v) |
| 50 | } |
| 51 | } |
| gio | 29f6b87 | 2024-09-08 16:14:58 +0400 | [diff] [blame] | 52 | |
| 53 | func 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 | } |
| gio | f6ad298 | 2024-08-23 17:42:49 +0400 | [diff] [blame] | 70 | v, err := deriveValues(input, input, schema, nil, nil, testKeyGen{}) |
| gio | 29f6b87 | 2024-09-08 16:14:58 +0400 | [diff] [blame] | 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | if _, ok := v["authKey"].(string); ok { |
| 75 | t.Fatal(v) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func 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 | } |
| gio | f6ad298 | 2024-08-23 17:42:49 +0400 | [diff] [blame] | 96 | v, err := deriveValues(input, input, schema, nil, nil, testKeyGen{}) |
| gio | 29f6b87 | 2024-09-08 16:14:58 +0400 | [diff] [blame] | 97 | if err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | if key, ok := v["authKey"].(string); !ok || key != "foo" { |
| 101 | t.Fatal(v) |
| 102 | } |
| 103 | } |