forked from content-services/content-sources-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenforce_identity_test.go
144 lines (123 loc) · 4.4 KB
/
enforce_identity_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
package middleware
import (
"encoding/base64"
"net/http"
"net/http/httptest"
"testing"
"github.com/content-services/content-sources-backend/pkg/config"
"github.com/content-services/content-sources-backend/pkg/handler"
"github.com/labstack/echo/v4"
"github.com/redhatinsights/platform-go-middlewares/identity"
"github.com/stretchr/testify/assert"
)
const urlPrefix = "/api/" + config.DefaultAppName
func TestSkipLivenessTrue(t *testing.T) {
listRoutes := []string{
"/ping",
urlPrefix + "/v1.0/ping",
urlPrefix + "/v1/ping",
}
e := echo.New()
handler.RegisterPing(e)
e.Use(WrapMiddlewareWithSkipper(identity.EnforceIdentity, SkipAuth))
for _, route := range listRoutes {
req := httptest.NewRequest(http.MethodGet, route, nil)
res := httptest.NewRecorder()
c := e.NewContext(req, res)
result := SkipAuth(c)
assert.True(t, result)
}
}
func TestSkipLivenessFalse(t *testing.T) {
listRoutes := []string{
"/api/v1/repositories",
"/api/v1/repositories/ping",
}
e := echo.New()
handler.RegisterPing(e)
e.Use(WrapMiddlewareWithSkipper(identity.EnforceIdentity, SkipAuth))
for _, route := range listRoutes {
req := httptest.NewRequest(http.MethodGet, route, nil)
res := httptest.NewRecorder()
c := e.NewContext(req, res)
result := SkipAuth(c)
assert.False(t, result)
}
}
func TestWrapMiddlewareWithSkipper(t *testing.T) {
var (
req *http.Request
rec *httptest.ResponseRecorder
c echo.Context
h func(c echo.Context) error
err error
listSuccessPaths []string
)
e := echo.New()
m := WrapMiddlewareWithSkipper(identity.EnforceIdentity, SkipAuth)
IdentityHeader := "X-Rh-Identity"
xrhidentityHeaderSuccess := `{"identity":{"type":"Associate","account_number":"2093","internal":{"org_id":"7066"}}}`
xrhidentityHeaderFailure := `{"identity":{"account_number":"2093","internal":{"org_id":"7066"}}}`
bodyResponse := "It Worded!"
h = func(c echo.Context) error {
body, err := []byte(bodyResponse), error(nil)
if err != nil {
return err
}
return c.String(http.StatusOK, string(body))
}
e.GET("/ping", h)
e.GET(urlPrefix+"/v1/ping", h)
e.GET(urlPrefix+"/v1.0/ping", h)
e.GET(urlPrefix+"/v1/repository_parameters/", h)
// A Success request to /ping family path
listSuccessPaths = []string{
"/ping",
urlPrefix + "/v1/ping",
urlPrefix + "/v1.0/ping",
}
for _, path := range listSuccessPaths {
req = httptest.NewRequest(http.MethodGet, path, nil)
req.Header.Set(IdentityHeader, base64.StdEncoding.EncodeToString([]byte(xrhidentityHeaderSuccess)))
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
err = m(h)(c)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, bodyResponse, rec.Body.String())
}
// A Failed request with failed header
req = httptest.NewRequest(http.MethodGet, urlPrefix+"/v1/repository_parameters/", nil)
req.Header.Set(IdentityHeader, base64.StdEncoding.EncodeToString([]byte(xrhidentityHeaderFailure)))
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
err = m(h)(c)
assert.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, rec.Code)
assert.Equal(t, "Bad Request: x-rh-identity header is missing type\n", rec.Body.String())
// A Success request with a right header
req = httptest.NewRequest(http.MethodGet, urlPrefix+"/v1/repository_parameters/", nil)
req.Header.Set(IdentityHeader, base64.StdEncoding.EncodeToString([]byte(xrhidentityHeaderSuccess)))
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
err = m(h)(c)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, bodyResponse, rec.Body.String())
encodedHeader := base64.StdEncoding.EncodeToString([]byte(xrhidentityHeaderSuccess))
assert.Equal(t, encodedHeader, rec.Header().Get(IdentityHeader))
// A Success request with failed header for /ping route
// The middleware should skip for this route and call the
// handler which fill the expected bodyResponse
listSuccessPaths = []string{"/ping", urlPrefix + "/v1/ping", urlPrefix + "/v1.0/ping"}
for _, path := range listSuccessPaths {
req = httptest.NewRequest(http.MethodGet, path, nil)
req.Header.Set(IdentityHeader, base64.StdEncoding.EncodeToString([]byte(xrhidentityHeaderFailure)))
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
err = m(h)(c)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, bodyResponse, rec.Body.String())
}
}