-
Notifications
You must be signed in to change notification settings - Fork 54
/
apis_suite_test.go
174 lines (154 loc) · 4.1 KB
/
apis_suite_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
164
165
166
167
168
169
170
171
172
173
174
package handlers_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"code.cloudfoundry.org/korifi/api/authorization"
"code.cloudfoundry.org/korifi/api/correlation"
"github.com/go-http-utils/headers"
"github.com/google/uuid"
"github.com/gorilla/mux"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gstruct"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
const (
defaultServerURL = "https://api.example.org"
jsonHeader = "application/json"
)
var (
rr *httptest.ResponseRecorder
router *mux.Router
serverURL *url.URL
ctx context.Context
authInfo authorization.Info
correlationID string
)
func TestApis(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Apis Suite")
}
var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter)))
})
var _ = BeforeEach(func() {
authInfo = authorization.Info{Token: "a-token"}
correlationID = generateGUID("corrID")
ctx = correlation.ContextWithId(authorization.NewContext(context.Background(), &authInfo), correlationID)
rr = httptest.NewRecorder()
router = mux.NewRouter()
var err error
serverURL, err = url.Parse(defaultServerURL)
Expect(err).NotTo(HaveOccurred())
})
func defaultServerURI(paths ...string) string {
return fmt.Sprintf("%s%s", defaultServerURL, strings.Join(paths, ""))
}
func expectJSONResponse(status int, body string) {
ExpectWithOffset(2, rr).To(HaveHTTPStatus(status))
ExpectWithOffset(2, rr).To(HaveHTTPHeaderWithValue("Content-Type", jsonHeader))
ExpectWithOffset(2, rr).To(HaveHTTPBody(MatchJSON(body)))
}
func expectUnknownError() {
expectJSONResponse(http.StatusInternalServerError, `{
"errors": [
{
"title": "UnknownError",
"detail": "An unknown error occurred.",
"code": 10001
}
]
}`)
}
func expectNotAuthorizedError() {
expectJSONResponse(http.StatusForbidden, `{
"errors": [
{
"code": 10003,
"title": "CF-NotAuthorized",
"detail": "You are not authorized to perform the requested action"
}
]
}`)
}
func expectNotFoundError(detail string) {
ExpectWithOffset(1, rr).To(HaveHTTPStatus(http.StatusNotFound))
ExpectWithOffset(1, rr).To(HaveHTTPHeaderWithValue(headers.ContentType, jsonHeader))
var bodyJSON map[string]interface{}
Expect(json.Unmarshal(rr.Body.Bytes(), &bodyJSON)).To(Succeed())
Expect(bodyJSON).To(HaveKey("errors"))
Expect(bodyJSON["errors"]).To(HaveLen(1))
Expect(bodyJSON["errors"]).To(ConsistOf(
gstruct.MatchAllKeys(gstruct.Keys{
"code": BeEquivalentTo(10010),
"title": Equal("CF-ResourceNotFound"),
"detail": HavePrefix(detail),
}),
))
}
func expectUnprocessableEntityError(detail string) {
expectJSONResponse(http.StatusUnprocessableEntity, fmt.Sprintf(`{
"errors": [
{
"detail": %q,
"title": "CF-UnprocessableEntity",
"code": 10008
}
]
}`, detail))
}
func expectBadRequestError() {
expectJSONResponse(http.StatusBadRequest, `{
"errors": [
{
"title": "CF-MessageParseError",
"detail": "Request invalid due to parse error: invalid request body",
"code": 1001
}
]
}`)
}
func expectUnknownKeyError(detail string) {
expectJSONResponse(http.StatusBadRequest, fmt.Sprintf(`{
"errors": [
{
"code": 10005,
"title": "CF-BadQueryParameter",
"detail": %q
}
]
}`, detail))
}
func expectNotAuthenticatedError() {
expectJSONResponse(http.StatusUnauthorized, `{
"errors": [
{
"detail": "Authentication error",
"title": "CF-NotAuthenticated",
"code": 10002
}
]
}`)
}
func expectInvalidAuthError() {
expectJSONResponse(http.StatusUnauthorized, `{
"errors": [
{
"detail": "Invalid Auth Token",
"title": "CF-InvalidAuthToken",
"code": 1000
}
]
}`)
}
func generateGUID(prefix string) string {
guid := uuid.NewString()
return fmt.Sprintf("%s-%s", prefix, guid[:13])
}