go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth_service/impl/servers/changelogs/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 changelogs
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"google.golang.org/protobuf/types/known/timestamppb"
    23  
    24  	"go.chromium.org/luci/gae/impl/memory"
    25  	"go.chromium.org/luci/gae/service/datastore"
    26  
    27  	"go.chromium.org/luci/auth_service/api/rpcpb"
    28  	"go.chromium.org/luci/auth_service/impl/model"
    29  
    30  	. "github.com/smartystreets/goconvey/convey"
    31  	. "go.chromium.org/luci/common/testing/assertions"
    32  )
    33  
    34  func TestChangeLogsServer(t *testing.T) {
    35  	t.Parallel()
    36  	srv := Server{}
    37  
    38  	Convey("ListChangeLogs RPC call", t, func() {
    39  		ctx := memory.Use(context.Background())
    40  		datastore.GetTestable(ctx).AutoIndex(true)
    41  		datastore.GetTestable(ctx).Consistent(true)
    42  
    43  		So(datastore.Put(ctx,
    44  			&model.AuthDBChange{
    45  				Kind:           "AuthDBChange",
    46  				ID:             "AuthIPWhitelist$a!3000",
    47  				Parent:         model.ChangeLogRevisionKey(ctx, 10000, false),
    48  				Class:          []string{"AuthDBChange", "AuthDBIPWhitelistChange"},
    49  				ChangeType:     3000,
    50  				Comment:        "comment",
    51  				Description:    "description",
    52  				OldDescription: "",
    53  				Target:         "AuthIPWhitelist$a",
    54  				When:           time.Date(2021, time.December, 12, 1, 0, 0, 0, time.UTC),
    55  				Who:            "user:test@example.com",
    56  				AppVersion:     "123-45abc",
    57  			},
    58  			&model.AuthDBChange{
    59  				Kind:              "AuthDBChange",
    60  				ID:                "AuthGlobalConfig$test!7000",
    61  				Parent:            model.ChangeLogRevisionKey(ctx, 10000, false),
    62  				Class:             []string{"AuthDBChange", "AuthDBConfigChange"},
    63  				ChangeType:        7000,
    64  				Comment:           "comment",
    65  				OauthClientID:     "123.test.example.com",
    66  				OauthClientSecret: "aBcD",
    67  				Target:            "AuthGlobalConfig$test",
    68  				When:              time.Date(2019, time.December, 11, 1, 0, 0, 0, time.UTC),
    69  				Who:               "user:test@example.com",
    70  				AppVersion:        "123-45abc",
    71  			},
    72  			&model.AuthDBChange{
    73  				Kind:             "AuthDBChange",
    74  				ID:               "AuthRealmsGlobals$globals!9000",
    75  				Parent:           model.ChangeLogRevisionKey(ctx, 10020, false),
    76  				Class:            []string{"AuthDBChange", "AuthRealmsGlobalsChange"},
    77  				ChangeType:       9000,
    78  				Comment:          "comment",
    79  				PermissionsAdded: []string{"a.existInRealm"},
    80  				Target:           "AuthRealmsGlobals$globals",
    81  				When:             time.Date(2021, time.January, 11, 1, 0, 0, 0, time.UTC),
    82  				Who:              "user:test@example.com",
    83  				AppVersion:       "123-45abc",
    84  			},
    85  		), ShouldBeNil)
    86  
    87  		Convey("List all", func() {
    88  			res, err := srv.ListChangeLogs(ctx, &rpcpb.ListChangeLogsRequest{
    89  				PageSize: 2,
    90  			})
    91  			So(err, ShouldBeNil)
    92  			So(res.NextPageToken, ShouldNotBeEmpty)
    93  			So(len(res.Changes), ShouldEqual, 2)
    94  			So(res.Changes[0], ShouldResembleProto, &rpcpb.AuthDBChange{
    95  				ChangeType:       "REALMS_GLOBALS_CHANGED",
    96  				Comment:          "comment",
    97  				PermissionsAdded: []string{"a.existInRealm"},
    98  				Target:           "AuthRealmsGlobals$globals",
    99  				When:             timestamppb.New(time.Date(2021, time.January, 11, 1, 0, 0, 0, time.UTC)),
   100  				Who:              "user:test@example.com",
   101  				AppVersion:       "123-45abc",
   102  			})
   103  			So(res.Changes[1], ShouldResembleProto, &rpcpb.AuthDBChange{
   104  				ChangeType:     "IPWL_CREATED",
   105  				Comment:        "comment",
   106  				Description:    "description",
   107  				OldDescription: "",
   108  				Target:         "AuthIPWhitelist$a",
   109  				When:           timestamppb.New(time.Date(2021, time.December, 12, 1, 0, 0, 0, time.UTC)),
   110  				Who:            "user:test@example.com",
   111  				AppVersion:     "123-45abc",
   112  			})
   113  
   114  			res, err = srv.ListChangeLogs(ctx, &rpcpb.ListChangeLogsRequest{
   115  				PageToken: res.NextPageToken,
   116  				PageSize:  2,
   117  			})
   118  			So(err, ShouldBeNil)
   119  			So(res.NextPageToken, ShouldBeEmpty)
   120  			So(len(res.Changes), ShouldEqual, 1)
   121  			So(res.Changes[0], ShouldResembleProto, &rpcpb.AuthDBChange{
   122  				ChangeType:        "CONF_OAUTH_CLIENT_CHANGED",
   123  				Comment:           "comment",
   124  				OauthClientId:     "123.test.example.com",
   125  				OauthClientSecret: "aBcD",
   126  				Target:            "AuthGlobalConfig$test",
   127  				When:              timestamppb.New(time.Date(2019, time.December, 11, 1, 0, 0, 0, time.UTC)),
   128  				Who:               "user:test@example.com",
   129  				AppVersion:        "123-45abc",
   130  			})
   131  		})
   132  		Convey("Filter by target", func() {
   133  			res, err := srv.ListChangeLogs(ctx, &rpcpb.ListChangeLogsRequest{
   134  				Target:   "AuthRealmsGlobals$globals",
   135  				PageSize: 3,
   136  			})
   137  			So(err, ShouldBeNil)
   138  			So(len(res.Changes), ShouldEqual, 1)
   139  			So(res.Changes[0], ShouldResembleProto, &rpcpb.AuthDBChange{
   140  				ChangeType:       "REALMS_GLOBALS_CHANGED",
   141  				Comment:          "comment",
   142  				PermissionsAdded: []string{"a.existInRealm"},
   143  				Target:           "AuthRealmsGlobals$globals",
   144  				When:             timestamppb.New(time.Date(2021, time.January, 11, 1, 0, 0, 0, time.UTC)),
   145  				Who:              "user:test@example.com",
   146  				AppVersion:       "123-45abc",
   147  			})
   148  		})
   149  		Convey("Filter by AuthDBRev", func() {
   150  			res, err := srv.ListChangeLogs(ctx, &rpcpb.ListChangeLogsRequest{
   151  				AuthDbRev: 10020,
   152  				PageSize:  3,
   153  			})
   154  			So(err, ShouldBeNil)
   155  			So(len(res.Changes), ShouldEqual, 1)
   156  			So(res.Changes[0], ShouldResembleProto, &rpcpb.AuthDBChange{
   157  				ChangeType:       "REALMS_GLOBALS_CHANGED",
   158  				Comment:          "comment",
   159  				PermissionsAdded: []string{"a.existInRealm"},
   160  				Target:           "AuthRealmsGlobals$globals",
   161  				When:             timestamppb.New(time.Date(2021, time.January, 11, 1, 0, 0, 0, time.UTC)),
   162  				Who:              "user:test@example.com",
   163  				AppVersion:       "123-45abc",
   164  			})
   165  		})
   166  	})
   167  }