// Vikunja is a to-do list application to facilitate your life. // Copyright 2018-present Vikunja and contributors. All rights reserved. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . package upload import ( "os" "strconv" "testing" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/modules/keyvalue" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // TestMain initializes the test environment func TestMain(m *testing.M) { // Initialize logger for tests log.InitLogger() 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() t.Run("handles valid cached type", func(t *testing.T) { provider := &Provider{} // Create a test user testUser := &user.User{ ID: 888888, // Use a different ID to avoid cache conflicts AvatarFileID: 0, } // Store a valid cached avatar cacheKey := CacheKeyPrefix + "888888_32" validCachedAvatar := CachedAvatar{ Content: []byte("fake_image_data"), MimeType: "image/png", } err := keyvalue.Put(cacheKey, validCachedAvatar) require.NoError(t, err) // This should work correctly with the valid cached data avatar, mimeType, err := provider.GetAvatar(testUser, 32) // Should return the cached data successfully require.NoError(t, err) assert.Equal(t, []byte("fake_image_data"), avatar) assert.Equal(t, "image/png", mimeType) }) }