/
complete_auth_test.go
135 lines (101 loc) · 4.45 KB
/
complete_auth_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
package oauth2
import (
"github.com/stretchr/gomniauth/common"
"github.com/stretchr/gomniauth/test"
"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
)
func TestOAuth2Provider_CompleteAuth_URLEncodedResponse(t *testing.T) {
config := &common.Config{
Map: objx.MSI(
OAuth2KeyRedirectUrl, OAuth2KeyRedirectUrl,
OAuth2KeyScope, OAuth2KeyScope,
OAuth2KeyClientID, OAuth2KeyClientID,
OAuth2KeySecret, OAuth2KeySecret,
OAuth2KeyAuthURL, OAuth2KeyAuthURL,
OAuth2KeyTokenURL, OAuth2KeyTokenURL)}
testTripperFactory := new(test.TestTripperFactory)
testTripper := new(test.TestTripper)
testProvider := new(test.TestProvider)
creds := new(common.Credentials)
testResponse := new(http.Response)
testResponse.Header = make(http.Header)
testResponse.Header.Set("Content-Type", "text/plain")
testResponse.StatusCode = 200
testResponse.Body = ioutil.NopCloser(strings.NewReader("expires_in=20&access_token=ACCESSTOKEN&refresh_token=REFRESHTOKEN"))
testTripperFactory.On("NewTripper", common.EmptyCredentials, mock.Anything).Return(testTripper, nil)
testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)
data := objx.MSI(OAuth2KeyCode, []string{"123"})
creds, err := CompleteAuth(testTripperFactory, data, config, testProvider)
if assert.NoError(t, err) {
if assert.NotNil(t, creds, "Creds should be returned") {
assert.Equal(t, creds.Get(OAuth2KeyAccessToken).Str(), "ACCESSTOKEN")
assert.Equal(t, creds.Get(OAuth2KeyRefreshToken).Str(), "REFRESHTOKEN")
assert.Equal(t, creds.Get(OAuth2KeyExpiresIn).Data().(time.Duration), 20000000000)
}
}
mock.AssertExpectationsForObjects(t, testTripperFactory.Mock, testTripper.Mock, testProvider.Mock)
}
func TestOAuth2Provider_Non200Response(t *testing.T) {
config := &common.Config{
Map: objx.MSI(
OAuth2KeyRedirectUrl, OAuth2KeyRedirectUrl,
OAuth2KeyScope, OAuth2KeyScope,
OAuth2KeyClientID, OAuth2KeyClientID,
OAuth2KeySecret, OAuth2KeySecret,
OAuth2KeyAuthURL, OAuth2KeyAuthURL,
OAuth2KeyTokenURL, OAuth2KeyTokenURL)}
testTripperFactory := new(test.TestTripperFactory)
testTripper := new(test.TestTripper)
testProvider := new(test.TestProvider)
testResponse := new(http.Response)
testResponse.Header = make(http.Header)
testResponse.Header.Set("Content-Type", "text/plain")
testResponse.StatusCode = 401
testResponse.Body = ioutil.NopCloser(strings.NewReader("No mate"))
testTripperFactory.On("NewTripper", common.EmptyCredentials, mock.Anything).Return(testTripper, nil)
testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)
data := objx.MSI(OAuth2KeyCode, []string{"123"})
_, err := CompleteAuth(testTripperFactory, data, config, testProvider)
if assert.Error(t, err) {
assert.IsType(t, &common.AuthServerError{}, err)
}
mock.AssertExpectationsForObjects(t, testTripperFactory.Mock, testTripper.Mock, testProvider.Mock)
}
func TestOAuth2Provider_CompleteAuth_JSON(t *testing.T) {
config := &common.Config{
Map: objx.MSI(
OAuth2KeyRedirectUrl, OAuth2KeyRedirectUrl,
OAuth2KeyScope, OAuth2KeyScope,
OAuth2KeyClientID, OAuth2KeyClientID,
OAuth2KeySecret, OAuth2KeySecret,
OAuth2KeyAuthURL, OAuth2KeyAuthURL,
OAuth2KeyTokenURL, OAuth2KeyTokenURL)}
testTripperFactory := new(test.TestTripperFactory)
testTripper := new(test.TestTripper)
testProvider := new(test.TestProvider)
creds := new(common.Credentials)
testResponse := new(http.Response)
testResponse.Header = make(http.Header)
testResponse.Header.Set("Content-Type", "application/json")
testResponse.StatusCode = 200
testResponse.Body = ioutil.NopCloser(strings.NewReader(`{"expires_in":20,"access_token":"ACCESSTOKEN","refresh_token":"REFRESHTOKEN"}`))
testTripperFactory.On("NewTripper", common.EmptyCredentials, mock.Anything).Return(testTripper, nil)
testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)
data := objx.MSI(OAuth2KeyCode, []string{"123"})
creds, err := CompleteAuth(testTripperFactory, data, config, testProvider)
if assert.NoError(t, err) {
if assert.NotNil(t, creds, "Creds should be returned") {
assert.Equal(t, creds.Get(OAuth2KeyAccessToken).Str(), "ACCESSTOKEN")
assert.Equal(t, creds.Get(OAuth2KeyRefreshToken).Str(), "REFRESHTOKEN")
assert.Equal(t, creds.Get(OAuth2KeyExpiresIn).Data().(time.Duration), 20000000000)
}
}
mock.AssertExpectationsForObjects(t, testTripperFactory.Mock, testTripper.Mock, testProvider.Mock)
}