go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/loginsessions/internal/store_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 internal
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"google.golang.org/protobuf/types/known/timestamppb"
    23  
    24  	"go.chromium.org/luci/common/clock/testclock"
    25  	"go.chromium.org/luci/gae/impl/memory"
    26  	"go.chromium.org/luci/gae/service/datastore"
    27  
    28  	"go.chromium.org/luci/server/loginsessions/internal/statepb"
    29  
    30  	. "github.com/smartystreets/goconvey/convey"
    31  	. "go.chromium.org/luci/common/testing/assertions"
    32  )
    33  
    34  func testStore(ctx context.Context, store SessionStore) {
    35  	Convey("Create", func() {
    36  		So(store.Create(ctx, &statepb.LoginSession{Id: "session-0"}), ShouldBeNil)
    37  		So(store.Create(ctx, &statepb.LoginSession{Id: "session-0"}), ShouldNotBeNil)
    38  	})
    39  
    40  	Convey("Get", func() {
    41  		stored := &statepb.LoginSession{
    42  			Id:                     "session-0",
    43  			OauthS256CodeChallenge: "some-string",
    44  		}
    45  		So(store.Create(ctx, stored), ShouldBeNil)
    46  
    47  		session, err := store.Get(ctx, "session-0")
    48  		So(err, ShouldBeNil)
    49  		So(session, ShouldResembleProto, stored)
    50  
    51  		session, err = store.Get(ctx, "another")
    52  		So(err, ShouldEqual, ErrNoSession)
    53  		So(session, ShouldBeNil)
    54  	})
    55  
    56  	Convey("Update", func() {
    57  		stored := &statepb.LoginSession{
    58  			Id:                     "session-0",
    59  			OauthS256CodeChallenge: "some-string",
    60  		}
    61  		So(store.Create(ctx, stored), ShouldBeNil)
    62  
    63  		unchanged, err := store.Update(ctx, "session-0", func(session *statepb.LoginSession) {
    64  			So(session, ShouldResembleProto, stored)
    65  		})
    66  		So(err, ShouldBeNil)
    67  		So(unchanged, ShouldResembleProto, stored)
    68  
    69  		updated, err := store.Update(ctx, "session-0", func(session *statepb.LoginSession) {
    70  			session.OauthS256CodeChallenge = "updated-string"
    71  		})
    72  		So(err, ShouldBeNil)
    73  		So(updated, ShouldResembleProto, &statepb.LoginSession{
    74  			Id:                     "session-0",
    75  			OauthS256CodeChallenge: "updated-string",
    76  		})
    77  
    78  		fetched, err := store.Get(ctx, "session-0")
    79  		So(err, ShouldBeNil)
    80  		So(fetched, ShouldResembleProto, updated)
    81  	})
    82  
    83  	Convey("Update missing", func() {
    84  		_, err := store.Update(ctx, "session-0", func(*statepb.LoginSession) {
    85  			panic("must not be called")
    86  		})
    87  		So(err, ShouldEqual, ErrNoSession)
    88  	})
    89  
    90  	Convey("Update ID change", func() {
    91  		So(store.Create(ctx, &statepb.LoginSession{
    92  			Id:                     "session-0",
    93  			OauthS256CodeChallenge: "some-string",
    94  		}), ShouldBeNil)
    95  
    96  		So(func() {
    97  			store.Update(ctx, "session-0", func(session *statepb.LoginSession) {
    98  				session.Id = "another"
    99  			})
   100  		}, ShouldPanic)
   101  	})
   102  }
   103  
   104  func TestMemorySessionStore(t *testing.T) {
   105  	t.Parallel()
   106  
   107  	Convey("Works", t, func() {
   108  		ctx := context.Background()
   109  
   110  		testStore(ctx, &MemorySessionStore{})
   111  	})
   112  }
   113  
   114  func TestDatastoreSessionStore(t *testing.T) {
   115  	t.Parallel()
   116  
   117  	Convey("Works", t, func() {
   118  		var now = testclock.TestRecentTimeUTC.Round(time.Millisecond)
   119  
   120  		ctx, tc := testclock.UseTime(memory.Use(context.Background()), now)
   121  		datastore.GetTestable(ctx).Consistent(true)
   122  
   123  		testStore(ctx, &DatastoreSessionStore{})
   124  
   125  		Convey("Cleanup", func() {
   126  			store := &DatastoreSessionStore{}
   127  
   128  			put := func(id string, dur time.Duration) {
   129  				So(store.Create(ctx, &statepb.LoginSession{
   130  					Id:     id,
   131  					Expiry: timestamppb.New(now.Add(dur)),
   132  				}), ShouldBeNil)
   133  			}
   134  
   135  			put("keep", time.Minute)
   136  			put("delete-1", time.Second)
   137  
   138  			// Make sure changing Expiry also moves the cleanup deadline.
   139  			put("delete-2", time.Minute)
   140  			_, err := store.Update(ctx, "delete-2", func(s *statepb.LoginSession) {
   141  				s.Expiry = timestamppb.New(now.Add(time.Second))
   142  			})
   143  			So(err, ShouldBeNil)
   144  
   145  			tc.Add(sessionCleanupDelay + 2*time.Second)
   146  			So(store.Cleanup(ctx), ShouldBeNil)
   147  
   148  			get := func(id string) bool {
   149  				_, err := store.Get(ctx, id)
   150  				return err == nil
   151  			}
   152  			So(get("keep"), ShouldBeTrue)
   153  			So(get("delete-1"), ShouldBeFalse)
   154  			So(get("delete-2"), ShouldBeFalse)
   155  		})
   156  	})
   157  }