-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy_internal_test.go
171 lines (146 loc) · 4.14 KB
/
strategy_internal_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 oauth2
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/botopolis/bot"
"github.com/botopolis/bot/mock"
"github.com/stretchr/testify/assert"
"golang.org/x/oauth2"
)
func TestAuth_cacheHit(t *testing.T) {
var run bool
s := testStrategy("")
store := newStore("", bot.NewBrain())
store.Set("Jean", oauth2.Token{})
s.store = store
res := bot.Responder{}
res.User = "Jean"
s.Auth(res, func(c *http.Client, err error) {
run = true
assert.NotNil(t, c)
assert.Nil(t, err)
})
assert.True(t, run)
}
func TestAuth_cacheMiss(t *testing.T) {
var run bool
s := testStrategy("")
s.store = newStore("", bot.NewBrain())
res := bot.Responder{Robot: bot.New(mock.NewChat())}
cb := func(c *http.Client, err error) { run = true }
s.Auth(res, cb)
assert.False(t, run, "callback is not evaluated")
for _, as := range s.authSessions.registry {
as.Run(nil, nil)
}
assert.True(t, run, "callback is saved in authSessions")
}
func TestHandleLogin(t *testing.T) {
s := testStrategy("")
cases := []struct {
ReqURL string
ResCode int
ResURL string
}{
{
ReqURL: "http://foo.com/login?state=foo",
ResCode: http.StatusFound,
ResURL: "/auth?client_id=&redirect_uri=http%3A%2F%2Ffoo.com%2Fauth&response_type=code&state=foo",
},
{
ReqURL: "http://foo.com/login",
ResCode: http.StatusBadRequest,
ResURL: "",
},
}
for _, c := range cases {
requestURL, err := url.Parse(c.ReqURL)
recorder := httptest.NewRecorder()
s.HandleLogin(recorder, &http.Request{URL: requestURL})
assert.Nil(t, err)
assert.Equal(t, c.ResCode, recorder.Code)
assert.Equal(t, c.ResURL, recorder.Header().Get("Location"))
}
}
func TestHandleAuth_success(t *testing.T) {
d := handleAuthTestData{State: "foo", URL: "http://foo.com?state=foo&code=secret"}
ts := d.Server(http.StatusOK)
s := testStrategy(ts.URL)
s.store = newStore("", bot.NewBrain())
s.authSessions.Set(d.State, authSession{
Func: func(c *http.Client, err error) {
d.Ran = true
assert.Nil(t, err)
},
User: "Jean",
})
// under test
s.HandleAuth(d.Recorder(), d.Request())
assert.True(t, d.Ran)
assert.Equal(t, http.StatusOK, d.Recorder().Code)
}
func TestHandleAuth_invalidState(t *testing.T) {
d := handleAuthTestData{State: "foo", URL: "http://foo.com?state=foo&code=secret"}
ts := d.Server(http.StatusOK)
defer ts.Close()
s := testStrategy(ts.URL)
// under test
s.HandleAuth(d.Recorder(), d.Request())
assert.Equal(t, http.StatusUnauthorized, d.Recorder().Code, "Returns Unauthorized code when state isn't in our session cache")
}
func TestHandleAuth_failedExchange(t *testing.T) {
// setup
d := handleAuthTestData{State: "foo", URL: "http://foo.com?state=foo&code=secret"}
ts := d.Server(http.StatusBadGateway)
defer ts.Close()
s := testStrategy(ts.URL)
s.authSessions.Set(d.State, authSession{Func: func(c *http.Client, err error) {
d.Ran = true
assert.Nil(t, c)
assert.NotNil(t, err)
}})
// under test
s.HandleAuth(d.Recorder(), d.Request())
assert.True(t, d.Ran)
assert.Equal(t, http.StatusBadRequest, d.Recorder().Code, "Returns Unauthorized code when state isn't in our session cache")
}
type handleAuthTestData struct {
Ran bool
State string
URL string
recorder *httptest.ResponseRecorder
}
func (d *handleAuthTestData) Request() *http.Request {
url, _ := url.Parse(d.URL)
return &http.Request{URL: url}
}
func (d *handleAuthTestData) Recorder() *httptest.ResponseRecorder {
if d.recorder == nil {
d.recorder = httptest.NewRecorder()
}
return d.recorder
}
func (d *handleAuthTestData) Server(statusCode int) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if statusCode == 200 {
w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
w.Write([]byte("access_token=90d64460d14870c08c81352a05dedd3465940a7c&scope=user&token_type=bearer"))
return
}
w.WriteHeader(statusCode)
}))
}
func testStrategy(url string) Strategy {
return Strategy{
Opts: Options{},
Config: &oauth2.Config{
RedirectURL: "http://foo.com/auth",
Endpoint: oauth2.Endpoint{
AuthURL: url + "/auth",
TokenURL: url + "/token",
},
},
}
}