| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. | 1 // Copyright 2017 The LUCI Authors. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 "github.com/luci/luci-go/common/retry/transient" | 29 "github.com/luci/luci-go/common/retry/transient" |
| 30 "github.com/luci/luci-go/lucictx" | 30 "github.com/luci/luci-go/lucictx" |
| 31 | 31 |
| 32 . "github.com/luci/luci-go/common/testing/assertions" | 32 . "github.com/luci/luci-go/common/testing/assertions" |
| 33 . "github.com/smartystreets/goconvey/convey" | 33 . "github.com/smartystreets/goconvey/convey" |
| 34 ) | 34 ) |
| 35 | 35 |
| 36 func TestLUCIContextProvider(t *testing.T) { | 36 func TestLUCIContextProvider(t *testing.T) { |
| 37 t.Parallel() | 37 t.Parallel() |
| 38 | 38 |
| 39 // Clear any existing LUCI_CONTEXT["local_auth"], it may be present if t
he |
| 40 // test runs on a LUCI bot. |
| 41 baseCtx, err := lucictx.Set(context.Background(), "local_auth", nil) |
| 42 if err != nil { |
| 43 t.Fatal(err) // this should never happen |
| 44 } |
| 45 |
| 39 Convey("Requires local_auth", t, func() { | 46 Convey("Requires local_auth", t, func() { |
| 40 » » _, err := NewLUCIContextTokenProvider(context.Background(), []st
ring{"A"}, http.DefaultTransport) | 47 » » _, err := NewLUCIContextTokenProvider(baseCtx, []string{"A"}, ht
tp.DefaultTransport) |
| 41 So(err, ShouldErrLike, `no "local_auth" in LUCI_CONTEXT`) | 48 So(err, ShouldErrLike, `no "local_auth" in LUCI_CONTEXT`) |
| 42 }) | 49 }) |
| 43 | 50 |
| 44 Convey("Requires default_account_id", t, func() { | 51 Convey("Requires default_account_id", t, func() { |
| 45 » » ctx := context.Background() | 52 » » ctx := lucictx.SetLocalAuth(baseCtx, &lucictx.LocalAuth{ |
| 46 » » ctx = lucictx.SetLocalAuth(ctx, &lucictx.LocalAuth{ | |
| 47 Accounts: []lucictx.LocalAuthAccount{{ID: "zzz"}}, | 53 Accounts: []lucictx.LocalAuthAccount{{ID: "zzz"}}, |
| 48 }) | 54 }) |
| 49 _, err := NewLUCIContextTokenProvider(ctx, []string{"A"}, http.D
efaultTransport) | 55 _, err := NewLUCIContextTokenProvider(ctx, []string{"A"}, http.D
efaultTransport) |
| 50 So(err, ShouldErrLike, `no "default_account_id"`) | 56 So(err, ShouldErrLike, `no "default_account_id"`) |
| 51 }) | 57 }) |
| 52 | 58 |
| 53 Convey("With mock server", t, func(c C) { | 59 Convey("With mock server", t, func(c C) { |
| 54 requests := make(chan rpcs.GetOAuthTokenRequest, 10000) | 60 requests := make(chan rpcs.GetOAuthTokenRequest, 10000) |
| 55 responses := make(chan interface{}, 1) | 61 responses := make(chan interface{}, 1) |
| 56 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWr
iter, r *http.Request) { | 62 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWr
iter, r *http.Request) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 75 w.WriteHeader(200) | 81 w.WriteHeader(200) |
| 76 c.So(json.NewEncoder(w).Encode(resp), ShouldBeNi
l) | 82 c.So(json.NewEncoder(w).Encode(resp), ShouldBeNi
l) |
| 77 case int: | 83 case int: |
| 78 http.Error(w, http.StatusText(resp), resp) | 84 http.Error(w, http.StatusText(resp), resp) |
| 79 default: | 85 default: |
| 80 panic("unexpected response type") | 86 panic("unexpected response type") |
| 81 } | 87 } |
| 82 })) | 88 })) |
| 83 defer ts.Close() | 89 defer ts.Close() |
| 84 | 90 |
| 85 » » ctx := context.Background() | 91 » » ctx := lucictx.SetLocalAuth(baseCtx, &lucictx.LocalAuth{ |
| 86 » » ctx = lucictx.SetLocalAuth(ctx, &lucictx.LocalAuth{ | |
| 87 RPCPort: uint32(ts.Listener.Addr().(*net.TCPAdd
r).Port), | 92 RPCPort: uint32(ts.Listener.Addr().(*net.TCPAdd
r).Port), |
| 88 Secret: []byte("zekret"), | 93 Secret: []byte("zekret"), |
| 89 DefaultAccountID: "acc_id", | 94 DefaultAccountID: "acc_id", |
| 90 }) | 95 }) |
| 91 | 96 |
| 92 p, err := NewLUCIContextTokenProvider(ctx, []string{"B", "A"}, h
ttp.DefaultTransport) | 97 p, err := NewLUCIContextTokenProvider(ctx, []string{"B", "A"}, h
ttp.DefaultTransport) |
| 93 So(err, ShouldBeNil) | 98 So(err, ShouldBeNil) |
| 94 | 99 |
| 95 Convey("Happy path", func() { | 100 Convey("Happy path", func() { |
| 96 responses <- rpcs.GetOAuthTokenResponse{ | 101 responses <- rpcs.GetOAuthTokenResponse{ |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 ErrorMessage: "omg, error", | 141 ErrorMessage: "omg, error", |
| 137 }, | 142 }, |
| 138 } | 143 } |
| 139 tok, err := p.MintToken(ctx, nil) | 144 tok, err := p.MintToken(ctx, nil) |
| 140 So(tok, ShouldBeNil) | 145 So(tok, ShouldBeNil) |
| 141 So(err, ShouldErrLike, `local auth - RPC code 123: omg,
error`) | 146 So(err, ShouldErrLike, `local auth - RPC code 123: omg,
error`) |
| 142 So(transient.Tag.In(err), ShouldBeFalse) | 147 So(transient.Tag.In(err), ShouldBeFalse) |
| 143 }) | 148 }) |
| 144 }) | 149 }) |
| 145 } | 150 } |
| OLD | NEW |