-
Notifications
You must be signed in to change notification settings - Fork 0
/
OauthClient_test.go
95 lines (76 loc) · 2.33 KB
/
OauthClient_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
package kneu
import (
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
"testing"
)
func TestOauthClient_GetOauthUrl(t *testing.T) {
client := OauthClient{
ClientId: 100,
}
expectedUrl := AuthBaseUri + "/oauth?response_type=code&client_id=100&redirect_uri=http%3A%2F%2Fself%2Fredirect.html&state=state88"
redirectUrl := client.GetOauthUrl("http://self/redirect.html", "state88")
assert.Equal(t, expectedUrl, redirectUrl)
}
func TestOauthClient_GetOauthToken(t *testing.T) {
clientId := uint(100)
clientSecret := "test_secret"
redirectUri := "http://localhost/redirect_uri"
expectedPost := "client_id=100&client_secret=test_secret&code=test_code&redirect_uri=http%3A%2F%2Flocalhost%2Fredirect_uri&grant_type=authorization_code"
defer gock.Off()
t.Run("success", func(t *testing.T) {
gock.New(AuthBaseUri).
Post("/oauth/token").
MatchType("url").
BodyString(expectedPost).
Reply(200).
JSON(`{
"access_token": "00000eb1ed29a47b4c38f9700d49AA00",
"token_type": "Bearer",
"expires_in": 7200,
"user_id": 999
}`)
client := OauthClient{
ClientId: clientId,
ClientSecret: clientSecret,
}
tokenResponse, err := client.GetOauthToken(redirectUri, "test_code")
assert.NoError(t, err)
assert.Equal(t, "00000eb1ed29a47b4c38f9700d49AA00", tokenResponse.AccessToken)
assert.Equal(t, "Bearer", tokenResponse.TokenType)
assert.Equal(t, 7200, tokenResponse.ExpiresIn)
assert.Equal(t, uint(999), tokenResponse.UserId)
})
t.Run("http_error", func(t *testing.T) {
gock.New(AuthBaseUri).
Post("/oauth/token").
MatchType("url").
BodyString(expectedPost).
Reply(500)
client := OauthClient{
ClientId: clientId,
ClientSecret: clientSecret,
}
_, err := client.GetOauthToken(redirectUri, "test_code")
assert.Error(t, err)
assert.Equal(t, "Receive http code: 500", err.Error())
})
t.Run("api_error", func(t *testing.T) {
gock.New(AuthBaseUri).
Post("/oauth/token").
MatchType("url").
BodyString(expectedPost).
Reply(404).
JSON(`{
"error": "Fake",
"error_description": "Test error description"
}`)
client := OauthClient{
ClientId: clientId,
ClientSecret: clientSecret,
}
_, err := client.GetOauthToken(redirectUri, "test_code")
assert.Error(t, err)
assert.Equal(t, "Fake: Test error description", err.Error())
})
}