-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadom_test.go
More file actions
145 lines (132 loc) · 3.39 KB
/
Copy pathadom_test.go
File metadata and controls
145 lines (132 loc) · 3.39 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package fortimgr
import (
"context"
"testing"
)
func TestListADOMs(t *testing.T) {
t.Run("not logged in", func(t *testing.T) {
c, _ := NewClient("https://example.com", WithCredentials("u", "p"))
_, err := c.ListADOMs(context.Background())
if err != ErrNotLoggedIn {
t.Errorf("err = %v, want ErrNotLoggedIn", err)
}
})
// Full fixture: 3 real ADOMs + 2 factory-preset ADOMs the session cannot access.
forwardFixtures := map[string]string{
"/dvmdb/adom": `[
{
"name": "root",
"desc": "Default ADOM",
"state": 1,
"mode": 1,
"os_ver": 7,
"mr": 4
},
{
"name": "customer-a",
"desc": "Customer A tenant",
"state": "enabled",
"mode": 2,
"os_ver": 7,
"mr": 2
},
{
"name": "disabled-adom",
"desc": "",
"state": 0,
"mode": 1,
"os_ver": 0,
"mr": 0
},
{
"name": "FortiAnalyzer",
"desc": "",
"state": 1,
"mode": 1,
"os_ver": 7,
"mr": 0
},
{
"name": "FortiMail",
"desc": "",
"state": 1,
"mode": 1,
"os_ver": 7,
"mr": 0
}
]`,
}
// Session scope: current ADOM is "root", plus "customer-a" and "disabled-adom" accessible.
// The factory presets (FortiAnalyzer, FortiMail) are not in scope.
proxyFixtures := map[string]string{
"/gui/sys/config": `{
"admin_user_name": "admin",
"adom": {"name": "root"},
"adoms": ["customer-a", "disabled-adom"]
}`,
}
t.Run("filtered to session scope", func(t *testing.T) {
client := newTestClientWithProxy(t, forwardFixtures, proxyFixtures)
adoms, err := client.ListADOMs(context.Background())
if err != nil {
t.Fatal(err)
}
if len(adoms) != 3 {
t.Fatalf("len = %d, want 3 (factory presets should be filtered)", len(adoms))
}
got := map[string]ADOM{}
for _, a := range adoms {
got[a.Name] = a
}
for _, name := range []string{"root", "customer-a", "disabled-adom"} {
if _, ok := got[name]; !ok {
t.Errorf("missing %q in filtered result", name)
}
}
for _, name := range []string{"FortiAnalyzer", "FortiMail"} {
if _, ok := got[name]; ok {
t.Errorf("factory preset %q leaked through filter", name)
}
}
// Spot-check field decoding still works after filtering.
a := got["root"]
if a.Desc != "Default ADOM" {
t.Errorf("Desc = %q", a.Desc)
}
if a.State != "enabled" {
t.Errorf("State = %q, want \"enabled\"", a.State)
}
if a.Mode != "normal" {
t.Errorf("Mode = %q, want \"normal\"", a.Mode)
}
if a.OSVer != "7.4" {
t.Errorf("OSVer = %q, want \"7.4\"", a.OSVer)
}
if got["customer-a"].Mode != "backup" || got["customer-a"].OSVer != "7.2" {
t.Errorf("customer-a = %+v", got["customer-a"])
}
if got["disabled-adom"].State != "disabled" {
t.Errorf("disabled-adom state = %q", got["disabled-adom"].State)
}
})
t.Run("all=true returns global list", func(t *testing.T) {
client := newTestClientWithProxy(t, forwardFixtures, proxyFixtures)
adoms, err := client.ListADOMs(context.Background(), true)
if err != nil {
t.Fatal(err)
}
if len(adoms) != 5 {
t.Fatalf("len = %d, want 5 (all ADOMs including factory presets)", len(adoms))
}
})
t.Run("all=false is equivalent to default", func(t *testing.T) {
client := newTestClientWithProxy(t, forwardFixtures, proxyFixtures)
adoms, err := client.ListADOMs(context.Background(), false)
if err != nil {
t.Fatal(err)
}
if len(adoms) != 3 {
t.Fatalf("len = %d, want 3", len(adoms))
}
})
}