Skip to content

Commit e0f78c2

Browse files
committed
refactor(middleware): simplify CreateAuthMiddleware function
• Remove unused retriever parameter for clarity • Clean up related test cases to reflect changes
1 parent 68eaa07 commit e0f78c2

File tree

2 files changed

+6
-40
lines changed

2 files changed

+6
-40
lines changed

middleware/authzMiddlewares.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package middleware
22

33
import (
44
"net/http"
5-
6-
"github.com/platform-mesh/golang-commons/policy_services"
75
)
86

97
// CreateAuthMiddleware returns a slice of HTTP middleware constructors that populate
@@ -13,10 +11,7 @@ import (
1311
// 1. StoreWebToken()
1412
// 2. StoreAuthHeader()
1513
// 3. StoreSpiffeHeader()
16-
//
17-
// The `retriever` parameter is accepted for compatibility with caller signatures but is
18-
// not used by this implementation.
19-
func CreateAuthMiddleware(retriever policy_services.TenantRetriever) []func(http.Handler) http.Handler {
14+
func CreateAuthMiddleware() []func(http.Handler) http.Handler {
2015
mws := make([]func(http.Handler) http.Handler, 0, 5)
2116

2217
mws = append(mws, StoreWebToken())
Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
package middleware
22

33
import (
4-
"context"
4+
_ "context"
55
"testing"
66

77
"github.com/stretchr/testify/assert"
8-
"github.com/stretchr/testify/mock"
9-
10-
"github.com/platform-mesh/golang-commons/policy_services"
8+
_ "github.com/stretchr/testify/mock"
119
)
1210

13-
// MockTenantRetriever is a mock implementation of TenantRetriever
14-
type MockTenantRetriever struct {
15-
mock.Mock
16-
}
17-
18-
func (m *MockTenantRetriever) RetrieveTenant(ctx context.Context) (string, error) {
19-
args := m.Called(ctx)
20-
return args.String(0), args.Error(1)
21-
}
22-
2311
func TestCreateAuthMiddleware(t *testing.T) {
24-
mockRetriever := &MockTenantRetriever{}
25-
26-
middlewares := CreateAuthMiddleware(mockRetriever)
12+
middlewares := CreateAuthMiddleware()
2713

2814
// Should return 3 middlewares: StoreWebToken, StoreAuthHeader, StoreSpiffeHeader
2915
assert.Len(t, middlewares, 3)
@@ -35,7 +21,7 @@ func TestCreateAuthMiddleware(t *testing.T) {
3521
}
3622

3723
func TestCreateAuthMiddleware_WithNilRetriever(t *testing.T) {
38-
middlewares := CreateAuthMiddleware(nil)
24+
middlewares := CreateAuthMiddleware()
3925

4026
// Should still return 3 middlewares even with nil retriever
4127
assert.Len(t, middlewares, 3)
@@ -47,9 +33,7 @@ func TestCreateAuthMiddleware_WithNilRetriever(t *testing.T) {
4733
}
4834

4935
func TestCreateAuthMiddleware_ReturnsCorrectMiddlewares(t *testing.T) {
50-
mockRetriever := &MockTenantRetriever{}
51-
52-
middlewares := CreateAuthMiddleware(mockRetriever)
36+
middlewares := CreateAuthMiddleware()
5337

5438
// Verify we get exactly 3 middlewares
5539
assert.Len(t, middlewares, 3)
@@ -62,16 +46,3 @@ func TestCreateAuthMiddleware_ReturnsCorrectMiddlewares(t *testing.T) {
6246
// This is implicitly tested by the fact that the function compiles and returns without error
6347
}
6448
}
65-
66-
// Test that implements policy_services.TenantRetriever interface
67-
func TestTenantRetrieverInterface(t *testing.T) {
68-
mockRetriever := &MockTenantRetriever{}
69-
70-
// Verify our mock implements the interface
71-
var retriever policy_services.TenantRetriever = mockRetriever
72-
assert.NotNil(t, retriever)
73-
74-
// Test that we can use it with the middleware functions
75-
middlewares := CreateAuthMiddleware(retriever)
76-
assert.Len(t, middlewares, 3)
77-
}

0 commit comments

Comments
 (0)