Skip to content

Commit 1951c2f

Browse files
committed
fix: Update the validator criteria to allow wifi config is empty when DHCP is false
1 parent 5b7221f commit 1951c2f

3 files changed

Lines changed: 137 additions & 1 deletion

File tree

internal/controller/httpapi/v1/profiles.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func NewProfileRoutes(handler *gin.RouterGroup, t profiles.Feature, l logger.Int
2727
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
2828
_ = v.RegisterValidation("genpasswordwone", dto.ValidateAMTPassOrGenRan)
2929
_ = v.RegisterValidation("ciraortls", dto.ValidateCIRAOrTLS)
30+
_ = v.RegisterValidation("wifidhcp", dto.ValidateWiFiDHCP)
3031
}
3132
}
3233

internal/entity/dto/v1/profile.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Profile struct {
2121
DHCPEnabled bool `json:"dhcpEnabled" example:"true"`
2222
IPSyncEnabled bool `json:"ipSyncEnabled" example:"true"`
2323
LocalWiFiSyncEnabled bool `json:"localWifiSyncEnabled" example:"true"`
24-
WiFiConfigs []ProfileWiFiConfigs `json:"wifiConfigs,omitempty" binding:"excluded_if=DHCPEnabled false,dive"`
24+
WiFiConfigs []ProfileWiFiConfigs `json:"wifiConfigs,omitempty" binding:"wifidhcp,dive"`
2525
TenantID string `json:"tenantId" example:"abc123"`
2626
TLSMode int `json:"tlsMode,omitempty" binding:"omitempty,min=1,max=4,ciraortls" example:"1"`
2727
TLSCerts *TLSCerts `json:"tlsCerts,omitempty"`
@@ -66,6 +66,18 @@ var ValidateUserConsent validator.Func = func(fl validator.FieldLevel) bool {
6666
return userConsent == "none" || userConsent == "kvm" || userConsent == "all"
6767
}
6868

69+
var ValidateWiFiDHCP validator.Func = func(fl validator.FieldLevel) bool {
70+
dhcpEnabled := fl.Parent().FieldByName("DHCPEnabled").Bool()
71+
wifiConfigs := fl.Field()
72+
73+
// If WiFiConfigs has items and DHCP is disabled, fail validation
74+
if wifiConfigs.Len() > 0 && !dhcpEnabled {
75+
return false
76+
}
77+
78+
return true
79+
}
80+
6981
type ProfileCountResponse struct {
7082
Count int `json:"totalCount"`
7183
Data []Profile `json:"data"`
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package dto
2+
3+
import (
4+
"testing"
5+
6+
"github.com/go-playground/validator/v10"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestValidateWiFiDHCP(t *testing.T) {
11+
t.Parallel()
12+
13+
validate := validator.New()
14+
err := validate.RegisterValidation("wifidhcp", ValidateWiFiDHCP)
15+
assert.NoError(t, err)
16+
17+
tests := []struct {
18+
name string
19+
dhcpEnabled bool
20+
wifiConfigs []ProfileWiFiConfigs
21+
wantErr bool
22+
}{
23+
{
24+
name: "valid with WiFi configs and DHCP enabled",
25+
dhcpEnabled: true,
26+
wifiConfigs: []ProfileWiFiConfigs{
27+
{
28+
Priority: 1,
29+
WirelessProfileName: "MyWiFiProfile",
30+
ProfileName: "MyProfile",
31+
TenantID: "tenant1",
32+
},
33+
},
34+
wantErr: false,
35+
},
36+
{
37+
name: "invalid with WiFi configs and DHCP disabled",
38+
dhcpEnabled: false,
39+
wifiConfigs: []ProfileWiFiConfigs{
40+
{
41+
Priority: 1,
42+
WirelessProfileName: "MyWiFiProfile",
43+
ProfileName: "MyProfile",
44+
TenantID: "tenant1",
45+
},
46+
},
47+
wantErr: true,
48+
},
49+
{
50+
name: "valid with no WiFi configs and DHCP enabled",
51+
dhcpEnabled: true,
52+
wifiConfigs: []ProfileWiFiConfigs{},
53+
wantErr: false,
54+
},
55+
{
56+
name: "valid with no WiFi configs and DHCP disabled",
57+
dhcpEnabled: false,
58+
wifiConfigs: []ProfileWiFiConfigs{},
59+
wantErr: false,
60+
},
61+
{
62+
name: "valid with multiple WiFi configs and DHCP enabled",
63+
dhcpEnabled: true,
64+
wifiConfigs: []ProfileWiFiConfigs{
65+
{
66+
Priority: 1,
67+
WirelessProfileName: "WiFiProfile1",
68+
ProfileName: "Profile1",
69+
TenantID: "tenant1",
70+
},
71+
{
72+
Priority: 2,
73+
WirelessProfileName: "WiFiProfile2",
74+
ProfileName: "Profile2",
75+
TenantID: "tenant1",
76+
},
77+
},
78+
wantErr: false,
79+
},
80+
{
81+
name: "invalid with multiple WiFi configs and DHCP disabled",
82+
dhcpEnabled: false,
83+
wifiConfigs: []ProfileWiFiConfigs{
84+
{
85+
Priority: 1,
86+
WirelessProfileName: "WiFiProfile1",
87+
ProfileName: "Profile1",
88+
TenantID: "tenant1",
89+
},
90+
{
91+
Priority: 2,
92+
WirelessProfileName: "WiFiProfile2",
93+
ProfileName: "Profile2",
94+
TenantID: "tenant1",
95+
},
96+
},
97+
wantErr: true,
98+
},
99+
}
100+
101+
for _, tt := range tests {
102+
t.Run(tt.name, func(t *testing.T) {
103+
t.Parallel()
104+
105+
type testStruct struct {
106+
DHCPEnabled bool `validate:"omitempty"`
107+
WiFiConfigs []ProfileWiFiConfigs `validate:"wifidhcp"`
108+
}
109+
110+
s := testStruct{
111+
DHCPEnabled: tt.dhcpEnabled,
112+
WiFiConfigs: tt.wifiConfigs,
113+
}
114+
err := validate.Struct(s)
115+
116+
if tt.wantErr {
117+
assert.Error(t, err)
118+
} else {
119+
assert.NoError(t, err)
120+
}
121+
})
122+
}
123+
}

0 commit comments

Comments
 (0)