go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/clustering/rules/clear_users_cron_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 rules
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"go.chromium.org/luci/server/span"
    22  
    23  	"go.chromium.org/luci/analysis/internal/testutil"
    24  
    25  	. "github.com/smartystreets/goconvey/convey"
    26  )
    27  
    28  func TestClearRuleUsers(t *testing.T) {
    29  	Convey(`With Spanner Test Database`, t, func() {
    30  
    31  		ctx := testutil.IntegrationTestContext(t)
    32  
    33  		Convey(`Rules older than 30 days should have their CreationUser cleared`, func() {
    34  			expectedRule := NewRule(101).
    35  				WithCreateTime(time.Now().AddDate(0, 0, -31)).
    36  				WithCreateUser("user@example.com").
    37  				Build()
    38  			err := SetForTesting(ctx, []*Entry{expectedRule})
    39  			So(err, ShouldBeNil)
    40  
    41  			rows, err := clearCreationUserColumn(ctx)
    42  			So(err, ShouldBeNil)
    43  			So(rows, ShouldEqual, 1)
    44  
    45  			rule, err := Read(span.Single(ctx), testProject, expectedRule.RuleID)
    46  			So(err, ShouldBeNil)
    47  			So(rule.CreateUser, ShouldEqual, "")
    48  		})
    49  
    50  		Convey(`Rules created less than 30 days ago should not change`, func() {
    51  			expectedRule := NewRule(101).
    52  				WithCreateTime(time.Now()).
    53  				WithCreateUser("user@example.com").
    54  				Build()
    55  			err := SetForTesting(ctx, []*Entry{expectedRule})
    56  			So(err, ShouldBeNil)
    57  
    58  			rows, err := clearCreationUserColumn(ctx)
    59  			So(err, ShouldBeNil)
    60  			So(rows, ShouldEqual, 0)
    61  		})
    62  
    63  		Convey(`Rules with auditable updates more than 30 days ago have their LastAuditableUpdateUser cleared`, func() {
    64  			expectedRule := NewRule(101).
    65  				WithLastAuditableUpdateTime(time.Now().AddDate(0, 0, -31)).
    66  				WithLastAuditableUpdateUser("user@example.com").
    67  				Build()
    68  			err := SetForTesting(ctx, []*Entry{expectedRule})
    69  			So(err, ShouldBeNil)
    70  
    71  			rows, err := clearLastUpdatedUserColumn(ctx)
    72  			So(err, ShouldBeNil)
    73  			So(rows, ShouldEqual, 1)
    74  
    75  			rule, err := Read(span.Single(ctx), testProject, expectedRule.RuleID)
    76  			So(err, ShouldBeNil)
    77  			So(rule.LastAuditableUpdateUser, ShouldEqual, "")
    78  		})
    79  
    80  		Convey(`Rules with auditable updates less than 30 days ago should not change`, func() {
    81  			expectedRule := NewRule(101).
    82  				WithLastAuditableUpdateTime(time.Now().AddDate(0, 0, -29)).
    83  				WithLastAuditableUpdateUser("user@example.com").
    84  				Build()
    85  			err := SetForTesting(ctx, []*Entry{expectedRule})
    86  			So(err, ShouldBeNil)
    87  
    88  			rows, err := clearLastUpdatedUserColumn(ctx)
    89  			So(err, ShouldBeNil)
    90  			So(rows, ShouldEqual, 0)
    91  		})
    92  
    93  		Convey(`ClearRulesUsers clears both LastAuditableUpdateUser and CreationUser`, func() {
    94  			expectedRule := NewRule(101).
    95  				WithCreateTime(time.Now().AddDate(0, 0, -31)).
    96  				WithCreateUser("user@example.com").
    97  				WithLastAuditableUpdateTime(time.Now().AddDate(0, 0, -31)).
    98  				WithLastAuditableUpdateUser("user@example.com").
    99  				Build()
   100  			err := SetForTesting(ctx, []*Entry{expectedRule})
   101  			So(err, ShouldBeNil)
   102  
   103  			err = ClearRulesUsers(ctx)
   104  			So(err, ShouldBeNil)
   105  
   106  			rule, err := Read(span.Single(ctx), testProject, expectedRule.RuleID)
   107  			So(err, ShouldBeNil)
   108  			So(rule.CreateUser, ShouldEqual, "")
   109  			So(rule.LastAuditableUpdateUser, ShouldEqual, "")
   110  		})
   111  	})
   112  }