go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/appengine/gaeauth/server/internal/authdbimpl/handlers_test.go (about)

     1  // Copyright 2015 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 authdbimpl
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"net/http"
    21  	"net/http/httptest"
    22  	"testing"
    23  
    24  	"go.chromium.org/luci/appengine/gaetesting"
    25  	"go.chromium.org/luci/server/auth/service"
    26  	"go.chromium.org/luci/server/router"
    27  
    28  	. "github.com/smartystreets/goconvey/convey"
    29  )
    30  
    31  func TestPubSubHandlers(t *testing.T) {
    32  	Convey("pubSubPush skips when not configured", t, func() {
    33  		c := gaetesting.TestingContext()
    34  		rec := httptest.NewRecorder()
    35  		pubSubPush(&router.Context{
    36  			Writer:  rec,
    37  			Request: makePostRequest(c),
    38  		})
    39  		So(rec.Code, ShouldEqual, 200)
    40  		So(rec.Body.String(), ShouldEqual, "Auth Service URL is not configured, skipping the message")
    41  	})
    42  
    43  	Convey("pubSubPush works when no notification", t, func() {
    44  		c, _ := setupCtx()
    45  		rec := httptest.NewRecorder()
    46  		pubSubPush(&router.Context{
    47  			Writer:  rec,
    48  			Request: makePostRequest(c),
    49  		})
    50  		So(rec.Code, ShouldEqual, 200)
    51  		So(rec.Body.String(), ShouldEqual, "No new valid AuthDB change notifications")
    52  	})
    53  
    54  	Convey("pubSubPush old notification", t, func() {
    55  		c, srv := setupCtx()
    56  		srv.Notification = &service.Notification{Revision: 122} // older than 123
    57  		rec := httptest.NewRecorder()
    58  		pubSubPush(&router.Context{
    59  			Writer:  rec,
    60  			Request: makePostRequest(c),
    61  		})
    62  		So(rec.Code, ShouldEqual, 200)
    63  		So(rec.Body.String(), ShouldEqual, "Processed PubSub notification for rev 122: 123 -> 123")
    64  	})
    65  
    66  	Convey("pubSubPush fresh notification", t, func() {
    67  		c, srv := setupCtx()
    68  		srv.LatestRev = 130
    69  		srv.Notification = &service.Notification{Revision: 124}
    70  		rec := httptest.NewRecorder()
    71  		pubSubPush(&router.Context{
    72  			Writer:  rec,
    73  			Request: makePostRequest(c),
    74  		})
    75  		So(rec.Code, ShouldEqual, 200)
    76  		So(rec.Body.String(), ShouldEqual, "Processed PubSub notification for rev 124: 123 -> 130")
    77  	})
    78  }
    79  
    80  func setupCtx() (context.Context, *fakeAuthService) {
    81  	srv := &fakeAuthService{LatestRev: 123}
    82  	c := setAuthService(gaetesting.TestingContext(), srv)
    83  	So(ConfigureAuthService(c, "http://base_url", "http://auth-service"), ShouldBeNil)
    84  	return c, srv
    85  }
    86  
    87  func makePostRequest(ctx context.Context) *http.Request {
    88  	req, _ := http.NewRequestWithContext(ctx, "POST", "/doesntmatter", bytes.NewReader(nil))
    89  	return req
    90  }