| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package client |
| 6 |
| 7 import ( |
| 8 "encoding/base64" |
| 9 "encoding/json" |
| 10 "fmt" |
| 11 "net/http" |
| 12 "net/http/httptest" |
| 13 "net/url" |
| 14 "testing" |
| 15 |
| 16 configApi "github.com/luci/luci-go/common/api/luci_config/config/v1" |
| 17 "github.com/luci/luci-go/luci_config/server/cfgclient" |
| 18 "github.com/luci/luci-go/luci_config/server/cfgclient/backend" |
| 19 "github.com/luci/luci-go/server/auth" |
| 20 "github.com/luci/luci-go/server/auth/authtest" |
| 21 "github.com/luci/luci-go/server/auth/delegation" |
| 22 |
| 23 "golang.org/x/net/context" |
| 24 |
| 25 . "github.com/smartystreets/goconvey/convey" |
| 26 ) |
| 27 |
| 28 type remoteInterfaceService struct { |
| 29 err error |
| 30 lastReq *http.Request |
| 31 res interface{} |
| 32 } |
| 33 |
| 34 func (ris *remoteInterfaceService) ServeHTTP(rw http.ResponseWriter, req *http.R
equest) { |
| 35 ris.lastReq = req |
| 36 if err := ris.err; err != nil { |
| 37 http.Error(rw, err.Error(), http.StatusInternalServerError) |
| 38 return |
| 39 } |
| 40 |
| 41 if err := json.NewEncoder(rw).Encode(ris.res); err != nil { |
| 42 http.Error(rw, fmt.Sprintf("failed to encode JSON: %s", err), ht
tp.StatusInternalServerError) |
| 43 return |
| 44 } |
| 45 } |
| 46 |
| 47 func TestRemoteService(t *testing.T) { |
| 48 t.Parallel() |
| 49 |
| 50 Convey(`Testing the remote service`, t, func() { |
| 51 c := context.Background() |
| 52 |
| 53 fs := authtest.FakeState{ |
| 54 Identity: "user:foo@bar.baz", |
| 55 } |
| 56 c = auth.WithState(c, &fs) |
| 57 c = authtest.MockAuthConfig(c) |
| 58 |
| 59 ris := remoteInterfaceService{} |
| 60 svr := httptest.NewServer(&ris) |
| 61 defer svr.Close() |
| 62 |
| 63 c = backend.WithBackend(c, &Backend{ |
| 64 Provider: &RemoteProvider{ |
| 65 BaseURL: svr.URL, |
| 66 testUserDelegationToken: "user token", |
| 67 }, |
| 68 }) |
| 69 |
| 70 b64 := func(v string) string { return base64.StdEncoding.EncodeT
oString([]byte(v)) } |
| 71 |
| 72 Convey(`Can get the service URL`, func() { |
| 73 hostURL, err := url.Parse(svr.URL) |
| 74 if err != nil { |
| 75 panic(err) |
| 76 } |
| 77 hostURL.Path = "/_ah/api/config/v1/" |
| 78 |
| 79 So(cfgclient.ServiceURL(c), ShouldResemble, *hostURL) |
| 80 }) |
| 81 |
| 82 Convey(`Can resolve a single config`, func() { |
| 83 ris.res = configApi.LuciConfigGetConfigResponseMessage{ |
| 84 Content: b64("ohai"), |
| 85 ContentHash: "####", |
| 86 Revision: "v1", |
| 87 } |
| 88 |
| 89 Convey(`AsService`, func() { |
| 90 var val string |
| 91 So(cfgclient.Get(c, cfgclient.AsService, "foo/ba
r", "baz", cfgclient.String(&val), nil), ShouldBeNil) |
| 92 So(val, ShouldEqual, "ohai") |
| 93 |
| 94 So(ris.lastReq.URL.Path, ShouldEqual, "/_ah/api/
config/v1/config_sets/foo/bar/config/baz") |
| 95 So(ris.lastReq.Header.Get("Authorization"), Shou
ldEqual, "Bearer fake_token") |
| 96 So(ris.lastReq.Header.Get(delegation.HTTPHeaderN
ame), ShouldEqual, "") |
| 97 }) |
| 98 |
| 99 Convey(`AsUser`, func() { |
| 100 var val string |
| 101 So(cfgclient.Get(c, cfgclient.AsUser, "foo/bar",
"baz", cfgclient.String(&val), nil), ShouldBeNil) |
| 102 So(val, ShouldEqual, "ohai") |
| 103 |
| 104 So(ris.lastReq.URL.Path, ShouldEqual, "/_ah/api/
config/v1/config_sets/foo/bar/config/baz") |
| 105 So(ris.lastReq.Header.Get("Authorization"), Shou
ldEqual, "Bearer fake_token") |
| 106 So(ris.lastReq.Header.Get(delegation.HTTPHeaderN
ame), ShouldEqual, "user token") |
| 107 }) |
| 108 |
| 109 Convey(`AsAnonymous`, func() { |
| 110 var val string |
| 111 So(cfgclient.Get(c, cfgclient.AsAnonymous, "foo/
bar", "baz", cfgclient.String(&val), nil), ShouldBeNil) |
| 112 So(val, ShouldEqual, "ohai") |
| 113 |
| 114 So(ris.lastReq.URL.Path, ShouldEqual, "/_ah/api/
config/v1/config_sets/foo/bar/config/baz") |
| 115 So(ris.lastReq.Header.Get("Authorization"), Shou
ldEqual, "") |
| 116 So(ris.lastReq.Header.Get(delegation.HTTPHeaderN
ame), ShouldEqual, "") |
| 117 }) |
| 118 }) |
| 119 |
| 120 Convey(`Can resolve multiple configs`, func() { |
| 121 ris.res = configApi.LuciConfigGetConfigMultiResponseMess
age{ |
| 122 Configs: []*configApi.LuciConfigGetConfigMultiRe
sponseMessageConfigEntry{ |
| 123 {ConfigSet: "projects/foo", Content: b64
("foo"), ContentHash: "####", Revision: "v1"}, |
| 124 {ConfigSet: "projects/bar", Content: b64
("bar"), ContentHash: "####", Revision: "v1"}, |
| 125 }, |
| 126 } |
| 127 |
| 128 for _, tc := range []struct { |
| 129 name string |
| 130 fn func(context.Context, cfgclient.Authority,
string, cfgclient.MultiResolver, *[]*cfgclient.Meta) error |
| 131 path string |
| 132 }{ |
| 133 {"Project", cfgclient.Projects, "/_ah/api/config
/v1/configs/projects/baz"}, |
| 134 {"Ref", cfgclient.Refs, "/_ah/api/config/v1/conf
igs/refs/baz"}, |
| 135 } { |
| 136 Convey(tc.name, func() { |
| 137 var ( |
| 138 val []string |
| 139 meta []*cfgclient.Meta |
| 140 ) |
| 141 So(tc.fn(c, cfgclient.AsService, "baz",
cfgclient.StringSlice(&val), &meta), ShouldBeNil) |
| 142 So(val, ShouldResemble, []string{"foo",
"bar"}) |
| 143 So(meta, ShouldResemble, []*cfgclient.Me
ta{ |
| 144 {"projects/foo", "baz", "####",
"v1"}, |
| 145 {"projects/bar", "baz", "####",
"v1"}, |
| 146 }) |
| 147 |
| 148 So(ris.lastReq.URL.Path, ShouldEqual, tc
.path) |
| 149 }) |
| 150 } |
| 151 }) |
| 152 }) |
| 153 } |
| 154 |
| 155 func TestAuthorityRPCTranslation(t *testing.T) { |
| 156 t.Parallel() |
| 157 |
| 158 Convey(`Testing authority RPC kind translation`, t, func() { |
| 159 So(rpcAuthorityKind(backend.AsAnonymous), ShouldEqual, auth.NoAu
th) |
| 160 So(rpcAuthorityKind(backend.AsUser), ShouldEqual, auth.AsUser) |
| 161 So(rpcAuthorityKind(backend.AsService), ShouldEqual, auth.AsSelf
) |
| 162 So(func() { rpcAuthorityKind(backend.Authority(-1)) }, ShouldPan
ic) |
| 163 }) |
| 164 } |
| OLD | NEW |