forked from cloudtrust/common-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
163 lines (143 loc) · 5.62 KB
/
middleware_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package middleware
import (
"bytes"
"context"
"errors"
"math/rand"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
errorhandler "github.com/cloudtrust/common-service/v2/errors"
cs "github.com/cloudtrust/common-service/v2"
"github.com/cloudtrust/common-service/v2/configuration"
"github.com/cloudtrust/common-service/v2/log"
"github.com/cloudtrust/common-service/v2/middleware/mock"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func dummyEndpoint(ctx context.Context, request any) (response any, err error) {
return nil, nil
}
func TestEndpointLoggingMW(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
var mockLogger = mock.NewLogger(mockCtrl)
var mInOut = MakeEndpointLoggingMW(mockLogger)(dummyEndpoint)
var mIn = MakeEndpointLoggingInMW(mockLogger)(dummyEndpoint)
var mOut = MakeEndpointLoggingOutMW(mockLogger)(dummyEndpoint)
// Context with correlation ID.
var corrID = strconv.FormatUint(rand.Uint64(), 10)
var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID)
t.Run("In/Out logging - With correlation ID", func(t *testing.T) {
var req = "req"
mockLogger.EXPECT().Debug(gomock.Any(), "req", req).Times(1)
mockLogger.EXPECT().Debug(gomock.Any(), "res", nil).Times(1)
mInOut(ctx, req)
})
t.Run("In logging - With correlation ID", func(t *testing.T) {
var req = "req"
mockLogger.EXPECT().Debug(gomock.Any(), "req", req).Times(1)
mockLogger.EXPECT().Debug(gomock.Any(), "res", "hidden").Times(1)
mIn(ctx, req)
})
t.Run("Out logging - With correlation ID", func(t *testing.T) {
var req = "req"
mockLogger.EXPECT().Debug(gomock.Any(), "req", "hidden").Times(1)
mockLogger.EXPECT().Debug(gomock.Any(), "res", nil).Times(1)
mOut(ctx, req)
})
t.Run("In/Out logging - Without correlation ID", func(t *testing.T) {
var f = func() {
mInOut(context.Background(), nil)
}
assert.Panics(t, f)
})
}
func TestEndpointInstrumentingMW(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
var mockMetrics = mock.NewMetrics(mockCtrl)
var mockHisto = mock.NewHistogram(mockCtrl)
var histoName = "histo_name"
var corrID = strconv.FormatUint(rand.Uint64(), 10)
var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID)
mockMetrics.EXPECT().NewHistogram(histoName).Return(mockHisto).Times(1)
mockHisto.EXPECT().With("corr_id", corrID).Return(mockHisto).Times(1)
mockHisto.EXPECT().Observe(gomock.Any())
var m = MakeEndpointInstrumentingMW(mockMetrics, histoName)(dummyEndpoint)
t.Run("With correlation ID", func(t *testing.T) {
m(ctx, nil)
})
t.Run("Without correlation ID", func(t *testing.T) {
var f = func() {
m(context.Background(), nil)
}
assert.Panics(t, f)
})
}
func TestEndpointAvailableMW(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
var mockIDRetriever = mock.NewIDRetriever(mockCtrl)
var mockConfRetriever = mock.NewAdminConfigurationRetriever(mockCtrl)
var logger = log.NewNopLogger()
var accessToken = "jwtaccesstoken"
var realmName = "myrealm"
var realmID = "abcdefgh-1234-5678"
var ctx = context.TODO()
var expectedError = errors.New("the error")
var feature = configuration.CheckKeyPhysical
var adminConfig configuration.RealmAdminConfiguration
var dummyHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
var availabilityChecker = NewEndpointAvailabilityChecker(feature, mockIDRetriever, mockConfRetriever)
var m = MakeEndpointAvailableCheckMW(availabilityChecker, logger)(dummyHandler)
// HTTP request
var req = httptest.NewRequest("POST", "http://cloudtrust.io/api", bytes.NewReader([]byte{}))
ctx = context.WithValue(ctx, cs.CtContextAccessToken, accessToken)
ctx = context.WithValue(ctx, cs.CtContextRealm, realmName)
req = req.WithContext(ctx)
t.Run("Can't get realmID - unexpected error", func(t *testing.T) {
var w = httptest.NewRecorder()
mockIDRetriever.EXPECT().GetID(accessToken, realmName).Return(realmID, expectedError)
m.ServeHTTP(w, req)
var result = w.Result()
assert.Equal(t, http.StatusInternalServerError, result.StatusCode)
})
t.Run("Can't get realmID - unauthorized", func(t *testing.T) {
var w = httptest.NewRecorder()
mockIDRetriever.EXPECT().GetID(accessToken, realmName).Return(realmID, errorhandler.Error{Status: http.StatusForbidden})
m.ServeHTTP(w, req)
var result = w.Result()
assert.Equal(t, http.StatusForbidden, result.StatusCode)
})
t.Run("Get admin configuration fails", func(t *testing.T) {
var w = httptest.NewRecorder()
mockIDRetriever.EXPECT().GetID(accessToken, realmName).Return(realmID, nil)
mockConfRetriever.EXPECT().GetAdminConfiguration(ctx, realmID).Return(adminConfig, expectedError)
m.ServeHTTP(w, req)
var result = w.Result()
assert.Equal(t, http.StatusInternalServerError, result.StatusCode)
})
t.Run("Feature is not enabled", func(t *testing.T) {
var w = httptest.NewRecorder()
mockIDRetriever.EXPECT().GetID(accessToken, realmName).Return(realmID, nil)
mockConfRetriever.EXPECT().GetAdminConfiguration(ctx, realmID).Return(adminConfig, nil)
m.ServeHTTP(w, req)
var result = w.Result()
assert.Equal(t, http.StatusConflict, result.StatusCode)
})
t.Run("Feature is enabled", func(t *testing.T) {
var w = httptest.NewRecorder()
adminConfig.AvailableChecks = map[string]bool{feature: true}
mockIDRetriever.EXPECT().GetID(accessToken, realmName).Return(realmID, nil)
mockConfRetriever.EXPECT().GetAdminConfiguration(ctx, realmID).Return(adminConfig, nil)
m.ServeHTTP(w, req)
var result = w.Result()
assert.Equal(t, http.StatusOK, result.StatusCode)
})
}