Skip to content

Commit

Permalink
Tests for pkg/utils (#3155)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Valdivia <[email protected]>
  • Loading branch information
dvaldivia authored Dec 11, 2023
1 parent 4cadaf7 commit 9db5d1e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
4 changes: 3 additions & 1 deletion pkg/logger/message/audit/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"strings"
"time"

"github.com/google/uuid"

"github.com/golang-jwt/jwt/v4"

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

Expand Down
11 changes: 0 additions & 11 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,8 @@ package utils
import (
"context"
"encoding/base64"

"github.com/google/uuid"
)

// NewUUID - get a random UUID.
func NewUUID() (string, error) {
u, err := uuid.NewRandom()
if err != nil {
return "", err
}
return u.String(), nil
}

// DecodeBase64 : decoded base64 input into utf-8 text
func DecodeBase64(s string) (string, error) {
decodedInput, err := base64.StdEncoding.DecodeString(s)
Expand Down
38 changes: 37 additions & 1 deletion pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package utils

import "testing"
import (
"context"
"testing"
)

func TestDecodeInput(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -90,3 +93,36 @@ func TestDecodeInput(t *testing.T) {
})
}
}

func TestClientIPFromContext(t *testing.T) {
type args struct {
ctx context.Context
}
tests := []struct {
name string
args args
want string
}{
{
name: "working return",
args: args{
ctx: context.Background(),
},
want: "127.0.0.1",
},
{
name: "context contains ip",
args: args{
ctx: context.WithValue(context.Background(), ContextClientIP, "10.0.0.1"),
},
want: "10.0.0.1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ClientIPFromContext(tt.args.ctx); got != tt.want {
t.Errorf("ClientIPFromContext() = %v, want %v", got, tt.want)
}
})
}
}
8 changes: 3 additions & 5 deletions restapi/configure_console.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
"sync"
"time"

"github.com/google/uuid"

"github.com/minio/console/pkg/logger"
"github.com/minio/console/pkg/utils"
"github.com/minio/minio-go/v7/pkg/credentials"
Expand Down Expand Up @@ -192,11 +194,7 @@ func setupMiddlewares(handler http.Handler) http.Handler {

func ContextMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestID, err := utils.NewUUID()
if err != nil && err != auth.ErrNoAuthToken {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
requestID := uuid.NewString()
ctx := context.WithValue(r.Context(), utils.ContextRequestID, requestID)
ctx = context.WithValue(ctx, utils.ContextRequestUserAgent, r.UserAgent())
ctx = context.WithValue(ctx, utils.ContextRequestHost, r.Host)
Expand Down

0 comments on commit 9db5d1e

Please sign in to comment.