mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 22:06:38 +08:00
528240026d
- make format 现在与 code-check 使用同一格式化器(golangci-lint fmt),消除 goimports -local 与 gofumpt 的格式拉锯 - .golangci.yml 关闭默认 50/3 截断,完整上报所有问题(只增强不弱化) - 全库 gofumpt 规范化(203 files, 纯格式无行为变更)
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package cache
|
|
|
|
import (
|
|
"Wavelet/plugins/domain/upload/shared"
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
uploadstorage "Wavelet/plugins/domain/upload/storage"
|
|
)
|
|
|
|
func TestLoadMigrationAccessStateCachesResult(t *testing.T) {
|
|
_, cleanup := shared.SetupTestEnv(t)
|
|
defer cleanup()
|
|
ResetAccessCaches()
|
|
|
|
ctx := context.Background()
|
|
first := uploadstorage.LoadMigrationAccessState(ctx)
|
|
second := uploadstorage.LoadMigrationAccessState(ctx)
|
|
|
|
if first.ReadOnly != second.ReadOnly {
|
|
t.Fatalf("readOnly mismatch: first=%v second=%v", first.ReadOnly, second.ReadOnly)
|
|
}
|
|
if first.HasTarget != second.HasTarget {
|
|
t.Fatalf("hasTarget mismatch: first=%v second=%v", first.HasTarget, second.HasTarget)
|
|
}
|
|
}
|
|
|
|
func TestIsFilePublicUsesCachedWhitelist(t *testing.T) {
|
|
_, cleanup := shared.SetupTestEnv(t)
|
|
defer cleanup()
|
|
ResetAccessCaches()
|
|
|
|
ctx := context.Background()
|
|
if !IsFilePublic(ctx, "avatar") {
|
|
t.Fatal("expected avatar to be public by default")
|
|
}
|
|
if IsFilePublic(ctx, "attachment") {
|
|
t.Fatal("expected attachment to be private by default")
|
|
}
|
|
if !IsFilePublic(ctx, "AVATAR") {
|
|
t.Fatal("expected whitelist lookup to be case-insensitive")
|
|
}
|
|
}
|
|
|
|
func TestResetAccessCachesRefreshesWhitelist(t *testing.T) {
|
|
dbConn, cleanup := shared.SetupTestEnv(t)
|
|
defer cleanup()
|
|
ResetAccessCaches()
|
|
|
|
ctx := context.Background()
|
|
if !IsFilePublic(ctx, "avatar") {
|
|
t.Fatal("expected seeded avatar whitelist before reset")
|
|
}
|
|
|
|
if err := dbConn.Table("w_system_configs").Where("key = ?", "file_access_whitelist").Update("value", `["attachment"]`).Error; err != nil {
|
|
t.Fatalf("update whitelist config: %v", err)
|
|
}
|
|
|
|
ResetAccessCaches()
|
|
if !IsFilePublic(ctx, "attachment") {
|
|
t.Fatal("expected attachment to be public after whitelist refresh")
|
|
}
|
|
if IsFilePublic(ctx, "avatar") {
|
|
t.Fatal("expected avatar to be private after whitelist refresh")
|
|
}
|
|
}
|
|
|
|
func TestAccessCacheTTLExpires(t *testing.T) {
|
|
_, cleanup := shared.SetupTestEnv(t)
|
|
defer cleanup()
|
|
ResetAccessCaches()
|
|
|
|
ctx := context.Background()
|
|
_ = loadFileAccessWhitelist(ctx)
|
|
|
|
fileAccessWhitelistMu.Lock()
|
|
fileAccessWhitelistCheckedAt = time.Now().Add(-time.Duration(shared.AccessCacheTTL)*time.Second - time.Second)
|
|
fileAccessWhitelistMu.Unlock()
|
|
|
|
// Should still work after TTL by reloading from config.
|
|
if !IsFilePublic(ctx, "avatar") {
|
|
t.Fatal("expected whitelist reload after TTL expiration")
|
|
}
|
|
}
|