-
Notifications
You must be signed in to change notification settings - Fork 1
/
oauth_test.go
171 lines (142 loc) · 3.98 KB
/
oauth_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
package goutils_test
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
"time"
"github.com/alwitt/goutils"
"github.com/apex/log"
"github.com/go-resty/resty/v2"
"github.com/google/uuid"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
func TestClientCredOAuthTokenManager(t *testing.T) {
assert := assert.New(t)
log.SetLevel(log.DebugLevel)
utCtxt := context.Background()
httpmock.Activate()
defer httpmock.DeactivateAndReset()
testClient := resty.New()
// Install with mock
httpmock.ActivateNonDefault(testClient.GetClient())
// ------------------------------------------------------------------------------------
// Prepare mock
idpBaseURL := "http://idp.testing.dev"
configURL := fmt.Sprintf("%s/.well-known/openid-configuration", idpBaseURL)
tokenURL := fmt.Sprintf("%s/auth/token", idpBaseURL)
testCfg := goutils.ClientCredOAuthTokenManagerParam{
IDPIssuerURL: idpBaseURL,
ClientID: uuid.NewString(),
ClientSecret: uuid.NewString(),
TargetAudience: "http://application.testing.dev",
LogTags: log.Fields{"module": "goutils", "component": "client-cred-oauth"},
CustomLogModifiers: []goutils.LogMetadataModifier{},
}
// Prepare to receive IDP config call
httpmock.RegisterResponder(
"GET",
configURL,
func(r *http.Request) (*http.Response, error) {
return httpmock.NewJsonResponse(200, map[string]string{
"token_endpoint": tokenURL,
})
},
)
uut, err := goutils.GetNewClientCredOAuthTokenManager(utCtxt, testClient, testCfg)
assert.Nil(err)
currentTime := time.Now().UTC()
// Case 0: get token
testToken0 := map[string]interface{}{
"access_token": uuid.NewString(),
"expires_in": 300,
}
{
// Prepare mock
httpmock.RegisterResponder(
"POST",
tokenURL,
func(r *http.Request) (*http.Response, error) {
req, err := io.ReadAll(r.Body)
assert.Nil(err)
reqBody := map[string]string{}
assert.Nil(json.Unmarshal(req, &reqBody))
clientID, ok := reqBody["client_id"]
assert.True(ok)
assert.Equal(testCfg.ClientID, clientID)
clientSecret, ok := reqBody["client_secret"]
assert.True(ok)
assert.Equal(testCfg.ClientSecret, clientSecret)
audience, ok := reqBody["audience"]
assert.True(ok)
assert.Equal(testCfg.TargetAudience, audience)
grantType, ok := reqBody["grant_type"]
assert.True(ok)
assert.Equal("client_credentials", grantType)
// Return the token
return httpmock.NewJsonResponse(200, testToken0)
},
)
waitChan := make(chan bool, 1)
lclCtxt, lclCancel := context.WithTimeout(utCtxt, time.Second)
go func() {
workingToken, err := uut.GetToken(lclCtxt, currentTime)
assert.Nil(err)
assert.Equal(testToken0["access_token"], workingToken)
waitChan <- true
}()
select {
case <-lclCtxt.Done():
assert.False(true, "request timed out")
case <-waitChan:
break
}
lclCancel()
}
// Clear mocks
httpmock.Reset()
// Case 1: get token again
{
lclCtxt, lclCancel := context.WithTimeout(utCtxt, time.Second)
workingToken, err := uut.GetToken(lclCtxt, currentTime)
assert.Nil(err)
assert.Equal(testToken0["access_token"], workingToken)
lclCancel()
}
// Case 2: token timeout, get new token
testToken1 := map[string]interface{}{
"access_token": uuid.NewString(),
"expires_in": 300,
}
currentTime = currentTime.Add(time.Second * 400)
{
// Prepare mock
httpmock.RegisterResponder(
"POST",
tokenURL,
func(r *http.Request) (*http.Response, error) {
return httpmock.NewJsonResponse(200, testToken1)
},
)
waitChan := make(chan bool, 1)
lclCtxt, lclCancel := context.WithTimeout(utCtxt, time.Second)
go func() {
workingToken, err := uut.GetToken(lclCtxt, currentTime)
assert.Nil(err)
assert.Equal(testToken1["access_token"], workingToken)
waitChan <- true
}()
select {
case <-lclCtxt.Done():
assert.False(true, "request timed out")
case <-waitChan:
break
}
lclCancel()
}
// Clean up
assert.Nil(uut.Stop(utCtxt))
}