Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(584)

Unified Diff: common/auth/localauth/server_test.go

Issue 2951553002: Extend LUCI_CONTEXT["local_auth"] protocol to understand accounts. (Closed)
Patch Set: few more tests Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « common/auth/localauth/server.go ('k') | lucictx/local_auth.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: common/auth/localauth/server_test.go
diff --git a/common/auth/localauth/server_test.go b/common/auth/localauth/server_test.go
index bd9cf7c5c181e229e645c8dfa5d38057a59ec7bb..565735c91e12c64b5067b6576b2ecf32d1c4cefe 100644
--- a/common/auth/localauth/server_test.go
+++ b/common/auth/localauth/server_test.go
@@ -90,29 +90,39 @@ func TestProtocol(t *testing.T) {
// Use channels to pass mocked requests/responses back and forth.
requests := make(chan []string, 10000)
responses := make(chan interface{}, 1)
+
+ testGen := func(ctx context.Context, scopes []string, lifetime time.Duration) (*oauth2.Token, error) {
+ requests <- scopes
+ var resp interface{}
+ select {
+ case resp = <-responses:
+ default:
+ c.Println("Unexpected token request")
+ return nil, fmt.Errorf("Unexpected request")
+ }
+ switch resp := resp.(type) {
+ case error:
+ return nil, resp
+ case *oauth2.Token:
+ return resp, nil
+ default:
+ panic("unknown response")
+ }
+ }
+
s := Server{
- TokenGenerator: func(ctx context.Context, scopes []string, lifetime time.Duration) (*oauth2.Token, error) {
- requests <- scopes
- var resp interface{}
- select {
- case resp = <-responses:
- default:
- c.Println("Unexpected token request")
- return nil, fmt.Errorf("Unexpected request")
- }
- switch resp := resp.(type) {
- case error:
- return nil, resp
- case *oauth2.Token:
- return resp, nil
- default:
- panic("unknown response")
- }
+ TokenGenerators: map[string]TokenGenerator{
+ "acc_id": testGen,
+ "another_id": testGen,
},
+ DefaultAccountID: "acc_id",
}
p, err := s.Initialize(ctx)
So(err, ShouldBeNil)
+ So(p.Accounts, ShouldResemble, []lucictx.LocalAuthAccount{{ID: "acc_id"}, {ID: "another_id"}})
+ So(p.DefaultAccountID, ShouldEqual, "acc_id")
+
done := make(chan struct{})
go func() {
s.Serve()
@@ -125,8 +135,9 @@ func TestProtocol(t *testing.T) {
goodRequest := func() *http.Request {
return prepReq(p, "/rpc/LuciLocalAuthService.GetOAuthToken", map[string]interface{}{
- "scopes": []string{"B", "A"},
- "secret": p.Secret,
+ "scopes": []string{"B", "A"},
+ "secret": p.Secret,
+ "account_id": "acc_id",
})
}
@@ -201,24 +212,42 @@ func TestProtocol(t *testing.T) {
req := prepReq(p, "/rpc/LuciLocalAuthService.GetOAuthToken", map[string]interface{}{
"secret": p.Secret,
})
- So(call(req), ShouldEqual, `HTTP 400: Field "scopes" is required.`)
+ So(call(req), ShouldEqual, `HTTP 400: Bad request: field "scopes" is required.`)
})
Convey("No secret", func() {
req := prepReq(p, "/rpc/LuciLocalAuthService.GetOAuthToken", map[string]interface{}{
"scopes": []string{"B", "A"},
})
- So(call(req), ShouldEqual, `HTTP 400: Field "secret" is required.`)
+ So(call(req), ShouldEqual, `HTTP 400: Bad request: field "secret" is required.`)
})
Convey("Bad secret", func() {
req := prepReq(p, "/rpc/LuciLocalAuthService.GetOAuthToken", map[string]interface{}{
- "scopes": []string{"B", "A"},
- "secret": []byte{0, 1, 2, 3},
+ "scopes": []string{"B", "A"},
+ "secret": []byte{0, 1, 2, 3},
+ "account_id": "acc_id",
})
So(call(req), ShouldEqual, `HTTP 403: Invalid secret.`)
})
+ Convey("No account ID", func() {
+ req := prepReq(p, "/rpc/LuciLocalAuthService.GetOAuthToken", map[string]interface{}{
+ "scopes": []string{"B", "A"},
+ "secret": p.Secret,
+ })
+ So(call(req), ShouldEqual, `HTTP 400: Bad request: field "account_id" is required.`)
+ })
+
+ Convey("Unknown account ID", func() {
+ req := prepReq(p, "/rpc/LuciLocalAuthService.GetOAuthToken", map[string]interface{}{
+ "scopes": []string{"B", "A"},
+ "secret": p.Secret,
+ "account_id": "unknown_acc_id",
+ })
+ So(call(req), ShouldEqual, `HTTP 404: Unrecognized account ID "unknown_acc_id".`)
+ })
+
Convey("Token generator returns fatal error", func() {
responses <- fmt.Errorf("fatal!!111")
So(call(goodRequest()), ShouldEqual, `HTTP 200 (json): {"error_code":-1,"error_message":"fatal!!111"}`)
« no previous file with comments | « common/auth/localauth/server.go ('k') | lucictx/local_auth.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698