/
scenario.go
176 lines (152 loc) · 7.11 KB
/
scenario.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
175
176
package ory
import (
_ "embed"
"fmt"
"github.com/avast/retry-go/v4"
"github.com/kyma-project/api-gateway/tests/integration/pkg/auth"
"github.com/kyma-project/api-gateway/tests/integration/pkg/helpers"
"github.com/kyma-project/api-gateway/tests/integration/pkg/manifestprocessor"
"github.com/kyma-project/api-gateway/tests/integration/pkg/resource"
"github.com/kyma-project/api-gateway/tests/integration/pkg/testcontext"
"golang.org/x/oauth2/clientcredentials"
"k8s.io/client-go/dynamic"
"net/http"
"strings"
)
type scenario struct {
Namespace string
TestID string
Domain string
ApiResourceManifestPath string
ApiResourceDirectory string
ManifestTemplate map[string]string
Url string
k8sClient dynamic.Interface
oauth2Cfg *clientcredentials.Config
jwtConfig *clientcredentials.Config
httpClient *helpers.RetryableHttpClient
resourceManager *resource.Manager
config testcontext.Config
}
func (s *scenario) callingTheEndpointWithValidTokenShouldResultInStatusBetween(path string, tokenType string, lower, higher int) error {
asserter := &helpers.StatusPredicate{LowerStatusBound: lower, UpperStatusBound: higher}
return s.callingTheEndpointWithMethodWithValidToken(fmt.Sprintf("%s%s", s.Url, path), http.MethodGet, tokenType, asserter)
}
func (s *scenario) callingTheEndpointWithMethodWithValidTokenShouldResultInStatusBetween(path string, method string, tokenType string, lower, higher int) error {
asserter := &helpers.StatusPredicate{LowerStatusBound: lower, UpperStatusBound: higher}
return s.callingTheEndpointWithMethodWithValidToken(fmt.Sprintf("%s%s", s.Url, path), method, tokenType, asserter)
}
func (s *scenario) callingTheEndpointWithMethodWithValidToken(url string, method string, tokenType string, asserter helpers.HttpResponseAsserter) error {
requestHeaders := make(map[string]string)
switch tokenType {
case "OAuth2":
tokenOpaque, err := auth.GetAccessToken(*s.oauth2Cfg, "opaque")
if err != nil {
return fmt.Errorf("failed to fetch an id_token: %s", err.Error())
}
requestHeaders[testcontext.AuthorizationHeaderName] = fmt.Sprintf("Bearer %s", tokenOpaque)
case "JWT":
tokenJwt, err := auth.GetAccessToken(*s.jwtConfig, "jwt")
if err != nil {
return fmt.Errorf("failed to fetch an id_token: %s", err.Error())
}
requestHeaders[testcontext.AuthorizationHeaderName] = fmt.Sprintf("Bearer %s", tokenJwt)
default:
return fmt.Errorf("unsupported token type: %s", tokenType)
}
return s.httpClient.CallEndpointWithHeadersAndMethod(requestHeaders, url, method, asserter)
}
func (s *scenario) thereIsAHttpbinServiceAndApiRuleIsApplied() error {
err := s.thereIsAHttpbinService()
if err != nil {
return err
}
r, err := manifestprocessor.ParseFromFileWithTemplate(s.ApiResourceManifestPath, s.ApiResourceDirectory, s.ManifestTemplate)
if err != nil {
return err
}
return helpers.ApplyApiRule(s.resourceManager.CreateResources, s.resourceManager.UpdateResources, s.k8sClient, testcontext.GetRetryOpts(), r)
}
func (s *scenario) theAPIRuleIsApplied() error {
r, err := manifestprocessor.ParseFromFileWithTemplate(s.ApiResourceManifestPath, s.ApiResourceDirectory, s.ManifestTemplate)
if err != nil {
return err
}
return helpers.ApplyApiRule(s.resourceManager.CreateResources, s.resourceManager.UpdateResources, s.k8sClient, testcontext.GetRetryOpts(), r)
}
func (s *scenario) callingTheEndpointWithMethodWithInvalidTokenShouldResultInStatusBetween(path string, method string, lower, higher int) error {
requestHeaders := map[string]string{testcontext.AuthorizationHeaderName: testcontext.AnyToken}
return s.httpClient.CallEndpointWithHeadersAndMethod(requestHeaders, fmt.Sprintf("%s%s", s.Url, path), method, &helpers.StatusPredicate{LowerStatusBound: lower, UpperStatusBound: higher})
}
func (s *scenario) callingTheEndpointWithInvalidTokenShouldResultInStatusBetween(path string, lower, higher int) error {
requestHeaders := map[string]string{testcontext.AuthorizationHeaderName: testcontext.AnyToken}
return s.httpClient.CallEndpointWithHeadersWithRetries(requestHeaders, fmt.Sprintf("%s%s", s.Url, path), &helpers.StatusPredicate{LowerStatusBound: lower, UpperStatusBound: higher})
}
func (s *scenario) callingTheEndpointWithoutTokenShouldResultInStatusBetween(path string, lower, higher int) error {
return s.httpClient.CallEndpointWithRetries(fmt.Sprintf("%s/%s", s.Url, strings.TrimLeft(path, "/")), &helpers.StatusPredicate{LowerStatusBound: lower, UpperStatusBound: higher})
}
func (s *scenario) thereIsAHttpbinService() error {
resources, err := manifestprocessor.ParseFromFileWithTemplate("testing-app.yaml", s.ApiResourceDirectory, s.ManifestTemplate)
if err != nil {
return err
}
_, err = s.resourceManager.CreateResources(s.k8sClient, resources...)
if err != nil {
return err
}
s.Url = fmt.Sprintf("https://httpbin-%s.%s", s.TestID, s.Domain)
return nil
}
// teardownHttpbinService deletes the httpbin service and reset the url in the scenario. This should be considered a temporary solution
// to reduce resource consumption until we implement a better way to clean up the resources by a scenario. If the test fails before this step the teardown won't be executed.
func (s *scenario) teardownHttpbinService() error {
resources, err := manifestprocessor.ParseFromFileWithTemplate("testing-app.yaml", s.ApiResourceDirectory, s.ManifestTemplate)
if err != nil {
return err
}
err = s.resourceManager.DeleteResources(s.k8sClient, resources...)
if err != nil {
return err
}
s.Url = ""
return nil
}
func (s *scenario) preflightEndpointCallResponseHeaders(endpoint, origin string, statusCode int, headerKey, headerValue string) error {
headers := map[string]string{
"Origin": origin,
"Access-Control-Request-Method": "GET,POST,PUT,DELETE,PATCH",
}
return retry.Do(func() error {
resp, err := s.httpClient.CallEndpointWithRetriesAndGetResponse(headers, nil, http.MethodOptions, s.Url+endpoint)
if err != nil {
return err
}
if resp.StatusCode != statusCode {
return fmt.Errorf("expected response status code %d got %d", statusCode, resp.StatusCode)
}
rhv := resp.Header.Get(headerKey)
if rhv != headerValue {
return fmt.Errorf("expected header %s with value %s, got %s", headerKey, headerValue, rhv)
}
return nil
}, testcontext.GetRetryOpts()...)
}
func (s *scenario) preflightEndpointCallNoResponseHeader(endpoint, origin string, statusCode int, headerKey string) error {
headers := map[string]string{
"Origin": origin,
"Access-Control-Request-Method": "GET,POST,PUT,DELETE,PATCH",
}
return retry.Do(func() error {
resp, err := s.httpClient.CallEndpointWithRetriesAndGetResponse(headers, nil, http.MethodOptions, s.Url+endpoint)
if err != nil {
return err
}
if resp.StatusCode != statusCode {
return fmt.Errorf("expected response status code %d got %d", statusCode, resp.StatusCode)
}
if len(resp.Header.Values(headerKey)) > 0 {
return fmt.Errorf("expected that the response will not contain %s header, but did", headerKey)
}
return nil
}, testcontext.GetRetryOpts()...)
}