Skip to content

Commit

Permalink
Merge pull request #442 from xmidt-org/normalize-deviceid
Browse files Browse the repository at this point in the history
Normalize the deviceid before it is put into the logs for ease of sea…
  • Loading branch information
schmidtw authored Aug 10, 2023
2 parents 9571284 + 17681d4 commit fe5b7d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
9 changes: 8 additions & 1 deletion transaction/transactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/gorilla/mux"
"github.com/xmidt-org/candlelight"
"github.com/xmidt-org/sallust"
"github.com/xmidt-org/wrp-go/v3"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -222,6 +223,12 @@ func getDeviceId(r *http.Request) string {
if id == "" {
id = "mac:000000000000"
}


normalized, err := wrp.ParseDeviceID(id)
if err != nil {
id = "invalid:" + id
} else {
id = string(normalized)
}
return id
}
13 changes: 11 additions & 2 deletions transaction/transactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,10 @@ func TestGetDeviceId(t *testing.T) {
desc: "Request has id",
req: func() (r *http.Request) {
r = httptest.NewRequest(http.MethodGet, "http://localhost:6100/api/v2/device/", nil)
r = mux.SetURLVars(r, map[string]string{"deviceid": "mac:112233445577"})
r = mux.SetURLVars(r, map[string]string{"deviceid": "mac:11:22:33:44:55:Aa"})
return
},
expected: "mac:112233445577",
expected: "mac:1122334455aa",
},
{
desc: "no id",
Expand All @@ -301,6 +301,15 @@ func TestGetDeviceId(t *testing.T) {
},
expected: "mac:000000000000",
},
{
desc: "invalid id",
req: func() (r *http.Request) {
r = httptest.NewRequest(http.MethodGet, "http://localhost:6100/api/v2/device/", nil)
r = mux.SetURLVars(r, map[string]string{"deviceid": "unsupported:frog"})
return
},
expected: "invalid:unsupported:frog",
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
Expand Down

0 comments on commit fe5b7d0

Please sign in to comment.