From c93fa1b4ae1e78537a21d7563534a571b43c86ea Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 13 Feb 2026 09:00:29 +0100 Subject: [PATCH] test: add failing test for upload avatar FlushCache The test populates the cache with multiple size-suffixed keys and verifies that FlushCache removes all of them. Currently fails because FlushCache uses Del with the base key which doesn't match the actual size-suffixed cache keys. --- pkg/modules/avatar/upload/upload_test.go | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/pkg/modules/avatar/upload/upload_test.go b/pkg/modules/avatar/upload/upload_test.go index 50efb16d3..4b97473dd 100644 --- a/pkg/modules/avatar/upload/upload_test.go +++ b/pkg/modules/avatar/upload/upload_test.go @@ -18,6 +18,7 @@ package upload import ( "os" + "strconv" "testing" "code.vikunja.io/api/pkg/log" @@ -35,6 +36,47 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } +func TestFlushCache(t *testing.T) { + keyvalue.InitStorage() + + provider := &Provider{} + + testUser := &user.User{ + ID: 777777, + } + + // Populate cache with multiple size variants (matching the real key format from line 65) + sizes := []int64{32, 64, 128, 250} + for _, size := range sizes { + cacheKey := CacheKeyPrefix + strconv.Itoa(int(testUser.ID)) + "_" + strconv.FormatInt(size, 10) + err := keyvalue.Put(cacheKey, CachedAvatar{ + Content: []byte("fake_avatar_data"), + MimeType: "image/png", + }) + require.NoError(t, err) + } + + // Verify all entries exist before flush + for _, size := range sizes { + cacheKey := CacheKeyPrefix + strconv.Itoa(int(testUser.ID)) + "_" + strconv.FormatInt(size, 10) + _, exists, err := keyvalue.Get(cacheKey) + require.NoError(t, err) + assert.True(t, exists, "cache entry for size %d should exist before flush", size) + } + + // Flush cache + err := provider.FlushCache(testUser) + require.NoError(t, err) + + // Verify ALL size variants are removed + for _, size := range sizes { + cacheKey := CacheKeyPrefix + strconv.Itoa(int(testUser.ID)) + "_" + strconv.FormatInt(size, 10) + _, exists, err := keyvalue.Get(cacheKey) + require.NoError(t, err) + assert.False(t, exists, "cache entry for size %d should be removed after flush", size) + } +} + func TestGetAvatar(t *testing.T) { // Initialize storage for testing keyvalue.InitStorage()