Add test for (*JWTMux).Login()

Ensures that state is send properly to Provider.
pull/922/head
Tim Raymond 2017-02-16 17:28:14 -05:00
parent 15e1700fda
commit e5457e7e5b
1 changed files with 41 additions and 2 deletions

View File

@ -12,14 +12,14 @@ import (
"github.com/influxdata/chronograf/oauth2"
)
var testTime time.Time = time.Date(1985, time.October, 25, 18, 0, 0, 0, time.UTC)
func Test_JWTMux_Logout_DeletesSessionCookie(t *testing.T) {
t.Parallel()
mp := &MockProvider{"biff@example.com"}
jm := oauth2.NewJWTMux(mp, &YesManAuthenticator{}, clog.New(clog.ParseLevel("debug")))
testTime := time.Date(1985, time.October, 25, 18, 0, 0, 0, time.UTC)
jm.Now = func() time.Time {
return testTime
}
@ -62,3 +62,42 @@ func Test_JWTMux_Logout_DeletesSessionCookie(t *testing.T) {
t.Fatal("Expected cookie to be expired but wasn't")
}
}
func Test_JWTMux_Login_RedirectsToCorrectURL(t *testing.T) {
t.Parallel()
mp := &MockProvider{"biff@example.com"}
jm := oauth2.NewJWTMux(mp, &YesManAuthenticator{}, clog.New(clog.ParseLevel("debug")))
jm.Now = func() time.Time {
return testTime
}
ts := httptest.NewServer(jm.Login())
defer ts.Close()
hc := http.Client{
CheckRedirect: func(r *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := hc.Get(ts.URL)
if err != nil {
t.Fatal("Error communicating with Login() handler: err:", err)
}
// Ensure we were redirected
if resp.StatusCode < 300 || resp.StatusCode >= 400 {
t.Fatal("Expected to be redirected, but received status code", resp.StatusCode)
}
loc, err := resp.Location()
if err != nil {
t.Fatal("Expected a location to be redirected to, but wasn't present")
}
if state := loc.Query().Get("state"); state != "HELLO?!MCFLY?!ANYONEINTHERE?!" {
t.Fatal("Expected state to be set but was", state)
}
}