From dbd2411f10b2e78b93307e4b6b8bd9079e13dbb3 Mon Sep 17 00:00:00 2001 From: Marco Ardizzone Date: Thu, 11 Sep 2025 18:05:25 +0200 Subject: [PATCH] add cache mock for organization --- backend/v3/domain/organization.go | 8 +-- backend/v3/domain/organization_test.go | 2 +- .../storage/cache/cachemock/organization.go | 50 +++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 backend/v3/storage/cache/cachemock/organization.go diff --git a/backend/v3/domain/organization.go b/backend/v3/domain/organization.go index 40f2dce0324..19c7d5850fe 100644 --- a/backend/v3/domain/organization.go +++ b/backend/v3/domain/organization.go @@ -27,22 +27,22 @@ type Organization struct { Domains []*OrganizationDomain `json:"domains,omitempty" db:"-"` // domains need to be handled separately } -type orgCacheIndex uint8 +type OrgCacheIndex uint8 const ( - orgCacheIndexUndefined orgCacheIndex = iota + orgCacheIndexUndefined OrgCacheIndex = iota orgCacheIndexID ) // Keys implements the [cache.Entry]. -func (o *Organization) Keys(index orgCacheIndex) (key []string) { +func (o *Organization) Keys(index OrgCacheIndex) (key []string) { if index == orgCacheIndexID { return []string{o.ID} } return nil } -var _ cache.Entry[orgCacheIndex, string] = (*Organization)(nil) +var _ cache.Entry[OrgCacheIndex, string] = (*Organization)(nil) // OrgIdentifierCondition is used to help specify a single Organization, // it will either be used as the organization ID or organization name, diff --git a/backend/v3/domain/organization_test.go b/backend/v3/domain/organization_test.go index dd5290a2192..389cb7430c5 100644 --- a/backend/v3/domain/organization_test.go +++ b/backend/v3/domain/organization_test.go @@ -12,7 +12,7 @@ func TestOrganization_Keys(t *testing.T) { tt := []struct { name string org *Organization - index orgCacheIndex + index OrgCacheIndex wantKeys []string }{ { diff --git a/backend/v3/storage/cache/cachemock/organization.go b/backend/v3/storage/cache/cachemock/organization.go new file mode 100644 index 00000000000..2ae43907080 --- /dev/null +++ b/backend/v3/storage/cache/cachemock/organization.go @@ -0,0 +1,50 @@ +package cachemock + +import ( + "context" + + "github.com/zitadel/zitadel/backend/v3/domain" + "github.com/zitadel/zitadel/backend/v3/storage/cache" +) + +type OrganizationCacheMock struct { + c map[string]*domain.Organization +} + +func NewOrganizationCacheMock() *OrganizationCacheMock { + return &OrganizationCacheMock{ + c: make(map[string]*domain.Organization), + } +} + +// Delete implements cache.Cache. +func (o *OrganizationCacheMock) Delete(_ context.Context, _ domain.OrgCacheIndex, keys ...string) error { + for _, key := range keys { + delete(o.c, key) + } + return nil +} + +// Get implements cache.Cache. +func (o *OrganizationCacheMock) Get(_ context.Context, _ domain.OrgCacheIndex, key string) (*domain.Organization, bool) { + res, ok := o.c[key] + return res, ok +} + +// Invalidate implements cache.Cache. +func (o *OrganizationCacheMock) Invalidate(ctx context.Context, i domain.OrgCacheIndex, key ...string) error { + return o.Delete(ctx, i, key...) +} + +// Set implements cache.Cache. +func (o *OrganizationCacheMock) Set(_ context.Context, organization *domain.Organization) { + o.c[organization.ID] = organization +} + +// Truncate implements cache.Cache. +func (o *OrganizationCacheMock) Truncate(_ context.Context) error { + o.c = make(map[string]*domain.Organization) + return nil +} + +var _ cache.Cache[domain.OrgCacheIndex, string, *domain.Organization] = (*OrganizationCacheMock)(nil)