Skip to content

Commit

Permalink
Merge pull request #88 from unpoller/mocking-devices-fix
Browse files Browse the repository at this point in the history
Mocking Devices Fix
  • Loading branch information
platinummonkey committed Jul 26, 2023
2 parents f0fa2e6 + d2c4624 commit 1203f11
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 10 deletions.
8 changes: 6 additions & 2 deletions devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (u *Unifi) GetDevices(sites []*Site) (*Devices, error) {
devices.USWs = append(devices.USWs, loopDevices.USWs...)
devices.UDMs = append(devices.UDMs, loopDevices.UDMs...)
devices.UXGs = append(devices.UXGs, loopDevices.UXGs...)
devices.PDUs = append(devices.PDUs, loopDevices.PDUs...)
}

return devices, nil
Expand Down Expand Up @@ -129,17 +130,20 @@ func (u *Unifi) parseDevices(data []json.RawMessage, site *Site) *Devices {
case "ugw", "usg": // in case they ever fix the name in the api.
u.unmarshallUSG(site, r, devices)
case "usw":
if strings.Contains(model, "PDU") {
if strings.Contains(strings.ToLower(model), "pdu") {
// this may actually happen, unifi APIs are all over the place
u.unmarshallPDU(site, r, devices)
} else {
u.unmarshallUSW(site, r, devices)
}
case "pdu":
u.unmarshallPDU(site, r, devices)
case "udm":
u.unmarshallUDM(site, r, devices)
case "uxg":
u.unmarshallUXG(site, r, devices)
default:
u.ErrorLog("unknown asset type - %v - skipping", assetType)
u.ErrorLog("unknown asset type - %v - skipping: data=%+v", assetType, string(r))
}
}

Expand Down
23 changes: 21 additions & 2 deletions mocks/mock_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,27 @@ func (m *MockHTTPTestServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
case apiDevicePath.MatchString(p):
device, err := m.mocked.GetDevices(nil)
// we need to wrap devices more so
devices := []*unifi.Devices{device}
// we need to change the format response for devices.
// it is an array of mixed types in a singular {"data": [...all]}
devices := make([]any, 0)
for _, d := range device.UAPs {
devices = append(devices, d)
}
for _, d := range device.UDMs {
devices = append(devices, d)
}
for _, d := range device.USGs {
devices = append(devices, d)
}
for _, d := range device.USWs {
devices = append(devices, d)
}
for _, d := range device.PDUs {
devices = append(devices, d)
}
for _, d := range device.UXGs {
devices = append(devices, d)
}
respondResultOrErr(w, devices, err, true)
return
case apiLoginPath.MatchString(p):
Expand Down
2 changes: 1 addition & 1 deletion pdu.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type PDU struct {
TotalMaxPower FlexInt `json:"total_max_power"`
TwoPhaseAdopt FlexBool `json:"two_phase_adopt"`
TxBytes FlexInt `json:"tx_bytes"`
Type string `json:"type"`
Type string `json:"type" fake:"{lexify:pdu}"`
Unsupported FlexBool `json:"unsupported"`
UnsupportedReason FlexInt `json:"unsupported_reason"`
Upgradeable FlexBool `json:"upgradable"`
Expand Down
2 changes: 1 addition & 1 deletion uap.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ type UAP struct {
TwoPhaseAdopt FlexBool `json:"two_phase_adopt,omitempty"`
TxBytes FlexInt `json:"tx_bytes"`
TxBytesD FlexInt `json:"tx_bytes-d"`
Type string `json:"type"`
Type string `json:"type" fake:"{lexify:uap}"`
UUptime FlexInt `json:"_uptime"`
Unsupported FlexBool `json:"unsupported"`
UnsupportedReason FlexInt `json:"unsupported_reason"`
Expand Down
2 changes: 1 addition & 1 deletion udm.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ type UDM struct {
TwoPhaseAdopt FlexBool `json:"two_phase_adopt"`
TxBytes FlexInt `json:"tx_bytes"`
TxBytesD FlexInt `json:"tx_bytes-d"`
Type string `json:"type"`
Type string `json:"type" fake:"{lexify:udmp}"`
UdapiCaps FlexInt `json:"udapi_caps"`
UnifiCare struct {
ActivationDismissed FlexBool `json:"activation_dismissed"`
Expand Down
2 changes: 1 addition & 1 deletion usg.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type USG struct {
Serial string `json:"serial"`
SiteID string `json:"site_id" fake:"{uuid}"`
SiteName string `json:"-"`
Type string `json:"type"`
Type string `json:"type" fake:"{lexify:usg}"`
UsgCaps FlexInt `json:"usg_caps"`
Version string `json:"version" fake:"{appversion}"`
RequiredVersion string `json:"required_version" fake:"{appversion}"`
Expand Down
2 changes: 1 addition & 1 deletion usw.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type USW struct {
TotalMaxPower FlexInt `json:"total_max_power"`
TwoPhaseAdopt FlexBool `json:"two_phase_adopt"`
TxBytes FlexInt `json:"tx_bytes"`
Type string `json:"type"`
Type string `json:"type" fake:"{randomstring:[usg,pdu]}"`
Unsupported FlexBool `json:"unsupported"`
UnsupportedReason FlexInt `json:"unsupported_reason"`
Upgradable FlexBool `json:"upgradable,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion uxg.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type UXG struct {
Temperatures []Temperature `json:"temperatures"`
TwoPhaseAdopt FlexBool `json:"two_phase_adopt"`
TxBytes FlexInt `json:"tx_bytes"`
Type string `json:"type"`
Type string `json:"type" fake:"{lexify:uxg}"`
UdapiCaps FlexInt `json:"udapi_caps"`
UnderscoreUptime FlexInt `json:"_uptime"`
Unsupported FlexBool `json:"unsupported"`
Expand Down

0 comments on commit 1203f11

Please sign in to comment.