go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/tree_status/internal/status/clear_users_cron_test.go (about)

     1  // Copyright 2024 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 status
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"go.chromium.org/luci/server/span"
    22  
    23  	"go.chromium.org/luci/tree_status/internal/testutil"
    24  
    25  	. "github.com/smartystreets/goconvey/convey"
    26  )
    27  
    28  func TestClearStatusUsers(t *testing.T) {
    29  	Convey(`With Spanner Test Database`, t, func() {
    30  
    31  		ctx := testutil.IntegrationTestContext(t)
    32  
    33  		Convey(`Status older than 30 days should have their CreateUser cleared`, func() {
    34  			NewStatusBuilder().
    35  				WithCreateTime(time.Now().AddDate(0, 0, -31)).
    36  				WithCreateUser("user@example.com").
    37  				CreateInDB(ctx)
    38  
    39  			rows, err := clearCreateUserColumn(ctx)
    40  			So(err, ShouldBeNil)
    41  			So(rows, ShouldEqual, 1)
    42  
    43  			status, err := ReadLatest(span.Single(ctx), "chromium")
    44  			So(err, ShouldBeNil)
    45  			So(status.CreateUser, ShouldEqual, "")
    46  		})
    47  
    48  		Convey(`Status created less than 30 days ago should not change`, func() {
    49  			NewStatusBuilder().
    50  				WithCreateTime(time.Now()).
    51  				WithCreateUser("user@example.com").
    52  				CreateInDB(ctx)
    53  
    54  			rows, err := clearCreateUserColumn(ctx)
    55  			So(err, ShouldBeNil)
    56  			So(rows, ShouldEqual, 0)
    57  
    58  			status, err := ReadLatest(span.Single(ctx), "chromium")
    59  			So(err, ShouldBeNil)
    60  			So(status.CreateUser, ShouldEqual, "user@example.com")
    61  		})
    62  
    63  		Convey(`ClearStatusUsers clears CreateUser`, func() {
    64  			NewStatusBuilder().
    65  				WithCreateTime(time.Now().AddDate(0, 0, -31)).
    66  				WithCreateUser("user@example.com").
    67  				CreateInDB(ctx)
    68  
    69  			err := ClearStatusUsers(ctx)
    70  			So(err, ShouldBeNil)
    71  
    72  			status, err := ReadLatest(span.Single(ctx), "chromium")
    73  			So(err, ShouldBeNil)
    74  			So(status.CreateUser, ShouldEqual, "")
    75  		})
    76  	})
    77  }