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