| 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 config |
| 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/server/auth" |
| 18 "github.com/luci/luci-go/server/auth/authtest" |
| 19 "github.com/luci/luci-go/server/auth/delegation" |
| 20 |
| 21 "golang.org/x/net/context" |
| 22 |
| 23 . "github.com/smartystreets/goconvey/convey" |
| 24 ) |
| 25 |
| 26 type remoteInterfaceService struct { |
| 27 err error |
| 28 lastReq *http.Request |
| 29 res interface{} |
| 30 } |
| 31 |
| 32 func (ris *remoteInterfaceService) ServeHTTP(rw http.ResponseWriter, req *http.R
equest) { |
| 33 ris.lastReq = req |
| 34 if err := ris.err; err != nil { |
| 35 http.Error(rw, err.Error(), http.StatusInternalServerError) |
| 36 return |
| 37 } |
| 38 |
| 39 if err := json.NewEncoder(rw).Encode(ris.res); err != nil { |
| 40 http.Error(rw, fmt.Sprintf("failed to encode JSON: %s", err), ht
tp.StatusInternalServerError) |
| 41 return |
| 42 } |
| 43 } |
| 44 |
| 45 func TestRemoteService(t *testing.T) { |
| 46 t.Parallel() |
| 47 |
| 48 Convey(`Testing the remote service`, t, func() { |
| 49 c := context.Background() |
| 50 |
| 51 fs := authtest.FakeState{ |
| 52 Identity: "user:foo@bar.baz", |
| 53 } |
| 54 c = auth.WithState(c, &fs) |
| 55 c = authtest.MockAuthConfig(c) |
| 56 |
| 57 ris := remoteInterfaceService{} |
| 58 svr := httptest.NewServer(&ris) |
| 59 defer svr.Close() |
| 60 |
| 61 c = WithBackend(c, &ClientBackend{ |
| 62 Provider: &RemoteClientProvider{ |
| 63 BaseURL: svr.URL, |
| 64 testUserDelegationToken: "user token", |
| 65 }, |
| 66 }) |
| 67 |
| 68 b64 := func(v string) string { return base64.StdEncoding.EncodeT
oString([]byte(v)) } |
| 69 |
| 70 Convey(`Can get the service URL`, func() { |
| 71 hostURL, err := url.Parse(svr.URL) |
| 72 if err != nil { |
| 73 panic(err) |
| 74 } |
| 75 hostURL.Path = "/_ah/api/config/v1/" |
| 76 |
| 77 So(ServiceURL(c), ShouldResemble, *hostURL) |
| 78 }) |
| 79 |
| 80 Convey(`Can resolve a single config`, func() { |
| 81 ris.res = configApi.LuciConfigGetConfigResponseMessage{ |
| 82 Content: b64("ohai"), |
| 83 ContentHash: "####", |
| 84 Revision: "v1", |
| 85 } |
| 86 |
| 87 Convey(`AsService`, func() { |
| 88 var val string |
| 89 So(Get(c, AsService, "foo/bar", "baz", String(&v
al), nil), ShouldBeNil) |
| 90 So(val, ShouldEqual, "ohai") |
| 91 |
| 92 So(ris.lastReq.URL.Path, ShouldEqual, "/_ah/api/
config/v1/config_sets/foo/bar/config/baz") |
| 93 So(ris.lastReq.Header.Get("Authorization"), Shou
ldEqual, "Bearer fake_token") |
| 94 So(ris.lastReq.Header.Get(delegation.HTTPHeaderN
ame), ShouldEqual, "") |
| 95 }) |
| 96 |
| 97 Convey(`AsUser`, func() { |
| 98 var val string |
| 99 So(Get(c, AsUser, "foo/bar", "baz", String(&val)
, nil), ShouldBeNil) |
| 100 So(val, ShouldEqual, "ohai") |
| 101 |
| 102 So(ris.lastReq.URL.Path, ShouldEqual, "/_ah/api/
config/v1/config_sets/foo/bar/config/baz") |
| 103 So(ris.lastReq.Header.Get("Authorization"), Shou
ldEqual, "Bearer fake_token") |
| 104 So(ris.lastReq.Header.Get(delegation.HTTPHeaderN
ame), ShouldEqual, "user token") |
| 105 }) |
| 106 |
| 107 Convey(`AsAnonymous`, func() { |
| 108 var val string |
| 109 So(Get(c, AsAnonymous, "foo/bar", "baz", String(
&val), nil), ShouldBeNil) |
| 110 So(val, ShouldEqual, "ohai") |
| 111 |
| 112 So(ris.lastReq.URL.Path, ShouldEqual, "/_ah/api/
config/v1/config_sets/foo/bar/config/baz") |
| 113 So(ris.lastReq.Header.Get("Authorization"), Shou
ldEqual, "") |
| 114 So(ris.lastReq.Header.Get(delegation.HTTPHeaderN
ame), ShouldEqual, "") |
| 115 }) |
| 116 }) |
| 117 |
| 118 Convey(`Can resolve multiple configs`, func() { |
| 119 ris.res = configApi.LuciConfigGetConfigMultiResponseMess
age{ |
| 120 Configs: []*configApi.LuciConfigGetConfigMultiRe
sponseMessageConfigEntry{ |
| 121 {ConfigSet: "projects/foo", Content: b64
("foo"), ContentHash: "####", Revision: "v1"}, |
| 122 {ConfigSet: "projects/bar", Content: b64
("bar"), ContentHash: "####", Revision: "v1"}, |
| 123 }, |
| 124 } |
| 125 |
| 126 for _, tc := range []struct { |
| 127 name string |
| 128 t GetAllType |
| 129 path string |
| 130 }{ |
| 131 {"Project", Project, "/_ah/api/config/v1/configs
/projects/baz"}, |
| 132 {"Ref", Ref, "/_ah/api/config/v1/configs/refs/ba
z"}, |
| 133 } { |
| 134 Convey(tc.name, func() { |
| 135 var ( |
| 136 val []string |
| 137 meta []*Meta |
| 138 ) |
| 139 So(GetAll(c, AsService, tc.t, "baz", Str
ingSlice(&val), &meta), ShouldBeNil) |
| 140 So(val, ShouldResemble, []string{"foo",
"bar"}) |
| 141 So(meta, ShouldResemble, []*Meta{ |
| 142 {"projects/foo", "baz", "####",
"v1"}, |
| 143 {"projects/bar", "baz", "####",
"v1"}, |
| 144 }) |
| 145 |
| 146 So(ris.lastReq.URL.Path, ShouldEqual, tc
.path) |
| 147 }) |
| 148 } |
| 149 }) |
| 150 }) |
| 151 } |
| OLD | NEW |