go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/auth/signing/info_test.go (about)

     1  // Copyright 2016 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (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
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package signing
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  	"testing"
    21  
    22  	"go.chromium.org/luci/auth/identity"
    23  
    24  	"go.chromium.org/luci/server/auth/internal"
    25  	"go.chromium.org/luci/server/caching"
    26  	"go.chromium.org/luci/server/caching/cachingtest"
    27  
    28  	. "github.com/smartystreets/goconvey/convey"
    29  	. "go.chromium.org/luci/common/testing/assertions"
    30  )
    31  
    32  func TestFetchServiceInfo(t *testing.T) {
    33  	t.Parallel()
    34  
    35  	const testURL = "https://test.example.com"
    36  
    37  	Convey("With empty cache", t, func() {
    38  		ctx := caching.WithEmptyProcessCache(context.Background())
    39  		ctx = cachingtest.WithGlobalCache(ctx, map[string]caching.BlobCache{
    40  			infoCacheNamespace: cachingtest.NewBlobCache(),
    41  		})
    42  
    43  		Convey("Works", func() {
    44  			calls := 0
    45  			ctx := internal.WithTestTransport(ctx, func(r *http.Request, body string) (int, string) {
    46  				So(r.URL.String(), ShouldEqual, testURL)
    47  				calls++
    48  				return 200, `{
    49  					"app_id": "some-app-id",
    50  					"app_runtime": "go",
    51  					"app_runtime_version": "go1.5.1",
    52  					"app_version": "1234-abcdef",
    53  					"service_account_name": "some-app-id@appspot.gserviceaccount.com"
    54  				}`
    55  			})
    56  
    57  			expected := &ServiceInfo{
    58  				AppID:              "some-app-id",
    59  				AppRuntime:         "go",
    60  				AppRuntimeVersion:  "go1.5.1",
    61  				AppVersion:         "1234-abcdef",
    62  				ServiceAccountName: "some-app-id@appspot.gserviceaccount.com",
    63  			}
    64  
    65  			info1, err := FetchServiceInfo(ctx, testURL)
    66  			So(err, ShouldBeNil)
    67  			So(info1, ShouldResemble, expected)
    68  			So(calls, ShouldEqual, 1)
    69  
    70  			// The in-process cache works.
    71  			info2, err := FetchServiceInfo(ctx, testURL)
    72  			So(err, ShouldBeNil)
    73  			So(info2, ShouldEqual, info1) // the exact same object
    74  			So(calls, ShouldEqual, 1)     // no new calls
    75  
    76  			// The global cache works too.
    77  			ctx = caching.WithEmptyProcessCache(ctx)
    78  			info3, err := FetchServiceInfo(ctx, testURL)
    79  			So(err, ShouldBeNil)
    80  			So(info3, ShouldNotPointTo, info1)  // a new deserialized object
    81  			So(info3, ShouldResemble, expected) // still has the correct value
    82  			So(calls, ShouldEqual, 1)           // no new calls
    83  		})
    84  
    85  		Convey("Error", func() {
    86  			ctx := internal.WithTestTransport(ctx, func(r *http.Request, body string) (int, string) {
    87  				return 500, "error"
    88  			})
    89  			info, err := FetchServiceInfo(ctx, testURL)
    90  			So(info, ShouldBeNil)
    91  			So(err, ShouldNotBeNil)
    92  		})
    93  	})
    94  }
    95  
    96  func TestFetchLUCIServiceIdentity(t *testing.T) {
    97  	t.Parallel()
    98  
    99  	Convey("Works", t, func() {
   100  		ctx := caching.WithEmptyProcessCache(context.Background())
   101  		ctx = internal.WithTestTransport(ctx, func(r *http.Request, body string) (code int, response string) {
   102  			if r.URL.String() != "https://blah/auth/api/v1/server/info" {
   103  				return 400, "Wrong URL"
   104  			}
   105  			return 200, `{"app_id": "blah-app-id"}`
   106  		})
   107  
   108  		id, err := FetchLUCIServiceIdentity(ctx, "https://blah")
   109  		So(err, ShouldBeNil)
   110  		So(id, ShouldEqual, identity.Identity("service:blah-app-id"))
   111  
   112  		_, err = FetchLUCIServiceIdentity(ctx, "http://blah")
   113  		So(err, ShouldErrLike, "not an https:// URL")
   114  
   115  		_, err = FetchLUCIServiceIdentity(ctx, "https://blah/blah")
   116  		So(err, ShouldErrLike, "not a root URL")
   117  
   118  		_, err = FetchLUCIServiceIdentity(ctx, "https://")
   119  		So(err, ShouldErrLike, "not a root URL")
   120  	})
   121  }