| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package auth | 5 package auth |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "net/http" | 9 "net/http" |
| 10 "net/http/httptest" | 10 "net/http/httptest" |
| 11 "net/url" | 11 "net/url" |
| 12 "testing" | 12 "testing" |
| 13 | 13 |
| 14 "golang.org/x/net/context" | 14 "golang.org/x/net/context" |
| 15 | 15 |
| 16 "github.com/luci/luci-go/common/errors" | 16 "github.com/luci/luci-go/common/errors" |
| 17 "github.com/luci/luci-go/server/auth/identity" | 17 "github.com/luci/luci-go/server/auth/identity" |
| 18 "github.com/luci/luci-go/server/router" | 18 "github.com/luci/luci-go/server/router" |
| 19 . "github.com/smartystreets/goconvey/convey" | 19 . "github.com/smartystreets/goconvey/convey" |
| 20 ) | 20 ) |
| 21 | 21 |
| 22 func TestContext(t *testing.T) { | 22 func TestContext(t *testing.T) { |
| 23 t.Parallel() |
| 24 |
| 23 Convey("Works", t, func() { | 25 Convey("Works", t, func() { |
| 24 c := context.Background() | 26 c := context.Background() |
| 25 | 27 |
| 26 So(getAuthenticator(c), ShouldBeNil) | 28 So(getAuthenticator(c), ShouldBeNil) |
| 27 _, err := LoginURL(c, "dest") | 29 _, err := LoginURL(c, "dest") |
| 28 So(err, ShouldEqual, ErrNoUsersAPI) | 30 So(err, ShouldEqual, ErrNoUsersAPI) |
| 29 _, err = LogoutURL(c, "dest") | 31 _, err = LogoutURL(c, "dest") |
| 30 So(err, ShouldEqual, ErrNoUsersAPI) | 32 So(err, ShouldEqual, ErrNoUsersAPI) |
| 31 | 33 |
| 32 // Authenticator without UsersAPI. | 34 // Authenticator without UsersAPI. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 46 So(err, ShouldBeNil) | 48 So(err, ShouldBeNil) |
| 47 So(dest, ShouldEqual, "http://login_url?r=dest") | 49 So(dest, ShouldEqual, "http://login_url?r=dest") |
| 48 dest, err = LogoutURL(c, "dest") | 50 dest, err = LogoutURL(c, "dest") |
| 49 So(err, ShouldBeNil) | 51 So(err, ShouldBeNil) |
| 50 So(dest, ShouldEqual, "http://logout_url?r=dest") | 52 So(dest, ShouldEqual, "http://logout_url?r=dest") |
| 51 }) | 53 }) |
| 52 | 54 |
| 53 } | 55 } |
| 54 | 56 |
| 55 func TestContextAuthenticate(t *testing.T) { | 57 func TestContextAuthenticate(t *testing.T) { |
| 58 t.Parallel() |
| 59 |
| 56 call := func(c context.Context, m router.MiddlewareChain, h router.Handl
er) *httptest.ResponseRecorder { | 60 call := func(c context.Context, m router.MiddlewareChain, h router.Handl
er) *httptest.ResponseRecorder { |
| 57 req, err := http.NewRequest("GET", "http://example.com/foo", nil
) | 61 req, err := http.NewRequest("GET", "http://example.com/foo", nil
) |
| 58 So(err, ShouldBeNil) | 62 So(err, ShouldBeNil) |
| 59 w := httptest.NewRecorder() | 63 w := httptest.NewRecorder() |
| 60 router.RunMiddleware(&router.Context{ | 64 router.RunMiddleware(&router.Context{ |
| 61 Context: c, | 65 Context: c, |
| 62 Writer: w, | 66 Writer: w, |
| 63 Request: req, | 67 Request: req, |
| 64 }, m, h) | 68 }, m, h) |
| 65 return w | 69 return w |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 | 109 |
| 106 Convey("Broken ID is rejected", t, func() { | 110 Convey("Broken ID is rejected", t, func() { |
| 107 c := prepareCtx(fakeMethod{userID: "???"}) | 111 c := prepareCtx(fakeMethod{userID: "???"}) |
| 108 rr := call(c, router.NewMiddlewareChain(Authenticate), handler) | 112 rr := call(c, router.NewMiddlewareChain(Authenticate), handler) |
| 109 So(rr.Code, ShouldEqual, 401) | 113 So(rr.Code, ShouldEqual, 401) |
| 110 So(rr.Body.String(), ShouldEqual, "Authentication error - auth:
bad identity string \"???\"\n") | 114 So(rr.Body.String(), ShouldEqual, "Authentication error - auth:
bad identity string \"???\"\n") |
| 111 }) | 115 }) |
| 112 } | 116 } |
| 113 | 117 |
| 114 func TestAutologin(t *testing.T) { | 118 func TestAutologin(t *testing.T) { |
| 119 t.Parallel() |
| 120 |
| 115 call := func(c context.Context, m router.MiddlewareChain, h router.Handl
er) *httptest.ResponseRecorder { | 121 call := func(c context.Context, m router.MiddlewareChain, h router.Handl
er) *httptest.ResponseRecorder { |
| 116 req, err := http.NewRequest("GET", "http://example.com/foo", nil
) | 122 req, err := http.NewRequest("GET", "http://example.com/foo", nil
) |
| 117 So(err, ShouldBeNil) | 123 So(err, ShouldBeNil) |
| 118 w := httptest.NewRecorder() | 124 w := httptest.NewRecorder() |
| 119 router.RunMiddleware(&router.Context{ | 125 router.RunMiddleware(&router.Context{ |
| 120 Context: c, | 126 Context: c, |
| 121 Writer: w, | 127 Writer: w, |
| 122 Request: req, | 128 Request: req, |
| 123 }, m, h) | 129 }, m, h) |
| 124 return w | 130 return w |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 v := url.Values{} | 217 v := url.Values{} |
| 212 v.Set("r", dest) | 218 v.Set("r", dest) |
| 213 return "http://login_url?" + v.Encode(), nil | 219 return "http://login_url?" + v.Encode(), nil |
| 214 } | 220 } |
| 215 | 221 |
| 216 func (m fakeMethod) LogoutURL(c context.Context, dest string) (string, error) { | 222 func (m fakeMethod) LogoutURL(c context.Context, dest string) (string, error) { |
| 217 v := url.Values{} | 223 v := url.Values{} |
| 218 v.Set("r", dest) | 224 v.Set("r", dest) |
| 219 return "http://logout_url?" + v.Encode(), nil | 225 return "http://logout_url?" + v.Encode(), nil |
| 220 } | 226 } |
| OLD | NEW |