go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/tokenserver/appengine/impl/utils/projectidentity/storage_test.go (about)

     1  // Copyright 2019 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 projectidentity
    16  
    17  import (
    18  	"testing"
    19  
    20  	"go.chromium.org/luci/appengine/gaetesting"
    21  
    22  	. "github.com/smartystreets/goconvey/convey"
    23  )
    24  
    25  func TestScopedServiceAccountStorage(t *testing.T) {
    26  
    27  	Convey("Test successful creation of several identities", t, func() {
    28  		ctx := gaetesting.TestingContext()
    29  		var err error
    30  		var actual *ProjectIdentity
    31  		var expected *ProjectIdentity
    32  
    33  		storage := &persistentStorage{}
    34  
    35  		expected = &ProjectIdentity{
    36  			Email:   "sa1@project1.iamserviceaccounts.com",
    37  			Project: "project1",
    38  		}
    39  
    40  		actual, err = storage.Create(ctx, expected)
    41  		So(err, ShouldBeNil)
    42  		So(actual, ShouldResemble, expected)
    43  
    44  		expected = &ProjectIdentity{
    45  			Email:   "sa1@project2.iamserviceaccounts.com",
    46  			Project: "project2",
    47  		}
    48  
    49  		actual, err = storage.Create(ctx, expected)
    50  		So(err, ShouldBeNil)
    51  		So(actual, ShouldResemble, expected)
    52  
    53  		expected = &ProjectIdentity{
    54  			Email:   "sa3@project3.iamserviceaccounts.com",
    55  			Project: "project3",
    56  		}
    57  
    58  		actual, err = storage.Create(ctx, expected)
    59  		So(err, ShouldBeNil)
    60  		So(actual, ShouldResemble, expected)
    61  
    62  		Convey("Test error raised upon duplicate", func() {
    63  			actual, err = storage.Create(ctx, expected)
    64  			So(err, ShouldBeNil)
    65  			So(actual, ShouldResemble, expected)
    66  		})
    67  
    68  		Convey("Test reading by identities by project and email", func() {
    69  			actual, err = storage.lookup(ctx, expected)
    70  			So(err, ShouldBeNil)
    71  			So(actual, ShouldResemble, expected)
    72  
    73  			actual, err = storage.LookupByProject(ctx, expected.Project)
    74  			So(err, ShouldBeNil)
    75  			So(actual, ShouldResemble, expected)
    76  		})
    77  
    78  		Convey("Test deleting identity", func() {
    79  			actual, err = storage.lookup(ctx, expected)
    80  			So(err, ShouldBeNil)
    81  			So(actual, ShouldResemble, expected)
    82  
    83  			err = storage.Delete(ctx, expected)
    84  			So(err, ShouldBeNil)
    85  
    86  			_, err = storage.lookup(ctx, expected)
    87  			So(err, ShouldNotBeNil)
    88  
    89  		})
    90  
    91  		Convey("Test update identity", func() {
    92  			// Make sure we successfully read the expected entry
    93  			actual, err := storage.LookupByProject(ctx, expected.Project)
    94  			So(err, ShouldBeNil)
    95  			So(actual, ShouldResemble, expected)
    96  
    97  			// Create an updated entry
    98  			updated := &ProjectIdentity{
    99  				Project: expected.Project,
   100  				Email:   "foo@bar.com",
   101  			}
   102  			So(updated, ShouldNotResemble, expected)
   103  
   104  			// Update the entry in the datastore
   105  			actual, err = storage.Update(ctx, updated)
   106  			So(err, ShouldBeNil)
   107  			So(actual, ShouldResemble, updated)
   108  
   109  			// Read from datastore, verify its changed
   110  			actual, err = storage.lookup(ctx, expected)
   111  			So(err, ShouldBeNil)
   112  			So(updated, ShouldNotResemble, expected)
   113  			So(actual, ShouldNotResemble, expected)
   114  			So(actual, ShouldResemble, updated)
   115  
   116  			// Create a new entry via update
   117  			expected = &ProjectIdentity{
   118  				Project: "project4",
   119  				Email:   "foo@example.com",
   120  			}
   121  			actual, err = storage.Update(ctx, expected)
   122  			So(err, ShouldBeNil)
   123  			So(actual, ShouldResemble, expected)
   124  
   125  		})
   126  	})
   127  }