go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth_service/impl/servers/oauth/server_test.go (about) 1 // Copyright 2022 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 oauth contains methods to work with oauth endpoint. 16 package oauth 17 18 import ( 19 "context" 20 "encoding/json" 21 "net/http" 22 "net/http/httptest" 23 "testing" 24 "time" 25 26 "google.golang.org/grpc/codes" 27 28 "go.chromium.org/luci/gae/impl/memory" 29 "go.chromium.org/luci/gae/service/datastore" 30 "go.chromium.org/luci/server/router" 31 32 "go.chromium.org/luci/auth_service/impl/model" 33 34 . "github.com/smartystreets/goconvey/convey" 35 . "go.chromium.org/luci/common/testing/assertions" 36 ) 37 38 func TestOAuthServing(t *testing.T) { 39 testTS := time.Date(2021, time.August, 16, 15, 20, 0, 0, time.UTC) 40 testClientID := "test-client-id" 41 testAdditionalClientIDs := []string{ 42 "additional-client-id-0", 43 "additional-client-id-1", 44 } 45 testClientSecret := "test-client-secret" 46 testTokenServer := "https://token-server.example.com" 47 testPrimaryURL := "test-primary-url" 48 49 testGlobalConfig := &model.AuthGlobalConfig{ 50 AuthVersionedEntityMixin: model.AuthVersionedEntityMixin{ 51 ModifiedTS: testTS, 52 ModifiedBy: "user:someone@example.com", 53 AuthDBRev: 2, 54 AuthDBPrevRev: 1, 55 }, 56 Kind: "AuthGlobalConfig", 57 ID: "root", 58 OAuthClientID: testClientID, 59 OAuthAdditionalClientIDs: testAdditionalClientIDs, 60 OAuthClientSecret: testClientSecret, 61 TokenServerURL: testTokenServer, 62 } 63 64 testAuthReplicationState := func(ctx context.Context) *model.AuthReplicationState { 65 return &model.AuthReplicationState{ 66 Kind: "AuthReplicationState", 67 ID: "self", 68 Parent: model.RootKey(ctx), 69 AuthDBRev: 2, 70 ModifiedTS: testTS, 71 PrimaryURL: testPrimaryURL, 72 } 73 } 74 75 legacyCall := func(ctx context.Context) ([]byte, error) { 76 rw := httptest.NewRecorder() 77 78 rctx := &router.Context{ 79 Writer: rw, 80 Request: (&http.Request{}).WithContext(ctx), 81 } 82 83 if err := HandleLegacyOAuthEndpoint(rctx); err != nil { 84 return nil, err 85 } 86 return rw.Body.Bytes(), nil 87 } 88 89 t.Parallel() 90 91 type OAuthJSON struct { 92 TokenServerURL string `json:"token_server_url"` 93 ClientSecret string `json:"client_not_so_secret"` 94 AdditionalClientIDs []string `json:"additional_client_ids"` 95 ClientID string `json:"client_id"` 96 PrimaryURL string `json:"primary_url"` 97 } 98 99 Convey("Testing legacy endpoint with JSON response", t, func() { 100 ctx := memory.Use(context.Background()) 101 102 Convey("AuthReplicationState entity not found", func() { 103 So(datastore.Put(ctx, testGlobalConfig), ShouldBeNil) 104 _, err := legacyCall(ctx) 105 So(err, ShouldHaveGRPCStatus, codes.Internal) 106 So(err.Error(), ShouldContainSubstring, "no Replication State entity found in datastore.") 107 }) 108 109 Convey("AuthGlobalConfig entity not found", func() { 110 So(datastore.Put(ctx, testAuthReplicationState(ctx)), ShouldBeNil) 111 _, err := legacyCall(ctx) 112 So(err, ShouldHaveGRPCStatus, codes.Internal) 113 So(err.Error(), ShouldContainSubstring, "no Global Config entity found in datastore.") 114 }) 115 116 Convey("OK", func() { 117 So(datastore.Put(ctx, 118 testGlobalConfig, 119 testAuthReplicationState(ctx)), ShouldBeNil) 120 actualBlob, err := legacyCall(ctx) 121 So(err, ShouldBeNil) 122 123 actualJSON := &OAuthJSON{} 124 expectedJSON := &OAuthJSON{ 125 TokenServerURL: testTokenServer, 126 ClientSecret: testClientSecret, 127 AdditionalClientIDs: testAdditionalClientIDs, 128 ClientID: testClientID, 129 PrimaryURL: testPrimaryURL, 130 } 131 So(json.Unmarshal(actualBlob, actualJSON), ShouldBeNil) 132 So(actualJSON, ShouldResemble, expectedJSON) 133 }) 134 135 }) 136 }