-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth_test.go
107 lines (89 loc) · 2.26 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
package oauth
import (
"testing"
"github.com/stretchr/testify/assert"
)
var oAuthTest OAuth
func TestScopes(t *testing.T) {
oAuthTest.Scopes([]string{"email"})
assert.Equal(t, oAuthTest.scopes, []string{"email"})
assert.NotEqual(t, oAuthTest.scopes, []string{})
oAuthTest.
Driver("google").
Scopes([]string{"calendar.readonly"})
assert.Equal(t, oAuthTest.scopes, []string{"profile", "email", "calendar.readonly"})
assert.NotEqual(t, oAuthTest.scopes, []string{"profile", "email"})
assert.NotEqual(t, oAuthTest.scopes, []string{})
}
func TestConf(t *testing.T) {
assert := assert.New(t)
oAuthTest.
Driver("github").
Redirect(
"foo",
"bar",
"http://example.com/auth/callback",
)
assert.Equal(oAuthTest.conf.ClientID, "foo")
assert.NotEqual(oAuthTest.conf.ClientID, "")
assert.NotNil(oAuthTest.conf.ClientID)
assert.Equal(oAuthTest.conf.ClientSecret, "bar")
assert.NotEqual(oAuthTest.conf.ClientSecret, "")
assert.NotNil(oAuthTest.conf.ClientSecret)
assert.Equal(oAuthTest.conf.RedirectURL, "http://example.com/auth/callback")
assert.NotEqual(oAuthTest.conf.RedirectURL, "")
assert.NotNil(oAuthTest.conf.RedirectURL)
}
func TestDriver(t *testing.T) {
var err error
_, err = oAuthTest.Driver("unknown").
Redirect(
"xxxxxxxx",
"xxxxxxxxxxxxxxxxxxxxxxxx",
"http://example.com/auth/callback",
)
assert.NotNil(t, err)
_, err = oAuthTest.Driver("github").
Redirect(
"xxxxxxxx",
"xxxxxxxxxxxxxxxxxxxxxxxx",
"http://example.com/auth/callback",
)
assert.Nil(t, err)
}
func TestRedirectURL(t *testing.T) {
var err error
_, err = oAuthTest.Driver("github").
Redirect(
"xxxxxxxx",
"xxxxxxxxxxxxxxxxxxxxxxxx",
"/auth/callback",
)
assert.NotNil(t, err)
_, err = oAuthTest.Driver("github").
Redirect(
"xxxxxxxx",
"xxxxxxxxxxxxxxxxxxxxxxxx",
"http://example.com/auth/callback",
)
assert.Nil(t, err)
}
func TestState(t *testing.T) {
var err error
err = oAuthTest.Driver("github").
Handle("fakeState", "foo")
assert.NotNil(t, err)
}
func TestExchange(t *testing.T) {
var err error
// Generate a state
oAuthTest.
Driver("github").
Redirect(
"xxxxxxxx",
"xxxxxxxxxxxxxxxxxxxxxxxx",
"http://example.com/auth/callback",
)
err = oAuthTest.Handle(oAuthTest.state, "foo")
assert.NotNil(t, err)
}