| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 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 internal | 5 package internal |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "context" | 8 "context" |
| 9 "encoding/json" | 9 "encoding/json" |
| 10 "net" | 10 "net" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 ) | 24 ) |
| 25 | 25 |
| 26 func TestLUCIContextProvider(t *testing.T) { | 26 func TestLUCIContextProvider(t *testing.T) { |
| 27 t.Parallel() | 27 t.Parallel() |
| 28 | 28 |
| 29 Convey("Requires local_auth", t, func() { | 29 Convey("Requires local_auth", t, func() { |
| 30 _, err := NewLUCIContextTokenProvider(context.Background(), []st
ring{"A"}, http.DefaultTransport) | 30 _, err := NewLUCIContextTokenProvider(context.Background(), []st
ring{"A"}, http.DefaultTransport) |
| 31 So(err, ShouldErrLike, `no "local_auth" in LUCI_CONTEXT`) | 31 So(err, ShouldErrLike, `no "local_auth" in LUCI_CONTEXT`) |
| 32 }) | 32 }) |
| 33 | 33 |
| 34 Convey("Requires default_account_id", t, func() { |
| 35 ctx := context.Background() |
| 36 ctx = lucictx.SetLocalAuth(ctx, &lucictx.LocalAuth{ |
| 37 Accounts: []lucictx.LocalAuthAccount{{ID: "zzz"}}, |
| 38 }) |
| 39 _, err := NewLUCIContextTokenProvider(ctx, []string{"A"}, http.D
efaultTransport) |
| 40 So(err, ShouldErrLike, `no "default_account_id"`) |
| 41 }) |
| 42 |
| 34 Convey("With mock server", t, func(c C) { | 43 Convey("With mock server", t, func(c C) { |
| 35 requests := make(chan rpcs.GetOAuthTokenRequest, 10000) | 44 requests := make(chan rpcs.GetOAuthTokenRequest, 10000) |
| 36 responses := make(chan interface{}, 1) | 45 responses := make(chan interface{}, 1) |
| 37 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWr
iter, r *http.Request) { | 46 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWr
iter, r *http.Request) { |
| 38 c.So(r.Method, ShouldEqual, "POST") | 47 c.So(r.Method, ShouldEqual, "POST") |
| 39 c.So(r.RequestURI, ShouldEqual, "/rpc/LuciLocalAuthServi
ce.GetOAuthToken") | 48 c.So(r.RequestURI, ShouldEqual, "/rpc/LuciLocalAuthServi
ce.GetOAuthToken") |
| 40 c.So(r.Header.Get("Content-Type"), ShouldEqual, "applica
tion/json") | 49 c.So(r.Header.Get("Content-Type"), ShouldEqual, "applica
tion/json") |
| 41 | 50 |
| 42 req := rpcs.GetOAuthTokenRequest{} | 51 req := rpcs.GetOAuthTokenRequest{} |
| 43 c.So(json.NewDecoder(r.Body).Decode(&req), ShouldBeNil) | 52 c.So(json.NewDecoder(r.Body).Decode(&req), ShouldBeNil) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 58 case int: | 67 case int: |
| 59 http.Error(w, http.StatusText(resp), resp) | 68 http.Error(w, http.StatusText(resp), resp) |
| 60 default: | 69 default: |
| 61 panic("unexpected response type") | 70 panic("unexpected response type") |
| 62 } | 71 } |
| 63 })) | 72 })) |
| 64 defer ts.Close() | 73 defer ts.Close() |
| 65 | 74 |
| 66 ctx := context.Background() | 75 ctx := context.Background() |
| 67 ctx = lucictx.SetLocalAuth(ctx, &lucictx.LocalAuth{ | 76 ctx = lucictx.SetLocalAuth(ctx, &lucictx.LocalAuth{ |
| 68 » » » RPCPort: uint32(ts.Listener.Addr().(*net.TCPAddr).Port), | 77 » » » RPCPort: uint32(ts.Listener.Addr().(*net.TCPAdd
r).Port), |
| 69 » » » Secret: []byte("zekret"), | 78 » » » Secret: []byte("zekret"), |
| 79 » » » DefaultAccountID: "acc_id", |
| 70 }) | 80 }) |
| 71 | 81 |
| 72 p, err := NewLUCIContextTokenProvider(ctx, []string{"B", "A"}, h
ttp.DefaultTransport) | 82 p, err := NewLUCIContextTokenProvider(ctx, []string{"B", "A"}, h
ttp.DefaultTransport) |
| 73 So(err, ShouldBeNil) | 83 So(err, ShouldBeNil) |
| 74 | 84 |
| 75 Convey("Happy path", func() { | 85 Convey("Happy path", func() { |
| 76 responses <- rpcs.GetOAuthTokenResponse{ | 86 responses <- rpcs.GetOAuthTokenResponse{ |
| 77 AccessToken: "zzz", | 87 AccessToken: "zzz", |
| 78 Expiry: 1487456796, | 88 Expiry: 1487456796, |
| 79 } | 89 } |
| 80 | 90 |
| 81 tok, err := p.MintToken(ctx, nil) | 91 tok, err := p.MintToken(ctx, nil) |
| 82 So(err, ShouldBeNil) | 92 So(err, ShouldBeNil) |
| 83 So(tok, ShouldResemble, &oauth2.Token{ | 93 So(tok, ShouldResemble, &oauth2.Token{ |
| 84 AccessToken: "zzz", | 94 AccessToken: "zzz", |
| 85 TokenType: "Bearer", | 95 TokenType: "Bearer", |
| 86 Expiry: time.Unix(1487456796, 0).UTC(), | 96 Expiry: time.Unix(1487456796, 0).UTC(), |
| 87 }) | 97 }) |
| 88 | 98 |
| 89 So(<-requests, ShouldResemble, rpcs.GetOAuthTokenRequest
{ | 99 So(<-requests, ShouldResemble, rpcs.GetOAuthTokenRequest
{ |
| 90 » » » » Scopes: []string{"B", "A"}, | 100 » » » » Scopes: []string{"B", "A"}, |
| 91 » » » » Secret: []byte("zekret"), | 101 » » » » Secret: []byte("zekret"), |
| 102 » » » » AccountID: "acc_id", |
| 92 }) | 103 }) |
| 93 }) | 104 }) |
| 94 | 105 |
| 95 Convey("HTTP 500", func() { | 106 Convey("HTTP 500", func() { |
| 96 responses <- 500 | 107 responses <- 500 |
| 97 tok, err := p.MintToken(ctx, nil) | 108 tok, err := p.MintToken(ctx, nil) |
| 98 So(tok, ShouldBeNil) | 109 So(tok, ShouldBeNil) |
| 99 So(err, ShouldErrLike, `local auth - HTTP 500`) | 110 So(err, ShouldErrLike, `local auth - HTTP 500`) |
| 100 So(errors.IsTransient(err), ShouldBeTrue) | 111 So(errors.IsTransient(err), ShouldBeTrue) |
| 101 }) | 112 }) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 115 ErrorMessage: "omg, error", | 126 ErrorMessage: "omg, error", |
| 116 }, | 127 }, |
| 117 } | 128 } |
| 118 tok, err := p.MintToken(ctx, nil) | 129 tok, err := p.MintToken(ctx, nil) |
| 119 So(tok, ShouldBeNil) | 130 So(tok, ShouldBeNil) |
| 120 So(err, ShouldErrLike, `local auth - RPC code 123: omg,
error`) | 131 So(err, ShouldErrLike, `local auth - RPC code 123: omg,
error`) |
| 121 So(errors.IsTransient(err), ShouldBeFalse) | 132 So(errors.IsTransient(err), ShouldBeFalse) |
| 122 }) | 133 }) |
| 123 }) | 134 }) |
| 124 } | 135 } |
| OLD | NEW |