This repository has been archived by the owner on Oct 20, 2021. It is now read-only.
/
client_suite_test.go
140 lines (118 loc) · 4.71 KB
/
client_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
package uaa_go_client_test
import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
"io/ioutil"
"net/http"
"sync"
"time"
"code.cloudfoundry.org/clock/fakeclock"
uaa_go_client "code.cloudfoundry.org/uaa-go-client"
"code.cloudfoundry.org/uaa-go-client/config"
"code.cloudfoundry.org/uaa-go-client/schema"
"encoding/json"
"code.cloudfoundry.org/lager"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/ghttp"
)
func TestClient(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Client Suite")
}
const (
TokenKeyEndpoint = "/token_key"
OpenIDConfigEndpoint = "/.well-known/openid-configuration"
DefaultMaxNumberOfRetries = 3
DefaultRetryInterval = 15 * time.Second
DefaultRequestTimeout = 1 * time.Second
DefaultExpirationBufferTime = 30
ValidPemPublicKey = `-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHFr+KICms+tuT1OXJwhCUmR2d\nKVy7psa8xzElSyzqx7oJyfJ1JZyOzToj9T5SfTIq396agbHJWVfYphNahvZ/7uMX\nqHxf+ZH9BL1gk9Y6kCnbM5R60gfwjyW1/dQPjOzn9N394zd2FJoFHwdq9Qs0wBug\nspULZVNRxq7veq/fzwIDAQAB\n-----END PUBLIC KEY-----`
InvalidPemPublicKey = `-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCm5HB9+O6fyjsvWSKzyEZkXv+p\nRHmWq61cYx2ywzBaR59TuH4E98nsWr7o5TaEmX9cuxN261Gq03Qb9D3XRL3KHCaz\n21OTp5bU92CKZWQoZW5jd5ENhopMpUGcV1x9ra1IkdYoTsDt8dRjiOHmOdsc7jZ1\noHp3lFu0JlGTn3TJowIDAQAB\n-----END PUBLIC KEY-----\n`
PemDecodedKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHFr+KICms+tuT1OXJwhCUmR2d
KVy7psa8xzElSyzqx7oJyfJ1JZyOzToj9T5SfTIq396agbHJWVfYphNahvZ/7uMX
qHxf+ZH9BL1gk9Y6kCnbM5R60gfwjyW1/dQPjOzn9N394zd2FJoFHwdq9Qs0wBug
spULZVNRxq7veq/fzwIDAQAB
-----END PUBLIC KEY-----`
)
var (
logger lager.Logger
forceUpdate bool
server *ghttp.Server
clock *fakeclock.FakeClock
cfg *config.Config
)
var verifyBody = func(expectedBody string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())
defer r.Body.Close()
Expect(string(body)).To(Equal(expectedBody))
}
}
var verifyLogs = func(reqMessage, resMessage string) {
Expect(logger).To(gbytes.Say(reqMessage))
Expect(logger).To(gbytes.Say(resMessage))
}
var getOauthHandlerFunc = func(status int, token *schema.Token, optionalHeader ...http.Header) http.HandlerFunc {
return ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/oauth/token"),
ghttp.VerifyBasicAuth("client-name", "client-secret"),
ghttp.VerifyContentType("application/x-www-form-urlencoded; charset=UTF-8"),
ghttp.VerifyHeader(http.Header{
"Accept": []string{"application/json; charset=utf-8"},
}),
verifyBody("grant_type=client_credentials"),
ghttp.RespondWithJSONEncoded(status, token, optionalHeader...),
)
}
var getSuccessKeyFetchHandler = func(key string) http.HandlerFunc {
return ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", TokenKeyEndpoint),
ghttp.RespondWith(http.StatusOK, fmt.Sprintf("{\"alg\":\"alg\", \"value\": \"%s\" }", key)),
)
}
// This function is flaky, there's a race condition between the FetchToken and
// the expectation on the recieved requests size. The tests calling it have been
// marked as skiped since we are expecting to deprecate this repo around fall 2019.
// If we end up continuing to use this repo, we should re-enable the tests and fix
// the race condition in this function.
var verifyFetchWithRetries = func(client uaa_go_client.Client, server *ghttp.Server, numRetries int, expectedErrorMsg string) {
var err error
wg := sync.WaitGroup{}
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer GinkgoRecover()
defer wg.Done()
_, err = client.FetchToken(forceUpdate)
Expect(err).To(HaveOccurred())
}(&wg)
for i := 0; i < numRetries; i++ {
Eventually(server.ReceivedRequests, 7*time.Second, 1*time.Second).Should(HaveLen(i + 1))
clock.Increment(DefaultRetryInterval + 10*time.Second)
}
wg.Wait()
Expect(err.Error()).To(ContainSubstring(expectedErrorMsg))
}
var getRegisterOauthClientHandlerFunc = func(status int, token *schema.Token, oauthClient *schema.OauthClient) http.HandlerFunc {
oauthClientString, err := json.Marshal(oauthClient)
var responseBody string
if status == http.StatusCreated {
responseBody = string(oauthClientString)
} else {
responseBody = ""
}
Expect(err).ToNot(HaveOccurred())
return ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/oauth/clients"),
ghttp.VerifyContentType("application/json; charset=UTF-8"),
ghttp.VerifyHeader(http.Header{
"Accept": []string{"application/json; charset=utf-8"},
"Authorization": []string{"bearer " + token.AccessToken},
}),
verifyBody(string(oauthClientString)),
ghttp.RespondWith(status, responseBody),
)
}