Skip to content

Commit 9db5d1e

Browse files
authored
Tests for pkg/utils (#3155)
Signed-off-by: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com>
1 parent 4cadaf7 commit 9db5d1e

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

pkg/logger/message/audit/entry.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"strings"
2323
"time"
2424

25+
"github.com/google/uuid"
26+
2527
"github.com/golang-jwt/jwt/v4"
2628

2729
"github.com/minio/console/pkg/utils"
@@ -100,7 +102,7 @@ func ToEntry(w http.ResponseWriter, r *http.Request, reqClaims map[string]interf
100102
var requestID interface{}
101103
requestID = r.Context().Value(utils.ContextRequestID)
102104
if requestID == nil {
103-
requestID, _ = utils.NewUUID()
105+
requestID = uuid.NewString()
104106
}
105107
entry.RequestID = requestID.(string)
106108

pkg/utils/utils.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,8 @@ package utils
1919
import (
2020
"context"
2121
"encoding/base64"
22-
23-
"github.com/google/uuid"
2422
)
2523

26-
// NewUUID - get a random UUID.
27-
func NewUUID() (string, error) {
28-
u, err := uuid.NewRandom()
29-
if err != nil {
30-
return "", err
31-
}
32-
return u.String(), nil
33-
}
34-
3524
// DecodeBase64 : decoded base64 input into utf-8 text
3625
func DecodeBase64(s string) (string, error) {
3726
decodedInput, err := base64.StdEncoding.DecodeString(s)

pkg/utils/utils_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package utils
1818

19-
import "testing"
19+
import (
20+
"context"
21+
"testing"
22+
)
2023

2124
func TestDecodeInput(t *testing.T) {
2225
type args struct {
@@ -90,3 +93,36 @@ func TestDecodeInput(t *testing.T) {
9093
})
9194
}
9295
}
96+
97+
func TestClientIPFromContext(t *testing.T) {
98+
type args struct {
99+
ctx context.Context
100+
}
101+
tests := []struct {
102+
name string
103+
args args
104+
want string
105+
}{
106+
{
107+
name: "working return",
108+
args: args{
109+
ctx: context.Background(),
110+
},
111+
want: "127.0.0.1",
112+
},
113+
{
114+
name: "context contains ip",
115+
args: args{
116+
ctx: context.WithValue(context.Background(), ContextClientIP, "10.0.0.1"),
117+
},
118+
want: "10.0.0.1",
119+
},
120+
}
121+
for _, tt := range tests {
122+
t.Run(tt.name, func(t *testing.T) {
123+
if got := ClientIPFromContext(tt.args.ctx); got != tt.want {
124+
t.Errorf("ClientIPFromContext() = %v, want %v", got, tt.want)
125+
}
126+
})
127+
}
128+
}

restapi/configure_console.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import (
3535
"sync"
3636
"time"
3737

38+
"github.com/google/uuid"
39+
3840
"github.com/minio/console/pkg/logger"
3941
"github.com/minio/console/pkg/utils"
4042
"github.com/minio/minio-go/v7/pkg/credentials"
@@ -192,11 +194,7 @@ func setupMiddlewares(handler http.Handler) http.Handler {
192194

193195
func ContextMiddleware(next http.Handler) http.Handler {
194196
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
195-
requestID, err := utils.NewUUID()
196-
if err != nil && err != auth.ErrNoAuthToken {
197-
http.Error(w, err.Error(), http.StatusInternalServerError)
198-
return
199-
}
197+
requestID := uuid.NewString()
200198
ctx := context.WithValue(r.Context(), utils.ContextRequestID, requestID)
201199
ctx = context.WithValue(ctx, utils.ContextRequestUserAgent, r.UserAgent())
202200
ctx = context.WithValue(ctx, utils.ContextRequestHost, r.Host)

0 commit comments

Comments
 (0)