| Index: server/config/service_test.go
|
| diff --git a/server/config/service_test.go b/server/config/service_test.go
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..da394a08c357205a1ee2d781e7ea74ad425ad69b
|
| --- /dev/null
|
| +++ b/server/config/service_test.go
|
| @@ -0,0 +1,151 @@
|
| +// Copyright 2016 The LUCI Authors. All rights reserved.
|
| +// Use of this source code is governed under the Apache License, Version 2.0
|
| +// that can be found in the LICENSE file.
|
| +
|
| +package config
|
| +
|
| +import (
|
| + "encoding/base64"
|
| + "encoding/json"
|
| + "fmt"
|
| + "net/http"
|
| + "net/http/httptest"
|
| + "net/url"
|
| + "testing"
|
| +
|
| + configApi "github.com/luci/luci-go/common/api/luci_config/config/v1"
|
| + "github.com/luci/luci-go/server/auth"
|
| + "github.com/luci/luci-go/server/auth/authtest"
|
| + "github.com/luci/luci-go/server/auth/delegation"
|
| +
|
| + "golang.org/x/net/context"
|
| +
|
| + . "github.com/smartystreets/goconvey/convey"
|
| +)
|
| +
|
| +type remoteInterfaceService struct {
|
| + err error
|
| + lastReq *http.Request
|
| + res interface{}
|
| +}
|
| +
|
| +func (ris *remoteInterfaceService) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
| + ris.lastReq = req
|
| + if err := ris.err; err != nil {
|
| + http.Error(rw, err.Error(), http.StatusInternalServerError)
|
| + return
|
| + }
|
| +
|
| + if err := json.NewEncoder(rw).Encode(ris.res); err != nil {
|
| + http.Error(rw, fmt.Sprintf("failed to encode JSON: %s", err), http.StatusInternalServerError)
|
| + return
|
| + }
|
| +}
|
| +
|
| +func TestRemoteService(t *testing.T) {
|
| + t.Parallel()
|
| +
|
| + Convey(`Testing the remote service`, t, func() {
|
| + c := context.Background()
|
| +
|
| + fs := authtest.FakeState{
|
| + Identity: "user:foo@bar.baz",
|
| + }
|
| + c = auth.WithState(c, &fs)
|
| + c = authtest.MockAuthConfig(c)
|
| +
|
| + ris := remoteInterfaceService{}
|
| + svr := httptest.NewServer(&ris)
|
| + defer svr.Close()
|
| +
|
| + c = WithBackend(c, &ClientBackend{
|
| + Provider: &RemoteClientProvider{
|
| + BaseURL: svr.URL,
|
| + testUserDelegationToken: "user token",
|
| + },
|
| + })
|
| +
|
| + b64 := func(v string) string { return base64.StdEncoding.EncodeToString([]byte(v)) }
|
| +
|
| + Convey(`Can get the service URL`, func() {
|
| + hostURL, err := url.Parse(svr.URL)
|
| + if err != nil {
|
| + panic(err)
|
| + }
|
| + hostURL.Path = "/_ah/api/config/v1/"
|
| +
|
| + So(ServiceURL(c), ShouldResemble, *hostURL)
|
| + })
|
| +
|
| + Convey(`Can resolve a single config`, func() {
|
| + ris.res = configApi.LuciConfigGetConfigResponseMessage{
|
| + Content: b64("ohai"),
|
| + ContentHash: "####",
|
| + Revision: "v1",
|
| + }
|
| +
|
| + Convey(`AsService`, func() {
|
| + var val string
|
| + So(Get(c, AsService, "foo/bar", "baz", String(&val), nil), ShouldBeNil)
|
| + So(val, ShouldEqual, "ohai")
|
| +
|
| + So(ris.lastReq.URL.Path, ShouldEqual, "/_ah/api/config/v1/config_sets/foo/bar/config/baz")
|
| + So(ris.lastReq.Header.Get("Authorization"), ShouldEqual, "Bearer fake_token")
|
| + So(ris.lastReq.Header.Get(delegation.HTTPHeaderName), ShouldEqual, "")
|
| + })
|
| +
|
| + Convey(`AsUser`, func() {
|
| + var val string
|
| + So(Get(c, AsUser, "foo/bar", "baz", String(&val), nil), ShouldBeNil)
|
| + So(val, ShouldEqual, "ohai")
|
| +
|
| + So(ris.lastReq.URL.Path, ShouldEqual, "/_ah/api/config/v1/config_sets/foo/bar/config/baz")
|
| + So(ris.lastReq.Header.Get("Authorization"), ShouldEqual, "Bearer fake_token")
|
| + So(ris.lastReq.Header.Get(delegation.HTTPHeaderName), ShouldEqual, "user token")
|
| + })
|
| +
|
| + Convey(`AsAnonymous`, func() {
|
| + var val string
|
| + So(Get(c, AsAnonymous, "foo/bar", "baz", String(&val), nil), ShouldBeNil)
|
| + So(val, ShouldEqual, "ohai")
|
| +
|
| + So(ris.lastReq.URL.Path, ShouldEqual, "/_ah/api/config/v1/config_sets/foo/bar/config/baz")
|
| + So(ris.lastReq.Header.Get("Authorization"), ShouldEqual, "")
|
| + So(ris.lastReq.Header.Get(delegation.HTTPHeaderName), ShouldEqual, "")
|
| + })
|
| + })
|
| +
|
| + Convey(`Can resolve multiple configs`, func() {
|
| + ris.res = configApi.LuciConfigGetConfigMultiResponseMessage{
|
| + Configs: []*configApi.LuciConfigGetConfigMultiResponseMessageConfigEntry{
|
| + {ConfigSet: "projects/foo", Content: b64("foo"), ContentHash: "####", Revision: "v1"},
|
| + {ConfigSet: "projects/bar", Content: b64("bar"), ContentHash: "####", Revision: "v1"},
|
| + },
|
| + }
|
| +
|
| + for _, tc := range []struct {
|
| + name string
|
| + t GetAllType
|
| + path string
|
| + }{
|
| + {"Project", Project, "/_ah/api/config/v1/configs/projects/baz"},
|
| + {"Ref", Ref, "/_ah/api/config/v1/configs/refs/baz"},
|
| + } {
|
| + Convey(tc.name, func() {
|
| + var (
|
| + val []string
|
| + meta []*Meta
|
| + )
|
| + So(GetAll(c, AsService, tc.t, "baz", StringSlice(&val), &meta), ShouldBeNil)
|
| + So(val, ShouldResemble, []string{"foo", "bar"})
|
| + So(meta, ShouldResemble, []*Meta{
|
| + {"projects/foo", "baz", "####", "v1"},
|
| + {"projects/bar", "baz", "####", "v1"},
|
| + })
|
| +
|
| + So(ris.lastReq.URL.Path, ShouldEqual, tc.path)
|
| + })
|
| + }
|
| + })
|
| + })
|
| +}
|
|
|