add tests

Signed-off-by: Christian Richter <c.richter@opencloud.eu>
This commit is contained in:
Christian Richter
2025-10-30 12:16:42 +01:00
parent 8ffabad1e4
commit 5af51b089f
2 changed files with 54 additions and 6 deletions

View File

@@ -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()

View File

@@ -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())
})
})
})