| Index: server/auth/openid/method_test.go
|
| diff --git a/server/auth/openid/method_test.go b/server/auth/openid/method_test.go
|
| index 14008c4722ea373ebb2e992ab9d05cb2399fa3ac..11cc4196dcdb64b9a1b4e7a3084c96261a98e97a 100644
|
| --- a/server/auth/openid/method_test.go
|
| +++ b/server/auth/openid/method_test.go
|
| @@ -8,20 +8,21 @@ import (
|
| "fmt"
|
| "net/http"
|
| "net/http/httptest"
|
| "net/url"
|
| "testing"
|
| "time"
|
|
|
| "github.com/luci/luci-go/common/clock/testclock"
|
| "github.com/luci/luci-go/server/auth"
|
| "github.com/luci/luci-go/server/auth/authtest"
|
| + "github.com/luci/luci-go/server/router"
|
| "github.com/luci/luci-go/server/secrets/testsecrets"
|
| "github.com/luci/luci-go/server/settings"
|
| "golang.org/x/net/context"
|
|
|
| . "github.com/luci/luci-go/common/testing/assertions"
|
| . "github.com/smartystreets/goconvey/convey"
|
| )
|
|
|
| func TestFullFlow(t *testing.T) {
|
| Convey("with test context", t, func(c C) {
|
| @@ -85,21 +86,25 @@ func TestFullFlow(t *testing.T) {
|
|
|
| // Generate login URL.
|
| loginURL, err := method.LoginURL(ctx, "/destination")
|
| So(err, ShouldBeNil)
|
| So(loginURL, ShouldEqual, "/auth/openid/login?r=%2Fdestination")
|
|
|
| // "Visit" login URL.
|
| req, err := http.NewRequest("GET", "http://fake"+loginURL, nil)
|
| So(err, ShouldBeNil)
|
| rec := httptest.NewRecorder()
|
| - method.loginHandler(ctx, rec, req, nil)
|
| + method.loginHandler(&router.Context{
|
| + Context: ctx,
|
| + Writer: rec,
|
| + Request: req,
|
| + })
|
|
|
| // It asks us to visit authorizarion endpoint.
|
| So(rec.Code, ShouldEqual, http.StatusFound)
|
| parsed, err := url.Parse(rec.Header().Get("Location"))
|
| So(err, ShouldBeNil)
|
| So(parsed.Host, ShouldEqual, ts.URL[len("http://"):])
|
| So(parsed.Path, ShouldEqual, "/authorization")
|
| So(parsed.Query(), ShouldResemble, url.Values{
|
| "client_id": {"client_id"},
|
| "redirect_uri": {"http://fake/redirect"},
|
| @@ -115,21 +120,25 @@ func TestFullFlow(t *testing.T) {
|
|
|
| // Pretend we've done it. OpenID redirects user's browser to callback URI.
|
| // `callbackHandler` will call /token and /userinfo fake endpoints exposed
|
| // by testserver.
|
| callbackParams := url.Values{}
|
| callbackParams.Set("code", "omg_auth_code")
|
| callbackParams.Set("state", parsed.Query().Get("state"))
|
| req, err = http.NewRequest("GET", "http://fake/redirect?"+callbackParams.Encode(), nil)
|
| So(err, ShouldBeNil)
|
| rec = httptest.NewRecorder()
|
| - method.callbackHandler(ctx, rec, req, nil)
|
| + method.callbackHandler(&router.Context{
|
| + Context: ctx,
|
| + Writer: rec,
|
| + Request: req,
|
| + })
|
|
|
| // We should be redirected to the login page, with session cookie set.
|
| expectedCookie := "oid_session=AXsiX2kiOiIxNDQyNTQwMDAwMDAwIiwic2lkIjoi" +
|
| "dXNlcl9pZF9zdWIvMSJ9PmRzaOv-mS0PMHkve897iiELNmpiLi_j3ICG1VKuNCs"
|
| So(rec.Code, ShouldEqual, http.StatusFound)
|
| So(rec.Header().Get("Location"), ShouldEqual, "/destination")
|
| So(rec.Header().Get("Set-Cookie"), ShouldEqual,
|
| expectedCookie+"; Path=/; Expires=Sun, 18 Oct 2015 01:18:20 GMT; Max-Age=2591100; HttpOnly")
|
|
|
| // Use the cookie to authenticate some call.
|
| @@ -146,21 +155,25 @@ func TestFullFlow(t *testing.T) {
|
| })
|
|
|
| // Now generate URL to and visit logout page.
|
| logoutURL, err := method.LogoutURL(ctx, "/another_destination")
|
| So(err, ShouldBeNil)
|
| So(logoutURL, ShouldEqual, "/auth/openid/logout?r=%2Fanother_destination")
|
| req, err = http.NewRequest("GET", "http://fake"+logoutURL, nil)
|
| So(err, ShouldBeNil)
|
| req.Header.Add("Cookie", expectedCookie)
|
| rec = httptest.NewRecorder()
|
| - method.logoutHandler(ctx, rec, req, nil)
|
| + method.logoutHandler(&router.Context{
|
| + Context: ctx,
|
| + Writer: rec,
|
| + Request: req,
|
| + })
|
|
|
| // Should be redirected to destination with the cookie killed.
|
| So(rec.Code, ShouldEqual, http.StatusFound)
|
| So(rec.Header().Get("Location"), ShouldEqual, "/another_destination")
|
| So(rec.Header().Get("Set-Cookie"), ShouldEqual,
|
| "oid_session=deleted; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Max-Age=0")
|
| })
|
| })
|
| }
|
|
|
| @@ -175,21 +188,25 @@ func TestCallbackHandleEdgeCases(t *testing.T) {
|
|
|
| call := func(query map[string]string) *httptest.ResponseRecorder {
|
| q := url.Values{}
|
| for k, v := range query {
|
| q.Add(k, v)
|
| }
|
| req, err := http.NewRequest("GET", "/auth/openid/callback?"+q.Encode(), nil)
|
| c.So(err, ShouldBeNil)
|
| req.Host = "fake.com"
|
| rec := httptest.NewRecorder()
|
| - method.callbackHandler(ctx, rec, req, nil)
|
| + method.callbackHandler(&router.Context{
|
| + Context: ctx,
|
| + Writer: rec,
|
| + Request: req,
|
| + })
|
| return rec
|
| }
|
|
|
| Convey("handles 'error'", func() {
|
| rec := call(map[string]string{"error": "Omg, error"})
|
| So(rec.Code, ShouldEqual, 400)
|
| So(rec.Body.String(), ShouldEqual, "OpenID login error: Omg, error\n")
|
| })
|
|
|
| Convey("handles no 'code'", func() {
|
|
|