-
Notifications
You must be signed in to change notification settings - Fork 1
/
functional_test.go
175 lines (145 loc) · 4.71 KB
/
functional_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
175
package proxy
import (
"fmt"
"io"
"math/rand"
"net/http"
"net/http/cookiejar"
"testing"
"time"
"github.com/cucumber/godog"
"github.com/stretchr/testify/assert"
)
type testResponse struct {
response *http.Response
body string
}
var (
p Proxy
last testResponse
client = http.DefaultClient
)
func Test_Functional(t *testing.T) {
// run functional tests
status := godog.TestSuite{
Name: "functional tests",
ScenarioInitializer: InitializeScenario,
TestSuiteInitializer: InitializeTestSuite,
}.Run()
// Any test initialization should be done in Godog hooks, e.g.: InitializeTestSuite or InitializeScenario
assert.Equal(t, 0, status, "One or more functional tests failed.")
}
func InitializeTestSuite(ctx *godog.TestSuiteContext) {
ctx.BeforeSuite(func() {
var err error
if p, err = newProxy(); err != nil {
panic(err.Error())
}
client.Jar, _ = cookiejar.New(nil)
})
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^we send a request with (\w+) authorization data$`, weSendARequestWithAuthorizationData)
ctx.Step(`^we send a request with authorization data in the (\w+) authorizing (\w+) access$`,
weSendARequestWithAuthorizationDataAuthorizingAccess)
ctx.Step(`^we send a request with a user-agent that is trusted$`, weSendARequestWithAUserAgentThatIsTrusted)
ctx.Step(`^we will be redirected to the management api$`, weWillBeRedirectedToTheManagementApi)
ctx.Step(`^we do not see an error message$`, weDoNotSeeAnErrorMessage)
ctx.Step(`^we will see an error message$`, weWillSeeAnErrorMessage)
ctx.Step(`^we will see the (\w+) version of the website$`, weWillSeeTheAccessLevelVersionOfTheWebsite)
ctx.Step(`^we do not see the token parameter$`, weDoNotSeeTheTokenParameter)
}
func sendRequest(url string, c *http.Cookie, headers map[string]string) error {
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}
if c != nil {
request.AddCookie(c)
}
for k, v := range headers {
request.Header.Set(k, v)
}
response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return err
}
last = testResponse{response, string(body)}
return nil
}
func weSendARequestWithAuthorizationDataAuthorizingAccess(where, level string) error {
var c *http.Cookie
url := p.Host
expires := time.Now().Add(1000 + time.Second*time.Duration(rand.Intn(1000))) // #nosec G404 weak random number is OK
token := makeTestJWT(p.Secret, level, expires)
if where == "cookie" {
c = makeTestJWTCookie(p.CookieName, token)
} else {
url += fmt.Sprintf("?%s=%s", p.TokenParam, token)
}
return sendRequest(url, c, nil)
}
func weSendARequestWithAUserAgentThatIsTrusted() error {
return sendRequest(p.Host, nil, map[string]string{"User-Agent": "googlebot"})
}
func weSendARequestWithAuthorizationData(t string) error {
var c *http.Cookie
switch t {
case "expired":
token := makeTestJWT(p.Secret, "level", time.Now().AddDate(0, 0, -1))
c = makeTestJWTCookie(p.CookieName, token)
case "invalid":
token := makeTestJWT([]byte("bad"), "level", time.Now().AddDate(0, 0, 1))
c = makeTestJWTCookie(p.CookieName, token)
case "no":
c = nil
default:
return godog.ErrPending
}
return sendRequest(p.Host, c, nil)
}
func weWillBeRedirectedToTheManagementApi() error {
return assertContains(last.body, "<title>API</title>",
`did not see "API" in the response body title: %s`, last.body)
}
func weDoNotSeeAnErrorMessage() error {
return assertEqual(http.StatusOK, last.response.StatusCode, "incorrect http status, body=%s", last.body)
}
func weWillSeeAnErrorMessage() error {
return assertEqual(http.StatusBadRequest, last.response.StatusCode,
"expected a 400, --%s-- got a %d, body: %s", p.Host, last.response.StatusCode, last.body)
}
func weWillSeeTheAccessLevelVersionOfTheWebsite(level string) error {
proxy := last
if err := sendRequest("http://"+p.getSite(level), nil, nil); err != nil {
return fmt.Errorf("sendRequest failed: %w", err)
}
return assertEqual(last.body, proxy.body)
}
func weDoNotSeeTheTokenParameter() error {
token := last.response.Request.URL.Query().Get(p.TokenParam)
return assertEqual("", token)
}
// Helper functions
type asserter struct {
err error
}
// Errorf is used by the called assertion to report an error
func (a *asserter) Errorf(format string, args ...interface{}) {
a.err = fmt.Errorf(format, args...)
}
func assertEqual(expected, actual interface{}, msgAndArgs ...interface{}) error {
var a asserter
assert.Equal(&a, expected, actual, msgAndArgs...)
return a.err
}
func assertContains(s, contains interface{}, msgAndArgs ...interface{}) error {
var a asserter
assert.Contains(&a, s, contains, msgAndArgs...)
return a.err
}