From 5af51b089f092a5e952aaf64763314ceb0ec60ae Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Thu, 30 Oct 2025 12:16:42 +0100 Subject: [PATCH] add tests Signed-off-by: Christian Richter --- services/graph/pkg/identity/cache.go | 12 +++--- services/graph/pkg/identity/cache_test.go | 48 +++++++++++++++++++++++ 2 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 services/graph/pkg/identity/cache_test.go diff --git a/services/graph/pkg/identity/cache.go b/services/graph/pkg/identity/cache.go index 0d149242f0..5efab1ec0e 100644 --- a/services/graph/pkg/identity/cache.go +++ b/services/graph/pkg/identity/cache.go @@ -85,19 +85,19 @@ func NewIdentityCache(opts ...IdentityCacheOption) IdentityCache { } // GetUser looks up a user by id, if the user is not cached, yet it will do a lookup via the CS3 API -func (cache IdentityCache) GetUser(ctx context.Context, tennantId, userid string) (libregraph.User, error) { +func (cache IdentityCache) GetUser(ctx context.Context, tenantId, userid string) (libregraph.User, error) { // can we get the tenant from the context or do we have to pass it? - u, err := cache.GetCS3User(ctx, tennantId, userid) + u, err := cache.GetCS3User(ctx, tenantId, userid) if err != nil { return libregraph.User{}, err } - if tennantId != u.GetId().GetTenantId() { + if tenantId != u.GetId().GetTenantId() { return libregraph.User{}, ErrNotFound } return *CreateUserModelFromCS3(u), nil } -func (cache IdentityCache) GetCS3User(ctx context.Context, tennantId, userid string) (*cs3User.User, error) { +func (cache IdentityCache) GetCS3User(ctx context.Context, tenantId, userid string) (*cs3User.User, error) { var user *cs3User.User if item := cache.users.Get(userid); item == nil { gatewayClient, err := cache.gatewaySelector.Next() @@ -116,13 +116,13 @@ func (cache IdentityCache) GetCS3User(ctx context.Context, tennantId, userid str } // check if the user is in the correct tenant // if not we need to return before the cache is touched - if user.GetId().GetTenantId() != tennantId { + if user.GetId().GetTenantId() != tenantId { return nil, ErrNotFound } cache.users.Set(userid, user, ttlcache.DefaultTTL) } else { - if user.GetId().GetTenantId() != tennantId { + if user.GetId().GetTenantId() != tenantId { return nil, ErrNotFound } user = item.Value() diff --git a/services/graph/pkg/identity/cache_test.go b/services/graph/pkg/identity/cache_test.go new file mode 100644 index 0000000000..e6a054f2a1 --- /dev/null +++ b/services/graph/pkg/identity/cache_test.go @@ -0,0 +1,48 @@ +package identity + +import ( + "context" + + cs3User "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("Cache", func() { + var ( + ctx context.Context + idc IdentityCache + alan = cs3User.User{ + Id: &cs3User.UserId{ + OpaqueId: "alan", + TenantId: "", + }, + DisplayName: "Alan", + } + ) + BeforeEach(func() { + idc = NewIdentityCache() + ctx = context.Background() + }) + + Describe("GetUser", func() { + It("should return not error", func() { + // Persist the user to the cache for 1 hour + idc.users.Set(alan.GetId().OpaqueId, &alan, 3600) + + ru, err := idc.GetUser(ctx, "", "alan") + Expect(err).To(BeNil()) + Expect(ru).ToNot(BeNil()) + Expect(ru.GetId()).To(Equal(alan.GetId())) + Expect(ru.GetDisplayName()).To(Equal(alan.GetDisplayName())) + }) + + It("should return an error, if the tenant id does not match", func() { + alan.GetId().TenantId = "1234" + // Persist the user to the cache for 1 hour + idc.users.Set(alan.GetId().OpaqueId, &alan, 3600) + _, err := idc.GetUser(ctx, "5678", "alan") + Expect(err).ToNot(BeNil()) + }) + }) +})